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 ...
随机推荐
- redis的安装和启动
Windows下Redis的安装及PHP扩展使用 时间 2014-10-28 17:47:09 CSDN博客 原文 http://blog.csdn.net/wyqwclsn/article/de ...
- vim安装不上
前阵子,刚安装Ubuntu时,安装vim的问题,现在些出来分享一下.apt-get install vim正在读取软件包列表... 完成正在分析软件包的依赖关系树正在读取状态信息... 完成有一些软件 ...
- StaticPagedList
估计是因为水平原因,之前看别人写的用pagedList分页,老是云里雾里的.下面把自己写的整理一下放在上面.这里的List为对应页面展示的内容.不用查询所有. Action: public Actio ...
- IE6/IE7/IE8兼容H5标签
可以使用html5shiv(html5shiv主要解决HTML5提出的新元素不被IE6-8识别,这些新元素不能作为父节点包裹子元素,并且不能应用CSS样式)来解决 <!--[if lt IE 9 ...
- Expected one result (or null) to be returned by selectOne(), but found 2
这个问题在于你查询sql返回结果是多个值.一个集合,但是你在service的实现层的dao都调用了.get方法.而是应该使用.getlist方法等.
- (转载)python2+selenium自动化测试系列(一)
1.Selenium2+python自动化1-环境搭建 2.Selenium2+python自动化2-pip降级selenium3.0 3.Selenium2+python自动化3-解决pip使用异常 ...
- OO基本原则
1. 单一职责原则(SRP) 一个类应该最多只能有一个因素能够给导致其变化,类中的方法应该都是相关性很高的,即"高内聚" 2. 开放-封闭原则(OC) - 扩 ...
- 《Reactive_MircService_Architecture》 脑图
Reactive_MircService_Architecture Lightbend CTO的50页的小册子,对响应式系统以及微服务架构介绍非常全面,整理了一个脑图来先.
- mysql,实现数据库检索结果添加自增的序号
select t2.rowno from( select (@rownum:=@rownum+1) as rowno, t1.id from news t1 ,(select (@rownum ...
- Robot Framework入门学习1 安装部署详解
安装注意: 目前Robot framework-ride不支持python3,安装时请下载python2.7版本. Robot Framework安装时出现了一点小问题,网上没有找到直接的介绍,现将安 ...