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 代码进行对比,你会立即发现省掉 ...
随机推荐
- jQuary总结4: jquery操作字符串
1 jquery操作DOM -1 创建元素 $('<span>这是一个span元素</span>'); //创建了一个jQuery包装的span,此时并没有添加到DOM树上 - ...
- CoderForces 518D Ilya and Escalator (期望DP)
题意:给定 n 个人,在每一时刻一个人进入地铁的概率是 p,站着不动的概率是 1-p,然后问你 t 时间地铁里有多少人. 析:很明显这是一个期望DP,用d[i][j]表示 i 时刻 j 个人进入地铁的 ...
- Tomcat 开机自启动
一.安装JDK和Tomcat 1,安装JDK:直接运行jdk-7-windows-i586.exe可执行程序,默认安装即可. 备注:路径可以其他盘符,不建议路径包含中文名及特殊符号. 2.安装Tomc ...
- SPOJ - AMR11J ——(BFS)
The wizards and witches of Hogwarts School of Witchcraft found Prof. Binn's History of Magic lesson ...
- sqlite3使用详解(Qt版本)
初始化sqlite3 (创建表) QString url = QDir::currentPath() + QString::fromLocal8Bit("/Msg.db"); bo ...
- CentOS7下搭建yum仓库
服务端配置: 1.开启yum缓存 sed -i 's#keepcache=0#keepcache=1#g' /etc/yum.conf [root@control /]# cat /etc/yum.c ...
- Reporting Service服务SharePoint集成模式安装配置(8、配置用于SharePoint 2010的Reporting service模式)
从SQL Server 2012 起, SQL Server Reporting Service可以完全集成进SharePoint的场,直接作为SharePoint 的组件部分来运行,没有独立的Win ...
- mysql 循环插入
在mysql添加测试数据,想和mssql一样用循环实现,发现不管怎么样都执行失败 经查询发现mysql不支持匿名块,只能先创建出一个存储过程,执行,然后删除 CREATE PROCEDURE test ...
- 三张图片看懂ZKEACMS的设计思想
前言 如果你还不知道ZKEACMS,不妨先了解一下. ASP.NET MVC 开源建站系统 ZKEACMS 推荐,从此网站“拼”起来 官方地址:http://www.zkea.net/zkeacms ...
- asp.net core + 前端H5 页面视频站制作尝试
.net core 2.1出来一段时间了,一直关注,前周花了半天时间学习了一下,特制作了一个视频小站(欢迎扫码体验): 页面首页效果如下: 播放页面效果如下: 部分代码: using ENT.IBLL ...