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 ...
随机推荐
- tomcat配置性能调优1----server.xml文件详解
<?xml version='1.0' encoding='utf-8'?><!-- Licensed to the Apache Software Foundation (ASF ...
- nodejs学习笔记(一)
终于开始学NodeJs了 说说遇到的问题吧 1.安装express npm install -g express(按道理说,这个全局安装是没问题的,但他就是出问题了) 解决方案: 来源:http:// ...
- react js 之生命周期
react redux 结合是目前比较流行的前端开发框架,主要基于react 中的state 树为数据模型,借助redux 来控制 state 数据:下面直接从代码层面解析该框架中一个react 组件 ...
- hdu5442 Favorite Donut
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5442 题目大意:给你一个长度为n的字符串,将它首尾相连成环.问你这个环上找一个长度为n的字典序最大的串 ...
- iOS中UITableView使用总结
链接:http://www.open-open.com/lib/view/open1430008922468.html
- win10使用小技巧以及常见问题处理方案
1.win10开机一直处于黑屏状态或者反复重启怎么处理? 处理小方法:开机按win+X组合件进入高级修复模式---选择修复开启模式---f4进入安全模式开机状态---管理控制面板---禁用或者卸载显卡 ...
- 初窥Kaggle竞赛
初窥Kaggle竞赛 原文地址: https://www.dataquest.io/mission/74/getting-started-with-kaggle 1: Kaggle竞赛 我们接下来将要 ...
- [MOSEK] Stupid things when using mosek
1.2016-8-14 我希望把一个qp问题的代码从conic constraints改为无外加约束,仅适用variable bounds的线性不等式约束 于是原来的约束代码为 if (r == MS ...
- postgreSQL 统计语句
pg_stat_statements 是 postgresql 的一个扩展,用来统计查询语句,类似于 mysql 的 慢查询. 安装二进制文件 有些发行版可能没有附带这个扩展,则需要用户自己安装, 本 ...
- Tornado中Cookie过期问题
首先,web应用程序是使用HTTP协议进行数据传输,因为HTTP协议是无状态的,所以一旦提交数据完成后,客户端和服务器端的连接就会被关闭,再次进行数据的交换就得重新建立新的连接,那么,有个问题就是服务 ...