mybatis-动态sql使⽤⼩结什么是动态sql?
动态sql: 简单来说就是sql的内容是变化的,可以根据条件获取到不同的sql语句。
主要是sql语句中的where部分发⽣变化。
动态sql的实现,使⽤的是mybatis提供的标签<if> ,<where>,<foreach>...等
< if >
1. 简介
< if >是⽤于条件判断的
if:当条件为true,就会吧if之间的sql加⼊到主sql之后
2. 语法
<if test="判断java对象的属性值(逻辑表达式)">
部分sql语句
</if>
< choose >
< choose >也是⽤于条件判断的
2. 语法
<choose>
<when test="逻辑表达式">
sql语句
</when>
<when test="逻辑表达式">
sql语句
</when>
<otherwise><!--otherwise相当于if..else if.. else中的else-->
sql语句
</otherwise>
</choose>
3. 举个栗⼦
<select id="findCustomerByNameOrJobsOfChoose"parameterType="po.Customer"resultType="po.Customer">
select * from t_customer
<where>
<choose>
<when test="username!=null and username!=''">
usernamelikeconcat('%',#{username},'%')
</when>
<when test="jobs!=null and jobs!=''">
and jobs=#{jobs}
</when>
<otherwise>
and 1=1
</otherwise>
</choose>
</where>
</select>
< where >
1. 简介
< where >⽤来包含 多个< if >的, 当多个if有⼀个成⽴时, < where >会⾃动增加⼀个where关键字,并去掉 if中多余的 and ,or等。
2. 举个栗⼦
<select id="selectStudentsIfWhere"resultType="domain.Student">
select * from student
<where>
<if test="name!=null and name!=''">
name = #{name}
</if>
<if test="age>0">
or age > #{age}
</if>
</where>
</select>
< trim >
< trim >标签⼀般⽤来取出sql语句中多余的and关键字,逗号。
或者SQL语句前拼接“where”、“set”以及“values(”等前缀。
或者添加“)”后缀,可以⽤于选择性插⼊、更新、删除或者条件查询等操作。
2. 属性
属性描述
prefix给sql语句拼接的前缀suffix给sql语句拼接的后缀
prefixOverrides 去除sql语句前⾯的关键字或者字符,该关键字或者字符由prefixOverrides属性指定,假设该属性指定为"AND",当sql语句的开头
为"AND",trim标签将会去除该"AND"
suffixOverrides去除sql语句后⾯的关键字或者字符,该关键字或者字符由suffixOverrides属性指定
3. 举个栗⼦ - 去除多余的and关键字
<select id="findActiveBlogLike"resultType="Blog">
SELECT * FROM BLOG
<trim prefix="WHERE"prefixOverrides="AND">
<if test="state != null">
state=#{state}
</if>
<if test="title != null">
ANDtitlelike#{title}
</if>
<if test="author !=null and author.name != null">
ANDauthor_namelike#{author.name}
</if>
</trim>
</select>
< set >
Mybatis在⽣成updata语句时若使⽤if标签,如果前⾯的if没有执⾏,则可能有多余逗号的错误。
使⽤set标签可以将动态的配置set关键字,和剔除追加到条件末尾的任何不相关的逗号。
2. 举个栗⼦
<update id="updateByPrimaryKeySelective"parameterType="RecruitmentConfBanner">
UPDATE conf_banner_t
<set>
<if test="bannerName != null">
t.banner_name = #{bannerName},
</if>
<if test="bannerUrl != null">
t.banner_url = #{bannerUrl},
</if>
<if test="bannerLogo != null">
t.banner_logo = #{bannerLogo},
</if>
<if test="bannerDescription != null">
t.banner_description = #{bannerDescription},
</if>
<if test="sort != null">
t.sort = #{sort},
</if>
<if test="isEnabled != null">
t.is_enabled = #{isEnabled},
</if>
</set>
where t.banner_id = #{bannerId}
</update>
< foreach >
1. 简介
< foreach > 循环java中的数组,list集合的。 主要⽤在sql的in语句中。
2. 语法说明
mapper:
<foreach collection=""item=""open=""close=""separator="">
#{xxx}
</foreach>
属性描述
collection
1. 表⽰接⼝中的⽅法参数的类型, 如果是数组使⽤array , 如果是list集合使⽤list,Map对象没有默认的键。
2. 当然在作为⼊参时可以使⽤@Param("keyName")来设置键,设置keyName后,list,array将会失效。
3. 除了⼊参这种情况外,还有⼀种作为参数对象的某个字段的时候。举个例⼦:如果User有属性List ids。⼊参是User对象,那么这个collection = "ids";如果User有属性Ids ids;其中Ids是个对象,Ids有
个属性List id;⼊参是User对象,那么collection = "ids.id"。
item值是⾃定义的,表⽰数组和集合中的成员的变量open循环开始时的字符
close循环结束时的字符
separator集合成员之间的分隔符
要求:查询学⽣id是 1001,1002,1003的三个学⽣
sql代码:
select*from student where id in(1001,1002,1003)
java接⼝代码:
public List<Student>selectStudentsForList(List<Integer> ids);
java测试代码
@Test
public void testSelectStudentsForList(){
SqlSession sqlSession = SqlSession();
StudentDao studentDao = Mapper(StudentDao.class);
List<Integer> list =new ArrayList<Integer>();
list.add(1001);
list.add(1002);
list.add(1004);
list.add(1009);
//list=null;
List<Student> students = studentDao.selectStudentsForList(list);
students.forEach(student->System.out.println(student));
sqlSession.close();
}
mapper:
<select id="selectStudentsForList"resultType="domain.Student">  select * from student
<where>
<if test="list==null">
id=-1
</if>
<if test="list!=null">
<foreach collection="list"open="idin("close=")"item="stuId"separator=",">    #{stuId}
</foreach>
</if>
</where>
</select>
增加字段的sql语句< bind >

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。