MybatisPlus的代码生成器 使用详解(整合springboot)

阅读: 评论:0

MybatisPlus的代码生成器 使用详解(整合springboot)

MybatisPlus的代码生成器 使用详解(整合springboot)

MybatisPlus 代码生成器 使用详解(整合springboot)

文章目录

  • MybatisPlus 代码生成器 使用详解(整合springboot)
    • 代码生成器概述
    • 整合springboot步骤
      • 第一步:创建数据库表
      • 第二步:导入jar包
      • 第三步:去配置文件
      • 第四步:开启mapper接口扫描,添加分页插件
      • 第五步:代码生成
      • 第五步:运行,生成模板

代码生成器概述

​ AutoGenerator 是 MyBatis-Plus 的代码生成器,可能小伙伴目前还不了解代码生成器,是做什么的?通俗的来说:就是生成模板,我在下面给个生成的照片小伙伴就理解了,它可以通过我们给出数据库中的表名,来快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

左边的Entity、Mapper、Mapper XML、Service、Controller,都是通过下面给出的代码自动生成的,代码在下面

整合springboot步骤

第一步:创建数据库表

首先我在数据库中新建了一个m_user表和m_blog表:

CREATE TABLE `m_user` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`username` varchar(64) DEFAULT NULL,`avatar` varchar(255) DEFAULT NULL,`email` varchar(64) DEFAULT NULL,`password` varchar(64) DEFAULT NULL,`status` int(5) NOT NULL,`created` datetime DEFAULT NULL,`last_login` datetime DEFAULT NULL,PRIMARY KEY (`id`),KEY `UK_USERNAME` (`username`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `m_blog` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`user_id` bigint(20) NOT NULL,`title` varchar(255) NOT NULL,`description` varchar(255) NOT NULL,`content` longtext,`created` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP,`status` tinyint(4) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4;
INSERT INTO `vueblog`.`m_user` (`id`, `username`, `avatar`, `email`, `password`, `status`, `created`, `last_login`) VALUES ('1', 'markerhub', '.jpg', NULL, '96e79218965eb72c92a549dd5a330112', '0', '2020-04-20 10:44:01', NULL);

第二步:导入jar包

​ pom中导入mybatis plus的jar包,因为后面会涉及到代码生成,所以我们还需要导入页面模板引擎,这里我们用的是freemarker。

<!--mp-->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.2.0</version>
</dependency>
<!--pom中导入mybatis plus的jar包,因为后面会涉及到代码生成,所以我们还需要导入页面模板引擎,这里我们用的是freemarker。-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope>
</dependency>
<!--mp代码生成器-->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.2.0</version>
</dependency>

第三步:去配置文件

注意:这里的配置文件是l,里面的url username password 需要自己改动哦

# DataSource Config
spring:datasource:driver-class-name: sql.jdbc.Driverurl: jdbc:mysql://localhost:3306/vueblog?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghaiusername: rootpassword: rjh123
mybatis-plus:mapper-locations: classpath*:/mapper/**l
server:port: 8080

第四步:开启mapper接口扫描,添加分页插件

​ 新建一个包:通过@mapperScan注解指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类。PaginationInterceptor是一个分页插件。

package fig;import sion.plugins.PaginationInterceptor;
batis.spring.annotation.MapperScan;
import t.annotation.Bean;
import t.annotation.Configuration;
import ansaction.annotation.EnableTransactionManagement;@Configuration
@EnableTransactionManagement
//这里的扫描接口是自己的目录,要自己修改
@MapperScan("com.rjh.mapper")
public class MybatisPlusConfig {@Beanpublic PaginationInterceptor paginationInterceptor() {PaginationInterceptor paginationInterceptor = new PaginationInterceptor();return paginationInterceptor;}}

第五步:代码生成

==注意:==小伙伴,这里的全局配置、数据源配置和包配置都需要改成自己的,

package com.rjh;import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;import ptions.MybatisPlusException;
import lkit.StringPool;
import lkit.StringUtils;
import ator.AutoGenerator;
import ator.InjectionConfig;
import fig.DataSourceConfig;
import fig.FileOutConfig;
import fig.GlobalConfig;
import fig.PackageConfig;
import fig.StrategyConfig;
import fig.TemplateConfig;
import fig.po.TableInfo;
import fig.rules.NamingStrategy;
import ine.FreemarkerTemplateEngine;import javax.swing.*;/*** @author Van* @date 2020/5/1 - 15:43*/
public class CodeGenerator {/*** <p>* 读取控制台内容* </p>*/public static String scanner(String tip) {Scanner scanner = new Scanner(System.in);StringBuilder help = new StringBuilder();help.append("请输入" + tip + ":");System.out.String());if (scanner.hasNext()) {String ipt = ();if (StringUtils.isNotEmpty(ipt)) {return ipt;}}throw new MybatisPlusException("请输入正确的" + tip + "!");}/*** RUN THIS*/public static void main(String[] args) {// 代码生成器AutoGenerator mpg = new AutoGenerator();// 全局配置GlobalConfig gc = new GlobalConfig();String projectPath = Property("user.dir");gc.setOutputDir(projectPath + "/src/main/java");gc.setAuthor("rjh");gc.setOpen(false);gc.setServiceName("%Service");mpg.setGlobalConfig(gc);// 数据源配置DataSourceConfig dsc = new DataSourceConfig();dsc.setUrl("jdbc:mysql://localhost:3306/vueblog?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");// dsc.setSchemaName("public");dsc.setDriverName(&#sql.cj.jdbc.Driver");dsc.setUsername("root");dsc.setPassword("rjh123");mpg.setDataSource(dsc);// 包配置PackageConfig pc = new PackageConfig();pc.setModuleName(null);pc.setParent("com.rjh");mpg.setPackageInfo(pc);// 自定义配置InjectionConfig cfg = new InjectionConfig() {@Overridepublic void initMap() {// to do nothing}};List<FileOutConfig> focList = new ArrayList<>();focList.add(new FileOutConfig("/l.ftl") {@Overridepublic String outputFile(TableInfo tableInfo) {// 自定义输入文件名称return projectPath + "/mybatis-plus-sample-generator/src/main/resources/mapper/" + pc.getModuleName()+ "/" + EntityName() + "Mapper" + StringPool.DOT_XML;}});cfg.setFileOutConfigList(focList);mpg.setCfg(cfg);mpg.setTemplate(new TemplateConfig().setXml(null));// 策略配置StrategyConfig strategy = new StrategyConfig();strategy.setNaming(NamingStrategy.underline_to_camel);strategy.setColumnNaming(NamingStrategy.underline_to_camel);strategy.setEntityLombokModel(true);strategy.setRestControllerStyle(true);strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));strategy.setControllerMappingHyphenStyle(true);strategy.setTablePrefix("m_");mpg.setStrategy(strategy);// 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!mpg.setTemplateEngine(new FreemarkerTemplateEngine());ute();}
}

第五步:运行,生成模板

回车即可生成模板,像第一张图片似的。

如果这篇博客,对小伙伴,有所帮助,记得点赞,收藏哦。

本文发布于:2024-01-28 23:36:50,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170645621511116.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23