标签:
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。
MyBatis中用于实现动态SQL的元素主要有:
<select id="findAllInfosCount" resultType="int"> select count(*) from student where 1=1 <if test="name != null and name != ‘‘"> and NAME like CONCAT(‘%‘,‘${name}‘,‘%‘)</if> <if test="gender != null and gender != ‘‘"> and GENDER = #{gender}</if> <if test="course != null and course != ‘‘"> and COURSE = #{course}</if> <if test="examdate != null and examdate != ‘‘"> and EXAMDATE between #{examdate}</if> </select> <select id="findAllInfos" parameterType="com.cib.yypt.sample.vo.StudentVO" resultMap="studentMap"> select STUDENTZJ,STUDENTID,NAME,GENDER,COURSE,SCORE,EXAMDATE from student where 1=1 <if test="name != null and name != ‘‘"> and NAME like CONCAT(‘%‘,‘${name}‘,‘%‘)</if> <if test="gender != null and gender != ‘‘"> and GENDER = #{gender}</if> <if test="course != null and course != ‘‘"> and COURSE = #{course}</if> <if test="examdate != null and examdate != ‘‘"> and EXAMDATE between #{examdate}</if> </select>
<?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="com.cib.studentmanagement.dao.IStudentDao"> <resultMap id="khxxCxMap" type="com.cib.studentmanagement.vo.StudentVO"> <id column="STUDENTZJ" property="studentzj" /> <result column="STUDENTID" property="studentid" /> <result column="NAME" property="name" /> <result column="GENDER" property="gender" /> <result column="COURSE" property="course" /> <result column="SCORE" property="score" /> <result column="EXAMDATE" property="examdate" /> </resultMap> <select id="findAllInfosCount" resultType="int"> select count(*) from student where 1=1 <if test="studentid != null and studentid != ‘‘"> and STUDENTID = #{studentid}</if> <if test="name != null and name != ‘‘"> and NAME like ${name}</if> <if test="gender != null and gender != ‘‘"> and GENDER = #{gender}</if> <if test="course != null and course != ‘‘"> and COURSE = #{course}</if> <if test="score_min != null and score_min != ‘‘"> and SCORE >= #{score_min}</if> <if test="score_max != null and score_max != ‘‘"> and SCORE <![CDATA[<=]]> #{score_max}</if> <if test="examdate != null and examdate != ‘‘"> and EXAMDATE = #{examdate}</if> </select> <select id="findAllInfos" parameterType="com.cib.studentmanagement.vo.StudentVO" resultMap="studentMap"> select STUDENTZJ,STUDENTID,NAME,GENDER,COURSE,SCORE,EXAMDATE from student where 1=1 <if test="studentid != null and studentid != ‘‘"> and STUDENTID = #{studentid}</if> <if test="name != null and name != ‘‘"> and NAME like ${name}</if> <if test="gender != null and gender != ‘‘"> and GENDER = #{gender}</if> <if test="course != null and course != ‘‘"> and COURSE = #{course}</if> <if test="score_min != null and score_min != ‘‘"> and SCORE >= #{score_min}</if> <if test="score_max != null and score_max != ‘‘"> and SCORE <![CDATA[<=]]> #{score_max}</if> <if test="examdate != null and examdate != ‘‘"> and EXAMDATE = #{examdate}</if> </select> <insert id="save" parameterType="com.cib.studentmanagement.vo.StudentVO"> INSERT INTO student (STUDENTZJ,STUDENTID,NAME,GENDER,COURSE,SCORE,EXAMDATE) values (#{studentzj,jdbcType=CHAR},#{studentid,jdbcType=CHAR},#{name,jdbcType=VARCHAR},#{gender,jdbcType=CHAR},
#{course,jdbcType=VARCHAR},#{score,jdbcType=DOUBLE},#{examdate,jdbcType=TIMESTAMP}) </insert> <update id="update" parameterType="com.cib.studentmanagement.vo.StudentVO"> update student <trim prefix="SET" suffixOverrides=","> <if test="studentzj != null"> STUDENTZJ = #{studentzj},</if> <if test="studentid != null"> STUDENTID = #{studentid},</if> <if test="name != null"> NAME = #{name},</if> <if test="gender != null"> GENDER = #{gender},</if> <if test="course != null"> COURSE = #{course},</if> <if test="score != null"> SCORE = #{score},</if> <if test="examdate != null"> EXAMDATE = #{examdate},</if> </trim> where <trim prefix="" suffixOverrides="AND"> <if test="studentzj != null">STUDENTZJ = #{studentzj} AND </if> </trim> </update> <delete id="delete" parameterType="com.cib.studentmanagement.vo.StudentVO"> delete from student <trim prefix="WHERE" prefixOverrides="AND"> <if test="studentzj != null"> AND STUDENTZJ = #{studentzj}</if> </trim> </delete> </mapper>
choose元素的作用就相当于JAVA中的switch语句,基本上跟JSTL中的choose的作用和用法是一样的,通常都是与when和otherwise搭配的。看如下一个例子:
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog"> select * from t_blog where 1 = 1 <choose> <when test="title != null"> and title = #{title} </when> <when test="content != null"> and content = #{content} </when> <otherwise> and owner = "owner1" </otherwise> </choose> </select>
where语句的作用主要是简化SQL语句中where中的条件判断的,先看一个例子,再解释一下where的好处。
<select id="dynamicWhereTest" parameterType="Blog" resultType="Blog"> select * from t_blog <where> <if test="title != null"> title = #{title} </if> <if test="content != null"> and content = #{content} </if> <if test="owner != null"> and owner = #{owner} </if> </where> </select>
where元素的作用是会在写入where元素的地方输出一个where,另外一个好处是你不需要考虑where元素里面的条件输出是什么样子 的,MyBatis会智能的帮你处理,如果所有的条件都不满足那么MyBatis就会查出所有的记录,如果输出后是and 开头的,MyBatis会把第一个and忽略,当然如果是or开头的,MyBatis也会把它忽略;此外,在where元素中你不需要考虑空格的问 题,MyBatis会智能的帮你加上。像上述例子中,如果title=null, 而content != null,那么输出的整个语句会是select * from t_blog where content = #{content},而不是select * from t_blog where and content = #{content},因为MyBatis会智能的把首个and 或 or 给忽略。
<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog"> select * from t_blog <trim prefix="where" prefixOverrides="and |or"> <if test="title != null"> title = #{title} </if> <if test="content != null"> and content = #{content} </if> <if test="owner != null"> or owner = #{owner} </if> </trim> </select>
set元素主要是用在更新操作的时候,它的主要功能和where元素其实是差不多的,主要是在包含的语句前输出一个 set,然后如果包含的语句是以逗号结束的话将会把该逗号忽略,如果set包含的内容为空的话则会出错。有了set元素我们就可以动态的更新那些修改了的 字段。下面是一段示例代码:
<update id="dynamicSetTest" parameterType="Blog"> update t_blog <set> <if test="title != null"> title = #{title}, </if> <if test="content != null"> content = #{content}, </if> <if test="owner != null"> owner = #{owner} </if> </set> where id = #{id} </update>
<select id="dynamicForeachTest" resultType="Blog"> select * from t_blog where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection的值为list,对应的Mapper是这样的
public List<Blog> dynamicForeachTest(List<Integer> ids);
测试代码:
@Test public void dynamicForeachTest() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper.class); List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(3); ids.add(6); List<Blog> blogs = blogMapper.dynamicForeachTest(ids); for (Blog blog : blogs) System.out.println(blog); session.close(); }
2.单参数array数组的类型:
<select id="dynamicForeach2Test" resultType="Blog"> select * from t_blog where id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection为array,对应的Mapper代码:
public List<Blog> dynamicForeach2Test(int[] ids);
对应的测试代码:
@Test public void dynamicForeach2Test() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper.class); int[] ids = new int[] {1,3,6,9}; List<Blog> blogs = blogMapper.dynamicForeach2Test(ids); for (Blog blog : blogs) System.out.println(blog); session.close(); }
3.自己把参数封装成Map的类型
<select id="dynamicForeach3Test" resultType="Blog"> select * from t_blog where title like "%"#{title}"%" and id in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection的值为ids,是传入的参数Map的key,对应的Mapper代码:
public List<Blog> dynamicForeach3Test(Map<String, Object> params);
对应测试代码:
@Test public void dynamicForeach3Test() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper.class); final List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(2); ids.add(3); ids.add(6); ids.add(7); ids.add(9); Map<String, Object> params = new HashMap<String, Object>(); params.put("ids", ids); params.put("title", "中国"); List<Blog> blogs = blogMapper.dynamicForeach3Test(params); for (Blog blog : blogs) System.out.println(blog); session.close(); }
标签:
原文地址:http://www.cnblogs.com/mingforyou/p/4256054.html