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 ...
随机推荐
- Jquery / js 判断数据类型方法(限制文本框类型输入)
当想要判断文本框中的值是否为自己想要的类型时,可以通过一些方法作出判断,这里对于光标离开文本框时判断文本框中输入的是否是数值类型,如果不是,做出提示 $("#WORKYEARS") ...
- 利用 cookie 模拟网站登录
import urllib import urllib2 import cookielib filename = 'cookie.txt' \#声明一个MozillaCookieJar对象实例来保存c ...
- privoxy代理google浏览器访问缓慢
取消--no-daemon <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PU ...
- laravel(三):larave基本使用
1.基本视图显示 前文已经介绍如何创建控制器.动作和视图,下面我们来创建一些更实质的功能. 在此之前我们需要修改一些配置: app/config/app.php 文件中的 debug 选项设置为 tr ...
- GoogleNet tips
Inception Module googlenet的Inception Module Idea 1: Use 1x1, 3x3, and 5x5 convolutions in parallel t ...
- asp.net web api添加自定义认证
1.定义认证失败结果生成器 /// <summary> /// 认证失败结果生成器 /// </summary> public class AuthenticationFail ...
- 合并两个排好序的链表(c++)
#include<iostream> struct node{ int payload; node* next; node(int payload){this->payload=pa ...
- IIS上虚拟目录下站点的web.config与根站点的web.config冲突解决方法
IIS7.5上在站点下部署虚拟目录,访问虚拟目录下的项目提示与父节点配置冲突.,节点与的<system.web>节点与主站点的<system.web>冲突解决方法: 在站点下的 ...
- jQuery Select 自动选择默认值
////// the Select target Auto Select value extension plugin // // ---------------------------------- ...
- Android 动画详解
这次主要就介绍android动画,android动画目前分为三种形式,Tween Animation 这个只能应用于view对象上面的,Drawable Animation这个是帧动画,就是类似我们有 ...