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. 自动收缩数据库T-SQL

    alter database 数据库名 set auto_update_statistics off alter database 数据库名 set auto_update_statistics on

  2. HttpContext在多线程异步调用中的使用方案

    1.在线程调用中,有时候会碰到操作文件之类的功能.对于开发人员来说,他们并不知道网站会被部署在服务器的那个角落里面,因此根本无法确定真实的物理路径(当然可以使用配置文件来配置物理路径),他们唯一知道的 ...

  3. Entity Framework6 with Visual Studio 2013 update3 for Oracle 11g

    2014年7月的时候,写了一篇关于EF5 with visual studio 2010 for oracle 11g的博文 原文地址 :http://www.cnblogs.com/HouZhiHo ...

  4. nodejs操作excel并配合edatagrid使用

    nodejs读取文件夹下子文件(夹)名称: /** * 查询tmp文件夹下子文件夹名称 */ router.post("/tmpList", function (req, res) ...

  5. SQL语句表处理

    1.create table:创建表,语法: create table 表格名 ( 列名 数据类型, 列名 数据类型, ...... ) 2.constrain:约束,关键词 NOT NULL. UN ...

  6. mysql存储过程优化

    示例 WHILE s <> 1 DO select xxx; insert into xxx; END WHILE; 执行耗时27秒 优化点1: 添加事物 START TRANSACTIO ...

  7. java 断点续传(springMvc),可支持html5 vedio在线播放 posted @ 2017年3月11日 16:15:44

    Controller @RequestMapping(value = "/getVedio") public void getVedio(HttpServletRequest re ...

  8. SZU5

    A - Couple doubi 这种题不要想复杂,直接找规律.找不出规律就打表找规律 #include <iostream> #include <string> #inclu ...

  9. int btn = (Button) findViewById(View.getId());

    int btn = (Button) findViewById(View.getId());//这句话中的btn不能用来和按钮键Button的id号去比较 如果想存储Button,可以这样做: Sta ...

  10. H5前端的关于像素解释

    场景: 人物:前端实习生「阿树」与 切图工程师「玉凤」 事件:设计师出设计稿,前端实现页面 玉凤:树,设计稿发给你啦,差那么点像素,就叼死你┏(  ̄へ ̄)=☞ 阿树:~(>_<)~毛问题噶 ...