MyBatis的方便在于可以配置动态SQL,通过过滤器进行动态装配。在刚开始使用中,遇到不少问题,其中update语句也需要动态装配,核心在于DAO层要与.xml文件中的语句和变量名要匹配。例如:

DAO层如下配置:

public void updateByJid(@Param("objectResult")RBTaskScoreDO rbTaskScore, @Param("filterRules")List<FilterRule> filterRules);

xml中如下:

    <update id="updateByJid">
update
RB_TASK_SCORE
<set>
<if test="objectResult.userId != null">
USER_ID = #{objectResult.userId,jdbcType=VARCHAR},
</if>
<if test="objectResult.rid != null">
RID = #{objectResult.rid,jdbcType=NUMERIC},
</if>
<if test="objectResult.score != null">
SCORE = #{objectResult.score,jdbcType=NUMERIC},
</if>
<if test="objectResult.adddate != null">
ADDDATE = #{objectResult.adddate,jdbcType=TIMESTAMP},
</if>
<if test="objectResult.checkState != null">
CHECK_STATE = #{objectResult.checkState,jdbcType=NUMERIC},
</if>
<if test="objectResult.gid != null">
GID = #{objectResult.gid,jdbcType=NUMERIC},
</if>
<if test="objectResult.isCheck != null">
IS_CHECK = #{objectResult.isCheck,jdbcType=NUMERIC},
</if>
<if test="objectResult.roadLength != null">
ROAD_LENGTH = #{objectResult.roadLength,jdbcType=NUMERIC},
</if>
<if test="objectResult.checkIsBound != null">
CHECK_IS_BOUND = #{objectResult.checkIsBound,jdbcType=NUMERIC},
</if>
<if test="objectResult.matchLenPct != null">
MATCH_LEN_PCT = #{objectResult.matchLenPct,jdbcType=NUMERIC},
</if>
<if test="objectResult.matchPntNum != null">
MATCH_PNT_NUM = #{objectResult.matchPntNum,jdbcType=NUMERIC},
</if>
<if test="objectResult.isBoss != null">
IS_BOSS = #{objectResult.isBoss,jdbcType=NUMERIC},
</if>
<if test="objectResult.isBossDate != null">
IS_BOSS_DATE = #{objectResult.isBossDate,jdbcType=TIMESTAMP},
</if>
<if test="objectResult.md5 != null">
MD5 = #{objectResult.md5,jdbcType=VARCHAR},
</if>
<if test="objectResult.checkDate != null">
CHECK_DATE = #{objectResult.checkDate,jdbcType=TIMESTAMP},
</if>
<if test="objectResult.usertype != null">
USERTYPE = #{objectResult.usertype,jdbcType=NUMERIC},
</if>
<if test="objectResult.srcdate != null">
SRCDATE = #{objectResult.srcdate,jdbcType=TIMESTAMP},
</if>
<if test="objectResult.noBoundInfo != null">
NO_BOUND_INFO = #{objectResult.noBoundInfo,jdbcType=VARCHAR},
</if>
<if test="objectResult.matchPicNum != null">
MATCH_PIC_NUM = #{objectResult.matchPicNum,jdbcType=NUMERIC},
</if>
<if test="objectResult.pidStart != null">
PID_START = #{objectResult.pidStart,jdbcType=NUMERIC},
</if>
<if test="objectResult.pidEnd != null">
PID_END = #{objectResult.pidEnd,jdbcType=NUMERIC},
</if>
<if test="objectResult.taskDir != null">
TASK_DIR = #{objectResult.taskDir,jdbcType=NUMERIC},
</if>
<if test="objectResult.taskCategory != null">
TASK_CATEGORY = #{objectResult.taskCategory,jdbcType=NUMERIC},
</if>
<if test="objectResult.payState != null">
PAY_STATE = #{objectResult.payState,jdbcType=NUMERIC},
</if>
<if test="objectResult.payMd5 != null">
PAY_MD5 = #{objectResult.payMd5,jdbcType=VARCHAR},
</if>
<if test="objectResult.appType != null">
APP_TYPE = #{objectResult.appType,jdbcType=NUMERIC},
</if>
<if test="objectResult.autonaviId != null">
AUTONAVI_ID = #{objectResult.autonaviId,jdbcType=VARCHAR},
</if>
<if test="objectResult.tid != null">
TID = #{objectResult.tid,jdbcType=NUMERIC},
</if>
<if test="objectResult.coefficient != null">
COEFFICIENT = #{objectResult.coefficient,jdbcType=NUMERIC},
</if>
<if test="objectResult.payType != null">
PAY_TYPE = #{objectResult.payType,jdbcType=NUMERIC},
</if>
</set> <include refid="Common.parseFilterRules"/> </update>

objectResult是数据库映射到java的实体对象,在XML中配置如下:

    <resultMap type="com.autonavi.collect.ccs.dao.dbo.task.RBTaskScoreDO" id="objectResult">
<result column="SID" property="sid"/>
<result column="USER_ID" property="userId"/>
<result column="JID" property="jid"/>
<result column="RID" property="rid"/>
<result column="SCORE" property="score"/>
<result column="ADDDATE" property="adddate"/>
<result column="CHECK_STATE" property="checkState"/>
<result column="GID" property="gid"/>
<result column="IS_CHECK" property="isCheck"/>
<result column="ROAD_LENGTH" property="roadLength"/>
<result column="CHECK_IS_BOUND" property="checkIsBound"/>
<result column="MATCH_LEN_PCT" property="matchLenPct"/>
<result column="MATCH_PNT_NUM" property="matchPntNum"/>
<result column="IS_BOSS" property="isBoss"/>
<result column="IS_BOSS_DATE" property="isBossDate"/>
<result column="MD5" property="md5"/>
<result column="CHECK_DATE" property="checkDate"/>
<result column="USERTYPE" property="usertype"/>
<result column="SRCDATE" property="srcdate"/>
<result column="TASK_TYPE" property="taskType"/>
<result column="NO_BOUND_INFO" property="noBoundInfo"/>
<result column="MATCH_PIC_NUM" property="matchPicNum"/>
<result column="TASK_CLASSES" property="taskClasses"/>
<result column="PID_START" property="pidStart"/>
<result column="PID_END" property="pidEnd"/>
<result column="TASK_DIR" property="taskDir"/>
<result column="TASK_CATEGORY" property="taskCategory"/>
<result column="PAY_STATE" property="payState"/>
<result column="PAY_MD5" property="payMd5"/>
<result column="APP_TYPE" property="appType"/>
<result column="AUTONAVI_ID" property="autonaviId"/>
<result column="TID" property="tid"/>
<result column="COEFFICIENT" property="coefficient"/>
<result column="PAY_TYPE" property="payType"/>
</resultMap>

这样在配置好了之后,在java中调用update的时候,传入实体类和过滤器就OK。

RBTaskScoreDO entity = new RBTaskScoreDO();
entity.setRid(Long.parseLong(rid));
entity.setJid(Long.parseLong(jid));
entity.setCheckIsBound(Long.parseLong(checkIsBound));
entity.setNoBoundInfo(noBoundInfo);
entity.setCheckState(Long.parseLong(checkState)); List<FilterRule> filterRuleList = new ArrayList<>();
filterRuleList.add(new FilterRule("jid", "=", jid));
filterRuleList.add(new FilterRule("task_type", "=", taskType));
filterRuleList.add(new FilterRule("task_classes", "=", taskClasses));
filterRuleList.add(new FilterRule("is_check", "=", 1)); rbtaskscoreService.updateByJid(entity, filterRuleList);

MyBatis SQL动态装配的更多相关文章

  1. MyBatis的动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...

  2. Mybatis解析动态sql原理分析

    前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...

  3. mybatis 使用动态SQL

    RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...

  4. MyBatis 实践 -动态SQL/关联查询

    MyBatis 实践 标签: Java与存储 动态SQL 动态SQL提供了对SQL语句的灵活操作,通过表达式进行判断,对SQL进行拼接/组装. if 对查询条件进行判断,如果输入参数不为空才进行查询条 ...

  5. (转)mybatis:动态SQL

    概述:在mybatis中,动态语句是个非常强大和灵活的功能,并且动态语句可以放在sql的任何地方,利用该功能,我们可以写出非常灵活的代码.在mybatis的动态语句中常常可能会用到以下几个运算和逻辑判 ...

  6. MyBatis框架——动态SQL、缓存机制、逆向工程

    MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...

  7. 使用Mybatis实现动态SQL(一)

    使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面:        *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...

  8. MyBatis探究-----动态SQL详解

    1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...

  9. mybatis中的.xml文件总结——mybatis的动态sql

    resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...

随机推荐

  1. Linux驱动学习之驱动开发准备工作

    一.开启驱动开发之路 1.驱动开发的准备工作 (1)正常运行linux系统的开发板.要求开发板中的linux的zImage必须是自己编译的,不能是别人编译的.原因在于在安装模块的时候会进行安全性校验 ...

  2. 第十八周个人作业--The Final

    项目计划   完成这个项目需要的时间:5-7天项目开发  需求分析:    作为一名排球赛事管理者,我希望能够根据比赛查询每场比赛的结果,以便于确定每支球队的比赛名次.  设计文档    由排球比赛用 ...

  3. wcf用svcutil导出泛型的元数据

    D:\aaa>svcutil net.tcp://192.168.1.110:44444/TradingsService.svc/mex /ct:System.Collections.Gener ...

  4. linux中用户、组的管理(密码管理、权限管理及其修改用户、组)(转)

    process(进程) 1 计算资源 权限 用户(获取资源或服务的凭证或标识) 用户,容器,关联权限:用户组(标识符),方便地指派权限 2 用户.组.权限   安全上下文(secure context ...

  5. nginx https反向代理 tomcat

    前端nginx配置 server { listen ; server_name localhost; listen ssl; ssl_certificate /usr/local/nginx/sslk ...

  6. [题解]codevs1001 舒适的路线

    h3 { font-family: Consolas; color: #339966 } .math { font-family: Consolas; color: gray } 题目描述 Descr ...

  7. Java 枚举7种常见用法

    (转)原创地址:http://blog.lichengwu.cn/java/2011/09/26/the-usage-of-enum-in-java/ JDK1.5引入了新的类型--枚举.在 Java ...

  8. es6之set和map

    1.set ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值,set本身就是构造函数,所以可以通过new的方式来创建对象 // 例一 可以做数组去重用 var set ...

  9. 08JAVA多态

    //我并没有用多态,因为我也不知道怎么用多态实现ATM操作流程/* * 模拟ATM机 * 徐浩军 20153153 */ package 中转; import java.util.Scanner; p ...

  10. python学习之——pip的安装与使用

    安装步骤: 前提:已经安装python 1.下载安装包并解压: easy_install 地址:https://pypi.python.org/pypi/setuptools#downloads pi ...