赞
踩
目录
二、Mybatis-Plues 使用 redis 作为二级缓存
项目结构:
所需要的所有依赖坐标:
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>org.example</groupId>
- <artifactId>SpringBoot_MybatisPlues</artifactId>
- <version>1.0-SNAPSHOT</version>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.3.4.RELEASE</version>
- <relativePath/>
- </parent>
-
- <properties>
- <java.version>1.8</java.version>
- </properties>
-
-
- <dependencies>
- <!--SpringBoot的支持web-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>2.3.4.RELEASE</version>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
-
- <!--mybatis-plus-->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.4.0</version>
- </dependency>
-
- <!--mysql-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
-
- <!--lombok-->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.12</version>
- <scope>provided</scope>
- </dependency>
-
- <!--junit-->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
-
- <!--hutool工具包-->
- <dependency>
- <groupId>cn.hutool</groupId>
- <artifactId>hutool-all</artifactId>
- <version>5.3.7</version>
- </dependency>
-
- <!--redis-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <!-- <plugin>-->
- <!-- <groupId>org.springframework.boot</groupId>-->
- <!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
- <!-- </plugin>-->
- <!--逆向工程,不使用可剔除-->
- <plugin>
- <groupId>org.mybatis.generator</groupId>
- <artifactId>mybatis-generator-maven-plugin</artifactId>
- <version>1.3.2</version>
- <configuration>
- <!--指定配置文件的名称。默认值:${basedir}/src/main/resources/generatorConfig.xml-->
- <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
- <!--如果指定了该参数,如果生成的java文件存在已经同名的文件,新生成的文件会覆盖原有的文件。
- 如果没有指定该参数,如果存在同名的文件,MBG会给新生成的代码文件生成一个唯一的名字(例如: MyClass.java.1, MyClass.java.2 等等)。
- 重要: 生成器一定会自动合并或覆盖已经生成的XML文件。默认值:false-->
- <overwrite>true</overwrite>
- <!--如果指定该参数,执行过程会输出到控制台-->
- <verbose>true</verbose>
- </configuration>
- </plugin>
- </plugins>
- </build>
-
- </project>
1、导入如上 mybatis-plus 依赖
2、在 application.yml 配置文件中加入 Mybatis-plues 的配置信息
- mybatis-plus:
- # 加入此属性,可以再映射文件的 resultType 中直接写类名
- type-aliases-package: com.wanshi.spring.pojo
- # 遇到复杂sql语句时,可使用自定义语句,映射文件位置信息
- mapper-locations: classpath:/mappers/**.xml
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # mybatis-plus日志
- cache-enabled: true #开启二级缓存
3、编写Mybatis-Plues的配置类 MybatisPlusConfig
- package com.wanshi.spring.config;
-
- import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
- import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
-
- @Configuration
- @EnableTransactionManagement(proxyTargetClass = true)
- public class MybatisPlusConfig {
-
- //配置乐观锁插件
- @Bean
- public OptimisticLockerInterceptor optimisticLockerInterceptor() {
- return new OptimisticLockerInterceptor();
- }
-
- /**
- * 分页插件
- */
- @Bean
- public PaginationInterceptor paginationInterceptor() {
- PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
- // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
- // paginationInterceptor.setOverflow(false);
- // 设置最大单页限制数量,默认 500 条,-1 不受限制
- // paginationInterceptor.setLimit(500);
- // 开启 count 的 join 优化,只针对部分 left join
- //paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
- return paginationInterceptor;
- }
-
- }
4、建立一个实体类:User
- package com.wanshi.spring.pojo;
-
- import com.baomidou.mybatisplus.annotation.TableId;
- import com.baomidou.mybatisplus.annotation.TableName;
- import lombok.Data;
-
- import java.io.Serializable;
-
- @Data
- @TableName("t_user") //此类对应的表名
- public class User implements Serializable { //序列化
-
- @TableId //设置主键
- private int noid;
- // @TableField("name") //表名与属性名不一致时,使用此注释来修改
- private String username;
- private double balance;
-
- }
5、编写数据处理层:UserMapper(其实什么都不做,就继承个其他类)
- package com.wanshi.spring.mappers;
-
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.wanshi.spring.config.cache.MybatisPlusRedisCache;
- import com.wanshi.spring.pojo.User;
- import org.apache.ibatis.annotations.CacheNamespace;
- import org.apache.ibatis.annotations.Mapper;
-
- @Mapper
- @CacheNamespace(implementation= MybatisPlusRedisCache.class,eviction=MybatisPlusRedisCache.class)
- public interface UserMapper extends BaseMapper<User> {}
6、编写数据访问层:UserService
- package com.wanshi.spring.service;
-
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.baomidou.mybatisplus.extension.plugins.p
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。