Springboot整合MybatisPlus配置自动生成代码

Springboot整合MybatisPlus

在整合MybatisPlus之前我们先创建测试表

File Encoding         : 65001

Date: 2020-12-26 11:19:06
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` bigint(20) NOT NULL auto_increment COMMENT '主键ID',
  `name` varchar(30) default NULL COMMENT '姓名',
  `age` int(11) default NULL COMMENT '年龄',
  `email` varchar(50) default NULL COMMENT '邮箱',
  `update_time` datetime default NULL,
  `create_time` datetime NOT NULL,
  `is_delete` int(255) NOT NULL,
  `version` int(11) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;

1. 导入依赖

<!-- springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>
<!-- lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.16</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.37</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.0.5</version>
</dependency>
<!-- 模板引擎 -->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

2.新建springboot项目,配置数据源

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/Schema?useUnicode=true&characterEncoding=utf8&useSSL=true
    username: root
    password: root
  profiles:
    active: dev
mybatis-plus:
	# mapper 文件位置  
  mapper-locations: classpath:**/mapper/xml/*.xml
  # 别名  这个一定要跟你的pojo 路路径名一直
  type-aliases-package: com.springboot.test
  # 缓存
  configuration.cache-enabled: false
  global-config.db-config.db-type: mysql
  configuration.jdbc-type-for-null: null
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server: # 运行环境
  port: 9000

3.编写自动生成代码类

里面所有的配置都可以根据实际需求来
public static void main(String[] args) {
    // 创建一个 代码自动生成器 对象
    AutoGenerator mpg = new AutoGenerator();
    //1、全局配置
    GlobalConfig gc = new GlobalConfig();
    String prPath = System.getProperty("user.dir");//获取当前系统目录
    gc.setOutputDir(prPath+"/src/main/java");//指定输出的位置
    gc.setAuthor("wjh");//设置作者
    gc.setOpen(false);//是否打开资源管理器
    gc.setFileOverride(false);//是否覆盖原来的文件
    gc.setServiceName("%sService");//去掉service的i前缀
    gc.setIdType(IdType.ID_WORKER);//设置id的生成策略默认算法
    gc.setDateType(DateType.ONLY_DATE);//设置日期生成策略
    gc.setSwagger2(true);  // swagger 插件配置
    mpg.setGlobalConfig(gc);  // 设置配置

    //2、设置数据源
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://127.0.0.1:3306/Schema?useUnicode=true&characterEncoding=utf8&useSSL=true");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("root");
    dsc.setDbType(DbType.MYSQL);//数据库类型
    mpg.setDataSource(dsc);

    //3、配置包
    PackageConfig pc = new PackageConfig();
    pc.setModuleName("blog");//设置模块 把代码生成在那个包下  如果想生成 跟启动类同级的目录下  就注释这行
    pc.setParent("com.wjb");
    pc.setEntity("pojo");
    pc.setMapper("mapper");
    pc.setService("service");
    pc.setController("controller");
    mpg.setPackageInfo(pc);

    //4、策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setInclude("user");//指定要映射的数据库表,可以写多个
    strategy.setNaming(NamingStrategy.underline_to_camel);//设置命名规则下划线转驼峰
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);//列名规则
    strategy.setEntityLombokModel(true);//是否生成lombok注解

    //strategy.setLogicDeleteFieldName("deleted");//逻辑删除字段配置
    //自动填充的配置
    TableFill create_time = new TableFill("create_time", FieldFill.INSERT);//设置时的生成策略
    TableFill update_time = new TableFill("update_time", FieldFill.INSERT_UPDATE);//设置更新时间的生成策略
    ArrayList<TableFill> list = new ArrayList<>();
    list.add(create_time);
    list.add(update_time);
    strategy.setTableFillList(list);

    //乐观锁
    strategy.setVersionFieldName("version");
    strategy.setRestControllerStyle(true);//开启驼峰命名
    strategy.setControllerMappingHyphenStyle(true);//开启链接地址的下划线命名 localhost:8080/hello_id_2
    mpg.setStrategy(strategy);

    mpg.execute();
}

运行之后会出现:

准备生成文件…

文件生成完成!!!

说明你已经自动生成代码成功!也可以指定生成路径,具体配置可参考官方文档

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以按照以下步骤来整合Spring Boot和MyBatis Plus,并生成Mapper的抽象基类: 1. 首先,确保你已经在你的Spring Boot项目添加了MyBatis Plus的依赖。你可以在项目的pom.xml文件添加以下依赖: ```xml <dependencies> <!-- 其他依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> </dependencies> ``` 2. 接下来,创建一个Mapper抽象基类,用于继承MyBatis Plus提供的BaseMapper接口。该基类可以包含一些常用的CRUD方法,以供其他Mapper继承使用。以下是一个示例: ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface MyBaseMapper<T> extends BaseMapper<T> { // 自定义一些通用的方法 // ... } ``` 3. 然后,在你的实体类对应的Mapper接口继承自定义的MyBaseMapper接口。例如: ```java import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper extends MyBaseMapper<User> { // 可以在这里添加一些特定的方法 // ... } ``` 4. 最后,你可以通过MyBatis Plus的代码生成器来自动生成Mapper接口和XML映射文件。你可以在pom.xml文件配置相关插件,例如: ```xml <build> <plugins> <!-- 其他插件 --> <plugin> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-maven-plugin</artifactId> <version>最新版本</version> <executions> <execution> <id>generate</id> <phase>generate-sources</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <includeTables> <!-- 需要生成Mapper的表 --> </includeTables> <basePackage> <!-- Mapper接口的包名 --> </basePackage> <basePath> <!-- 生成文件的输出路径 --> </basePath> </configuration> </plugin> </plugins> </build> ``` 配置完成后,你可以启动Maven命令来生成Mapper接口和XML映射文件。执行以下命令: ``` mvn generate-sources ``` 这样,你就成功地整合了Spring Boot和MyBatis Plus,并生成了Mapper的抽象基类。接下来,你可以在项目使用这些Mapper来进行数据库操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值