Mapper.xml 基础模板

目录

模版:

时间范围查询数据精度缺失:

解决方法:

Idea 中 Mapper.xml 文件快捷生成配置:

使用方法:


模版:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="">
    <!-- 实体类 -->
    <resultMap type="${NAME}" id="${NAME}Result">
    </resultMap>
    
    <!-- sql语句 -->
    <sql id="select${NAME}List">
      
    </sql>
    
    <!-- 方法 -->
    <select id="query${NAME}List" parameterType="${NAME}" resultMap="${NAME}Result">
        <include refid="select${NAME}List"/>
        <where>
            <choose>--此处为if-else,项目启动时删除该注解,否则报错
                <when test="查询类的属性名 != null and 查询类的属性名 != ''">
                    and 表字段 like concat('%', #{查询类的属性名}, '%')
                </when>
                <otherwise>
                    and 查询类的属性名 like '值'
                </otherwise>
            </choose>
            <if test="fieldName != null  and fieldName != ''"> and tableFieldName like concat('%', #{fieldName}, '%')</if>
            <if test="startDate != null  and startDate != ''">and tableFieldName &gt;= #{startDate}</if>
            <if test="endDate != null  and endDate != ''">and tableFieldName &lt;= #{endDate}</if>
        </where>
    </select>
</mapper>

时间范围查询数据精度缺失:

如果数据库中时间格式为下述情况,在查询指定时间范围时,传入的时间只有 年月日 会导致数据不精准

yyyy-MM-dd HH:mm:ss 

解决方法:

<if test="startDate != null and startDate != ''">and CONVERT(VARCHAR(100),tableFieldName,23) &gt;= #{startDate}</if>
<if test="endDate != null and endDate != ''">and CONVERT(VARCHAR(100),tableFieldName,23) &lt;= #{endDate}</if>

Idea 中 Mapper.xml 文件快捷生成配置:

  1. idea 中打开设置,点击编辑器-文件和代码模版
  2. 选中【文件】点击 “+” 新建一个模版
  3. 取个模版名称,扩展(文件类型)指定为 xml
  4. 把上面的模版复制粘贴
  5. 应用-确认
  6. 完事

使用方法:

点击需要创建的位置,右键新建选择

                                                                Cheung Kwok Wing 随笔

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值