分页插件PageHelper和mybatis的代码生成器--generator

本文介绍了MyBatis的分页插件PageHelper的使用步骤,包括引入依赖、配置拦截器及示例代码。接着讲解了mybatis-generator的配置和运行,用于自动生成代码。最后,详细阐述了MyBatis的一级缓存和二级缓存机制,并提供了相关示例代码,展示查询顺序。通过这些工具和机制,可以提升数据库操作的效率。
摘要由CSDN通过智能技术生成

目录

  • 如何使用:分页插件PageHelper
  • mybatis的代码生成器--generator
  • mybatis的缓存


一、如何使用:分页插件PageHelper

1)引入pageHelper的jar包

<!--引入pageHelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.11</version>
        </dependency>

2)mybatis中设置pageHelper的拦截器

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

3)使用pageHelper

@Test
    public void testFindAll() throws Exception{
        Reader reader = Resources.getResourceAsReader("mybatis.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = factory.openSession();
        StudentDao studentDao = session.getMapper(StudentDao.class);

        PageHelper.startPage(2,5);
        List<Student> list = studentDao.findAll();
        //把查询的结果封装到PageInfo类中。
        PageInfo<Student> pageInfo=new PageInfo<Student>(list);
        System.out.println("总条数:"+pageInfo.getTotal());
        System.out.println("总页数:"+pageInfo.getPages());
        System.out.println("当前页码对应的数据:"+pageInfo.getList());

        session.commit();
        session.close();
    }

二、mybatis的代码生成器--generator

1)引入mybatis-generator的依赖jar包。

代码如下(示例):

<dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.4.0</version>
        </dependency>

2)generator的配置文件

代码如下(示例):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <classPathEntry location="D:\MavenRepository\mysql\mysql-connector-java\8.0.20\mysql-connector-java-8.0.20.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai"
                        userId="root"
                        password="317768414z">
        </jdbcConnection>

        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.zsb.entity" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mapper"  targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.zsb.dao"  targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table schema="mybatis" tableName="tbl_user" domainObjectName="Users"
               enableUpdateByExample="false" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false">

        </table>

    </context>
</generatorConfiguration>

3)运行配置文件

代码如下(示例):

@Test
    public void run() throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("generator.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);
    }

--》去除英文注释

        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

三、mybatis的缓存

缓存是存在于内存中的临时数据

缓存的好处:使用缓存减少和数据库的交互次数,提高执行效率。

适合放入缓存的数据:

        1.经常查询并且不经常改变的;

        2.数据的正确与否对最终结果影响不大的;

mybatis支持两种缓存:

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

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

一级缓存:

@Test
    public void test1() throws Exception{
        Reader reader = Resources.getResourceAsReader("mybatis.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = factory.openSession();
        UsersMapper usersMapper = session.getMapper(UsersMapper.class);
        //int row = usersMapper.deleteByPrimaryKey(1);
        Users users =usersMapper.selectByPrimaryKey(2);
        System.out.println(users);
        Users users1 =usersMapper.selectByPrimaryKey(2);
        System.out.println(users1);


        session.commit();
        session.close();
    }
@Test
    public void test2() throws Exception{
        Reader reader = Resources.getResourceAsReader("mybatis.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = factory.openSession();
        UsersMapper usersMapper = session.getMapper(UsersMapper.class);

        Users users =usersMapper.selectByPrimaryKey(2);
        System.out.println(users);
        session.close();

        SqlSession session1 = factory.openSession();
        UsersMapper usersMapper1 = session1.getMapper(UsersMapper.class);
        Users users1 =usersMapper1.selectByPrimaryKey(2);
        System.out.println(users1);


        session.commit();
        session.close();
    }

二级缓存:

1)开启二级缓存

    <settings>
        <setting name="cacheEnabled" value="true"/>
    </settings>

2)在映射文件中使用二级缓存

 3)实体一定要实现序列化接口

4) 测试二级缓存

@Test
    public void test04() throws Exception{
        Reader resourceAsReader = Resources.getResourceAsReader("mybatis.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(resourceAsReader);
        SqlSession session=factory.openSession();
        UsersMapper mapper = session.getMapper(UsersMapper.class);
        Users users = mapper.selectByPrimaryKey(2);
        System.out.println(users);
        session.close();
        SqlSession session1=factory.openSession();
        UsersMapper mapper1 = session1.getMapper(UsersMapper.class);
        Users users1 = mapper1.selectByPrimaryKey(2);
        System.out.println(users1);
    }

查询顺序: 二级缓存----一级缓存----数据库。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值