- PageHelper 插件的使用
- 导入jar包
- 在mybatis-config.xml中配置插件
<!--配置分页插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
- 在查询的结果集前 传入我们要分页的参数
PageHelper.offsetPage(起始值,分页单位);
- 在查询的结果集合后创建PageInfo对象并传入查询出的集合
PageInfo pageInfo = new PageInfo(plist);
5、使用pageInfo对象直接getTotal() 获取总条数
- ParameterType常见传入参数 (详解教案)
- 源码分析
- 为什么我们的mybatis-config.xml文件在src下
- Mybatis中关于数据库的连接池方式有三种
①UNPOOLED 不用连接池
第一次进行加载驱动的初始化
获取连接
事物级别的设置、自动提交的配置
返回coon对象
②POOLED 使用连接池
获取coon对象时的步骤:
第一步:判断空闲的集合是否为空,如果不为空就返回最早创建的coon对象
第二步:判断活跃集合中的conn对象数量是否最大,如果小于最大值就创建coon
第三步:获取最早的那个活跃的coon对象的时间看是否超时,若超时就移除, 移除后创建一个coon对象
第四步:线程等待
使用完后coon对象的走向:
每次我们使用的coon对象不是真正的coon,而是代理的一个coon,当我们调 用close方法时,其实不会关闭资源,而是将使用完后的coon放到了连接池 中
③JUDI
- 事物的自动提交
//这是事物的提交方式为自动提交
SqlSession session = sessionFactory.openSession(true);
//默认是false 不自动提交需要手动commit,将所有的更新动作当做是一个事务处理,
//如果其中某个操作有异常那么所有的事物将都会回滚
//true 自动提交 每个操作视为是一个事物,只要有事物可以顺利提交那么不影响其他的事物
- 事物的隔离级别设置
//设置事物的隔离级别
SqlSession session = sessionFactory.openSession(TransactionIsolationLevel.READ_COMMITTED);
- 懒加载
1、配置懒加载
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
2、mapper.xml
<resultMap type="Category" id="categoryBean2" autoMapping="true">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="products" ofType="Product" column="id" select="query" autoMapping="true" />
</resultMap>
<select id="lazyListCategory" resultMap="categoryBean2" parameterType="int">
select * from t_category where id = #{id}
</select>
<select id="query" resultType="Product" parameterType="int">
select * from t_product where cid = #{id}
</select>
- 注解关联查询
一对多
public interface CategoryMapper {
@Select(" select * from t_category where id = #{id}")
@Results({
@Result(id = true,column = "id",property = "id"),
@Result(column = "name",property = "name"),
@Result(property = "products",javaType = List.class,column = "id",
many = @Many(select = "com.tledu.dao.ProductMapper.getListById")
)
})
public Category getCategoryById(int id);
}
public interface ProductMapper {
@Select(" select * from t_product where cid = #{id} ")
public List<Product> getListById(int id);
}
多对一
public interface ProductMapper {
@Select(" select * from t_product where id = #{id} ")
@Results({
@Result(property = "category",javaType = Category.class,column = "cid",
one = @One(select = "com.tledu.dao.CategoryMapper.getCategoryById")
)
})
public Product getProductById(int id);
}
public interface CategoryMapper {
@Select(" select * from t_category where id = #{id}")
@Results({
@Result(id = true,column = "id",property = "id"),
@Result(column = "name",property = "name"),
@Result(property = "products",javaType = List.class,column = "id",
many = @Many(select = "com.tledu.dao.ProductMapper.getListById")
)
})
public Category getCategoryById(int id);
}
- 缓存策略
一级缓存
//一级缓存在sqlSession中
SqlSession sqlSession = sessionFactory.openSession();
只要我们不增 删 改 不close一级缓存一直存在
二级缓存
- 在配置文件中配置开启缓存
<!--开启缓存-->
<setting name="cacheEnabled" value="true"/>
- 实体类实现序列化接口
public class Category implements Serializable {
- 在要缓存的mapper.xml中添加<cache/>标签,表示当前mapper使用缓存
<mapper namespace="com.tledu.pojo">
<cache/>
<resultMap id="categoryBean" type="Category"> ......