mabatis重点是通过标签对sql灵活的组织,通过配置的方式完成输入 输出映射.

1.对mapper.xml中重复的sql抽取统一维护,以及foreach使用

UserMapperCustom.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.itcast.mybatis.mapper.UserMapperCustom"> <!-- 定义一个sql片段,将重复的sql语句抽取出来
建议:定义查询条件以单表为单位去定义,sql片段可重用性才高
建议不要包括where
建议以单个为单位去定义查询条件,一般要将查询条件写全
-->
<sql id="query_user_where">
<!-- 如果有条件值再拼接 -->
<if test="user!=null">
<if test="user.username!=null and user.username!=''">
<!-- 用户输入条件值再拼接 -->
and username like '%${user.username}%'
</if>
<if test="user.sex!=null and user.sex!=''">
and sex = #{user.sex}
</if>
<!-- 下边要拼接:
AND id IN (1,10,16)
-->
<!-- 遍历id列表
collection:接收输入参数中的集合属性
item:每次循环定义一个对象名
open:开始循环时拼接的sql
close:结束循环时拼接的sql
separator:每两次循环中间拼接的sql
-->
<foreach collection="ids" item="id" open=" AND id IN ( " close=" ) " separator=",">
#{id}
</foreach>
<!-- 思考:如何拼接 AND (id=1 OR id=10 OR id=16) 实现 SELECT * FROM USER WHERE sex = '1' AND  id IN (1,10,16)
-->
</if>
</sql> <!-- 综合条件查询用户 -->
<select id="findUserList" parameterType="queryUserVo"
resultType="user">
select id,username,birthday,sex,address from user <!-- where标签 相关于where关键字,可以将条件中的第一个and去掉 -->
<where>
<!-- 引用sql片段
如果跨mapper引用需要前边加namespace
-->
<include refid="query_user_where"></include>
</where>
</select> <!-- 综合条件查询用户记录汇总 -->
<select id="findUserCount" parameterType="queryUserVo" resultType="int">
select count(*) from user
<!-- where标签 相关于where关键字,可以将条件中的第一个and去掉 -->
<where>
<!-- 引用sql片段
如果跨mapper引用需要前边加namespace
-->
<include refid="query_user_where"></include>
</where>
</select> </mapper>

UserMapperCustom.java
public interface UserMapperCustom {

    //综合条件查询用户信息
public List<User> findUserList(QueryUserVo queryUserVo) throws Exception; //综合条件查询用户记录总数
public int findUserCount(QueryUserVo queryUserVo) throws Exception; }

mapper.xml中动态sql抽取重复项的更多相关文章

  1. IDEA 配置datasource,提升编码效率,让你在 Mapper.xml 中编写sql可以飞起来~

    IDEA 2018 创建springboot工程后,如果你打开一个.sql文件,或者一个mybatis的mapper.xml文件,会提示: No data source are configured ...

  2. mapper.xml中的<sql>标签

    原文链接:http://blog.csdn.net/a281246240/article/details/53445547 sql片段标签<sql>:通过该标签可定义能复用的sql语句片段 ...

  3. Mybaits 源码解析 (六)----- 全网最详细:Select 语句的执行过程分析(上篇)(Mapper方法是如何调用到XML中的SQL的?)

    上一篇我们分析了Mapper接口代理类的生成,本篇接着分析是如何调用到XML中的SQL 我们回顾一下MapperMethod 的execute方法 public Object execute(SqlS ...

  4. MyBatis中动态SQL元素的使用

    掌握MyBatis中动态SQL元素的使用 if choose(when,otherwise) trim where set foreach <SQL>和<include> 在应 ...

  5. 转载:MyBatis mapper.xml中使用静态常量或者静态方法

    转自:https://my.oschina.net/wtslh/blog/682704 今天偶然之间刷到了这样一篇博客,有点意外 mybatis 还可以这样使用ONGL常量的方式,该方式针对 xml的 ...

  6. Mybatis中动态SQL语句中的parameterType不同数据类型的用法

    Mybatis中动态SQL语句中的parameterType不同数据类型的用法1. 简单数据类型,    此时#{id,jdbcType=INTEGER}中id可以取任意名字如#{a,jdbcType ...

  7. org.apache.commons.lang3.tuple.Pair 作为更新参数,XML 中的 Sql 取不到值、报错

    项目用的 Mybatis,今天改一个需求,落地实现是批量更新,且只需要根据主键(id)来更新一个字段(name). 于是,没有犹豫,像下面这样设计了数据结构: 既然是批量更新,那外层肯定是 List ...

  8. SSM框架 mapper.xml中 value的空值判断问题

    先看解决方案,其他的都是问题的出处 解决方案:if中使用 _parameter,#{value}不变 <if test="_parameter!='' and _parameter!= ...

  9. PL/SQL开发中动态SQL的使用方法

    一般的PL/SQL程序设计中,在DML和事务控制的语句中可以直接使用SQL,但是DDL语句及系统控制语句却不能在PL/SQL中直接使用,要想实现在PL/SQL中使用DDL语句及系统控制语句,可以通过使 ...

随机推荐

  1. url字符转义

    作者在做短链接功能时,url参数里带了&字符,结果无法转换.后来查了一下,发现可以用其它符号代替.下面是对应表 +    URL 中+号表示空格                         ...

  2. 提交代码至coding.net

    1.首先在本地任意目录下创建项目 2.cmd切换至该目录下,然后依次输入: git init git add . git commit -m "version 3.9" git r ...

  3. 深入浅出ConcurrentHashMap1.8

    转载:https://www.jianshu.com/p/c0642afe03e0 好文 关于文章中的疑问:为什么要构造一个反序链表,放在nextTable的i+n的位置上呢,在<深入分析Con ...

  4. csharp:qq weather

    using System; using System.Data; using System.Configuration; using System.Collections; using System. ...

  5. Jquery trigger 与 triggerHandler

    secying Jquery trigger与triggerHandler trigger: 在每一个匹配的元素上触发某类事件(即触发jQuery对象集合中每一个元素). 这个函数也会导致浏览器同名的 ...

  6. go的编译与重启

    ps -ef|grep pro-name| grep -v grep|awk '{print $2}'|xargs kill -9 > /dev/null go build nohup ./xe ...

  7. 如何判断单链表是否存在环 & 判断两链表是否相交

    给定一个单链表,只给出头指针h: 1.如何判断是否存在环? 2.如何知道环的长度? 3.如何找出环的连接点在哪里? 4.带环链表的长度是多少? 解法: 1.对于问题1,使用追赶的方法,设定两个指针sl ...

  8. windows的共享内存

    https://blog.csdn.net/stpeace/article/details/39534361

  9. msvcr100.dll丢失原因及解决方法

    msvcr100.dll为Visual Studio 2010的一个动态链接库,如果某程序是用它开发出来的,那么该程序的运行就有可能需要此动态链接库.有些程序直接将其打包到了安装目录,并注册,就不会出 ...

  10. Windows下Redis集群配置

    Redis集群学习地址:http://blog.csdn.net/dc_726/article/details/11694437 Windows-32系统下搭建Redis集群 一.Redis主从同步原 ...