Mysql动态查询
if条件查询
格式: <if test=”条件判断”> 添加到sql的语句 </if>
where标签
简化SQL语句中WHERE条件判断
智能处理and和or
如果使用几个if条件标签,如果第一个条件不成立,那么该sql语句就不成立了.
把所有的if条件语句加入到where标签内,则会根据if语句成立是否添加where条件,若标签返回的内容是以and或者or开头的,会自动剔除.
案例:
<!-- 分类后根据关键字进行搜索 -->
<select id="getUserListByClassify" resultMap="userList">
SELECT user.*,role.roleName
FROM smbms_user user
INNER JOIN smbms_role role ON user.userRole=role.id
<where>
<if test="userName != null">
AND userName like CONCAT('%',#{userName},'%')
</if>
<if test="userRole != null">
AND userRole=#{userRole}
</if>
</where>
</select>
trim标签
属性:
prefix:前缀 如果有返回值,在标签前加的语句
suffix:后缀 如果有返回值,在标签后加的语句
prefixOverrides:对于trim包含内容的首部进行指定内容的忽略
sufixOverrides:对于trim包含内容的尾部进行指定内容的忽略
可以去除多余的关键字(自定义) where只能去除多余的and/or
<!-- 分类后根据关键字进行搜索 -->
<select id="getUserListByClassify" resultMap="userList">
SELECT user.*,role.roleName
FROM smbms_user user
INNER JOIN smbms_role role ON user.userRole=role.id
<trim prefix="where" prefixOverrides="and | or">
<if test="userName != null">
userName like CONCAT('%',#{userName},'%')
</if>
<if test="userRole != null">
and userRole=#{userRole}
</if>
</trim>
</select>
if+set进行修改(在实际开发中很少使用)
如何只对有值得参数进行修改,而不必每次都要获取整个对象.
案例:
<!-- 更新用户属性 -->
<update id="update" parameterType="User">
UPDATE smbms_user
<set>
<if test="userCode != null">userCode=#{userCode},</if>
<if test="userName!= null">userName=#{userName},</if>
<if test="userPassword!= null">userPassword=#{userPassword},</if>
<if test="gender!= null">gender=#{gender},</if>
<if test="birthday!= null">birthday=#{birthday},</if>
<if test="phone!= null">phone=#{phone},</if>
<if test="address!= null">address=#{address},</if>
<if test="modifyDate!= null">modifyDate=#{modifyDate}</if>
</set>
where id=#{id}
</update>
if+trim进行修改
添加条件判断修改
<!-- 更新用户属性 -->
<update id="update" parameterType="User">
UPDATE smbms_user
<trim prefix="set" prefixOverrides="," resultMap="userList">
<if test="userCode != null">userCode=#{userCode},</if>
<if test="userCode != null">userName=#{userName},</if>
<if test="userCode != null">userPassword=#{userPassword},</if>
<if test="userCode != null">gender=#{gender},</if>
<if test="userCode != null">birthday=#{birthday},</if>
<if test="userCode != null">phone=#{phone},</if>
<if test="userCode != null">address=#{address},</if>
<if test="userCode != null">modifyDate=#{modifyDate}</if>
</trim>
where id=#{id}
</update>
使用foreach进行复杂查询
迭代一个集合,通常用于in条件
属性:
item:表示集合中每一个元素进行迭代时候的别名
index:指定一个名称,表示迭代过程中,每次迭代到的位置.(可省略...)
open:表示语句以什么开始 例:open=”(”
separator:表示每次进行迭代之间以什么作为分隔符.
close:表示该语句以什么结束.
collection:最关键并最容易出错的属性,该属性必须指定,不同情况下,该属性的值也不一样,主要分三种:
- 若传入的参数为单参数且参数类型是一个List的时候,collection属性值为list.
- 若入参为单参数且参数类型为数组的时候,collection属性值为array
- 若传入的参数为多参数,就需要把他们封装为一个Map处理,属性值为Map的key.
案例:使用foreach实现 全选 删除
<!-- 删除用户 -->
<delete id="delete" parameterType="map">
DELETE FROM smbms_user WHERE id IN
<!-- 传入的参数为map集合时 collection=为map的key -->
<foreach collection="id" item="userId" open="(" separator="," close=")">
#{userId}
</foreach>
<!-- 传入的参数为数组时候 collection="array" -->
<foreach collection="array" item="userId" open="(" separator="," close=")">
#{userId}
</foreach>
<!-- 传入的参数为List时候 collection="list" -->
<foreach collection="list" item="userId" open="(" separator="," close=")">
#{userId}
</foreach>
</delete>
choose(when,otherwise)
类似于java的switch --case语句,跟JSTL的choose使用方式一样.
when元素:当test属性中条件满足的时候.就会输出when元素的内容.跟java中switch一样,依次向下执行,当有一个when中的test条件成立后,执行该语句立马跳出choose,所以choose中只有一个条件会输出.
MyBatis分页
获取数据的页数
<!-- 获取数据的总记录数 -->
<select id="getAllPages" resultType="Integer">
SELECT COUNT(`id`) AS pages FROM smbms_user
<trim prefix="where" prefixOverrides="and | or">
<if test="userRole != null">
userRole=#{userRole};
</if>
</trim>
</select>
注:if或when标签是针对JAVABEAN或者MAP的,进行判断的参数要么使用注解进行封装,或传入对象,或添加进map在进行传参数.
分页
<!-- 查询所有用户 并进行分页 -->
<select id="getAllUser" resultType="User">
SELECT user.*,role.roleName
FROM smbms_user user
INNER JOIN smbms_role role ON user.userRole=role.id
ORDER BY creationDate DESC LIMIT #{from},#{pageSize}
</select>
获取最后一条插入数据的id(自增主键)
最新文章
- system verilog的一些总结(从其他博客复制来的)
- 从Prototype学习JavaScript面向对象编程
- 区别 Jquery对象和Dom对象
- java 深度探险 java 泛型
- 用多态来实现U盘,Mp3,移动硬盘和电脑的对接,读取写入数据。
- ModSecurity SQL注入攻击
- php连接ftp的研究,自带ftp函数 | fsockopen | curl实现ftp的连接
- 【POJ】【2699】The Maximum Number of Strong Kings
- linux下c程序调用reboot函数实现直接重启【转】
- Yii 1 数据库操作 笔记
- 地图 ajax-数据库
- PowerDesigner16.5 生成SQL脚本执行出错:collate chinese_prc_ci_as
- java JDBC操作MySQL数据库
- ubuntu系统分区方案
- poj 2723 Get Luffy Out 二分+2-sat
- Struts 2.x仍然明显落后于时代。 Struts 2.x这一类老牌Web MVC开发框架仅能用于开发瘦客户端应用,无法用来开发对于交互体验要求更高的应用。
- RabbitMQ集群和失败处理
- oracle 例外
- JAVA面向对象-----成员内部类访问细节
- Git 简单粗暴使用
热门文章
- [bzoj1207][HNOI2004]打鼹鼠_动态规划
- java web 验证码 第一次不正确的问题,解决方案
- Linux 网络配置,ifconfig不显示ip地址的解决办法
- JSP的客户端请求
- 怎么让Excel显示时间时候能把秒显示出来
- C++ auto 与 register、static keyword 浅析
- powerShell赋权限
- java中关于&#39;&;&;&#39;、&#39;||&#39;混合运算优先级问题小结
- 华为OJ:数字颠倒
- 2014阿里巴巴WEB前端实习生在线笔试题