作者:longgangbai

以前用过ibatis2,但是听说ibatis3有较大的性能提升,而且设计也更合理,他不兼容ibatis2.尽管ibatis3还是beta10的状态,但还是打算直接使用ibatis3.0, 
ibatis3.0应该更简单高效.最近还自己写了个ibatis3.0与spring集成的bean,运行还正常,还自鸣得意了一番,但是当独立使用ibatis时,在事务管理这个方面还是出现不少问题,所以还是打算再认真研究一番ibatis3.0

1.SqlSessionFactory 
每个ibatis应用都应该只有一个SqlSessionFactory的实例对象,所以一般设置为static属性或者使用spring管理时返回singleton类型,与spring集成时其实也是写一个怎样构建SqlSessionFactory的Bean, 
构建SqlSessionFactory一般是SqlSessionFactoryBuild通过读取ibatis的配置文件而进行build的: 
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml"); 
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuild().build(reader);

配置文件SqlMapConfig.xml的一般结构(元素顺序不能变)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration      PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"      "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties" />
<settings>
<setting name="cacheEnabled" value="false" />
<setting name="lazyLoadingEnabled" value="true" />
<setting name="multipleResultSetsEnabled" value="false" />
<setting name="useColumnLabel" value="true" />
<setting name="defaultExecutorType" value="SIMPLE" />
</settings>
<typeAliases>
<typeAlias alias="Person" type="test.Person"/>
</typeAliases>
<environments default="dev">
<environment id="dev">
<transactionManager type="jdbc">
<property name="" value="" />
</transactionManager> <dataSource type="POOLED"> <property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="sqlMappers/Person.xml" />
<mapper resource="sqlMappers/UserShop.xml" />
</mappers>
</configuration>

<settings />是配置ibatis的具体行为属性的,

<typeAliases />是为了将较长的module类名简化,在<mappers />里可以使用 
<environment />是配置transaction manager和connection pooling的 
<mappers />是sql语句的构造地方,一般每个module对应一个文件

2.SqlSession 
可以从SqlSessionFactory得到SqlSession: sessionFactory.openSession(); 
SqlSession是一切Sql相关数据库操作的中心,insert,select,update,delete... 
SqlSession不是线程安全的(也就是有状态的),所以它的作用域最好是在一个Thread下,每个Thread有自己的SqlSession对象实例,彼此不相关. 
Never keep references to a SqlSession instance in a static field or even an instance field of a class.  Never keep references to a 
SqlSession in any sort of managed scope, such as HttpSession of of the Servlet framework.

默认sessionFacory.openSession()拿到的SqlSession不是自动commit的,所以如果是更新操作必须自己执行session.commit() 
关闭SqlSession很重要,必须保证在线程结束时关闭这个SqlSession,可以在finally中 
session.close(); 
那跟Spring集成是怎样做到这一点的呢,因为dataSource是由spring管理的,所以他可以保证在一个Thread的每个方法中拿到的Connection是同一个对象, 
虽然每个方法从sessionFactory.openSession()拿到的SqlSession对象是不同的,但是sqlSession对象中的connection是相同的,所以spring就可以在service层的方法结束之前将这个connection commit跟close,这样就实现了事务控制. 
我们往往在dao层是一个方法对应一个sql语句的,不在这里控制事务,控制事务应该在service层, dao的每个方法拿到的sqlsession对象都是不相同的(尽管它的connection可能相同). 
那我们应该怎样在没有spring的情况下实现ibatis的事务控制呢?还要保持dao的结构,以保持能跟spring随时切换? 
看来ThreadLocal要派上用场了 
---占位----

3.讲完了SqlSession这个大头,再来说说具体的配置信息 
配置文件结构 
configuration 
    properties 
    settings 
    typeAliases 
    typeHandlers 
    objectFactory 
    plugins 
    environments 
       environment 
           transactionManager 
           dataSource 
    mappers 
       
4.ibatis可以配置多个environment环境 
供开发,测试,上线等切换 
但是一个SqlSessionFactory只能对应一个environment, 
也就是 one SqlSessionFactory per database

5.transactionManager 
There are two TransactionManager types (i.e. type=”?????”) that are included with iBATIS: 
    JDBC – This configuration simply makes use of the JDBC commit and rollback facilities directly.  It relies on the connection retrieved from the dataSource to manage the scope of the transaction.   
    MANAGED  – This configuration simply does nothing, quite literally.  It never commits, rolls back or closes a connection.  Instead, it lets the container manage the full lifecycle of the transaction (e.g. Spring or a JEE Application Server context).

6.dataSource的配置 
类型可使用UNPOOLED,POOLED,JNDI三种

7.接下来是重头戏mappers 
这里是我们直接写sql的地方,ibatis在这里也花了最多功夫,特别是关于select的mapper.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"><mapper namespace="test.UserShop"> <insert ...> ... </insert> <select ...> ... </select> <update ...> ... </update> <delete ...> ... </delete>
</mapper>

select 元素的所有的如下:

<selectid=""parameterType=""resultType=""resultMap=""flushCache=""useCache=""timeout=""fetchSize=""statementType=""resultSetType="">

在Ibatis3中parameterMap已经不再使用。
(1).select 
<select id="getUser" parameterType="int" resultType="test.User"> 
       select * from user where userId = #{value} 
</select> 
注意#{value},resultType可以换成resultMap,这个resultMap就比较复杂了,也是最强大的工具,可以完成复杂的mapper(resultSet->object)

(2).insert,update,delete 
这几个操作都比较类似,返回的一般是受影响的行数. 
insert 可以返回auto_increament的值

<insert id="insertPerson" parameterType="Person"
useGeneratedKeys="true" keyProperty="userId">
insert into person(name,age) values(#{name},#{age})
</insert> 

这样,传入的Person类对象的userId就会被set成auto_increament那个值.

(3).可以使用<sql id="columns">name,age</sql>构造可重用的sql片段 
这样就可以在那四个主要的元素里用<include refid="columns">引用这个常用sql片段

(4).resultMap很复杂,它可以处理一对一,一对多,多对多关系 
比如一个blog对应一个author, 一个blog对应多个post, 一个post对应多个comment 
resultMap的结构: 
resultMap 
    constructor (向java构造函数设置值) 
       idArg 
       arg 
    id (id result,同result) 
    result (字段映射注入) 
    association (一对一关系,它里面有自己的result映射) 
    collection  (一对多关系, 它里面有自己的result映射,和可能的collection) 
    discriminator

(5).association (一个Blog对应一个Author)

<resultMap id="blogResult" type="Blog"> <association property="author" column="blog_author_id" javaType="Author"> <id property="id" column="author_id" /> <result property="username" column="author_username" /> </associaton>
</resultMap> 

当然association也可以使用 
<association property="author" column="blog_author_id" javaType="Author" resultMap="authorResult"/>

在Blog类定义里会有一个Author类字段,会自动装载数据 
private Author author;

(6).collection (一个Blog对应多个Post)

<resultMap id="blogResult" type="Blog"> <collection property="posts" column="blog_id" ofType="Post" javaType="ArrayList" resultMap="postResult" />
</resultMap>
collection也可以精简为
<collection property="posts" ofType="Post" resultMap="postResult" />
相就地,在Blog类定义里会有:
private List<Post> posts

8.cache

其实我是不打算在ibatis这层做cache的,一般cache还是在service层做. 
ibatis对于select才会进行cache,而我觉得这个时候cache没多大意义,因为多次請求相同的select語句(sql相同)而又没有进行表的相关update的情况并不多. 
但还是看看ibatis是怎样处理cache的 
要开启cache,需要在mapper文件中加上一句: <cache /> 
(1).cache所有select的結果 
(2).在同一mapper文件中的insert,update,delete会flush cache 
(3).cache使用least recently used(LRU)算法 
(4).cache没有定时flush的功能 
(5).cache保存1024个对象或list引用 
(6).cache是read/write cache, 从cache拿出对象不是共享的,caller可以任意修改而不用担心其他caller也拿到相同的对象(相同的reference)

9.动态SQL 
以前用jdbc时经常要根据输入条件而组装成不同的sql,这种就是动态sql 
ibatis的动态sql关键词:

if, choose(when,otherwise),trim(where,set),foreach
(1).if
<if test="title != null"> AND title like #{title}
</if>
<![CDATA[
]]>

(2).choose(when,otherwise)只选择其中一种情况

<select ...>
<choose> <when test="title != null"> AND title like #{title} </when> <when test="author != null and author.name != null"> AND title like #{author.name} </when> <otherwise> AND featured = 1 </otherwise>
</choose>
</select>

(3).trim, where, set

<select id="findBlog" paramaterType="Blog" resultType="Blog"> select * from blog <where> <if test=”state != null”> state = ${state} </if> <if test=”title != null”> AND title like ${title} </if> <if test=”author != null and author.name != null”> AND title like ${author.name} </if> </where>
</select>   <update id="updateAuthorIfNecessary" parameterType="domain.blog.Author"> update Author <set> <if test="username != null">username=#{username},</if> <if test="password != null">password=#{password},</if> <if test="email != null">email=#{email},</if> <if test="bio != null">bio=#{bio}</if> </set> where id=#{id}
</update>

(4).foreach

<select id="selectPostIn" resultType="domain.blog.Post"> SELECT * FROM POST P WHERE ID in <foreach item="item" index="index" collection="list"  open="(" separator="," close=")"> #{item} </foreach>
</select>

10.ibatis的annotation 
ibatis3.0也增加了annotation,当然主要还是用在mapper这个问题上 
个人感觉是鸡肋,暂时不会使用.

转载于:https://www.cnblogs.com/lxl57610/p/7399184.html

Mybayis的项目使用的Mapping文件使用总结参考(一)相关推荐

  1. 使用mybatis-generator自动生成model、dao、mapping文件

    参考文献:http://www.cnblogs.com/smileberry/p/4145872.html 一.所需库 1.mybatis-generator库 2.连接DB的驱动(此以mysql为例 ...

  2. web项目的启动时文件加载顺序

    web项目的启动时文件加载顺序 转自:http://www.cnblogs.com/yw-ah/p/5751509.html 一. 1.启动一个WEB项目,WEB容器会先去读取它的配置文件web.xm ...

  3. vue html引入资源dev下404,webpack vue 项目打包生成的文件,资源文件报404问题的修复方法(总结篇)...

    最近在使用webpack + vue做个人娱乐项目时,发现npm run build后,css js img静态资源文件均找不到路径,报404错误...网上查找了一堆解决办法,总结如下 一.首先修改c ...

  4. springboot项目输入打印日志文件到本地

    springboot项目输入打印日志文件到本地 pom.xml中添加jar包: <dependency><groupId>ch.qos.logback</groupId& ...

  5. android stadio mapping文件的使用

    mapping文件就是在对代码混淆以后,你用来查看混淆前和混淆后的对比文件. 一般用来查看线上的bug; java.lang.NullPointerException: Attempt to invo ...

  6. IDEA中创建maven项目后解决main文件夹下目录不全的问题

    IDEA中创建maven项目后解决main文件夹下目录不全的问题 参考文章: (1)IDEA中创建maven项目后解决main文件夹下目录不全的问题 (2)https://www.cnblogs.co ...

  7. XamarinSQLite教程在Xamarin.Android项目中提取数据库文件

    XamarinSQLite教程在Xamarin.Android项目中提取数据库文件 由于不能直接打开该文件,开发者需要先将数据库文件从Android系统中提取出来.操作步骤如下. (5)选择MyDoc ...

  8. XamarinSQLite教程在Xamarin.Android项目中定位数据库文件

    XamarinSQLite教程在Xamarin.Android项目中定位数据库文件 实际开发中,经常需要验证数据库操作的正确性.这个时候,需要打开数据库文件,进行确认.下面是如何找到MyDocumen ...

  9. XamarinSQLite教程Xamarin.iOS项目中打开数据库文件

    XamarinSQLite教程Xamarin.iOS项目中打开数据库文件 以下是打开MyDocuments.db数据库的具体操作步骤: (1)将Mac电脑上的MyDocuments.db数据库移动到W ...

最新文章

  1. 图解MongoDB的连接与使用,通俗易懂
  2. Java class loader调试
  3. java显示一个钟表_中秋团圆日,月相表来一个呗~
  4. 【TOJ1132】Square Root,二次同余方程
  5. 浏览器 调用 vue 组件_父子组件的通信
  6. 单链表的尾插,头插,遍历,查找和插入
  7. [Swift]LeetCode1044. 最长重复子串 | Longest Duplicate Substring
  8. c语言 屏幕亮度调节_4096级屏幕亮度调节:改善安卓机自动亮度调节顽疾
  9. projecteuler_problem12
  10. 免费的WordPress Video Player插件
  11. 读《从优秀到卓越》乱摘
  12. python-matplotlib制作图表与中文正常显示
  13. 5.2 PMBOK--收集需求
  14. javaEE项目--琪琪线上餐厅系统
  15. 公司两个路由器如何进行互相访问
  16. 这不是广告!千万不要相信淘宝上卖的CSDN会员!!!
  17. Java毕设项目师生健康信息管理系统(java+VUE+Mybatis+Maven+Mysql)
  18. 温州医科大学计算机学院分数线,2017温州医科大学录取分数线
  19. 计算机相关的外文翻译,计算机外文翻译
  20. 接口做的好怎么形容_MLC和TLC的差距大吗?宏旺半导体告诉你国产固态硬盘该怎么选?...

热门文章

  1. 数据结构快速掌握和温习-面试神器
  2. mysql 1066解决方法_MySQL查询语法帮助:错误#1066-表格/别名不唯...
  3. “之”字形打印矩阵~
  4. 《springcloud超级入门》微服务的概念和优缺点《一》
  5. 四叶草剧场服务器维修价格,四叶草剧场不合理报酬机制是什么-不合理报酬机制和收益详解-Appfound...
  6. 冯乐乐 unity_Unity常用矩阵运算的推导补遗——切线空间
  7. php 500 yii,yii2.0出现500错误怎么办
  8. 紫金计算机网络,南京理工大学紫金学院《计算机网络技术》考试复习题集试题(卷)(含答案解析)2.doc...
  9. c++数字金字塔_“资金管理是投资最大的秘密”(超级干货),一生死记“金字塔加仓减仓法”,最安全稳健的操盘法方式!...
  10. 数据采集与清洗基础习题(二)Python爬虫常用模块,头歌参考答案