iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >MyBatisPlus代码生成器的使用示例
  • 273
分享到

MyBatisPlus代码生成器的使用示例

2024-04-02 19:04:59 273人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录导入依赖表结构当前项目结构配置代码生成器1、globalConfig 全局策略配置2、dataSourceConfig 数据源配置AutoGenerator 是 mybatis-

AutoGenerator 是 mybatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

导入依赖


        <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>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.19</version>
        </dependency>

更详细的代码生成器配置请查看官方文档:https://baomidou.com/pages/061573/#superentityclass

表结构

在这里插入图片描述

当前项目结构

在这里插入图片描述

配置代码生成器

1、globalConfig 全局策略配置

outputDir

  • 生成文件的输出目录
  • 默认值:D 盘根目录

fileOverride

  • 是否覆盖已有文件
  • 默认值:false

open

  • 是否打开输出目录
  • 默认值:true

enableCache

  • 是否在 xml 中添加二级缓存配置
  • 默认值:false

开发人员

  • 默认值:null

Kotlin

  • 开启 Kotlin 模式
  • 默认值:false

swagger2

  • 开启 swagger2 模式
  • 默认值:false

activeRecord

  • 开启 ActiveRecord 模式
  • 默认值:false

baseResultMap

  • 开启 BaseResultMap
  • 默认值:false

baseColumnList

  • 开启 baseColumnList
  • 默认值:false

dateType

  • 时间类型对应策略
  • 默认值:TIME_PACK

entityName

  • 实体命名方式
  • 默认值:null 例如:%sEntity 生成 UserEntity

mapperName

  • mapper 命名方式
  • 默认值:null 例如:%sDao 生成 UserDao

xmlName

  • Mapper xml 命名方式
  • 默认值:null 例如:%sDao 生成 UserDao.xml

serviceName

  • service 命名方式
  • 默认值:null 例如:%sBusiness 生成 UserBusiness

serviceImplName

  • service impl 命名方式
  • 默认值:null 例如:%sBusinessImpl 生成 UserBusinessImpl

controllerName

  • controller 命名方式
  • 默认值:null 例如:%sAction 生成 UserAction

idType

  • 指定生成的主键的 ID 类型
  • 默认值:null

2、dataSourceConfig 数据源配置

dbQuery

​ 实现 IDbQuery 接口自定义数据库查询 sql 语句 定制化返回自己需要的内容

dbType

  • 数据库类型
  • 该类内置了常用的数据库类型【必须】

schemaName

  • 数据库 schema name
  • 例如 postgresql 可指定为 public

typeConvert

  • 类型转换
  • 默认由 dbType 类型决定选择对应数据库内置实现

​ 实现 ITypeConvert 接口自定义数据库 字段类型 转换为自己需要的 java 类型,内置转换类型无法满足可实现 IColumnType 接口自定义

url

  • 驱动连接的 URL

driverName

  • 驱动名称

username

  • 数据库连接用户名

passWord

  • 数据库连接密码

package com.haoming;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;


public class ChenGCode {
    public static void main(String[] args) {
        //构建代码生成器对象
        AutoGenerator mpg = new AutoGenerator();
        //1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");//生成文件的输出目录
        gc.setAuthor("鼠皓明");//作者
        gc.setOpen(false);//是否打开输出目录
        gc.setFileOverride(false);//是否覆盖已有的文件
        gc.setServiceName("%sService");//去除Service的I前缀
        gc.setIdType(IdType.ID_WORKER);//主键生成策略
        //ONLY_DATE 只使用 java.util.date 代替,SQL_PACK 使用 java.sql 包下的,TIME_PACK 使用 java.time 包下的 java8 新的时间类型
        gc.setDateType(DateType.TIME_PACK);//数据库时间类型 到 实体类时间类型 对应策略
        gc.setSwagger2(true);//开启swagger2模式
        mpg.setGlobalConfig(gc);

        //2、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:Mysql://localhost:3306/mybatis_plus?useSSl=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);//数据库类型
        mpg.setDataSource(dsc);

        //3、包的配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("blog");//父包模块名
        pc.setParent("com.cheng");//父包名,如果为空,将下面子包名必须写全部, 否则就只需写子包名
        pc.setEntity("pojo");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        //4、策略配置
        StrategyConfig sy = new StrategyConfig();
        sy.setInclude("user");//设置要映射的表,可以设置多张
        sy.setNaming(NamingStrategy.underline_to_camel);//从数据库表到文件的命名策略,下划线转驼峰命名
        sy.setColumnNaming(NamingStrategy.underline_to_camel);//列的命名策略
        sy.setEntityLombokModel(true);//开启lombok支持
        sy.setLogicDeleteFieldName("deleted");//设置逻辑删除字段
        sy.setVersionFieldName("version");//设置乐观
        sy.setRestControllerStyle(true);//开启controller的restful命名
        sy.setControllerMappingHyphenStyle(true);//开启controller中请求映射的连字符样式,如:localhost:8080/hello_id_1
        //设置自动填充
        TableFill create_time = new TableFill("create_time", FieldFill.INSERT);
        TableFill update_time = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(create_time);
        tableFills.add(update_time);
        mpg.setStrategy(sy);

        //执行代码生成器
        mpg.execute();
    }
}

执行代码生成器,查看项目结构的变化

在这里插入图片描述

代码生成器执行成功,自动生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码。

到此这篇关于MyBatisPlus代码生成器的使用示例的文章就介绍到这了,更多相关MyBatisPlus代码生成器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: MyBatisPlus代码生成器的使用示例

本文链接: https://www.lsjlt.com/news/161198.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作