建设网站需要的编程,800元五合一建站,网站开发测量像素工具,wordpress模板首页是哪个文件mybatis-plus插件
官网地址
分页插件
MyBatis Plus自带分页插件#xff0c;只要简单的配置即可实现分页功能
配置并使用自带分页插件
Configuration
MapperScan(com.itzhh.mapper)//可以将主类中的注解移到此处
public class MybatisPlusConfig {Beanpublic …mybatis-plus插件
官网地址
分页插件
MyBatis Plus自带分页插件只要简单的配置即可实现分页功能
配置并使用自带分页插件
Configuration
MapperScan(com.itzhh.mapper)//可以将主类中的注解移到此处
public class MybatisPlusConfig {Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor interceptor new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}
}Testpublic void testPage(){//设置分页参数PageUser page new Page(2, 3);//Preparing: SELECT uid AS id,username AS name,//age,email,is_deleted FROM t_user WHERE is_deleted0 LIMIT ?,?userMapper.selectPage(page,null);//获取分页数据ListUser list page.getRecords();list.forEach(System.out::println);System.out.println();System.out.println(当前页page.getCurrent());System.out.println(每页显示的条数page.getSize());System.out.println(总记录数page.getTotal());System.out.println(总页数page.getPages());System.out.println(是否有上一页page.hasPrevious());System.out.println(是否有下一页page.hasNext());}使用自定义分页插件
创建接口注意第一个参数一定要是Page
Repository
public interface UserMapper extends BaseMapperUser {PageUser selectPageVo(Param(page)PageUser page,Param(age)Integer age);
}UserMapper.xml中编写SQL
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.itzhh.mapper.UserMappersql idBaseColumns uid,username,age,email/sqlselect idselectPageVo resultTypecom.itzhh.pojo.Userselect include refidBaseColumns/includefrom t_user where age#{age}/select
/mapper测试 Testpublic void testSelectPageVo(){//设置分页参数PageUser page new Page(1, 2);userMapper.selectPageVo(page, 20);//获取分页数据ListUser list page.getRecords();list.forEach(System.out::println);System.out.println(当前页page.getCurrent());System.out.println(每页显示的条数page.getSize());System.out.println(总记录数page.getTotal());System.out.println(总页数page.getPages());System.out.println(是否有上一页page.hasPrevious());System.out.println(是否有下一页page.hasNext());}Mybatis-Plus实现乐观锁
添加version字段 添加乐观锁插件配置 Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor interceptor new MybatisPlusInterceptor();//添加分页插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//添加乐观锁配置interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return interceptor;}测试 Testpublic void testConcurrentVersionUpdate() {//小李取数据Product p1 productMapper.selectById(00000000000000000001L);//小王取数据Product p2 productMapper.selectById(00000000000000000001L);//小李修改 50p1.setPrice(p1.getPrice() 50);int result1 productMapper.updateById(p1);System.out.println(小李修改的结果 result1);//小王修改 - 30p2.setPrice(p2.getPrice() - 30);int result2 productMapper.updateById(p2);System.out.println(小王修改的结果 result2);if(result2 0){//失败重试重新获取version并更新p2 productMapper.selectById(00000000000000000001L);p2.setPrice(p2.getPrice() - 30);result2 productMapper.updateById(p2);}System.out.println(小王修改重试的结果 result2);//老板看价格Product p3 productMapper.selectById(00000000000000000001L);System.out.println(老板看价格 p3.getPrice());}Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession15dc339f] was not registered for synchronization because synchronization is not active
2023-02-14 09:42:36.662 INFO 21336 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-02-14 09:42:36.842 INFO 21336 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection695085082 wrapping com.mysql.cj.jdbc.ConnectionImpl59cda16e] will not be managed by SpringPreparing: SELECT id,name,price,version FROM t_product WHERE id?Parameters: 1(Long)Columns: id, name, price, versionRow: 1, 新华字典, 100, 1Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession15dc339f]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession10b1a751] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection1406114969 wrapping com.mysql.cj.jdbc.ConnectionImpl59cda16e] will not be managed by SpringPreparing: SELECT id,name,price,version FROM t_product WHERE id?Parameters: 1(Long)Columns: id, name, price, versionRow: 1, 新华字典, 100, 1Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession10b1a751]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession5a1c3cb4] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection709841971 wrapping com.mysql.cj.jdbc.ConnectionImpl59cda16e] will not be managed by SpringPreparing: UPDATE t_product SET name?, price?, version? WHERE id? AND version?Parameters: 新华字典(String), 150(Integer), 2(Integer), 1(Long), 1(Integer)Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession5a1c3cb4]
小李修改的结果1
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession7f5538a1] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection1929218620 wrapping com.mysql.cj.jdbc.ConnectionImpl59cda16e] will not be managed by SpringPreparing: UPDATE t_product SET name?, price?, version? WHERE id? AND version?Parameters: 新华字典(String), 70(Integer), 2(Integer), 1(Long), 1(Integer)Updates: 0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession7f5538a1]
小王修改的结果0
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession34780cd9] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection1648278215 wrapping com.mysql.cj.jdbc.ConnectionImpl59cda16e] will not be managed by SpringPreparing: SELECT id,name,price,version FROM t_product WHERE id?Parameters: 1(Long)Columns: id, name, price, versionRow: 1, 新华字典, 150, 2Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession34780cd9]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession1ab5f08a] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection1860118977 wrapping com.mysql.cj.jdbc.ConnectionImpl59cda16e] will not be managed by SpringPreparing: UPDATE t_product SET name?, price?, version? WHERE id? AND version?Parameters: 新华字典(String), 120(Integer), 3(Integer), 1(Long), 2(Integer)Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession1ab5f08a]
小王修改重试的结果1
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession66b59b7d] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection1395725953 wrapping com.mysql.cj.jdbc.ConnectionImpl59cda16e] will not be managed by SpringPreparing: SELECT id,name,price,version FROM t_product WHERE id?Parameters: 1(Long)Columns: id, name, price, versionRow: 1, 新华字典, 120, 3Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession66b59b7d]
老板看价格120通用枚举
表中的有些字段值是固定的例如性别男或女此时我们可以使用MyBatis-Plus的通用枚举来实现创建枚举类如果不加 EnumValue会报如下错误
package com.itzhh.enums;import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;/*** Author: zhh* Date: 2023-02-14 10:11* Description: 描述*/
Getter
public enum SexEnum {MALE(1,男),FEMALE(0,女);EnumValueprivate Integer sex;private String sexName;SexEnum(Integer sex, String sexName) {this.sex sex;this.sexName sexName;}
} 添加枚举字段 配置扫描通用枚举
# 配置mybatis日志
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:# 配置MyBatis-Plus操作表的默认前缀table-prefix: t_#配置mybatis-plus的主键策略id-type: auto# 配置扫描通用枚举type-enums-package: com.itzhh.enums测试
Testpublic void testSexEnum(){User user new User();user.setName(Enum);user.setAge(20);//设置性别信息为枚举项会将EnumValue注解所标识的属性值存储到数据库user.setSex(SexEnum.MALE);userMapper.insert(user);}代码生成器
引入依赖
dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-generator/artifactIdversion3.5.1/version/dependencydependencygroupIdorg.freemarker/groupIdartifactIdfreemarker/artifactIdversion2.3.31/version/dependencyTestvoid contextLoads() {FastAutoGenerator.create(jdbc:mysql://localhost:3306/mybatis_plus?serverTimezoneGMT%2B8characterEncodingutf-8useSSLfalse, root, 123456).globalConfig(builder - {builder.author(zhh) // 设置作者//.enableSwagger() // 开启 swagger 模式.fileOverride() // 覆盖已生成文件.outputDir(E:\\IDEA\\IDEAProjects\\mybatis_plus_auto); // 指定输出目录}).packageConfig(builder - {builder.parent(com.itzhh) // 设置父包名.moduleName(mybatisplus) // 设置父包模块名.pathInfo(Collections.singletonMap(OutputFile.mapperXml, E:\\IDEA\\IDEAProjects\\mybatis_plus_auto));// 设置mapperXml生成路径}).strategyConfig(builder - {builder.addInclude(t_user) // 设置需要生成的表名.addTablePrefix(t_, c_); // 设置过滤表前缀}).templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker 引擎模板默认的是Velocity引擎模板.execute();}多数据源
模拟场景 我们创建两个库分别为mybatis_plus以前的库不动与mybatis_plus_1新建将mybatis_plus库的product表移动到mybatis_plus_1库这样每个库一张表通过一个测试用例分别获取用户数据与商品数据如果获取到说明多库模拟成功添加依赖
dependencygroupIdcom.baomidou/groupIdartifactIddynamic-datasource-spring-boot-starter/artifactIdversion3.5.0/version/dependency配置多数据源
spring:datasource:dynamic:strict: falsedatasource:master:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezoneGMT%2B8characterEncodingutf-8useSSLfalseusername: rootpassword: 123456slave_1:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis_plus_1?serverTimezoneGMT%2B8characterEncodingutf-8useSSLfalseusername: rootpassword: 123456
# 配置mybatis日志
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
创建用户service
public interface UserService extends IServiceUser {
}DS(master) //指定所操作的数据源
Service
public class UserServiceImpl extends ServiceImplUserMapper, User implements UserService {
}
创建商品service
public interface ProductService extends IServiceProduct {
}DS(slave_1) //指定所操作的数据源
Service
public class ProductServiceImpl extends ServiceImplProductMapper, Product implements ProductService {
}
测试 Autowiredprivate UserService userService;Autowiredprivate ProductService productService;Testvoid contextLoads() {System.out.println(userService.getById(1L));System.out.println(productService.getById(1L));}安装MybatisX插件并使用其简化开发
这个我一直再用就不记录了