actual combat 44 动态sql,crud模板

crud

查询select

xml 复制代码
<!--列表查询-->
<select id="selectLoanInfoList" parameterType="com.zyamc.debt.domain.LoanInfo" resultMap="LoanInfoResult">
    select id, loaner_id, loanee_id, contract_number
    from tbl_loan_info
    <where>
        <if test="loanerId != null "> and loaner_id = #{loanerId} </if>
        <if test="loaneeId != null "> and loanee_id = #{loaneeId} </if>
        <if test="contractNumber != null  and contractNumber != ''"> and contract_number like concat('%', #{contractNumber}, '%') </if>
    </where>
    order by `status` desc, expire_date asc
</select>

<!--遍历idList查询-->
<select id="selectLoanInfoByIds" parameterType="Long" resultMap="LoanInfoResult">
    <include refid="selectLoanInfoVo"/>
    <where>
        <if test="ids != null and ids.size() != 0">
            id in
            <foreach collection="ids" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </where>
</select>

<!--通过id查询-->
<select id="selectLoanInfoById" parameterType="Long" resultMap="LoanInfoResult" flushCache="true">
    <include refid="selectLoanInfoVo"/>
    where id = #{id}
</select>

新增insert

xml 复制代码
<!--条件新增-->
<insert id="insertLoanInfo" parameterType="com.zyamc.debt.domain.LoanInfo" useGeneratedKeys="true"
        keyProperty="id">
    insert into tbl_loan_info
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="loanerId != null">loaner_id, </if>
        <if test="loaneeId != null">loanee_id, </if>
        <if test="contractNumber != null and contractNumber != ''"> contract_number, </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="loanerId != null"> #{loanerId}, </if>
        <if test="loaneeId != null"> #{loaneeId}, </if>
        <if test="contractNumber != null and contractNumber != ''"> #{contractNumber}, </if>
    </trim>
</insert>

修改update

xml 复制代码
<!--条件修改-->
<update id="updateLoanInfo" parameterType="com.zyamc.debt.domain.LoanInfo">
    update tbl_loan_info
    <trim prefix="SET" suffixOverrides=",">
        <if test="loanerId != null"> loaner_id =  #{loanerId}, </if>
        <if test="loaneeId != null"> loanee_id = #{loaneeId}, </if>
        <if test="contractNumber != null and contractNumber != ''"> contract_number = #{contractNumber}, </if>
    </trim>
    where id = #{id}
</update>

删除delete

xml 复制代码
<!--根据id删除-->
<delete id="deleteLoanInfoById" parameterType="Long">
    delete
    from tbl_loan_info where id = #{id}
</delete>

<!--根据ids(逗号隔开的字符串)删除-->
<delete id="deleteLoanInfoByIds" parameterType="String">
    delete from tbl_loan_info where id in
    <foreach item="id" collection="array" open="(" separator="," close=")">
        #{id}
    </foreach>
</delete>

全文件

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="com.zyamc.debt.mapper.LoanInfoMapper">

    <resultMap type="com.zyamc.debt.domain.LoanInfo" id="LoanInfoResult">
            <result property="id" column="id"/>
            <result property="loanerId" column="loaner_id"/>
            <result property="loaneeId" column="loanee_id"/>
            <result property="contractNumber" column="contract_number"/>
    </resultMap>

    <sql id="selectLoanInfoVo">
        select id, loaner_id, loanee_id, contract_number
        from tbl_loan_info
    </sql>

    <!--列表查询-->
    <select id="selectLoanInfoList" parameterType="com.zyamc.debt.domain.LoanInfo" resultMap="LoanInfoResult">
        select id, loaner_id, loanee_id, contract_number
        from tbl_loan_info
        <where>
            <if test="loanerId != null "> and loaner_id = #{loanerId} </if>
            <if test="loaneeId != null "> and loanee_id = #{loaneeId} </if>
            <if test="contractNumber != null  and contractNumber != ''"> and contract_number like concat('%', #{contractNumber}, '%') </if>
        </where>
        order by `status` desc, expire_date asc
    </select>

    <!--遍历idList查询-->
    <select id="selectLoanInfoByIds" parameterType="Long" resultMap="LoanInfoResult">
        <include refid="selectLoanInfoVo"/>
        <where>
            <if test="ids != null and ids.size() != 0">
            id in
                <foreach collection="ids" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>

    <!--通过id查询-->
    <select id="selectLoanInfoById" parameterType="Long" resultMap="LoanInfoResult" flushCache="true">
            <include refid="selectLoanInfoVo"/>
            where id = #{id}
    </select>
	
    <!--条件新增-->
    <insert id="insertLoanInfo" parameterType="com.zyamc.debt.domain.LoanInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into tbl_loan_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="loanerId != null">loaner_id, </if>
            <if test="loaneeId != null">loanee_id, </if>
            <if test="contractNumber != null and contractNumber != ''"> contract_number, </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="loanerId != null"> #{loanerId}, </if>
            <if test="loaneeId != null"> #{loaneeId}, </if>
            <if test="contractNumber != null and contractNumber != ''"> #{contractNumber}, </if>
        </trim>
    </insert>

    <!--条件修改-->
    <update id="updateLoanInfo" parameterType="com.zyamc.debt.domain.LoanInfo">
        update tbl_loan_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="loanerId != null"> loaner_id =  #{loanerId}, </if>
            <if test="loaneeId != null"> loanee_id = #{loaneeId}, </if>
            <if test="contractNumber != null and contractNumber != ''"> contract_number = #{contractNumber}, </if>
        </trim>
        where id = #{id}
    </update>

    <!--根据id删除-->
    <delete id="deleteLoanInfoById" parameterType="Long">
        delete
        from tbl_loan_info where id = #{id}
    </delete>

    <!--根据ids(逗号隔开的字符串)删除-->
    <delete id="deleteLoanInfoByIds" parameterType="String">
        delete from tbl_loan_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>

时间条件过滤举例

方式一:

xml 复制代码
<if test="startDate != null"> and lend_date <![CDATA[>=]]> #{startDate}</if>
<if test="endDate != null"> and lend_date <![CDATA[<=]]> #{endDate}</if>

方式二:

xml 复制代码
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
    and date_format(create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
    and date_format(create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
</if>
相关推荐
山岚的运维笔记11 分钟前
SQL Server笔记 -- 第20章:TRY/CATCH
java·数据库·笔记·sql·microsoft·sqlserver
Gain_chance13 分钟前
33-学习笔记尚硅谷数仓搭建-DWS层交易域用户粒度订单表分析及设计代码
数据库·数据仓库·hive·笔记·学习·datagrip
南极企鹅33 分钟前
springBoot项目有几个端口
java·spring boot·后端
清风拂山岗 明月照大江39 分钟前
Redis笔记汇总
java·redis·缓存
未来之窗软件服务41 分钟前
计算机等级考试—高频英语词汇—东方仙盟练气期
数据库·计算机软考·东方仙盟
lekami_兰44 分钟前
MySQL 长事务:藏在业务里的性能 “隐形杀手”
数据库·mysql·go·长事务
xiaoxue..1 小时前
合并两个升序链表 与 合并k个升序链表
java·javascript·数据结构·链表·面试
JQLvopkk1 小时前
C# 轻量级工业温湿度监控系统(含数据库与源码)
开发语言·数据库·c#
忧郁的Mr.Li1 小时前
SpringBoot中实现多数据源配置
java·spring boot·后端
yq1982043011561 小时前
静思书屋:基于Java Web技术栈构建高性能图书信息平台实践
java·开发语言·前端