MyBatis SQL动态装配
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动态装配的更多相关文章
- MyBatis的动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...
- Mybatis解析动态sql原理分析
前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...
- mybatis 使用动态SQL
RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...
- MyBatis 实践 -动态SQL/关联查询
MyBatis 实践 标签: Java与存储 动态SQL 动态SQL提供了对SQL语句的灵活操作,通过表达式进行判断,对SQL进行拼接/组装. if 对查询条件进行判断,如果输入参数不为空才进行查询条 ...
- (转)mybatis:动态SQL
概述:在mybatis中,动态语句是个非常强大和灵活的功能,并且动态语句可以放在sql的任何地方,利用该功能,我们可以写出非常灵活的代码.在mybatis的动态语句中常常可能会用到以下几个运算和逻辑判 ...
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
- 使用Mybatis实现动态SQL(一)
使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面: *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...
- MyBatis探究-----动态SQL详解
1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...
- mybatis中的.xml文件总结——mybatis的动态sql
resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...
随机推荐
- 2.struts2访问web资源(在struts2中获取session,request等等)
什么是web资源:web资源就是指request,response,session,servlet的api 为什么需要访问web资源:因为图片上传,需要获取图片的目录,就需要通过action来访问we ...
- oracle 批量插入-支持序列自增
1.创建表.序列 -- Create table create table test_batch ( id number not null, name ), account ) ) -- Create ...
- Linux内核开机保留大块内存的方法
http://www.linuxidc.com/Linux/2014-03/97952.htm
- angularjs(二)模板终常用的指令的使用方法
通过使用模板,我们可以把model和controller中的数据组装起来呈现给浏览器,还可以通过数据绑定,实时更新视图,让我们的页面变成动态的.ng的模板真是让我爱不释手.学习ng道路还很漫长,从模板 ...
- ZOJ 3209 Treasure Map (Dancing Links)
Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 KB Your boss once had got many copies of ...
- 驱动开发学习笔记. 0.05 linux 2.6 platform device register 平台设备注册 2/2 共2篇
驱动开发读书笔记. 0.05 linux 2.6 platform device register 平台设备注册 2/2 共2篇 下面这段摘自 linux源码里面的文档 : 内核版本2.6.22Doc ...
- Linux内核的总结认识
转载博文: http://www.linuxdiyf.com/linux/11234.html 1.内核是怎样实现其管理的职能? 以前在学校时一直不能理解内核是怎么做管理?比如内核如何知道在什么时候对 ...
- jq 部分用法
这几天一直在写前台,因为jq是在客服端处理数据的,所以公司,一般都用这种方法,下面是我这几天用到的一些东西 1.修改table表格的第一轮显示值 function changeTableRowValu ...
- 利用GCTA工具计算复杂性状/特征(Complex Trait)的遗传相关性(genetic correlation)
如文章"Genome-wide Complex Trait Analysis(GCTA)-全基因组复杂性状分析"中介绍的GCTA,是一款基于全基因组关联分析发展的分析工具,除了计算 ...
- GridLayout自定义数字键盘(两个EditText)
功能说明: 适用于两个EditText输入框,并控制输入字符的长度,点击键盘中的"确定"按钮完成输入,点击"前一项"光标跳到前一个EditText 运行效果图如 ...