springboot+mybatis-plus集成
2022/5/1 6:14:57
本文主要是介绍springboot+mybatis-plus集成,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在ORM框架中,也经常使用mybatis-plus,下面记录下springboot+mybaits-plus集成:
官网: https://baomidou.com/
1. 创建工程并在pom中添加引用
<dependencies> <!--注入web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.3.5.RELEASE</version> </dependency> <!--注入lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.14</version> </dependency> <!--knife4j swagger--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> <exclusions> <exclusion> <artifactId>swagger-annotations</artifactId> <groupId>io.swagger</groupId> </exclusion> </exclusions> </dependency> <dependency> <artifactId>swagger-annotations</artifactId> <groupId>io.swagger</groupId> <version>1.5.22</version> </dependency> <!-- 加载mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <!-- 加载jdbc连接数据库 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>2.3.6.RELEASE</version> </dependency> <!-- mybatis plus 代码生成器 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <!--测试包--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter-test</artifactId> <version>3.5.1</version> </dependency> </dependencies>
2.yaml配置文件中添加数据库配置
server: port: 8080 servlet: path: / # DataSource config 数据库配置 spring: datasource: name: mysql url: jdbc:mysql://127.0.0.1:3306/devdatabase?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 username: devuser password: admin driver-class-name: com.mysql.cj.jdbc.Driver
3.在mysql数据库中创建数据库表
CREATE TABLE `buyr_user` ( `USER_ID` bigint(19) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户ID', `USER_NAME` varchar(90) NOT NULL COMMENT '用户名称', `NICK_NAME` varchar(90) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '用户昵称', `USER_INTRO` varchar(300) DEFAULT NULL COMMENT '个性签名', `AVATAR` varchar(1000) DEFAULT NULL COMMENT '头像图片', `EMAIL` varchar(255) DEFAULT NULL COMMENT '邮件地址', `PHONE` varchar(255) DEFAULT NULL COMMENT '手机号', `USER_PASS` varchar(255) DEFAULT NULL COMMENT '密码', `PASS_SALT` varchar(255) DEFAULT NULL COMMENT '密码盐', `USER_STATUS` varchar(32) DEFAULT '1' COMMENT '用户状态', `USER_SCORE` int(11) DEFAULT NULL COMMENT '用户打分', `TOTAL_COST_AMT` decimal(24,6) DEFAULT NULL COMMENT '累计消费金额', `LAST_LOGIN_TIME` timestamp NULL DEFAULT NULL COMMENT '最后登录时间', `REVISION` int(11) DEFAULT NULL COMMENT '乐观锁', `CREATED_BY` varchar(32) DEFAULT NULL COMMENT '创建人', `CREATED_TIME` timestamp NULL DEFAULT NULL COMMENT '创建时间', `UPDATED_BY` varchar(32) DEFAULT NULL COMMENT '更新人', `UPDATED_TIME` timestamp NULL DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`USER_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='买家';
4.启动类上添加@MapperScan 注解
package com.mybatisplusdemo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan(value = "com.mybatisplusdemo.mapper") @SpringBootApplication public class MybatisPlusDemoApplication { public static void main(String[] args) { SpringApplication.run(MybatisPlusDemoApplication.class, args); } }
6.添加分页配置类
package com.mybatisplusdemo.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * mybatisPlus分页插件配置类 */ @EnableTransactionManagement @Configuration public class MybatisPlusConfig { /** * 分页拦截器 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
7.添加代码生成器
package com.mybatisplusdemo.config; import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.OutputFile; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.Collections; /** * mybatis-plus 代码生成器 */ public class MybatisPlusGenerator { /** * 包名 */ private static final String PACKAGE_NAME = "com.mybatisplusdemo"; /** * 模块名称 */ private static final String MODULE_NAME = "mybatisPlus-demo"; /** * 输出文件的路径 */ private static final String OUT_PATH = "//mybatisPlus-demo//src//main//java"; /** * xml输出文件的路径 */ private static final String XML_OUT_PATH = "//mybatisPlus-demo//src//main//resources//mapper"; /** * 代码生成者 */ private static final String AUTHOR = "author"; /** * JDBC相关配置 */ private static final String DRIVER = "com.mysql.cj.jdbc.Driver"; private static final String URL = "jdbc:mysql://127.0.0.1:3306/devdatabase?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8"; private static final String USER_NAME = "devuser"; private static final String PASSWORD = "admin"; /** * <p> * MySQL 生成演示 * </p> */ public static void main(String[] args) { MybatisPlusGenerator generator = MybatisPlusGenerator.getSingle(); generator.autoGeneration(); } private static MybatisPlusGenerator single = null; private MybatisPlusGenerator() { super(); } private static MybatisPlusGenerator getSingle() { if (single == null) { single = new MybatisPlusGenerator(); } return single; } public void autoGeneration() { String projectBaseUrl = System.getProperty("user.dir"); String outputDir = projectBaseUrl + OUT_PATH; String xmlOutputDir = projectBaseUrl + XML_OUT_PATH; FastAutoGenerator.create(URL, USER_NAME, PASSWORD) .globalConfig(builder -> { builder.author(AUTHOR) // 设置作者 .enableSwagger() // 开启 swagger 模式 .outputDir(outputDir); // 指定输出目录 }) .packageConfig(builder -> { builder.parent(PACKAGE_NAME) // 设置父包名 // .moduleName(MODULE_NAME) // 设置父包模块名 .pathInfo(Collections.singletonMap(OutputFile.xml, xmlOutputDir)); // 设置mapperXml生成路径 }) .strategyConfig(builder -> { builder.addInclude("buyr_user") // 设置需要生成的表名 .addTablePrefix("buyr_"); // 设置过滤表前缀 }) .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 .execute(); } }
运行代码生成器MybatisPlusGenerator 的main方法即可生成对应的java代码文件和xml配置文件
生成后的代码结构如下:
即可调用对应的service接口的CRUD的方法.
这篇关于springboot+mybatis-plus集成的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?