Mybatis 基于OGNL 表达式来完成SQL的动态编写,让SQL具有逻辑判断功能(类似jsp中的 JSTL表达式)
l If
l Choose
l Where
l Foreach
l Trim
l Set
1.创建实体 |
publicclass Student { privateintid; private String name; publicint getId() { returnid; } publicvoid setId(int id) { this.id = id; } public String getName() { returnname; } publicvoid setName(String name) { this.name = name; } } |
2.创建对应的映射文件Student.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="mapping.StudentMapping">
</mapper > |
3.myBatis 加载映射文件 |
<!-- 映射器:指定映射文件或者映射类 --> <mappers> <mapper resource="mapping/StudentMapping.xml"/> </mappers> |
5.1 if (test)使用
l StudentMapping.xml 代码 |
<select id="findStu" resultType="entity.Student" parameterType="entity.Student"> select * from student where 1=1 <if test="id!= null"> and id=#{id} </if> <if test="name!= null"> and name=#{name} </if> </select> |
l 测试代码 01 (都为空的时候查询全部) |
@Test publicvoid test01(){ SqlSession session = sFactory.openSession(); Student = new Student(); String sql = "mapping.StudentMapping.findStu"; List<Student> sList=session.selectList(sql,student); for(Student student2 :sList){ System.out.println(student2.getId()+"===="+student2.getName()); } } |
Ø Log4j 打印结果 |
==> Preparing: select * from student where 1=1 ==> Parameters: <== Total: 3 1====001 2====002 3====003 |
l 测试代码02 (设置 id ,满足第一个 id != null) |
@Test publicvoid test01(){ SqlSession session = sFactory.openSession(); Student = new Student(); student.setId("2"); String sql = "mapping.StudentMapping.findStu"; List<Student> sList=session.selectList(sql,student); for(Student student2 :sList){ System.out.println(student2.getId()+"===="+student2.getName()); } } |
Ø Log4j打印结果 |
==> Preparing: select * from student where 1=1 and id=? ==> Parameters: 2(String) <== Total: 1 2====002 |
5.2 Choose(when otherwise)
l 相当于Java 中条件分支语句(switch),满足<when>时,执行<when> ,当条件都不满足时,执行<otherwise>
l 也可以结合trim 使用
Mapping.xml |
<select id="findStu2" resultType="entity.Student" parameterType="entity.Student"> select * from student where 1=1 <choose> <when test="name!= null"> and name=#{name} </when> <when test="id!= null"> and id=#{id} </when> <otherwise> and name="s" </otherwise> </choose> </select> |
5.3 where (if)
l 主要是用来简化sql语句中where条件判断的书写的
l where 标记会自动将其后第一个条件的and或者是or给忽略掉
Mapping.xml |
<!--3. where 简化where条件判断 --> <select id="findStu3" resultType="entity.Student" parameterType="entity.Student"> select * from student <where> <if test="name!= null"> and name=#{name} </if> <if test="id!= null"> and id=#{id} </if> </where> </select> |
5.4 Foreach
l 通常用于在一定范围在类的数据查询
l 常用的必要操作是需要对一个集合进行遍历,通常是在构建 IN 条件语句的时候
Mapping.xml |
<!--5. foreach --> <select id="findStu5" resultType="entity.Student" parameterType="entity.Student"> select * from student where id in <foreach collection="list" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach> </select>
|
测试类的编写: |
publicvoid test5(){ System.out.println("test3"); SqlSession session = sFactory.openSession(); List<String> student=new ArrayList<String>(); student.add("1"); student.add("2"); String sql = "mapping.StudentMapping.findStu5"; List<Student> sList=session.selectList(sql,student); for(Student student2 :sList){ System.out.println(student2.getId()+"===="+student2.getName()); } } |
结果: ==> Parameters: 1(String), 2(String) <== Total: 2 1====001 2====s |
5.4 Foreach
l 通常用于在一定范围在类的数据查询
l 常用的必要操作是需要对一个集合进行遍历,通常是在构建 IN 条件语句的时候
Mapping.xml |
<!--5. foreach --> <select id="findStu5" resultType="entity.Student" parameterType="entity.Student"> select * from student where id in <foreach collection="list" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach> </select>
|
测试类的编写: |
publicvoid test5(){ System.out.println("test3"); SqlSession session = sFactory.openSession(); List<String> student=new ArrayList<String>(); student.add("1"); student.add("2"); String sql = "mapping.StudentMapping.findStu5"; List<Student> sList=session.selectList(sql,student); for(Student student2 :sList){ System.out.println(student2.getId()+"===="+student2.getName()); } } |
结果: ==> Parameters: 1(String), 2(String) <== Total: 2 1====001 2====s |
5.5 Trim
l 通过自定义 trim 元素来定制我们想要的功能
l 同上面的 choose 的第二种写法,通过自定义标签,实现想要的功能
<trim prefix =”WHERE” prefixOverrides =”AND | OR”> ….. </trim> |
l prefixOverrides
在SQL语句最前移除元素内的值
l prefix
在sql 语句之前添加指定值
l suffixOverrides
在SQL语句最后移除元素内的值
l suffix
在sql 语句之后添加指定值
5.6 set(if)
l 用于更新数据的动态SQL
Mapping.xml |
<update id="upStu" parameterType="entity.Student"> update student <set> <if test="name!= null"> name=#{name}, </if> </set> <where> <if test="id!=null"> id=#{id} </if> </where> </update> |
l set 元素会动态前置 SET 关键字,同时也会消除无关的逗号
<trim prefix="SET" suffixOverrides=","> ... </trim> |