mybatis 分页插件 源码分析 懒加载 注解开发 缓存

  • PageHelper 插件的使用

  1. 导入jar
  2. 在mybatis-config.xml中配置插件
<!--配置分页插件-->

<plugins>

   <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>

</plugins>
  1. 在查询的结果集前 传入我们要分页的参数
PageHelper.offsetPage(起始值,分页单位);
  1. 在查询的结果集合后创建PageInfo对象并传入查询出的集合
PageInfo pageInfo = new PageInfo(plist);

5、使用pageInfo对象直接getTotal() 获取总条数

  • ParameterType常见传入参数 (详解教案)

  • 源码分析
  1. 为什么我们的mybatis-config.xml文件在src
  2. 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一级缓存一直存在

 

  二级缓存

  1. 在配置文件中配置开启缓存
 <!--开启缓存-->

<setting name="cacheEnabled" value="true"/>
  1. 实体类实现序列化接口
public class Category implements Serializable {
  1. 在要缓存的mapper.xml中添加<cache/>标签,表示当前mapper使用缓存
<mapper namespace="com.tledu.pojo">



    <cache/>



    <resultMap id="categoryBean" type="Category"> ......

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值