动态sql

1、if

需求:若有为空的字段,则不加入数据库查询语句

mapper接口类:

List<Address> queryByCountryCity(@Param("state") String state, @Param("city") String city);

mapper xml文件:

 1 <select id="queryByCountryCity" resultType="com.th.pojo.Address">
 2     select * from address
 3     where
 4     1=1
 5     <if test="state != null and state != ''">
 6           and state = #{state}  //不为空,执行该句
 7     </if>
 8     <if test="city != null">
 9           and city = #{city}
10     </if>
11 </select>

2、where

需求:加入where元素,进行筛选,where元素可以自动过滤前置的多余的and

mapper接口类:

List<Address> queryByCountryCity(@Param("state") String state, @Param("city") String city);

mapper xml文件:

 1  <select id="queryByCountryCity" resultType="com.th.pojo.Address">
 2         select * from address
 3         <where>
 4             <if test="state != null and state != ''">
 5                 and state = #{state}
 6             </if>
 7             <if test="city != null">
 8                 and city = #{city}
 9             </if>
10         </where>
11     </select>

3、set

需求:加入set元素,进行更新时的参数设置,set元素可以自动去除多余的后置“,”(逗号)

mapper接口类:

int update(Address address);

mapper xml文件:

 1   <update id="update" parameterType="com.th.pojo.Address">
 2         update address
 3         <set>
 4             <if test = "name != null and name != ''">
 5                 name = #{name},
 6             </if>
 7             <if test = "city != null and city != ''">
 8                 city = #{city},
 9             </if>
10             <if test = "state != null and state != ''">
11                 state = #{state},
12             </if>
13             <if test = "zip != null and zip != ''">
14                 zip = #{zip},
15             </if>
16             <if test = "country != null and country != ''">
17                 country = #{country}
18             </if>
19         </set>
20 
21         <where>
22             ADDSID = #{addsId}
23         </where>
24     </update>

4、trim

属性:prefix、prefixOverrides、suffix、suffixOverrides

需求:可以综合处理前面或者后面的符号,只需要通过overrides去声明可能会多一个符号;prefix可以自动添加一个前缀

mapper接口类:

 List<Address> queryTrim(Address address);

mapper xml文件:

 1  <select id="queryTrim" resultType="com.th.pojo.Address">
 2       select * from address
 3         <trim prefix="where" suffixOverrides="AND">
 4             <if test="name != null and name != ''">
 5                    NAME = #{name} and
 6             </if>
 7             <if test="city != null and city != ''">
 8                   CITY = #{city} and
 9             </if>
10             <if test="state != null and state != ''">
11                        STATE = #{state} and
12             </if>
13             <if test="zip != null and zip != ''">
14                  ZIP = #{zip} and
15             </if>
16             <if test="country != null and country != ''">
17                   COUNTRY = #{country} and
18             </if>
19           </trim>
20       </select>

5、foreach

需求:类似sql语句中的包含in的查询语句,在mybatis的动态sql语句中写法需要用到foreach遍历in中的数据

mapper接口类:

List<Address> queryByIds(List<Integer> list);

对比mysql查询语句

select * from address where ADDSID IN(1,3);

mapper xml文件:

1  <!--
 2         foreach
 3         collection 描述集合 list set map
 4         open 是in后面的sql语句的拼接以什么开头
 5         close 以什么结尾
 6         item 是具体的一个数据项
 7         separator item之间的分隔符
 8         index 标号,对应item在collection中的下标
 9     -->
10     <select id="queryByIds" resultType="com.th.pojo.Address">
11         select * from address
12         <where>
13             ADDSID in
14             <foreach collection="list" open="(" close=")" item="item" separator="," index="index">
15                 #{item} + #{index}
16             </foreach>
17         </where>
18     </select>

6、like、bind

需求:模糊查询

mapper接口类:

List<Address> queryLike(@Param("city") String city);

mapper xml文件:

 1  <!--
 2         模糊查询
 3         1、在应用程序层直接传入拼接好的,如%city%
 4         2、通过mysql的函数concat
 5         city like concat("%",#{city},"%")
 6         3、通过bind标签对参数重新进行绑定
 7         <bind name="_city" value="'%'+city+'%'"/>
 8         city like #{_city}
 9         4、city like "%"#{city}"%"
10     -->
11     <select id="queryLike" resultType="com.th.pojo.Address">
12        
13         select * from address
14         <where>
15             city like "%"#{city}"%"
16         </where>
17     </select>

7、sql、include

需求:查询指定列名

mapper接口类:

List<Address> listAll();

mapper xml文件:

 1  <!--
 2         将常用的列抽取成一个sql片段,以便被引用
 3     -->
 4     <sql id="baseColumn">
 5         country,city,state
 6     </sql>
 7  <select id="listAll" resultType="com.th.pojo.Address">
 8         select
 9         <include refid="baseColumn"/>
10         from address
11     </select>
原文地址:https://www.cnblogs.com/slfh/p/10039675.html