1、对应注解@lSelect
2、
3、
4、
5、
6、
7、
- <choose>
- <when></when>
- <otherwise></otherwise>
- </choose>
8、
9、
10、
sql内容是变化的,可以根据条件获取到不同的sql语句,主要是where部分发生变化
动态sql的实现,使用的是mybatis提供的标签:
语法:<if test="判断java对象的属性值">
如下:
- <select id="selectUsersIf" resultType="com.mycompany.domain.User">
- select user_id,user_name,email,age
- from user
- where
-
- <if test="userName != null and userName != ''">
- user_name = #{userName}
- if>
- <if test="age > 0">
- or age > #{age}
- if>
- select>
这样会存在一个问题:如果 age 属性值为null,则会造成SQL语句错误,多带一个 "or" 字段,因此我们在where 条件语句后面加一个 恒成立等式,如 "1 = 1","user_id > 0",如下格式:
- <select id="selectUsersIf" resultType="com.mycompany.domain.User">
- select user_id,user_name,email,age
- from user
- where user_id > 0
-
- <if test="userName != null and userName != ''">
- and user_name = #{userName}
- if>
-
- <if test="age > 0">
- or age > #{age}
- if>
- select>
标签的中存在一个问题:需要在 where 后手工添加 1=1 恒成立的子句;因为若 where 后 的所有
使用
注:
第一个
其它
语法:<where> 其他动态 sql </where>
如下:
- <select id="selectUsersWhere" resultType="com.mycompany.domain.User">
-
-
- <include refid="selectUser" />
- <where>
- <if test="userName != null and userName != ''">
- user_name = #{userName}
- if>
-
- <if test="age > 0">
- or age > #{age}
- if>
- where>
- select>
- 语法:
-
- collection:表示接口中的方法参数的类型, 如果是数组使用array , 如果是list集合使用list
- item:自定义的,表示数组和集合成员的变量
- open:循环开始时的字符
- close:循环结束时的字符
- separator:集合成员之间的分隔符
-
- <foreach collection="集合类型" item="自定义字段" open="开始字符" close="结束字符" separator="分隔字符">
- #{item的值}
- </foreach>
collection:表示接口中的方法参数的类型, 如果是数组使用array , 如果是list集合使用list
item:自定义的,表示数组和集合成员的变量
open:循环开始时的字符
close:循环结束时的字符
separator:集合成员之间的分隔符
1、遍历List<简单类型>
- <select id="selectForeachOne" resultType="com.mycompany.domain.User">
- select user_id,user_name,email,age
- from user
- <if test="list !=null and list.size > 0 ">
- where user_id in
- <foreach collection="list" item="myId" open="(" close=")" separator=",">
- #{myId}
- foreach>
- if>
- select>
2、遍历List<对象类型>
- <select id="selectForeachTwo" resultType="com.mycompany.domain.User">
- select user_id,user_name,email,age
- from user
- <if test="list !=null and list.size > 0 ">
- where user_id in
- <foreach collection="list" item="user" open="(" close=")" separator=",">
- #{user.userId}
- foreach>
- if>
- select>
我们也可将上述写法手动拼接,如:
- <select id="selectForeachTwo2" resultType="com.mycompany.domain.User">
- select user_id,user_name,email,age
- from user
- <if test="list !=null and list.size > 0 ">
- where user_id in (
- <foreach collection="list" item="user">
- #{user.userId},
- foreach>
- -1 )
- if>
- select>
- 1、定义 <sql id="自定义名称唯一"> sql语句, 表名,字段等 sql>
- 2、使用, <include refid="id的值" />
定义SQL片段如下:
- <sql id="selectUser">
- select user_id,user_name,email,age
- from user
- sql>
-
- <sql id="selectUserOne">
- user_id,user_name,email,age
- sql>
使用SQL片段如下:
- <select id="selectUsersWhere" resultType="com.mycompany.domain.User">
-
-
-
-
- <include refid="selectUser" />
-
- <where>
- <if test="userName != null and userName != ''">
- user_name = #{userName}
- if>
- <if test="age > 0">
- or age > #{age}
- if>
- where>
- select>
1、如果使用数组作为参数,那么使用length字段
<if test="dataArr!=null and dataArr.length >0">
2、如果使用List作为参数,那么使用size()方法
<if test="dataList!=null and dataList.size() >0">