文章目录
一、对学生表实现条件查询
(1)、创建学生映射器配置文件
- 在resources/mapper目录里创建学生映射器配置文件 - StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.hlw.mybatis.mapper.StudentMapper">
<select id="findByCondition" parameterType="java.util.Map" resultMap="studentResultMap">
SELECT * FROM t_student
<trim prefix="WHERE" prefixOverrides="AND|OR">
<if test="name != null">
s_name LIKE CONCAT(#{name}, "%")
</if>
<if test="gender != null">
AND s_gender = #{gender}
</if>
<if test="age != null">
AND s_age = #{age}
</if>
</trim>
</select>
<resultMap id="studentResultMap" type="Student">
<result column="s_id" property="id"/>
<result column="s_name" property="name"/>
<result column="s_gender" property="gender"/>
<result column="s_age" property="age"/>
<association property="clazz" column="class_id" javaType="Clazz" select="getClazz"/>
</resultMap>
<select id="getClazz" resultType="Clazz">
SELECT c_id id, c_name name FROM t_class WHERE c_id = #{id};
</select>
</mapper>
(2)、在MyBatis配置文件里注册学生映射器配置文件
- 在<mappers> 元素里添加子元素<mapper resource = “mapper/StudentMapper.xml”/>
(3)、创建学生映射器接口
- 在net.hlw.mybatis.mapper包里创建学生映射器接口 - StudentMapper
package net.hlw.mybatis.mapper;
import net.hlw.mybatis.bean.Student;
import java.util.List;
import java.util.Map;
/**
* 功能:学生映射器接口
* 作者:H
* 日期:2021年3月17日
*/
public interface StudentMapper {
List<Student> findByCondition(Map<String, Object> condition);
}
(4)、创建测试类 TestStudentMapper
package net.hlw.mybatis.mapper;
import net.hlw.mybatis.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestStudentMapper {
private SqlSession sqlSession; //SQL会话
private StudentMapper studentMapper;//用户映射器
@Before
public void init() {
try {
// 读取MyBatis配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 基于MyBatis配置文件构建SQL会话工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
// 利用SQL会话工厂获取SQL会话
sqlSession = factory.openSession();
// 利用SQL会话获取用户映射器对象
studentMapper = sqlSession.getMapper(StudentMapper.class);
// 提示用户SQL会话创建成功
System.out.println("sqlSession对象已创建。");
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void destory() {
// 关闭SQL会话
sqlSession.close();
// 提示用户SQL会话对象关闭
System.out.println("sqlSession对象已关闭。");
}
}
1、查询女生记录
- 添加测试方法testFindByCondition()
@Test
public void testFindByCondition() {
Map<String, Object> condition = new HashMap<>();
condition.put("gender","女");
List<Student> students = studentMapper.findByCondition(condition);
students.forEach(student -> System.out.println(student));
}
- 运行测试方法testFindByCondition(),查看结果
2、查询19岁的女生
- 填加测试方法里的查询条件
- 运行测试方法,查看结果
3、查询姓吴的19岁女生
- 添加测试方法里的查询条件
- 运行测试方法,查看结果过
4、查找姓张的19岁女生
- 修改测试方法的查询条件
- 运行测试方法查看结果