1.
MyBatis是一个支持普通SQL查询、存储过程和高级映射的优秀的持久层框架,它消除了几乎所有的JDBC代 码、对参数的手工设置以及对结果集繁琐的处理,使用简单的XML或注解(annotation)用于配置和映射,将接口和POJO(Plain Old Java Object)映射成数据库中的记录。
2.mybatis以一个SqlSessionFactory对象的实例为核心,SqlSessionFactory对象可以通过 SqlSessionFactoryBuilder对象来获得,SqlSessionFactoryBuilder可以根据XML配置文件创建出 SqlSessionFactory对象。
3.基本的sql文件:
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<
mapper
namespace
=
"com.lovo.dao.UserDao"
>
<
resultMap
type
=
"User"
id
=
"User"
>
<
id
property
=
"id"
column
=
"id"
/>
<
result
property
=
"content"
column
=
"content"
/>
<
result
property
=
"pubdate"
column
=
"pubdate"
/>
<
association
property
=
"user"
javaType
=
"User"
>
<
id
property
=
"username"
column
=
"username"
/>
<
result
property
=
"email"
column
=
"email"
/>
</
association
>
</
resultMap
>
<
select
id
=
"findByUsername"
parameterType
=
"String"
resultType
=
"User"
>
select * from tb_user where username=#{username}
</
select
>
<
select
id
=
"findAll"
resultType
=
"User"
>
select * from tb_user
</
select
>
<
insert
id
=
"save"
parameterType
=
"User"
keyProperty
=
"username"
>
insert into tb_user (username, password, email) values (#{username}, #{password}, #{email})
</
insert
>
<!-- 这个批量删除数据 -->
<
delete
id
=
"bathDelete"
parameterType
=
"java.util.List"
>
delete from tb_user where id in
<
foreach
collection
=
"list"
index
=
"index"
item
=
"l"
open
=
"("
close
=
")"
separator
=
","
>
#{l}
</
foreach
>
</
delete
>
<!-- 有选择性的修改数据 -->(使用条件判断)
<
update
id
=
"updateSet"
parameterType
=
"com.test.entity.User"
>
update tb_user
<
set
>
<
if
test
=
"name != null"
>
name=#{name},
</
if
>
<
if
test
=
"address !=null "
>
address=#{address},
</
if
>
<
if
test
=
"createTime !=null "
>
create_time=#{createTime}
</
if
>
</
set
>
where id=#{id}
</
update
>
<!-- 分页功能 -->
<select id="getUserArticles" parameterType="Your_params" resultMap="resultUserArticleList">
select user.id,user.userName,user.userAddress,article.id aid,article.title,article.content from user,article
where user.id=article.userid and user.id=#{id} limit #{offset},#{pagesize}
</select>
</
mapper
>
4.需要指出的是 SqlSessionFactoryBuilder类的对象一旦创建出SqlSessionFactory后就没有用了,可以被丢弃;而 SqlSessionFactory对象一旦被创建,应该在整个应用程序执行期间都是存在的,最好是做成被其他程序共享的单例。对于 SqlSession,每 个线程都应该有它自己的SqlSession实例。SqlSession的实例不能被共享,因为它是线程不安全的,它的最佳作用范围是请求或方法范围。绝 对不能将SqlSession实例的引用放在一个类的静态字段甚至是实例字段中,也绝不能SqlSession实例的引用放在任何类型的管理范围中。
5.MyBatis支持声明式数据缓存(declarative data caching)。当一条SQL语句被标记为“可缓存”后,首次执行它时从数据库获取的所有数据会被存储在一段高速缓存中,今后执行这条语句时就会从高速缓存中读取结果,而不是再次命中数据库。MyBatis提供了默认下基于Java HashMap的缓存实现。
MyBatis一级缓存,一级缓存的作用域scope是SqlSession。
MyBatis同时还提供了一种全局作用域global scope的缓存,这也叫做二级缓存,也称作全局缓存。在同个SqlSession中,查询语句相同的sql会被缓存,但是一旦执行新增或更新或删除操作,缓存就会被清除。
6.orm工具基本思想: 从配置文件(通常是XML配置文件中)得到 sessionfactory.由sessionfactory 产生 session,在session 中完成对数据的增删改查和事务提交等.在用完之后关闭session 。在java 对象和 数据库之间有做mapping 的配置文件,也通常是xml 文件。
7.MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。在MyBatis进行查询映射的时候,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值。当提供的返回类型属性是resultType的时候,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。MyBatis的自身判断是把查询的field或其对应的别名与返回对象的属性进行比较,如果相匹配且类型也相匹配,MyBatis则会对其进行赋值。
8.spring与myibats融合
1)采用接口org.apache.ibatis.session.SqlSession的实现类org.mybatis.spring.SqlSessionTemplate。
mybatis中, sessionFactory可由SqlSessionFactoryBuilder.来创建。
MyBatis-Spring 中,使用了SqlSessionFactoryBean来替代。
SqlSessionFactoryBean有一个必须属性dataSource,
另外其还有一个通用属性configLocation(用来指定mybatis的xml配置文件路径)。
<!-- 创建SqlSessionFactory,同时指定数据源-->
<bean
id
=
"sqlSessionFactory"
class
=
"org.mybatis.spring.SqlSessionFactoryBean"
>
<property
name
=
"dataSource"
ref
=
"dataSource"
/>
<!-- 指定sqlMapConfig总配置文件,订制的environment在spring容器中不在生效-->
<property
name
=
"configLocation"
value
=
"classpath:sqlMapConfig.xml"
/>
<!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件,mapperLocations和configLocation有一个即可,当需要为实体类指定别名时,可指定configLocation属性,再在mybatis总配置文件中采用mapper引入实体类映射文件 -->
<!- -
<property
name
=
"mapperLocations"
value
=
"classpath*:com/xxt/ibatis/dbcp/**/*.xml"
/>
--
>
<
bean
>
mybatis总配置文件sqlMapConfig.xml:
<configuration>
<typeAliases>
<typeAlias type="com.xxt.ibatis.dbcp.domain.User" alias="User" />
</typeAliases>
<mappers>
<mapper resource="com/xxt/ibatis/dbcp/domain/user.map.xml" />
</mappers>
</configuration>
实体类映射文件user.map.xml,包含具体的sql语句。
2)采用抽象类org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession。
<bean id=
"sqlSessionFactory"
class
=
"org.mybatis.spring.SqlSessionFactoryBean"
>
<property name=
"dataSource"
ref=
"dataSource"
/>
<property name=
"configLocation"
value=
"classpath:sqlMapConfig.xml"
/>
<!-- <property name=
"mapperLocations"
value=
"classpath*:com/xxt/ibatis/dbcp/domain/user.map.xml"
/ > -->
</bean>
<bean id=
"sqlSession"
class
=
"org.mybatis.spring.SqlSessionTemplate"
>
<constructor-arg index=
"0"
ref=
"sqlSessionFactory"
/>
</bean>
<bean id=
"userDaoImpl3"
class
=
"com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl3"
>
<!--注入SqlSessionTemplate实例 -->
<property name=
"sqlSessionTemplate"
ref=
"sqlSession"
/>
<!--也可直接注入SqlSessionFactory实例,二者都指定时,SqlSessionFactory失效 -->
<!-- <property name=
"sqlSessionFactory"
ref=
"sqlSessionFactory"
/>
-->
</bean>
public
class
UserDaoImpl3
extends
SqlSessionDaoSupport
implements
UserDao {
public
User getUserById(User user) {
return
(User) getSqlSession().selectOne(
"com.xxt.ibatis.dbcp.domain.User.getUser"
, user);
}
}