日志门面SLF4J/Apache Commons-Logging===桥接==>日志实现log4j/logbacl/java.util.logging(jul)
一、日志管理
1.1 pom.xml添加依赖
控制台输出
1.2 自定义日志
resource下新建文件,命名强制为logback.xml
level日志级别
二、动态SQL
动态sql是根据参数数据动态组织sql的技术
mapper文件编写查询语句
或者
编写测试文件
@Test public void dynamic() { SqlSession sqlSession = null; try { sqlSession = MybatisUtils.openSession(); Map params = new HashMap(); params.put("categoryId", 44); params.put("currentPrice", 500); List
三、MyBatis二级缓存
3.1 二级缓存运行规则
一级缓存
public void testLv1Cache() { SqlSession sqlSession = null; try { sqlSession = MybatisUtils.openSession(); Goods goods = sqlSession.selectOne("goods.selectId", 1603); Goods good1 = sqlSession.selectOne("goods.selectId", 1603); System.out.println(goods.hashCode()); System.out.println(good1.hashCode()); } finally { MybatisUtils.closeSession(sqlSession); } }
打印出hascode相同(一级缓存生命周期SqlSession会话)
添加commit后缓存消失
public void testLv1Cache() { SqlSession sqlSession = null; try { sqlSession = MybatisUtils.openSession(); Goods goods = sqlSession.selectOne("goods.selectId", 1603); sqlSession.commit(); Goods good1 = sqlSession.selectOne("goods.selectId", 1603); System.out.println(goods.hashCode()); System.out.println(good1.hashCode()); } finally { MybatisUtils.closeSession(sqlSession); } }
在映射文件src/java/resource/mapper/goods.xml中开启二级缓存
public void testLv2Cache() { SqlSession sqlSession = null; try { sqlSession = MybatisUtils.openSession(); Goods goods = sqlSession.selectOne("goods.selectId", 1603); sqlSession.commit(); Goods good1 = sqlSession.selectOne("goods.selectId", 1603); System.out.println(goods.hashCode()); System.out.println(good1.hashCode()); } finally { MybatisUtils.closeSession(sqlSession); } try { sqlSession = MybatisUtils.openSession(); Goods goods = sqlSession.selectOne("goods.selectId", 1603); sqlSession.commit(); Goods good1 = sqlSession.selectOne("goods.selectId", 1603); System.out.println(goods.hashCode()); System.out.println(good1.hashCode()); } finally { MybatisUtils.closeSession(sqlSession); } }
只查询了一次,之后使用缓存
3.2 cache标签属性
eviction是缓存的清除策略,当缓存对象数量达到上限后,自动触发对应算法对缓存对象清楚
flushInterval 缓存间隔,间隔时间自动清除
size 最大缓存对象,实体类、list都是一个缓存对象
readOnly 每次缓存取出的是缓存对象本身,效率较高。设置为false,取出的是缓存对象的“副本”,每次取出对象都不同,安全性较高
useCache 是否使用缓存 (list对象少用缓存)
flushCache 执行完语句后立即情况,多用在写操作
四、MyBatis多表级联查询
4.1 一对多查询
创建src/main/java/entity/GoodsDetail.java实体类
package com.gu.entity; public class GoodsDetail { private Integer gdId; private Integer goodsId; private String gdPicUrl; private Integer gdOrder; public Integer getGdId() { return gdId; } public void setGdId(Integer gdId) { this.gdId = gdId; } public Integer getGoodsId() { return goodsId; } public void setGoodsId(Integer goodsId) { this.goodsId = goodsId; } public String getGdPicUrl() { return gdPicUrl; } public void setGdPicUrl(String gdPicUrl) { this.gdPicUrl = gdPicUrl; } public Integer getGdOrder() { return gdOrder; } public void setGdOrder(Integer gdOrder) { this.gdOrder = gdOrder; } }
由于与Goods是1:n关系,添加details
private List
创建映射文件resources/mappers/goods_detail.xml
在mybatis-config.xml配置文件中添加映射
在goods改动映射文件
测试查询
@Test public void testOneToMany() { SqlSession sqlSession = null; try { sqlSession = MybatisUtils.openSession(); List
4.2 多对一查询
多的一方,添加实体,在goodsDetaile中添加Goods实体
private Goods goods; public Goods getGoods() { return goods; } public void setGoods(Goods goods) { this.goods = goods; }
添加映射的sql方法
测试方法
public void testSelectManyToOne() { SqlSession sqlSession = null; try { sqlSession = MybatisUtils.openSession(); List
五、分页插件(PageHelper)
需要完成的工作:
5.1 根据pageHelper官网文档配置
pom.xml配置依赖
mybatis.config.xml配置插件选项
mappers/goods.xml中配置sql语句
测试代码
public void testSelectPage() { SqlSession sqlSession = null; try { sqlSession = MybatisUtils.openSession(); PageHelper.startPage(2, 10); Page
六、MyBatis整合C3P0连接池
C3P0连接池功能较MyBatis更为强大
引入依赖
新建文件存储数据源 java/com/gu/datasource/C3P0DataSourceFactory.java
package com.gu.datasource; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory; //c3p0与MyBatis兼容使用的数据源工厂 public class C3P0DataSourceFactory extends UnpooledDataSourceFactory { public C3P0DataSourceFactory() { // 由c3p0创建数据源 this.dataSource = new ComboPooledDataSource(); } }
mybatis-config.xml配置p3c0
七、批处理
mapper映射文件: 插入
mapper映射文件:删除
测试代码
sqlSession = MybatisUtils.openSession(); List
批量插入数据的局限
八、MyBatis注解开发
注解可以替换原有的xml标签进行开发
注解 | 对应xml | 说明 |
@Insert | 新增sql | |
@Update | 更新sql | |
@Delete | 删除sql | |
@Select | 查询sql | |
@Param | -- | 参数映射 |
@Results | 结果映射 | |
@Result | 字段映射 |
新建DAO层 com/gu/dao/GoodsDao.java文件
package com.imooc.mybatis.dao; import com.imooc.mybatis.entity.Goods; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.util.List; public interface GoodsDAO { @Select("select * from t_goods where current_price between #{min} and #{max} order by current_price limit 0, #{limit}") public List
mybatis-config.xml文件创建相关映射
测试代码
@Test public void testSelectByPriceRange() throws Exception { SqlSession session = null; try{ session = MyBatisUtils.openSession(); // session可以根据配置动态生成goodsDAO类 GoodsDAO goodsDAO = session.getMapper(GoodsDAO.class); List