mybatis.xml中sql编写规范

一、越少的代码,越强悍的功能,xml里面应该6个sql语句就够用了,修改,维护成本很低,见下表 下载

英文名 方法名称 核心点 建议
insert 1.新增数据 如果是自增主键,应该返回主键ID
deleteById 2. 根据主键ID删除数据 sql默认加limit 1,防止多删数据 此方法不建议有,建议逻辑删除
updateById 3. 根据主键ID修改数据 sql默认加limit 1,防止多修改数据
selectById 4. 根据主键查询数据 查询一条数据
selectByIdForUpdate 5. 根据主键加锁查询数据 加锁查询一条数据,事务处理用
queryListByParam 6. 根据输入条件查询数据列表 和7配合使用
queryCountByParam 7. 根据输入条件查询总数 和6配合使用

二、公共的查询条件和字段列表等抽出公共sql段,方便使用

英文名 方法名称 核心点 建议
_field_list 1.字段列表 修改方便,方便字段排序
_value_list 2. 字段值列表 修改方便,方便字段值排序
_common_where 3. 通用查询条件 每个字段的等值判断
_regin_where 4. 通用范围区间条件 字段的时间区间,字段的金额区间等的判断
_contain_where 5. 包含字段值范围条件 字段的常量值包含判断,in ,not in
_common_sorts 6. 通用排序条件 order by

三、一个mybatis.xml例子  下载

Sql代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="Assets">
  4. <!-- 设置1分钟缓存,缓存大小1024,采用最近最少使用算法 -->
  5. <cache readOnly="true" flushInterval="60000" size="10" eviction="LRU" />
  6. <resultMap type="Assets" id="AssetsResultMap">
  7. <id property="id" column="id" />
  8. <result property="userId" column="user_id" />
  9. <result property="amount" column="amount" />
  10. <result property="earning" column="earning" />
  11. <result property="type" column="type" />
  12. <result property="status" column="status" />
  13. <result property="productId" column="product_id" />
  14. <result property="productName" column="product_name" />
  15. <result property="cardNo" column="card_no" />
  16. <result property="bankCode" column="bank_code" />
  17. <result property="orderId" column="order_id" />
  18. <result property="effectiveDate" column="effective_date" />
  19. <result property="redeemType" column="redeem_type"/>
  20. <result property="initAmount" column="init_amount"/>
  21. <result property="initEarning" column="init_earning"/>
  22. <result property="redeemingAmount" column="redeeming_amount"/>
  23. <result property="redeemingEarning" column="redeeming_earning"/>
  24. <result property="redeemedAmount" column="redeemed_amount"/>
  25. <result property="redeemedEarning" column="redeemed_earning"/>
  26. <result property="punishAmount" column="punish_amount"/>
  27. <result property="latestRedeemTime" column="latest_redeem_time"/>
  28. <result property="maturityDate" column="maturity_date"/>
  29. <result property="createTime" column="create_time" />
  30. <result property="modifyTime" column="modify_time" />
  31. <result property="remark" column="remark" />
  32. </resultMap>
  33. <!-- 字段列表 -->
  34. <sql id="_field_list">
  35. id,
  36. user_id,
  37. amount,
  38. earning,
  39. type,
  40. status,
  41. product_id,
  42. product_name,
  43. card_no,
  44. bank_code,
  45. order_id,
  46. effective_date,
  47. redeem_type,
  48. init_amount,
  49. init_earning,
  50. redeeming_amount,
  51. redeeming_earning,
  52. redeemed_amount,
  53. redeemed_earning,
  54. punish_amount,
  55. latest_redeem_time,
  56. maturity_date,
  57. create_time,
  58. modify_time,
  59. remark
  60. </sql>
  61. <!-- 字段值列表 -->
  62. <sql id="_value_list">
  63. #{id},
  64. #{userId},
  65. #{amount},
  66. #{earning},
  67. #{type},
  68. #{status},
  69. #{productId},
  70. #{productName},
  71. #{cardNo},
  72. #{bankCode},
  73. #{orderId},
  74. #{effectiveDate},
  75. #{redeemType},
  76. #{initAmount},
  77. #{initEarning},
  78. #{redeemingAmount},
  79. #{redeemingEarning},
  80. #{redeemedAmount},
  81. #{redeemedEarning},
  82. #{punishAmount},
  83. #{latestRedeemTime},
  84. #{maturityDate},
  85. #{createTime},
  86. #{modifyTime},
  87. #{remark}
  88. </sql>
  89. <!-- 通用查询条件  不支持ID查询条件,ID的直接通过ID即可以查 -->
  90. <sql id="_common_where">
  91. <if test="id != null"> AND id = #{id}</if>
  92. <if test="userId != null"> AND user_id = #{userId}</if>
  93. <if test="amount != null"> AND amount = #{amount}</if>
  94. <if test="earning != null"> AND earning = #{earning}</if>
  95. <if test="type != null"> AND type = #{type}</if>
  96. <if test="status != null"> AND status = #{status}</if>
  97. <if test="productId != null"> AND product_id = #{productId}</if>
  98. <if test="productName != null"> AND product_name = #{productName}</if>
  99. <if test="cardNo != null"> AND card_no = #{cardNo}</if>
  100. <if test="bankCode != null"> AND bank_code = #{bankCode}</if>
  101. <if test="orderId != null"> AND order_id = #{orderId}</if>
  102. <if test="effectiveDate != null"> AND effective_date = #{effectiveDate}</if>
  103. <if test="redeemType != null"> AND redeem_type = #{redeemType}</if>
  104. <if test="initAmount != null"> AND init_amount = #{initAmount}</if>
  105. <if test="initEarning != null"> AND init_earning = #{initEarning}</if>
  106. <if test="redeemingAmount != null"> AND redeeming_amount = #{redeemingAmount}</if>
  107. <if test="redeemingEarning != null"> AND redeeming_earning = #{redeemingEarning}</if>
  108. <if test="redeemedAmount != null"> AND redeemed_amount = #{redeemedAmount}</if>
  109. <if test="redeemedEarning != null"> AND redeemed_earning = #{redeemedEarning}</if>
  110. <if test="punishAmount != null"> AND punish_amount = #{punishAmount}</if>
  111. <if test="latestRedeemTime != null">
  112. <![CDATA[
  113. AND latest_redeem_time = #{latestRedeemTime, jdbcType=TIMESTAMP}
  114. ]]>
  115. </if>
  116. <if test="maturityDate != null">
  117. <![CDATA[
  118. AND maturity_date = #{maturityDate, jdbcType=TIMESTAMP}
  119. ]]>
  120. </if>
  121. <if test="createTime != null">
  122. <![CDATA[
  123. AND create_time = #{createTime, jdbcType=TIMESTAMP}
  124. ]]>
  125. </if>
  126. <if test="modifyTime != null">
  127. <![CDATA[
  128. AND modify_time = #{modifyTime, jdbcType=TIMESTAMP}
  129. ]]>
  130. </if>
  131. <if test="remark != null"> AND remark = #{remark}</if>
  132. </sql>
  133. <!-- 通用范围区间查询 -->
  134. <sql id="_regin_where">
  135. <if test="egtCreateTime != null">
  136. <![CDATA[
  137. AND create_time >= #{egtCreateTime, jdbcType=TIMESTAMP}
  138. ]]>
  139. </if>
  140. <if test="ltCreateTime != null">
  141. <![CDATA[
  142. AND create_time < #{ltCreateTime, jdbcType=TIMESTAMP}
  143. ]]>
  144. </if>
  145. </sql>
  146. <!-- 通用排序处理 -->
  147. <sql id="_common_sorts">
  148. <if test="sorts != null">
  149. ORDER BY
  150. <foreach collection="sorts" item="item" separator=",">
  151. ${item.column.columnName} ${item.sortMode.mode}
  152. </foreach>
  153. </if>
  154. </sql>
  155. <!-- in 和 not in的通用查询where -->
  156. <sql id="_contain_where">
  157. <if test="containStatusSet!=null">
  158. AND status IN
  159. <foreach item="item" index="i" collection="containStatusSet" separator="," open="(" close=")" >
  160. #{item}
  161. </foreach>
  162. </if>
  163. </sql>
  164. <!-- 插入操作 -->
  165. <insert id="insert" parameterType="Assets">
  166. INSERT INTO assets (
  167. <include refid="_field_list"/>)
  168. VALUES (
  169. <include refid="_value_list"/>)
  170. </insert>
  171. <!-- 根据ID主键进行删除,注意limit 1 -->
  172. <delete id="deleteById"  parameterType="java.lang.String" >
  173. delete from assets where id = #{id} limit 1
  174. </delete>
  175. <!-- 根据主键ID进行更新,注意limit 1 -->
  176. <update id="updateById" parameterType="Assets">
  177. UPDATE assets
  178. <set>
  179. <if test="userId != null">
  180. user_id = #{userId},
  181. </if>
  182. <if test="amount != null">
  183. amount = #{amount},
  184. </if>
  185. <if test="earning != null">
  186. earning = #{earning},
  187. </if>
  188. <if test="type != null">
  189. type = #{type},
  190. </if>
  191. <if test="status != null">
  192. status = #{status},
  193. </if>
  194. <if test="productName != null">
  195. product_name = #{productName},
  196. </if>
  197. <if test="productId != null">
  198. product_id = #{productId},
  199. </if>
  200. <if test="cardNo != null">
  201. card_no = #{cardNo},
  202. </if>
  203. <if test="bankCode != null">
  204. bank_code = #{bankCode},
  205. </if>
  206. <if test="orderId != null">
  207. order_id = #{orderId},
  208. </if>
  209. <if test="effectiveDate != null">
  210. effective_date = #{effectiveDate},
  211. </if>
  212. <if test="redeemType != null">
  213. redeem_type = #{redeemType},
  214. </if>
  215. <if test="initAmount != null">
  216. init_amount = #{initAmount},
  217. </if>
  218. <if test="initEarning != null">
  219. init_earning = #{initEarning},
  220. </if>
  221. <if test="redeemingAmount != null">
  222. redeeming_amount = #{redeemingAmount},
  223. </if>
  224. <if test="redeemingEarning != null">
  225. redeeming_earning = #{redeemingEarning},
  226. </if>
  227. <if test="redeemedAmount != null">
  228. redeemed_amount = #{redeemedAmount},
  229. </if>
  230. <if test="redeemedEarning != null">
  231. redeemed_earning = #{redeemedEarning},
  232. </if>
  233. <if test="punishAmount != null">
  234. punish_amount = #{punishAmount},
  235. </if>
  236. <if test="latestRedeemTime != null">
  237. latest_redeem_time = #{latestRedeemTime},
  238. </if>
  239. <if test="maturityDate != null">
  240. maturity_date = #{maturityDate},
  241. </if>
  242. <if test="modifyTime != null">
  243. modify_time = #{modifyTime},
  244. </if>
  245. <if test="remark != null">
  246. remark = #{remark},
  247. </if>
  248. </set>
  249. <where>
  250. id = #{id} limit 1
  251. </where>
  252. </update>
  253. <!-- 根据ID进行查询 -->
  254. <select id="selectById" resultMap="AssetsResultMap">
  255. select * from assets where id = #{id}
  256. </select>
  257. <!-- 根据ID进行加行锁查询 -->
  258. <select id="selectByIdForUpdate" resultMap="AssetsResultMap">
  259. select * from assets where id = #{id} for update
  260. </select>
  261. <!-- 根据查询条件查询数据和queryCountByParam方法配对使用 -->
  262. <select id="queryListByParam" parameterType="map" resultMap="AssetsResultMap">
  263. SELECT
  264. <include refid="_field_list"/>
  265. FROM
  266. assets
  267. <where>
  268. 1 = 1
  269. <include refid="_common_where"/>
  270. <include refid="_regin_where"/>
  271. <include refid="_contain_where"/>
  272. </where>
  273. <include refid="_common_sorts"/>
  274. <if test="offset != null and rows != null">
  275. limit #{offset}, #{rows}
  276. </if>
  277. </select>
  278. <!-- 根据查询条件查询总数和queryListByParam方法配对使用 -->
  279. <select id="queryCountByParam" parameterType="map" resultType="java.lang.Integer">
  280. SELECT count(1) FROM assets
  281. <where>
  282. 1 = 1
  283. <include refid="_common_where"/>
  284. <include refid="_regin_where"/>
  285. <include refid="_contain_where"/>
  286. </where>
  287. </select>
  288. </mapper>
时间: 2024-12-19 10:06:33

mybatis.xml中sql编写规范的相关文章

详解Java的MyBatis框架中SQL语句映射部分的编写

这篇文章主要介绍了Java的MyBatis框架中SQL语句映射部分的编写,文中分为resultMap和增删查改实现两个部分来讲解,需要的朋友可以参考下 1.resultMap SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对应的接口类的路径.写好SQL语句映射文件后,需要在MyBAtis配置文件mappers标签中引用,例如: ? 1 2 3 4 5 6 <mappers>   <mapper resource="com/limi

MyBatis mapper.xml中SQL处理小于号与大于号

这种问题在xml处理sql的程序中经常需要我们来进行特殊处理. 其实很简单,我们只需作如下替换即可避免上述的错误: < <= > >= & ' " < <= > >= & &apos; " 例如常见的时间比较: <select id="select" parameterType="xxx" resultMap="xxx"> select dis

如何批量测试Mybatis项目中SQL是否正确

去Oracle行动 最近公司要发展海外项目,所以要将现有的系统全部平移过去,另外数据库也要从原来的Oracle变为Mysql.公司的数据库交互层面使用的是Mybatis,而Oracle与Mysql也有一些语法上的不同.所以在项目中的Sql要改动,但是多个项目中涉及到的Sql非常多,如果仅凭人工一条一条辨别的话,工作量有点大. 所以就萌发出了直接将数据源变为Mysql,利用反射批量执行Mapper中的方法,然后如果有参数的话,就设置为默认的初始值,然后记录下来成功的数据和失败的数据,这样就可以根据

mybatis的动态sql编写以及一对一关系查询和一对多的查询

创建mybatis数据库,运行以下sql语句 /* SQLyog Ultimate v8.32 MySQL - 5.5.27 : Database - mybatis ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @[email protected]@UNIQU

数据库应用SQL编写规范

1. 编程规范 本章讲述ORACLE数据库SQL脚本编码规范. 1.1. 总述 1.1.1. 编码规范 1)编写脚本时,Oracle保留字大写,其他一律使用小写,除非必要(如:作字符串时或注释语句中): 2)脚本必须规范,SQL编写不能采用缩略写法:如INSERT INTO中的INTO不能省略:INTO后面要列出字段名: 3)语句内的空格,统一为一个空格: 4)表定义中,字段名.字段类型(长度).缺省.(非)空.字段注释都必须左对齐: 5)使用空格键对齐,不要使用Tab键: 6)运算符前后,各保

Spring+MyBatis框架中sql语句的书写,数据集的传递以及多表关联查询

在很多Java EE项目中,Spring+MyBatis框架经常被用到,项目搭建在这里不再赘述,现在要将的是如何在项目中书写,增删改查的语句,如何操作数据库,以及后台如何获取数据,如何进行关联查询,以及MyBatis的分页问题. 首先先看看项目的架构,方便后边叙述. 这个项目中是一个Sping+MyBatis的完整demo(这边将页面没有展示.)这次的主题主要是后台数据处理逻辑.接下来为大家逐一介绍各个文件, org.config   Spring配置包括数据库的链接信息 org.control

mybatis xml中特殊字符处理

1,CDATA区: 它的全称为character data,以"<![CDATA[ "开始,以" ]]>" 结束,在两者之间嵌入不想被解析程序解析的原始数据,解析器不对CDATA区中的内容进行解析,而是将这些数据原封不动地交给下游程序处理. 2,特殊字符 : xml 中表示:   <= 小于等于.    >= 大于等于 需加  这样的标记:     <![CDATA[   ]]>      xml中有&的符号,需要 <

mybatis的xml中sql语句中in的写法(迭代遍历)

这里使用 foreach标签 <foreach  item="item" collection="listTag" index="index"  open="(" separator="," close=")"> #{item} </foreach> foreach元素的属性主要有 item,index,collection,open,separator,clos

MyBatis mapper.xml中SQL处理小于号与大于号 和小于等于号

我们只需作如下替换即可避免上述的错误: < <= > >= & ' " < <= > >= & &apos; " 错误写法 addtime   >=    start_time 正确写法 addtime  >=  start_time 原文地址:https://www.cnblogs.com/hjy2018/p/9571783.html