Mybatis- 动态sql总结
1、if标签
<select id="getEmpsByConditionIf" resultType="com.atguigu.mybatis.bean.Employee"> select * from tbl_employee where 1=1 <if test="id!=null"> and id = #{id} </if> <if test="lastName!=null and lastName !=''"> and last_name like '%${lastName}%' </if> <if test="email!=null"> and email like '%${email}%' </if> <if test="gender==0 or gender == 1"> and gender = #{gender} </if> </select>
2、where标签
去除动态sql中多余的and 和 or
<select id="getEmpsByConditionIf" resultType="com.atguigu.mybatis.bean.Employee"> select * from tbl_employee <where> <if test="id!=null"> and id = #{id} </if> <if test="lastName!=null and lastName !=''"> and last_name like '%${lastName}%' </if> <if test="email!=null"> and email like '%${email}%' </if> <if test="gender==0 or gender == 1"> and gender = #{gender} </if> </where> </select>
3、trim 字符串截取
<select id="getEmpsByConditionTrim" resultType="com.atguigu.mybatis.bean.Employee"> select * from tbl_employee <!-- 后面多出的and or where 标签不能解决 trim标签体中是整个字符串拼拼接后的结果 prefix:给拼串后的字符串加一个前缀 prefixOverrides="" : 前缀覆盖 去掉整个字符串前面多余的字符串 suffixOverrides="" : 后缀覆盖 去掉整个字符串后面多余的字符串 --> <trim prefix="where" suffixOverrides="and"> <if test="id!=null"> id = #{id} and </if> <if test="lastName!=null and lastName !=''"> last_name like '%${lastName}%' and </if> <if test="email!=null"> email like '%${email}%' and </if> <if test="gender==0 or gender == 1"> gender = #{gender} and </if> </trim> </select>
4、choose when
<select id="getEmpsByConditionChoose" resultType="com.atguigu.mybatis.bean.Employee"> select * from tbl_employee <where> <choose> <when test="id!=null"> and id = #{id} </when> <when test="lastName!=null and lastName !=''"> and last_name like '%${lastName}%' </when> <otherwise> and gender = 1 </otherwise> </choose> </where> </select>
5、set
用来更新数据库中的字段
<update id="updateEmp"> <!-- Set标签的使用 --> update tbl_employee <set> <if test="lastName!=null"> last_name=#{lastName}, </if> <if test="email!=null"> email=#{email}, </if> <if test="gender!=null"> gender=#{gender} </if> </set> where id=#{id} <!-- Trim:更新拼串 update tbl_employee <trim prefix="set" suffixOverrides=","> <if test="lastName!=null"> last_name=#{lastName}, </if> <if test="email!=null"> email=#{email}, </if> <if test="gender!=null"> gender=#{gender} </if> </trim> where id=#{id} --> </update>
6、foreach
<select id="getEmpsByConditionForeach" resultType="com.atguigu.mybatis.bean.Employee"> select * from tbl_employee <!-- collection:指定要遍历的集合: list类型的参数会特殊处理封装在map中,map的key就叫list item:将当前遍历出的元素赋值给指定的变量 separator:每个元素之间的分隔符 open:遍历出所有结果拼接一个开始的字符 close:遍历出所有结果拼接一个结束的字符 index:索引。遍历list的时候是index就是索引,item就是当前值 遍历map的时候index表示的就是map的key,item就是map的值 #{变量名}就能取出变量的值也就是当前遍历出的元素 --> <foreach collection="ids" item="item_id" separator="," open="where id in(" close=")"> #{item_id} </foreach> </select>
mysql foreach 批量保存的两种方式:
<!-- 第一种 --> <!--public void addEmps(@Param("emps")List<Employee> emps); --> <!--MySQL下批量保存:可以foreach遍历 mysql支持values(),(),()语法--> <insert id="addEmps"> insert into tbl_employee(last_name,email,gender,dept_id) values <foreach collection="emps" item="emp" separator=","> (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id}) </foreach> </insert> <!-- 第二种 --> <insert id="addEmps"> <foreach collection="emps" item="emp" separator=";"> insert into tbl_employee(last_name,email,gender,dept_id) values(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id}) </foreach> </insert>
7、内置参数_parameter&_databaseId
两个内置参数:
不只是方法传递过来的参数可以被用来判断,取值。。。
mybatis默认还有两个内置参数:
_parameter:代表整个参数
单个参数:_parameter就是这个参数
多个参数:参数会被封装为一个map;_parameter就是代表这个map
_databaseId:如果配置了databaseIdProvider标签。
_databaseId就是代表当前数据库的别名
<select id="getEmpsTestInnerParameter" resultType="com.atguigu.mybatis.bean.Employee"> <if test="_databaseId=='mysql'"> select * from tbl_employee <if test="_parameter!=null"> where last_name like #{lastName} </if> </if> <if test="_databaseId=='oracle'"> select * from employees <if test="_parameter!=null"> where last_name like #{_parameter.lastName} </if> </if> </select>
8、bind
可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值
<select id="getEmpsTestInnerParameter" resultType="com.atguigu.mybatis.bean.Employee"> <!-- bind:可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值 --> <bind name="_lastName" value="'%'+lastName+'%'"/> select * from tbl_employee <if test="_parameter!=null"> where last_name like #{_lastName} </if> </select>
9、可重用sql片段
定义sql片段:
<sql id="sqlColumn"> id,last_name,email,gender </sql>
引用sql片段:
<select id="getEmpsByConditionChoose" resultType="com.atguigu.mybatis.bean.Employee"> select <include refid="sqlColumn"></include> from tbl_employee <where> <choose> <when test="id!=null"> and id = #{id} </when> <when test="lastName!=null and lastName !=''"> and last_name like '%${lastName}%' </when> <otherwise> and gender = 1 </otherwise> </choose> </where> </select>