超全MyBatis动态SQL详解 MyBatis数据库教程( 三 )


select id="selectByIdOrName" resultMap="BaseResultMap" parameterType="com.homejim.mybatis.entity.Student" select include refid="Base_Column_List" / from student where 1=1 choose when test="studentId != null" and student_id=#{studentId} /when when test="name != null and name != ''" and name=#{name} /when otherwise and 1=2 /otherwise /choose /select3.3 测试
@Test public void selectByIdOrName() { SqlSession sqlSession = null; sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student = new Student(); student.setName("小飞机"); student.setStudentId(1); Student studentById = studentMapper.selectByIdOrName(student); System.out.println("有 ID 则根据 ID 获得"); System.out.println(ToStringBuilder.reflectionToString(studentById, ToStringStyle.MULTI_LINE_STYLE)); student.setStudentId(null); Student studentByName = studentMapper.selectByIdOrName(student); System.out.println("没有 ID 则根据 name 获得"); System.out.println(ToStringBuilder.reflectionToString(studentByName, ToStringStyle.MULTI_LINE_STYLE)); student.setName(null); Student studentNull = studentMapper.selectByIdOrName(student); System.out.println("没有 ID 和 name, 返回 null"); Assert.assertNull(studentNull); sqlSession.commit(); sqlSession.close(); }有 ID 则根据 ID 获得 。结果

超全MyBatis动态SQL详解 MyBatis数据库教程

文章插图
没有 ID 则根据 name 获得
超全MyBatis动态SQL详解 MyBatis数据库教程

文章插图
没有 ID 和 name, 返回 null
超全MyBatis动态SQL详解 MyBatis数据库教程

文章插图
4 trim(set、where)
这三个其实解决的是类似的问题 。如我们在编辑前面的[在 WHERE 条件中使用 if 标签] SQL 的时候 。where 1=1 这种条件我们是不渴望存在的 。
4.1 where
4.1.1 查询条件
根据输入的学生消息进行条件检索 。
当只输入玩家名时 。使用玩家名进行模糊检索;当只输入性别时 。使用性别进行完整匹配当玩家名和性别都存在时 。用这两个条件进行查询匹配查询不使用 where 1=1 。
4.1.2 动态 SQL
很明显 。我们要解决这几个问题
当条件都不满足时: 此时此刻 SQL 中大概要不可以有 where。不然导致出错
当 if 有条件满足时: SQL 中需要有 where 。且第一个成立的 if 标签下的 and | or 等要去掉
这时候 。我们可以使用 where 标签 。
接口途径
/** * 根据输入的学生消息进行条件检索 * 1. 当只输入玩家名时 。使用玩家名进行模糊检索; * 2. 当只输入邮箱时 。使用性别进行完整匹配 * 3. 当玩家名和性别都存在时 。用这两个条件进行查询匹配的用 */ ListStudent selectByStudentSelectiveWhereTag(Student student);对应的 SQL
select id="selectByStudentSelectiveWhereTag" resultMap="BaseResultMap" parameterType="com.homejim.mybatis.entity.Student" select include refid="Base_Column_List" / from student where if test="name != null and name !=''" and name like concat('%', #{name}, '%') /if if test="sex != null" and sex=#{sex} /if /where /select4.1.3 测试
@Test public void selectByStudentWhereTag() { SqlSession sqlSession = null; sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student search = new Student(); search.setName("明"); System.out.println("只有名字时的查询"); ListStudent studentsByName = studentMapper.selectByStudentSelectiveWhereTag(search); for (int i = 0; i studentsByName.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(studentsByName.get(i), ToStringStyle.MULTI_LINE_STYLE)); } search.setSex((byte) 1); System.out.println("姓名和性别同一时间存在的查询"); ListStudent studentsBySex = studentMapper.selectByStudentSelectiveWhereTag(search); for (int i = 0; i studentsBySex.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(studentsBySex.get(i), ToStringStyle.MULTI_LINE_STYLE)); } System.out.println("姓名和性别都不存在时查询"); search.setName(null); search.setSex(null); ListStudent studentsByNameAndSex = studentMapper.selectByStudentSelectiveWhereTag(search); for (int i = 0; i studentsByNameAndSex.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(studentsByNameAndSex.get(i), ToStringStyle.MULTI_LINE_STYLE)); } sqlSession.commit(); sqlSession.close(); }只有名字时的查询, 有 where
超全MyBatis动态SQL详解 MyBatis数据库教程

文章插图
姓名和性别同一时间存在的查询 。有 where
超全MyBatis动态SQL详解 MyBatis数据库教程

推荐阅读