Java开发

SpringBoot整合Mybatis-Plus 实战之Myba

2020-12-03  本文已影响0人  Java古德

前言

现在的JAVA行业,貌似已经是SpringBoot + SpringCloud 的天下了,早期的SSH,SSM框架已经老去,与SpringBoot相结合的JPA框架虽然省去了很多的增删改查sql,但是比较笨拙,在面对一些复杂多变的逻辑时常常力不从心,而相对应的Mybatis由于其高度的灵活性受到广大JAVA攻城狮的欢迎。之前整合过了springboot+mybatis,前几天看到一个面试的问一个问题,Mybatis的一级缓存,二级缓存。我想这个应该也是一个重点吧,所以今天决定来详细解读一下神秘的一二级缓存。

先知

MyBatis-plus 配置要点

核心要点1

mybatis-plus 在springboot 中的核心配置如下

mybatis-plus.configuration.cache-enabled=true
mybatis-plus.mapper-locations=classpath*:/mapper/*.xml
mybatis-plus.type-aliases-package=com.sch.app.mybatis.entity
logging.level.com.sch.app.mybatis.mapper= debug

所需依赖 除了基本的springboot依赖外,还有

核心要点2
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
            <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

核心要点3
<?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>
    <!--  <properties resource="mybatis.properties" />
         -->
    <classPathEntry location="D:\AJava\mysql-connector-java-8.0.16.jar" />
    <context id="msqlTables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <jdbcConnection connectionURL="jdbc:mysql://localhost:3306/alexshi?serverTimezone=GMT%2B8"
                        driverClass="com.mysql.cj.jdbc.Driver" password="1234" userId="root" >

            <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <javaModelGenerator targetPackage="com.sch.app.mybatis.entity" targetProject="SpringbootMybatis\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <!-- 从数据库返回的值被清理前后的空格  -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="mapper" targetProject="SpringbootMybatis\src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.sch.app.mybatis.mapper" targetProject="SpringbootMybatis\src\main\java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--数据库表-->
        <table  schema="" tableName="d_dictionary"></table>
        <table  schema="" tableName="d_dictionary_type"></table>
        <table  schema="" tableName="c_resource"></table>
        <table  schema="" tableName="c_role"></table>
        <table  schema="" tableName="c_role_resource"></table>
        <table  schema="" tableName="c_user_online"></table>
        <table  schema="" tableName="c_user"></table>
        <table  schema="" tableName="c_user_role"></table>
        <table  schema="" tableName="test"></table>
    </context>
</generatorConfiguration>

在这里插入图片描述

这个 Run Mybatis Generator 可以在eclipse 的插件市场下的

image.png

Mybatis-plus 一级缓存的测试

logging.level.com.sch.app.mybatis.mapper= debug

com.sch.app.mybatis.mapper 也就是 mapper接口的目录


在这里插入图片描述
@Autowired
private SqlSessionFactory sqlSessionFactory;

 @RequestMapping(value = "/testMybatis")
 @ResponseBody
 public void testMybatis(){
     SqlSession sqlSession = sqlSessionFactory.openSession();
     TestMapper testMapper = sqlSession.getMapper(TestMapper.class);
        for (int i = 0; i < 3; i++) {
            Test selectByPrimaryKey = testMapper.selectByPrimaryKey(5);
            log.info("结果:"+ selectByPrimaryKey.getUsername());
 }

在这里插入图片描述 image.png
 @RequestMapping(value = "/testMybatis")
     @ResponseBody
     public void testMybatis(){
         SqlSession sqlSession = sqlSessionFactory.openSession();
         TestMapper testMapper = sqlSession.getMapper(TestMapper.class);
            for (int i = 0; i < 3; i++) {
                Test selectByPrimaryKey = testMapper.selectByPrimaryKey(5);
                log.info("结果:"+ selectByPrimaryKey.getUsername());
                if (i == 2) {
                    selectByPrimaryKey.setUsername("刘惜君的妹妹");
                    testMapper.updateByPrimaryKey(selectByPrimaryKey);
                    Test selectByPrimaryKey2 = testMapper.selectByPrimaryKey(5);
                    log.info("更新后的用户名:"+ selectByPrimaryKey2.getUsername());
                }
     }

可见,第一次我加入了更新的代码后再次查询的时候,就又执行了sql语句,说明当执行插入、更新、删除,会清空SqlSession中的一级缓存。只有查询的操作,一级缓存才不会被清除。

Mybatis-plus二级缓存测试

二级缓存的开启除了在配置文件中打开开关 还要在mapper对应开启


在这里插入图片描述
            @RequestMapping(value = "/testMybatis2")
            @ResponseBody
            public void testMybatis2(){
                SqlSession openSession1 = sqlSessionFactory.openSession();
                SqlSession openSession2 = sqlSessionFactory.openSession();
                TestMapper mapper1 = openSession1.getMapper(TestMapper.class);
                TestMapper mapper2 = openSession2.getMapper(TestMapper.class);
                Test selectByPrimaryKey = mapper1.selectByPrimaryKey(5);
                System.out.println(selectByPrimaryKey.getUsername());
                openSession1.close();
                Test selectByPrimaryKey2 = mapper2.selectByPrimaryKey(5);
                System.out.println(selectByPrimaryKey2.getUsername());
                openSession2.close();
           }

            @RequestMapping(value = "/testMybatis3")
            @ResponseBody
            public void testMybatis3(){
                SqlSession openSession1 = sqlSessionFactory.openSession();
                SqlSession openSession2 = sqlSessionFactory.openSession();
                SqlSession openSession3 = sqlSessionFactory.openSession();
                TestMapper mapper1 = openSession1.getMapper(TestMapper.class);
                TestMapper mapper2 = openSession2.getMapper(TestMapper.class);
                TestMapper mapper3 = openSession3.getMapper(TestMapper.class);
                Test selectByPrimaryKey = mapper1.selectByPrimaryKey(5);
                System.out.println(selectByPrimaryKey.getUsername());
                openSession1.close();
                selectByPrimaryKey.setUsername("刘惜君的姐姐");
                mapper2.updateByPrimaryKey(selectByPrimaryKey);
                openSession2.commit();

                Test selectByPrimaryKey3 = mapper3.selectByPrimaryKey(5);
                System.out.println(selectByPrimaryKey3.getUsername());
                openSession3.close();
           }

初次之外,我们可以通过userCache是来设置具体的语句是否禁用二级缓存


在这里插入图片描述

小结

作者: 爱吃早餐的程序员
原文链接:https://blog.csdn.net/weixin_39076203/article/details/110129148

上一篇下一篇

猜你喜欢

热点阅读