栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis的缓存 mybatis-generator 分页插件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis的缓存 mybatis-generator 分页插件

1.分页插件PageHelper. (1)pageHelper的作用

帮你完成分页的功能

(2)如何使用: 1.引入pageHelper的jar包
     
        
            com.github.pagehelper
            pagehelper
            5.1.11
        
        2.在mybatis中设置pageHelper的拦截器

在environments标签下设置

 
        
    
        3使用pageHelper --在测试类中
      //设置分页属性pageNum:显示第几页  PageSize:每页显示的条数
        PageHelper.startPage(2,5);
        List list = userDao.findAll();
        //把查询的结果封装到PageInfo类中。
        PageInfo pageInfo=new PageInfo(list);
        System.out.println("总条数:"+pageInfo.getTotal());
        System.out.println("总页数:"+pageInfo.getPages());
        System.out.println("当前页码对应的数据:"+pageInfo.getList());

2.mybatis的代码生成器--generator         (1)mybatis-generator的作用

根据表帮你生成实体类,和dao和xml映射文件。就是简单的CRUD

         (2)如何使用         1.引入mybatis-generator的jar包
       
            org.mybatis.generator
            mybatis-generator-core
            1.4.0
        
        2generator的配置文件




  

  

    
    

    
      
    
 
    
      
      
    

    
      
    

    
      
    

    
        3.运行自己配置的文件
   List warnings = new ArrayList();
   boolean overwrite = true;
   File configFile = new File("generatorConfig.xml");
   ConfigurationParser cp = new ConfigurationParser(warnings);
   Configuration config = cp.parseConfiguration(configFile);
   DefaultShellCallback callback = new DefaultShellCallback(overwrite);
   MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
   myBatisGenerator.generate(null);

运行后有很多英文的注解,如果不想要写--在context下面

 
            
        
3.mybatis缓存         1.什么是缓存---缓存是存在于==内存中==的临时数据。         2.一级缓存

        一级缓存----基于SqlSession级别的缓存。默认一级缓存是开启的,不能关闭。

        

        //一级缓存
        Reader reader = Resources.getResourceAsReader("conf.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader);
        SqlSession session= factory.openSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        //一级缓存:如果第一次执行的时候,在缓存中可以找到,就不会去数据库中查找,如果在缓存中找不到,会去数据库中查找
        Student student = mapper.selectByPrimaryKey(59);
        System.out.println(student);
        Student student1 = mapper.selectByPrimaryKey(59);

 

 @Test
    public void Test02()throws Exception{
        //一级缓存 在不同的SQLsession中 查询的内容一样
        Reader reader = Resources.getResourceAsReader("conf.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader);
        SqlSession session= factory.openSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        Student student = mapper.selectByPrimaryKey(59);
        System.out.println(student);
        //开启新的SQlSession 
        SqlSession session1= factory.openSession();
        StudentMapper mapper1 = session1.getMapper(StudentMapper.class);
        Student student1 = mapper1.selectByPrimaryKey(59);
        System.out.println(student1);
    }

         3.二级缓存

二级缓存--基于SqlSessionFactory级别的缓存,它可以做到多个SqlSession共享数据。默认它是关闭。需要手动开启

                1.开启二级缓存---在configuration下

    
                2.使用二级缓存--在映射文件下
  
                3.实现对应实体的序列化接口
public class Student implements Serializable
                4.测试二级缓存
 public void Test03()throws Exception{
        //二级缓存
        Reader reader = Resources.getResourceAsReader("conf.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader);
        SqlSession session= factory.openSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        Student student = mapper.selectByPrimaryKey(59);
        System.out.println(student);
        session.close();
        //开启新的SQlSession
        SqlSession session1= factory.openSession();
        StudentMapper mapper1 = session1.getMapper(StudentMapper.class);
        Student student1 = mapper1.selectByPrimaryKey(59);
        System.out.println(student1);
    }

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955774.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号