做交互网站重庆网站推广人员
文章目录
- 零、本节学习目标
 - 一、查询需求
 - 二、打开MyBatisDemo项目
 - 三、对学生表实现条件查询
 - (一)创建学生映射器配置文件
 - (二)配置学生映射器文件
 - (三)创建学生映射器接口
 - (四)测试学生映射器接口
 - 任务1、查询女生记录
 - 任务2、查询19岁的女生
 - 任务3、查询姓吴的19岁女生
 - 任务4、查找姓张的19岁女生
 
零、本节学习目标
- 理解条件查询的含义
 - 掌握利用MyBatis实现条件查询
 
- 京东网购就涉及条件查询

 
一、查询需求
- 对学生表进行条件查询,涉及姓名、性别和年龄三个字段。

 - 比如查询姓“吴”,性别为“女”,同时年龄为19的学生记录。

 - 注意:通配符
%与_的区别 
二、打开MyBatisDemo项目
- 打开
MyBatisDemo项目

 
三、对学生表实现条件查询
(一)创建学生映射器配置文件
- 在
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.huawei.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"/>

 
(三)创建学生映射器接口
- 在
net.huawei.mybatis.mapper包里创建学生映射器接口 -StudentMapper

 
package net.huawei.mybatis.mapper;import net.huawei.mybatis.bean.Student;import java.util.List;
import java.util.Map;/*** 功能:学生映射器接口* 作者:华卫* 日期:2023年04月18日*/
public interface StudentMapper {List<Student> findByCondition(Map<String, Object> condition); // 按条件查询学生记录
}
 
- 对应关系图

 
(四)测试学生映射器接口
- 在
test/java的net.huawei.mybatis.mapper包里创建TestStudentMapper类

 
package net.huawei.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;/*** 功能:测试学生映射器接口* 作者:华卫* 日期: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();// 提示用户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(),查看结果

 
