mybatis映射文件模板mapper.xml格式
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格式的更多相关文章
- MyBatis 逆向工程——根据数据表自动生成model、xml映射文件、mapper接口
MyBatis Generator(MBG)的使用 MBG可以根据数据表生成对应的model.xml映射文件.mapper接口,只是简单的生成,还需要根据需求修改. 1.下载jar包 https:// ...
- mybatis映射文件mapper.xml的写法(collections...)
转自:https://blog.csdn.net/two_people/article/details/51759881 在学习mybatis的时候我们通常会在映射文件这样写: <?xml ve ...
- Mybatis映射文件完整模板参照
Mybatis映射文件完整模板参照 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...
- mybatis根据表逆向自动化生成代码(自动生成实体类、mapper文件、mapper.xml文件)
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- mybatis映射文件(转)
以下内容为转载, 格式未调整,略丑,可直接空降至: [Mybatis框架]输出映射-resultType与resultMap 有时间或看: Mybatis 3.1中 Mapper XML 文件 的学习 ...
- MyBatis映射文件 相关操作
一.MyBatis映射文件 1.简介 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行 ...
- SSM实战——秒杀系统之DAO层实体定义、接口设计、mybatis映射文件编写、整合Spring与Mybatis
一:DAO实体编码 1:首先,在src目录下,新建org.myseckill.entity包,用于存放实体类: 2:实体类设计 根据前面创建的数据库表以及映射关系,创建实体类. 表一:秒杀商品表 对应 ...
- MyBatis 映射文件详解
1. MyBatis 映射文件之<select>标签 <select>用来定义查询操作; "id": 唯一标识符,需要和接口中的方法名一致; paramet ...
- MyBatis 映射文件
Mybatis映射文件简介 1) MyBatis 的真正强大在于它的映射语句.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉 ...
随机推荐
- Ubuntu14.04-LTS 从系统安装到配置可用
1.安装Ubuntu14.04LTS-64bit 使用U盘安装很方便快捷,可以使用老毛桃使用iso模式制作一个U盘启动盘,然后分区安装. 如果使用硬盘安装的话需要注意的问题是: 如果电脑上以前有Lin ...
- Linux Mint 17 搭建 JSP 环境
一.配置Tomcat 服务器 1.下载 tomcat 2.解压后放到/usr/local目录下面 3.以root权限执行 chmod +x *.sh 4.启动 ./startup.sh#方式1 ./ ...
- [GO]方法的继承
package main import "fmt" type Person struct { name string sex byte age int } func (tmp Pe ...
- webstorm最新破解方法
方法来自 Rover12421 大神. 1.从官网下载WebStorm2016.1安装. 2.下载 破解补丁 并解压,记住路径 3.编辑WebStorm安装目录下 bin 文件夹中的 WebStorm ...
- Generated by NetworkManager、ubuntu DNS设置丢失(network-manager造成的情况)
方法一:去掉重启 方法二:卸载network-manager 实测网络不稳,经常掉线(kalinux2.0环境)
- 用Word 写csdn blog
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- C# 自带的.net类库 实现得到本机IP以及网关地址
今天需要用到一个功能,获取主机名和本机的IP 准备用API实现的,然后稍微查了一下,发现.net类库已经有了 就在System.Net命名空间中的DNS类中 GetHostName 获取本地计算机的主 ...
- Android性能分析Systrace工具
一.概述 保证系统流畅度,也就是保证系统能连续不间断地提供每秒60帧的运行状态.当出现掉帧时(也可称为Jank),需要知道当前整个系统所处的状态,systrace便是最佳的选择,它能手机检测Andro ...
- logback-spring.xml配置文件详解
logback-spring.xml配置文件 自己改下value="G:/logs/pmp"这个值,如果你相关依赖弄好的话,直接复制粘贴即用 输出的日志文件的名称最好也改下,下文中 ...
- 18、Semantic-UI之进度条
在使用进度条的时候也是必须要结合项目中ajax和后台数据结合使用的. 示例:定义基础进度条 <div class="ui progress"> <div cl ...