Java企业级开发学习笔记(2.4)利用MyBatis实现条件查询
该文章主要为完成实训任务,详细实现过程及结果见【http://t.csdn.cn/AZM1g】
文章目录
- 一、创建学生映射器配置文件
- 二、配置学生映射文件
- 三、创建学生映射器接口
- 四、测试学生映射器接口
-
- 任务1. 查询女生记录
- 任务2. 查询19岁的女生
- 任务3. 查询姓吴的19岁女生
- 任务4. 查找姓张的19岁女生
一、创建学生映射器配置文件
- 在
resources/mapper
目录里创建学生映射器配置文件 -StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.kox.mybatis.mapper.StudentMapper"><!--按编号查询班级--><select id="getClazz" resultType="Clazz">SELECT c_id id, c_name name FROM t_class WHERE c_id = #{id}</select><!--定义学生结果映射--><resultMap id="studentMap" 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"/><!--通过子查询getClazz关联到班级实体--><association column="class_id" property="clazz" javaType="Clazz" select="getClazz"/></resultMap><!--按条件查询学生记录,涉及姓名、性别与年龄的联合查询--><select id="findByCondition" parameterType="java.util.Map" resultMap="studentMap">SELECT * FROM t_student<trim prefix="WHERE" prefixOverrides="AND|OR"> <!--删除条件中多余的AND或OR--><!--关于姓名的条件,模糊查询--><if test="name != null">s_name LIKE CONCAT(#{name}, '%')</if><!--关于性别的条件--><if test="gender != null">AND s_gender = #{gender} <!--注意AND不能少--></if><!--关于年龄的条件--><if test="age != null">AND s_age = #{age} <!--注意AND不能少--></if></trim></select>
</mapper>
二、配置学生映射文件
- 在MyBatis配置文件的
<mappers>
元素里添加子元素<mapper resource="mapper/StudentMapper.xml"/>
三、创建学生映射器接口
- 在
cn.kox.mybatis.mapper
包里创建学生映射器接口 -StudentMapper
package cn.kox.mybatis.mapper;import cn.kox.mybatis.bean.Student;import java.util.List;
import java.util.Map;/ 学生映射器接口* */public interface StudentMapper {List<Student> findByCondition(Map<String, Object> condition); // 按条件查询学生记录
}
四、测试学生映射器接口
- 在
test/java
的cn.kox.mybatis.mapper
包里创建TestStudentMapper
类
package cn.kox.mybatis.mapper;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 java.io.IOException;
import java.io.Reader;/* @ClassName: TestStudentMapper* @Author: Kox* @Data: 2023/4/19* @Sketch:*/
public class TestStudentMapper {private SqlSession sqlSession; // SQL会话private StudentMapper studentMapper; // 学生映射器@Beforepublic 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("SQL会话创建成功~");} catch (IOException e) {e.printStackTrace();}}@Afterpublic void destroy() {// 关闭SQL会话sqlSession.close();// 提示用户SQL会话关闭System.out.println("SQL会话已经关闭~");}
}
任务1. 查询女生记录
- 添加测试方法
testFindByCondition()
@Test // 测试按条件查询学生记录 public void testFindByCondition() {// 创建条件对象Map<String, Object> condition = new HashMap<>();// 设置性别条件(女)condition.put("gender", "女");// 按条件查询学生记录List<Student> students = studentMapper.findByCondition(condition);// 判断是否查询到满足条件的记录if (students.size() > 0) {// 使用列表的遍历算子输出全部记录students.forEach(student -> System.out.println(student));} else {// 提示用户没有找到满足条件的记录System.out.println("遗憾,没找到满足条件的记录~");}}
- 运行测试方法
testFindByCondition()
,查看结果
任务2. 查询19岁的女生
- 修改测试方法里的查询条件
- 运行测试方法
testFindByCondition()
,查看结果
任务3. 查询姓吴的19岁女生
- 修改测试方法里的查询条件
- 运行测试方法
testFindByCondition()
,查看结果
任务4. 查找姓张的19岁女生
- 修改测试方法里的查询条件
- 运行测试方法
testFindByCondition()
,查看结果