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. [C#] IEnumerable vs IQueryable

    这篇博客将介绍IEnumerable和IQueryable之间的区别. 1. IQueryable是继承自IEnumerable接口的.所以IEnumerable能做的,IQueryable都能做. ...

  2. 3.3.7 跳表 SkipList

    一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下,ConcurrentHashMap 存取速度是ConcurrentSki ...

  3. java/rabbitmp发布订阅示例(转)

    原文:http://www.cnblogs.com/tinmh/p/6134875.html 发布/订阅模式即生产者将消息发送给多个消费者. 下面介绍几个在发布/订阅模式中的关键概念-- 1. Exc ...

  4. (1)WePHP 开启WePHP

    新建入口文件index.php,定义新项目的目录地址APP_PATH,引入WePHP项目入口文件 <?php define('APP_PATH','./index/'); require_onc ...

  5. 团体程序设计天梯赛L1-023 输出GPLT 2017-03-22 17:56 39人阅读 评论(0) 收藏

    L1-023. 输出GPLT 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个长度不超过10000的.仅由英文字母构成的 ...

  6. Android-ListView-SimpleCursorAdapter

    在上篇博客,Android-ListView-SimpleAdapter,中介绍了SimpleAdapter的使用操作(SimpleAdapter面向的数据是非Cursor数据),而SimpleCur ...

  7. Android-ListView-(BaseAdapter初步)

    在Android中就提供了专门列表显示条目的控件,ListView控件,ListView控件不是一次性加载全部数据,他是只加载用户在屏幕看得到的数据,当用户滑动的过程中在去加载新的数据,同时会自动销毁 ...

  8. [转]Control的Invoke和BeginInvoke

    转自:Control的Invoke和BeginInvoke  作者:Kuffy Wang 近日,被Control的Invoke和BeginInvoke搞的头大,就查了些相关的资料,整理如下.感谢这篇文 ...

  9. Windows解决多版本python执行pip3时出错AttributeError: module 'enum' has no attribute 'IntFlag'?

    摘要: 本机装有python2.7和python3.6,执行pip和pip2时没有问题,执行pip3时提示: C:\Users\>pip3 Traceback (most recent call ...

  10. centos 7 安装solr7.3.0 配置mysql

    1.下载solr :wget http://archive.apache.org/dist/lucene/solr/7.3.0/solr-7.3.0.tgz   或者去官网自己下:http://arc ...