一、打开MyBatisDemo项目
二、对学生表实现条件查询
1、创建学生映射器配置文件
在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="net.hf.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>
2、配置学生映射器文件
在MyBatis配置文件的元素里添加子元素
3、创建学生映射器接口
在net.hf.mybatis.mapper包里创建学生映射器接口 - StudentMapper
package net.hf.mybatis.mapper;import net.hf.mybatis.bean.Student;import java.util.List;
import java.util.Map;/*** 功能:学生映射器接口* 作者:hf* 日期:2023年04月18日*/
public interface StudentMapper {List<Student> findByCondition(Map<String, Object> condition); // 按条件查询学生记录
}
对应关系图
4、测试学生映射器接口
在test/java的net.hf.mybatis.mapper包里创建TestStudentMapper类
package net.hf.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;/*** 功能:测试学生映射器接口* 作者:hf* 日期:2023年04月18日*/
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();}
}
任务1、查询女生记录
添加测试方法testFindByCondition()
@Test // 测试按条件查询学生记录public void tstFindByCondition() {// 创建条件对象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(),查看结果