1.mybatis框架执行过程:
1、配置mybatis的配置文件,SqlMapConfig.xml(名称不固定)
2、通过配置文件,加载mybatis运行环境,创建SqlSessionFactory会话工厂,SqlSessionFactory在实际使用时按单例方式。
3、通过SqlSessionFactory创建SqlSession。SqlSession是一个面向用户接口(提供操作数据库方法),实现对象是线程不安全的,建议sqlSession应用场合在方法体内。
4、调用sqlSession的方法去操作数据。如果需要提交事务,需要执行SqlSession的commit()方法。
5、释放资源,关闭SqlSession
2.一般如果要扩展pojo属性可以用resultType和resultMap
需求:查询订单信息,关联查询创建订单的用户信息
1)使用resultType
a.SQL语句。确定查询的主表:订单表
确定查询的关联表:用户表,由于orders表中有一个外键(user_id),通过外键关联查询用户表只能查询出一条记录,可以使用内链接。
SELECT
orders.*,
USER.username,
USER.sex,
USER.address
FROM
orders,
USER
WHERE orders.user_id = user.id
b.创建pojo类
将上边sql查询的结果映射到pojo中,pojo中必须包括所有查询列名。原始的Orders.java不能映射全部字段,需要新创建的pojo。创建 一个pojo继承包括查询字段较多的po类。
2)使用resultMap
a.在Orders类中添加user
b.mapper.xml
定义resultMap:
<resultMap type="cn.itcast.mybatis.po.Orders"id="OrdersUserResultMap">
<id column="id" property="id"/>
<result column="user_id"property="userId"/>
<result column="number"property="number"/>
<result column="createtime"property="createtime"/>
<result column="note"property=note/>
<association property="user" javaType="cn.itcast.mybatis.po.User">
<id column="user_id" property="id"/>
<result column="username"property="username"/>
<result column="sex"property="sex"/>
<result column="address"property="address"/>
association>
resultMap>
statement定义:
3.resultType和resultMap实现一对一查询小结
resultType:使用resultType实现较为简单,如果pojo中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。如果没有查询结果的特殊要求建议使用resultType。
resultMap:需要单独定义resultMap,实现有点麻烦,如果对查询结果有特殊的要求,使用resultMap可以完成将关联查询映射pojo的属性中。
resultMap可以实现延迟加载,resultType无法实现延迟加载。
4.一对多
查询订单及订单明细的信息。
确定主查询表:订单表 确定关联查询表:订单明细表
在一对一查询基础上添加订单明细表关联即可。
SELECT
orders.*,
USER.username,
USER.sex,
USER.address,
orderdetail.id orderdetail_id,
orderdetail.items_id,
orderdetail.items_num,
orderdetail.orders_id
FROM
orders,
USER,
orderdetail
WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id
分析
使用resultType将上边的查询结果映射到pojo中,订单信息的就是重复。
c.resultMap定义
<resultMap type="cn.itcast.mybatis.po.Orders"id="OrdersAndOrderDetailResultMap" extends="OrdersUserResultMap">
<collection property="orderdetails"ofType="cn.itcast.mybatis.po.Orderdetail">
<id column="orderdetail_id"property="id"/>
<result column="items_id"property="itemsId"/>
<result column="items_num"property="itemsNum"/>
<result column="orders_id"property="ordersId"/>
collection>
resultMap>
d.mapper.java
5.多对多查询
需求:查询用户及用户购买商品信息。
查询主表是:用户表。关联表:由于用户和商品没有直接关联,通过订单和订单明细进行关联,所以关联表:orders、orderdetail、items
SELECT
orders.*,
USER.username,
USER.sex,
USER.address,
orderdetail.id orderdetail_id,
orderdetail.items_id,
orderdetail.items_num,
orderdetail.orders_id,
items.name items_name,
items.detail items_detail,
items.price items_price
FROM
orders,
USER,
orderdetail,
items
WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id ANDorderdetail.items_id = items.id
a.mapper.xml
b.resultMap定义:
映射思路:将用户信息映射到user中。
在user类中添加订单列表属性List
在Orders中添加订单明细列表属性List
在OrderDetail中添加Items属性,将订单明细所对应的商品映射到Items
<resultMap type="cn.itcast.mybatis.po.User"id="UserAndItemsResultMap">
<id column="user_id"property="id"/>
<result column="username"property="username"/>
<result column="sex"property="sex"/>
<result column="address"property="address"/>
<collection property="ordersList"ofType="cn.itcast.mybatis.po.Orders">
<id column="id"property="id"/>
<result column="user_id"property="userId"/>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
<collection property="orderdetails"ofType="cn.itcast.mybatis.po.Orderdetail">
<id column="orderdetail_id" property="id"/>
<result column="items_id"property="itemsId"/>
<result column="items_num"property="itemsNum"/>
<result column="orders_id"property="ordersId"/>
<association property="items"javaType="cn.itcast.mybatis.po.Items">
<id column="items_id"property="id"/>
<result column="items_name"property="name"/>
<result column="items_detail"property="detail"/>
<result column="items_price"property="price"/>
association>
collection>
collection>
resultMap>
c.mapper.java
//查询用户购买商品信息
public List
6.resultType与resultMap多对多小结
resultType:
作用:将查询结果按照sql列名pojo属性名一致性映射到pojo中。
场合:常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面时,此时可直接使用resultType将每一条记录映射到pojo中,在前端页面遍历list(list中是pojo)即可。
resultMap:
使用association和collection完成一对一和一对多高级映射(对结果有特殊的映射要求)。
association:
作用:将关联查询信息映射到一个pojo对象中。
场合:为了方便查询关联信息可以使用association将关联订单信息映射为用户对象的pojo属性中,比如:查询订单及关联用户信息。使用resultType无法将查询结果映射到pojo对象的pojo属性中,根据对结果集查询遍历的需要选择使用resultType还是resultMap。
collection:
作用:将关联查询信息映射到一个list集合中。
场合:为了方便查询遍历关联信息可以使用collection将关联信息映射到list集合中,比如:查询用户权限范围模块及模块下的菜单,可使用collection将模块映射到模块list中,将菜单列表映射到模块对象的菜单list属性中,这样的作的目的也是方便对查询结果集进行遍历查询。如果使用resultType无法将查询结果映射到list集合中。
7.延迟加载
resultMap可以实现高级映射(使用association、collection实现一对一及一对多映射),association、collection具备延迟加载功能。使用association实现延迟加载
a.mapper.xml
需要定义两个mapper的方法对应的statement。
1、只查询订单信息
SELECT * FROM orders
在查询订单的statement中使用association去延迟加载(执行)下边的satatement(关联查询用户信息)
3、延迟加载resultMap
使用association中的select指定延迟加载去执行的statement的id。
<resultMap type="cn.itcast.mybatis.po.Orders"id="OrdersUserLazyLoadingResultMap">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
<association property="user" javaType="cn.itcast.mybatis.po.User"
select="cn.itcast.mybatis.mapper.UserMapper.findUserById"column="user_id">
association>
resultMap>
mapper.java
//查询订单关联查询用户,用户信息是延迟加载
public List
b.测试
1、执行上边mapper方法(findOrdersUserLazyLoading),内部去调用cn.itcast.mybatis.mapper.OrdersMapperCustom中的findOrdersUserLazyLoading只查询orders信息(单表)。
2、在程序中去遍历上一步骤查询出的List
3、延迟加载,去调用UserMapper.xml中findUserbyId这个方法获取用户信息。
延迟加载配置在SqlMapConfig.xml中配置
8.缓存
1)mybatis中一级缓存不用配置,一级缓存基于 PerpetualCache 的 HashMap 本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该Session中的所有 Cache 就将清空。
2)二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,HashMap存储,不同在于其存储作用域为 Mapper(Namespace),并且可自定义存储源,如 Ehcache、Hazelcast等。
a.在核心配置文件SqlMapConfig.xml中加入
<setting name="cacheEnabled" value="true"/>
描述 |
允许值 |
|
cacheEnabled |
对在此配置文件下的所有cache进行全局性开/关设置 |
true false |
b.在单独的xml文件中配置
但是,为了提高系统的并发性能,一般对系统进行分布式部署(集群部署方式),但是mybatis自身不能对缓存数据进行集中管理,所以需要使用分布式缓存框架,如redis、memcached、ehcache。
3)mybatis整合ehcache框架
a.加入ehcache的包
9.缓存的算法
FIFO(First in First out)先进先出。核心原则:如果一个数据最先进入缓存中,则应该最早淘汰掉。
LFU(Least Frequently Used)最近最少使用。如果一个数据在最近一段时间内使用次数很少,那么在将来一段时间内被使用的可能性也很小。
LRU(Least Recently Used)最近最久未使用。如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也很小。
LFU是指使用频率上,LRU使用时间
10.spring整合mybatis
1)整合原生dao方法
a.在自己的xml方法写SQL语句
d.do接口的实现类
dao接口实现类需要注入SqlSessoinFactory,通过spring进行注入。
2)spring与mapper代理整合
a.先写mapper.java(一系列方法,与dao接口中方法类似)与mapper.xml(SQL语句)。如下为mapper.java代码
publicinterface UserMapper {
//用户信息综合查询
public List<UserCustom> findUserList(UserQueryVouserQueryVo) throws Exception;
//用户信息综合查询总数
publicint findUserCount(UserQueryVouserQueryVo) throws Exception;
//根据id查询用户信息
public User findUserById(int id) throws Exception;
//根据id查询用户信息,使用resultMap输出
public User findUserByIdResultMap(int id) throws Exception;
//根据用户名列查询用户列表
public List<User> findUserByName(String name)throws Exception;
//插入用户
publicvoid insertUser(User user)throws Exception;
//删除用户
publicvoid deleteUser(int id)throws Exception;
//更新用户
publicvoid updateUser(User user)throws Exception;
}
b.通过MapperFactoryBean创建代理对象