1、定义基础的映射

对象DO与数据库字段间的映射

<resultMap id="UserResult" type="UserDO">
<result property="id" column="id" jdbcType="BIGINT"/>
<result property="gmtCreate" column="gmt_create" jdbcType="TIMESTAMP"/>
<result property="gmtModified" column="gmt_modified" jdbcType="TIMESTAMP"/>
<result property="userName" column="user_name" jdbcType="VARCHAR"/>
<result property="deleted" column="deleted" jdbcType="DECIMAL"/>
</resultMap>

2、定义sql语句字段模版

字符类型判断null和' ',其他类型只判null

Integer,Long之类的类型传0,如果是id要加判断''或者判断是否等于0,这样对于0值,会直接跳过判断语句

如果是状态类型的数值,只要判null,不然会过滤掉0这个条件判断

    <sql id="baseColumns">
//sql语句前置DO对象字段模版
id,
gmt_create,
gmt_modified,
user_name,
deleted
</sql> <sql id="insertColumns">
//插入sql语句后置传入对象模版
#{id},
now(),
now(),
#{userName},
#{deleted}
</sql> <sql id="batchInsertColumns">
/批量插入sql语句后置传入对象模版,配合foreach使用
#{item.id},
now(),
now(),
#{item.userName},
#{item.deleted}
</sql> <!--动态更新列-->
<sql id="updateColumns">
//sql语句更新列模版
<if test="userName != null and userName != ''">,
user_name = #{userName},
</if>
<if test="deleted != null">,
deleted = #{deleted}
</if>
</sql> <!--批量动态更新列-->
<sql id="batchUpdateColumns">
//sql语句批量更新列模版
<if test="item.userName != null and item.userName != ''">,
user_name = #{item.userName}
</if>
</sql> <sql id="whereParam">
//sql语句查询条件模版
<where>
<if test="id != null and id != ''">
//基础模版
AND id = #{id}
</if>
<if test="start != null">
//日期格式模版
<![CDATA[
AND gmt_create >= #{start} ]]>
</if>
<if test="end != null">
// <![CDATA[]]> 不让xml编译内部语句
<![CDATA[
AND gmt_create <= #{end} ]]>
</if> <if test="userName != null and userName != ''">
//模糊查询模版
AND user_name like CONCAT('%',#{userName},'%')
</if> <if test="userName != null and userName != ''">
//模糊查询使用bind标签 https://www.cnblogs.com/youmingDDD/p/9435422.html
<bind name="userNameLike" value=" '%' + userName + '%' "/>
and user_name like #{userNameLike}
</if> </where>
</sql>

//基础实例

<insert id="insert" parameterType="UserDO">
insert into sx_user (<include refid="baseColumns"/>)
values (<include refid="insertColumns"/>)
</insert> <insert id="batchInsert" parameterType="java.util.List">
INSERT INTO sx_user (<include refid="baseColumns"/>)
VALUES
<foreach collection="list" item="item" index="index" separator=",">
(<include refid="batchInsertColumns"/>)
</foreach>
</insert> <update id="updateById" parameterType="UserDO">
update sx_user set gmt_modified=now(),
<include refid="updateColumns"/>
where id = #{id}
</update> <update id="batchUpdateById" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update sx_user set gmt_modified=now(),
<include refid="batchUpdateColumns"/>
where id = #{item.id}
</foreach>
</update> <select id="countPageByCondition" parameterType="QueryCondition" resultType="java.lang.Long">
SELECT COUNT(*)
FROM sx_user
<include refid="whereParam"/>
</select>
<select id="selectList" resultMap="UserResult">
select
<include refid="baseColumns"/>
from sx_user
where id = #{id}
</select> <select id="listByIds" resultMap="UserResult">
select
<include refid="baseColumns"/>
from sx_user
where id IN
<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
//in中多个参数拼接
<!-- 如果直接传String ids (case:1,2,3,4)
<foreach collection="ids .split(',')" item="id" open="(" separator="," close=")">
#{id}
</foreach>
-->
#{item}
</foreach>
</select> <delete id="deleteById">
delete from sx_user where id = #{id}
</delete>

mybatis映射文件模板mapper.xml格式的更多相关文章

  1. MyBatis 逆向工程——根据数据表自动生成model、xml映射文件、mapper接口

    MyBatis Generator(MBG)的使用 MBG可以根据数据表生成对应的model.xml映射文件.mapper接口,只是简单的生成,还需要根据需求修改. 1.下载jar包 https:// ...

  2. mybatis映射文件mapper.xml的写法(collections...)

    转自:https://blog.csdn.net/two_people/article/details/51759881 在学习mybatis的时候我们通常会在映射文件这样写: <?xml ve ...

  3. Mybatis映射文件完整模板参照

    Mybatis映射文件完整模板参照 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...

  4. mybatis根据表逆向自动化生成代码(自动生成实体类、mapper文件、mapper.xml文件)

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  5. mybatis映射文件(转)

    以下内容为转载, 格式未调整,略丑,可直接空降至: [Mybatis框架]输出映射-resultType与resultMap 有时间或看: Mybatis 3.1中 Mapper XML 文件 的学习 ...

  6. MyBatis映射文件 相关操作

    一.MyBatis映射文件 1.简介 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行 ...

  7. SSM实战——秒杀系统之DAO层实体定义、接口设计、mybatis映射文件编写、整合Spring与Mybatis

    一:DAO实体编码 1:首先,在src目录下,新建org.myseckill.entity包,用于存放实体类: 2:实体类设计 根据前面创建的数据库表以及映射关系,创建实体类. 表一:秒杀商品表 对应 ...

  8. MyBatis 映射文件详解

    1. MyBatis 映射文件之<select>标签 <select>用来定义查询操作; "id": 唯一标识符,需要和接口中的方法名一致; paramet ...

  9. MyBatis 映射文件

    Mybatis映射文件简介 1) MyBatis 的真正强大在于它的映射语句.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉 ...

随机推荐

  1. jQuary总结9:html()的常见用法

    1html() 不传参数 用于获取内容 //html <div> <p></p> <span></span> 文本 </div> ...

  2. 编写高质量代码改善C#程序的157个建议——建议100:静态方法和实例方法没有区别

    建议100:静态方法和实例方法没有区别 静态方法在加载时机和内存使用上和实例方法完全一致.在这里,我们先引出一个概念“类型对象”.比如类型Person,我们都知道new Person() 会产生一个对 ...

  3. MongoDB整理笔记のjava MongoDB分页优化

    最近项目在做网站用户数据新访客统计,数据存储在MongoDB中,统计的数据其实也并不是很大,1000W上下,但是公司只配给我4G内存的电脑,让我程序跑起来气喘吁吁...很是疲惫不堪. 最常见的问题莫过 ...

  4. SAH Benchmarks Of Natural History Museum Scene

    method                                                                                               ...

  5. 【04】循序渐进学 docker:Dockerfile

    写在前面的话 从前面我们简单的了解了镜像,也运行了容器,各种官方的镜像显然无法满足我们自己的需求,我们目的终究是运行自己的业务. 所以,本章节的 Dockerfile 就主要讲怎么在官方镜像的基础上制 ...

  6. Android源码:(一) 安卓2.1到4.4操作系统通用的Actionbar实现的tab导航例子。

    说一下我在完成这个例子之前的尝试吧 一,修改“actionbarsherlock”的导航例子.我在实现这个例子之前,尝试过“actionbarsherlock”,修改它的一个tab导航的例子,修改成功 ...

  7. 四、Centos linux系统优化

    1.     无论是哪个版本的linux,都会提供32位和64位的两个版本的镜像. i386为32位 x86_64为64位 两者的区别: 1)目标:需要大量的内存需求的行业为64位,普通用户的需求为3 ...

  8. 趣图:当我捕获Bug的时候

      趣图:当我以为已捕获了所有可能的异常...的时候 趣图:程序员调 Bug 的感觉,就是这样的

  9. “全栈2019”Java第三十四章:可变参数列表

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  10. [ActionScript 3.0] 动态改变影片剪辑的颜色

    flash.geom.ColorTransform 可使用 ColorTransform 类调整显示对象的颜色值.可以将颜色调整或颜色转换应用于所有四种通道:红色.绿色.蓝色和 Alpha 透明度. ...