mybatis使用小记
参考资料:http://blog.csdn.net/hupanfeng/article/details/9098453
1、设置不缓存每次查询的结果:
如题,通过设置 flushCache="true" useCache="false" 两个属性来达到目的。
|
flushCache |
将其设置为true,不论语句什么时候被调用,都会导致缓存被清空。默认值:false。 |
|
useCache |
将其设置为true,将会导致本条语句的结果被缓存。默认值:true。 |
2、返回类型的设置:
resultType、resultMap不可同时使用(虽然都指定这两个属性时不会报错)。
前者将数据库表的字段名映射成指定的字段名,并封装成指定的对象;后者将数据库表字段名映射成返回类型里同名字段,若有些字段在返回类型里没有对应字段则可能出错。
3、insert语句返回自增主键值
方法之一是在insert标签里加上useGeneratedKeys="true", keyProperty="addedTime" 这两个属性,其中addedTime是传入参数之一,插入后返回的最新主键值将赋给该变量,程序中就可以通过该传入参数获得新主键值。
4、MyBatis SQL语句中 foreach的使用
相关资料:http://blog.csdn.net/isea533/article/details/21237175
示例如下。foreach可对三种对象做循环,分别为List,[](array),Map三种。collection指出相对哪个元素做循环,item循环中的具体对象,index指序号(List、数组中)或key(map中)。
<insert id="insertPostImgsInfo" parameterType="map"> <!--传入的参数为map对象:{'postId':xxx, 'imgPath': [...] }-->
insert into post_img (pid,img_order,img_location) values
<foreach collection="imgPath" item="item" index="index"
separator=",">
(
#{postId},#{index},#{item}
)
</foreach>
</insert>
5、关于#{}和${}
默认情况下,前者有占位符的效果,使用#{}语法,MyBatis会产生PreparedStatement语句,并且安全地设置PreparedStatement参数,这个过程中MyBatis会进行必要的安全检查和转义;而后者则是直接拼接,传入什么就是什么。示例如下:
执行SQL:Select * from emp where name = #{employeeName}
参数:employeeName=>Smith
解析后执行的SQL:Select * from emp where name = ?
并设置参数为 'Smith'
执行SQL:Select * from emp where name = ${employeeName}
参数:employeeName传入值为:Smith
解析后执行的SQL:Select * from emp where name =Smith
总的来说:$y=2x+x^2$
${}方式会引发SQL注入的问题,同时也会影响SQL语句的预编译,所以从安全性和性能的角度出发,能使用#{}的情况下就不要使用 \${}
而对于动态表名、动态字段、排序字段,则只能使用${}才能达到预期的功能。注意:当使用${}参数作为字段名或表名时,需要指定statementType=STATEMENT,而使用#{}时不能有该语句
6、MyBatis使用like查询:select * from person where name like "%"#{name}"%"
7、MyBatis批量插入:
<!--参数是一个map,包含userId和arrayJourneyData两字段,后者是一个数组-->
<insert id="insertJourneyBatch" parameterType="map">
insert into
train_journey
(
user_id,
local_journey_id,
begin_time,
end_time,
journey_type,
begin_latitude,
end_latitude,
begin_longitude,
end_longitude,
driving_mode,
duration,
rapid_acc_times,
rapid_turn_times,
acc_score,
turn_score,final_score
)
values
<foreach collection="arrayJourneyData" item="item" index="index"
separator=",">
(
#{userId},
#{item.localJourneyId},
#{item.beginTime},
#{item.endTime},
#{item.journeyType},
#{item.beginLatitude}, #{item.endLatitude},
#{item.beginLongitude},
#{item.endLongitude},
#{item.drivingMode},
#{item.duration}, #{item.rapidAccTimes},
#{item.rapidTurnTimes},
#{item.accScore},
#{item.turnScore},
#{item.finalScore}
)
</foreach>
</insert>
8、MyBatis批量查询
<!--参数是个[{},{},..],数组每个元素包含userId、beginTime两字段-->
<select id="selectLatestGpsInJourneyByUseridBegintime" parameterType="list" resultType="map">
select
user_id as userId,
timestamp,
latitude,
longitude,
speed,
bearing
from train_feature_new
where
<foreach item="item" index="index" collection="list" open="(" separator="or" close=")">user_id=#{item.userId} and journey_time=#{item.beginTime}</foreach>
mybatis使用小记的更多相关文章
- MyBatis Collection小记—— 关联查询、递归查询、多字段关联
经常会用到mybatis的Collection标签来做级联查询或递归查询,现通过一个伪例来简单的说明一下使用中的关键点: 首先先列出三个表,给出一个场景: 1,角色表 t_role( id,name ...
- mybatis开启二级缓存小记
mybatis开启二级缓存小记 1.开启二级缓存 和一级缓存默认开启不一样,二级缓存需要我们手动开启 首先在全局配置文件 mybatis-configuration.xml 文件中加入如下代码: &l ...
- SpringBoot集成MyBatis小记
SpringBoot集成MyBatis小记 参考MyBatis官网 1. 添加maven依赖 添加到pom.xml <dependency> <groupId>org.myba ...
- mybatis中 ${}和#取值小记(Parameter index out of range)
mybatis mapperxml文件中有两种取值法.${}和#{} $的是原样,#的是取值并转成指定?#{ele1,jdbcType=VARCHAR} 有个坑, 错误的写法 <if test= ...
- mybatis批量增、删、改(更新)操作oracle和mysql批量写法小记
前言:用mybatis也好几年了,mybatis在批量的增删操作也写起来也是比较简单的,只有批量更新这一块是特别坑,特此记录. 注:本文主要用来记录oracle和mysql数据库在使用mybatis的 ...
- Mybatis generator使用小记
Mybatis generator可以使用命令行,eclipse插件和maven配置实现自动生成代码的功能,主要来看看使用eclipse插件生成代码方法. 先安装插件: maven加载mysql驱动: ...
- 问题小记(MyBatis传参出现的小问题)
问题一:在MyBatis中注解@Param和ParameterType不能一起用,会报错Parameter 'XXX' not found. Available parameters are [1, ...
- 【小记整理】mybatis配置多个扫描路径写法
百度得到,但是很乱,稍微整理下: 最近拆项目,遇到个小问题,稍微记录下: <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id ...
- Mybatis 小记
1,mybatis 中 $ # 区别 mybatis 动态传参的两种方式 #{ }在动态解析的时候,会将#{ } 解析为一个预编译阶段的一个标记符号?,在预处理阶段才会替换 ${ }在动态解析的时候, ...
随机推荐
- WP 8.1 中挂起时页面数据保存方式
1.保存到Applicaion Data配置信息中: 保存: private void testTB_TextChanged(object sender, TextChangedEventArgs e ...
- Linux网络共享管理(ssh,nfs,samba)
OpenSSH服务器: ssh协议:提供安全的ssh环境用于远程管理,默认端口:22: 服务名称:sshd openssh-server包的安装文件: 使用ssh客户端程序: 连接目标主机: ssh ...
- struts2中简单的文件上传
2016-08-31 一. 文件上传 利用commons-fileupload-1.2.1.jar实现简单的上传文件,首先在页面上填写表单,记得加上enctype="multip ...
- High Performance Animations
http://www.html5rocks.com/zh/tutorials/speed/high-performance-animations/
- C语言 百炼成钢18
//题目52:用递归打印以下图形 //* //*.*. //*..*..*.. //*...*...*...*... //*....*....*....*....*.... #include<s ...
- ZooKeeper学习第七期--ZooKeeper一致性原理
一.ZooKeeper 的实现 1.1 ZooKeeper处理单点故障 我们知道可以通过ZooKeeper对分布式系统进行Master选举,来解决分布式系统的单点故障,如图所示. 图 1.1 ZooK ...
- 管窥MVVMLight Command参数绑定和事件传递
前言 由于在实际项目中,业务功能的增加导致软件开发规模在逐渐变大,所以我准备找个Silverlight框架来组织当前项目中的文件,以期能够让后续的业务功能增添和维护更加容易一些.无意中,我在这篇文章中 ...
- 侧滑SilidingMenu ,ViewPager 和,PagerIndicator 冲突
今天敲代码过程中发现再ViewPager 页面中禁用的SilidingMenu侧滑菜单突然失效了,那么肯定是添加的PagerIndicator的问题, 然后我发现 把setOnPageChangeLi ...
- 面试准备(二) 绘制 Activity 的生命流程图
我们来看一下这一张经典的生命周期流程图: 相信不少朋友也已经看过这个流程图了,也基本了解了Activity生命周期的几个过程,我们就来说一说这几个过程. 1.启动Activity:系统会先调用onCr ...
- Win7 Qt4.8.5+QtCreator2.8.0+mingw配置过程
1:安装包 百度盘下载链接: Mingw: :安装步骤 1.首先安装qt creator,双击qt-creator-windows-opensource-2.8.0,注意安装目录不要有空格和特殊字符, ...