Mybaits动态SQL语句
在Mybaits的映射文件中,当业务逻辑复杂的时候,SQL语句是动态变化的,这个时候可以使用动态SQL来操作
我们定义一个实体类Blog
public class Blog {
private String id;
private String title;
private String author;
// <setting name="mapUnderscoreToCamelCase" value="true"/>
//开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。
private Date createTime;//字段名不一致
private int views;
}
1.1 if标签
在使用if标签的时候,可以根据实体类的不同取值,使用不同的SQL语句来进行查询。
public interface BlogMapper {
//插入数据
int addBlog(Blog blog);
List<Blog> queryBlogByIF(Map map);
List<Blog> queryBlogByChoose(Map map);
int updateBlogBySet(Map map);
List<Blog> queryBlogByForeach(Map map);
}
对应的insertSelective方法
<insert id="insertSelective" keyColumn="aid" keyProperty="aid" parameterType="com.gx.model.Account" useGeneratedKeys="true">
insert into account
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="aname != null">
aname,
</if>
<if test="apass != null">
apass,
</if>
<if test="aNikename != null">
a_nikename,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="aname != null">
#{aname,jdbcType=VARCHAR},
</if>
<if test="apass != null">
#{apass,jdbcType=VARCHAR},
</if>
<if test="aNikename != null">
#{aNikename,jdbcType=VARCHAR},
</if>
</trim>
</insert>
if标签里面的test属性写的是对象的属性名。像上面的例子的话,可以是如果该对象的属性不为null,那么就进行插入执行。
1.2 where标签
我们使用查询的时候,使用where标签
<select id="queryBlogByIF" parameterType="map" resultType="blog">
select
*
from
blog
<where>
<if test="title!=null">
and title=#{title}
</if>
<if test="author!=null">
and author=#{author}
</if>
</where>
</select>
使用where标签的话,如果where标签中的if标签成立,那么他就是自动补上一个where,保证sql语句的正确。
如果存在多个if标签,多个标签中存在关系,比如OR和AND 。使用where标签的话,就可以判断OR和AND的出现时机。如果标签中返回的内容是以OR和AND开头,那么会自动去除掉。
1.3 set标签
set的使用一般是在更新操作的时候。
<update id="updateBlogBySet" parameterType="map">
update blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="author != null">
author = #{author}
</if>
</set>
where id=#{id}
</update>
这个时候就可以根据set标签中的if标签进行判断,如果if标签中的条件成立,那么其中的sql语句就会拼接
1.4 choose标签
这个标签类似于Java中的switch语句
<select id="queryBlogByChoose" parameterType="map" resultType="blog">
select
*
from
blog
<where>
<choose>
<when test="title!=null">
title=#{title}
</when>
<when test="author!=null">
and author=#{author}
</when>
<otherwise>
and views=#{views}
</otherwise>
</choose>
</where>
</select>
测试类
@Test
public void queryBlogByChoose(){
SqlSession session = MybatisUtils.getSqlSession();
BlogMapper mapper = session.getMapper(BlogMapper.class);
HashMap<String, String> map = new HashMap<String, String>();
//只会选择一个匹配的。从上到下
map.put("views","9999");
map.put("title","Java如此简单");
map.put("author","LBJ");
List<Blog> list = mapper.queryBlogByChoose(map);
System.out.println(JSON.toJSONString(list,true));
}
choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则choose结束**。**当choose中所有when的条件都不满则时,则执行otherwise中的sql。
1.5 trim标签
常用的属性:
prefix=”where”
给第一符合条件的语句 加上前缀where
prefixOverrides=”and”
将最后一条语句的 前缀and 覆盖
suffix=”and”
给第一符合条件的语句 加上后缀 and
suffixOverrides=”and”
将最后一条语句的后缀 and 覆盖
<select id="selectEmployeeList" resultType="com.worldly.config.entity.Employee" databaseId="mysql">
select
*
from blog
<trim prefix="where" suffixOverrides="and">
<if test="name!=null and name!=''">
blog_name=#{name,jdbcType=VARCHAR} and
</if>
<if test="dep!=null">
blog_dep=#{dep.id,jdbcType=INTEGER} and
</if>
</trim>
</select>
1.6 sql
这个元素可以被用来定义可重用的 SQL 代码段
<sql id="if_title_author_views">
<if test="title!=nulll">
title=#{title},
</if>
<if test="author!=nulll">
author=#{author},
</if>
<if test="views!=nulll">
views=#{views}
</if>
</sql>
其他的语句可以使用include进行sql代码段的引用。
1.7 使用map集合
<!--传递一个map map中存在一个集合-->
<select id="queryBlogByForeach" parameterType="map" resultType="blog">
select * from blog
<where>
<foreach collection="ids" item="id" open="and (" close=")" separator="or">
id=#{id}
</foreach>
</where>
</select>
测试类
@Test
public void queryBlogByForeach(){
SqlSession session = MybatisUtils.getSqlSession();
BlogMapper mapper = session.getMapper(BlogMapper.class);
HashMap map = new HashMap();
List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(2);
ids.add(3);
map.put("ids",ids);
List<Blog> list = mapper.queryBlogByForeach(map);
System.out.println(JSON.toJSONString(list,true));
}
在xml中的foreach标签中,collection是一个名称为ids 的一个集合,对集合进行遍历,对应到的每一个属性的名
称为id。
这块使用map集合进行传参的时候比较好一些。指定map集合的键名为集合的名称,便可以自动匹配,进行对应
值(集合)的遍历。