Mybatis sql构造

项目场景:

简单记录一些Mybatis构造的常用sql写法。


1.数据插入通过map传参,灵活插入表格数据

<insert id="mapInsertTemplate">
        insert into  ${TableName}
        <foreach collection="params.keys" item="key" open="(" close=")"
                 separator=",">
            ${key}
        </foreach>
        values
        <foreach collection="params.values" item="value" open="("
                 close=")" separator=",">
            #{value}
        </foreach>
    </insert>

2.数据插入通过map传参并返回id

<insert id="mapInsertTemplate"   parameterType="java.util.Map"  useGeneratedKeys="true" keyProperty="params.ID" keyColumn="params.ID" >
        <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="params.ID" >
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into  ${TableName}
        <foreach collection="params.keys" item="key" open="(" close=")"
                 separator=",">
            ${key}
        </foreach>
        values
        <foreach collection="params.values" item="value" open="("
                 close=")" separator=",">
            #{value}
        </foreach>

    </insert>

3.数据更新通过map传参

<update id="mapUpdateTemplate" parameterType="java.util.Map">
        UPDATE  ${TableName}
        SET
        <foreach item="value" index="key" collection="params" separator=",">
            <if test="key != 'ID'">
                ${key} = #{value}
            </if>
        </foreach>
        WHERE
        <foreach item="value" index="key" collection="params" separator=",">
            <if test="key == 'ID'">
                ID = #{value}
            </if>
        </foreach>
    </update>

4. 构造where条件

mapper接口层

List<Map> selectMapsPage(@Param(Constants.WRAPPER) Wrapper query, @Param("tableName") String tableName);

mapper实现层

<select id="selectMapsPage" resultType="java.util.Map" parameterType="java.util.Map">
        SELECT *
        FROM ${tableName}
        <if test="ew.emptyOfWhere == false">
            ${ew.customSqlSegment}
        </if>
    </select>

5.构造where条件和sql语句where条件混用1

mapper接口层

    /**
     * 根据自定义sql查询
     * @param query
     * @param tableName
     * @param fields
     * @param joinCustomSqlSql
     * @param orderBySql
     * @return
     */
    List<Map> selectListByCustomSql(@Param(Constants.WRAPPER) Wrapper query, @Param("tableName") String tableName, @Param("fields") String fields, @Param("joinCustomSqlSql") String joinCustomSqlSql, @Param("orderBySql") String orderBySql);

mapper接口实现层

   <select id="selectListByCustomSql" resultType="java.util.Map" parameterType="java.util.Map">
        SELECT ${fields}
        FROM ${tableName}
        <if test="ew.emptyOfWhere == false">
            <where>
                ${joinCustomSqlSql}
                <if test="ew.customSqlSegment != null and ew.customSqlSegment.startsWith('WHERE')">
                    AND
                </if>
                ${ew.sqlSegment}
            </where>
        </if>
        order by ${orderBySql}
    </select>

6.构造where条件和sql语句where条件混用2

mapper接口层

List<Map> paymentList(@Param(Constants.WRAPPER) Wrapper query, @Param("examId") String examId);

mapper接口实现层

<select id="paymentList" resultType="java.util.Map" parameterType="java.lang.String">
        SELECT a.`ORDNO`, a.`ORDAMT`
        FROM `exam_${examId}_examinee` a,`exam_config` cg
        <if test="ew.emptyOfWhere == false">
            <where>
                a.SS = cg.department_code
                AND cg.examid = #{examId}
                <if test="ew.customSqlSegment != null and ew.customSqlSegment.startsWith('WHERE')">
                    AND
                </if>
                ${ew.sqlSegment}
            </where>
        </if>
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值