mybatis mapper映射文件全解
目录
select、update、delete、insert
这分别对应有四个标签<select>、<update>、<delete>、<insert>,在绑定sql语句的时候,可以有很多的属性,属性可以见名知意,具体如下:
<select
id="selectPerson"
parameterType="int"
resultType="hashmap"
resultMap="personResultMap"
flushCache="false"
useCache="true"
timeout="10000"
fetchSize="256"
statementType="PREPARED"
resultSetType="FORWARD_ONLY"> <insert
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
keyProperty=""
keyColumn=""
useGeneratedKeys=""
timeout="20"> <update
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20"> <delete
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20">
对于<select>、<insert>、<update>、<delete>,SqlSession分别有两个方法与之对应:
<select>
int SqlSession.select(String statement)
int SqlSession.select(String statement, Object parameter) <insert>
int SqlSession.insert(String statement)
int SqlSession.insert(String statement, Object parameter) <update>
int SqlSession.update(String statement)
int SqlSession.update(String statement, Object parameter) <delete>
int SqlSession.delete(String statement)
int SqlSession.delete(String statement, Object parameter)
设置参数类型以及取值
设置参数类型,是通过设置parameterType的值,设置的类型不同,sql中取值的方式也有所区别。
parameterType属性可以有下面几种取值:
1、基本数据类型
比如int、double,其实应该写为Integer、Double,但是有自动装箱功能,所以可以直接写基本数据类型。
sql取值的方式:对于这种的参数,在接收传入的参数值时,可以使用#{index}来获取,注意,index从0开始计数,如果只有一个参数,那么index可以写成任意名称。
<mapper namespace="cn.ganlixin.mapper.PersonMapper"> <select id="selectPersonById1" parameterType="int" resultType="cn.ganlixin.pojo.Person">
select * from person where id=#{0}
</select> <!-- 如果只有参数,可以将#{0} 写为任意名称 -->
<select id="selectPersonById2" parameterType="int" resultType="cn.ganlixin.pojo.Person">
select * from person where id=#{id}
</select>
</mapper>
2、Object类型
可以写实体类(如果没有配置alias,需要写全路径),比如cn.ganlixin.pojo.Person类。
sql取值的方式:对于对象这种类型,可以使用#{property}去访问实体类中的property属性值,比如#{age},可以获取Person的age属性值。
<mapper namespace="cn.ganlixin.mapper.PersonMapper">
<select id="selectPersonByName"
parameterType="cn.ganlixin.pojo.Person"
resultType="cn.ganlixin.pojo.Person"> select * from person where name=#{name}
<!-- 相当于取Person.name的值 -->
</select>
</mapper>
简单测试:
String method = "cn.ganlixin.mapper.PersonMapper.selectPersonByName";
Person person = new Person();
person.setName("aaaa");
person = (Person) session.selectOne(method, person);
3、map类型
将要传入一个数据,封装到一个map中,然后再传入即可。
sql取值的方式:对于map的取值,和object的取值类似,使用#{key}即可获取map中key对应value。
<mapper namespace="cn.ganlixin.mapper.PersonMapper">
<select id="selectPersonByName" parameterType="map" resultType="cn.ganlixin.pojo.Person">
select * from person where name=#{name}
<!-- 相当于取map.get(name)的值 -->
</select>
</mapper>
简单测试:
String method = "cn.ganlixin.mapper.PersonMapper.selectPersonByName";
Map<String, String> data = new HashMap<>();
data.put("name", "aaaa");
Person person = (Person) session.selectOne(method, data);
4、#{ } 和 ${ } 的区别
需要注意的是,sql中取值可以使用#{ } 和 ${ }两种方式,区别在于:
1、#{ } 会使用预处理操作,而${ }不会进行预处理操作。
2、${ } 只是很简单的使用字符串拼接
select * from person where name='${name}'
如果上面不适用引号将${name}括起来,那么就会将${name}的值当做字段名,而不是字段值 select * from person where name=${0}
上面这条sql语句,并不会使用传入的第1个参数,而是真的name=0
ResultMap
关于resultMap的使用,可以查看:
Auto-mapping
关于auto-mapping的使用,可以查看:
mybatis 使用auto mapping原理实现表间关联
cache
mybatis中的缓存有两种,一级缓存和二级缓存。
可以查看:mybatis 使用缓存策略
最新文章
- ASP.NET MVC 项目中 一般处理程序ashx 获取Session
- AngularJS中使用service,并同步数据
- 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表
- c#基础,面试前迅速巩固c#最基础知识点
- JAVA小记
- 函数buf_LRU_free_block
- oracle自动编号
- Android多项目依赖在Eclipse中无法关联源代码的问题解决 Ctril 点不进去的解决方法
- 用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的Code First迁移和部署
- Android Material Design 系列之 SnackBar详解
- h5 做app时和原生交互的小常识。
- Python并发编程之线程中的信息隔离(五)
- Topshelf的使用
- Others-常用数学符号大全
- 自动发现项目中的所有URL
- 用01随机函数构造[a,b]整数范围随机数
- Union和Union All的区别[转]
- grep常用用法
- Oracle EBS Patch Demo
- JDK自带监控工具
热门文章
- MySql如何查询JSON字段值的指定key的数据
- [日常] nginx的错误日志error_log设置
- C# 处理PPT水印(一)——添加水印效果(文字水印、图片水印)
- 关于火狐和IE下href=";javascript:void(0)";兼容性的问题
- js生成随机颜色
- 51nod“省选”模测第二场 B 异或约数和(数论分块)
- iPad----------教你如何查询ipad型号
- 关于ipv6被拒的问题
- po编程——自动化测试面试必问
- FT 软件项目管理