mybatis plus 生成表_MyBatisPlus系列之代码生成器

news/2025/5/23 12:25:13

代码生成器,也叫逆向工程,是根据数据库里的表结构,自动生成对应的实体类、映射文件和接口。

这和hibernate的自动建表恰好相反。

很早之前,写了一个代码生成器的工程,自己也一直在用,很方便,也经过大量验证,也支持很多数据库。

看到很多小伙伴在为数据库生成实体类发愁,现分享给大家,提高开发效率。

mybatis-plus-code-generator 工程介绍

工程地址:mybatis-plus-code-generator:https://github.com/fengwenyi/mybatis-plus-code-generator

目录结构:

fe7e725bda6361eb0597dda3e750cea5.png

Config类

针对每个人的习惯,可以在这个类里进行配置

    /** 包名:service */    public static final String PACKAGE_NAME_SERVICE = "repository";    /** 包名:service.impl */    public static final String PACKAGE_NAME_SERVICE_IMPL = "repository.impl";    /** 包名:model */    public static final String PACKAGE_NAME_MODEL = "entity";    /** 包名:dao */    public static final String PACKAGE_NAME_DAO = "mapper";    /** 包名:xml */    public static final String PACKAGE_NAME_XML = "xml";    /** 文件名后缀:Model */    public static final String FILE_NAME_MODEL = "%sEntity";    /** 文件名后缀:Dao */    public static final String FILE_NAME_DAO = "I%sMapper";    /** 文件名后缀:Mapper */    public static final String FILE_NAME_XML = "%sMapper";    /** MP开头,Service结尾 */    public static final String FILE_NAME_SERVICE = "MP%sRepository";    /** 文件名后缀:ServiceImpl */    public static final String FILE_NAME_SERVICE_IMPL = "%sRepositoryImpl";    /** 逻辑删除字段 */    public static final String FIELD_LOGIC_DELETE_NAME = "delete_status";    /** 作者 */    public static final String AUTHOR = "Erwin Feng";    /** 是否支持Swagger,默认不支持 */    public static final Boolean SWAGGER_SUPPORT = false;

MySQL8CodeGenerator

9dab5eb83388ab1c90c7235aba439bda.png

圈中的地方是必须要改的。

修改之后,执行就可以生成相应的代码文件了,下面我们举个例子。

示例

我们以MyBatis-Plus官网的例子为例,拿来演示

SQL

DROP TABLE IF EXISTS user;CREATE TABLE user(    id BIGINT(20) NOT NULL COMMENT '主键ID',    name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',    age INT(11) NULL DEFAULT NULL COMMENT '年龄',    email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',    PRIMARY KEY (id));DELETE FROM user;INSERT INTO user (id, name, age, email) VALUES(1, 'Jone', 18, 'test1@baomidou.com'),(2, 'Jack', 20, 'test2@baomidou.com'),(3, 'Tom', 28, 'test3@baomidou.com'),(4, 'Sandy', 21, 'test4@baomidou.com'),(5, 'Billie', 24, 'test5@baomidou.com');

testMySQLCodeGenerator

DbType dbType = DbType.MYSQL;String dbUrl = "jdbc:mysql://192.168.16.128:3306/study-spring-boot-mybatis-plus";String username = "root";String password = "123456";String driver = "com.mysql.cj.jdbc.Driver";// 表前缀,生成的实体类,不含前缀String [] tablePrefixes = {};// 表名,为空,生成所有的表String [] tableNames = {};// 字段前缀String [] fieldPrefixes = {};// 基础包名String packageName = "com.fengwenyi.studyspringbootmybatisplus.db";CommonUtils.execute(dbType, dbUrl, username, password, driver, tablePrefixes, tableNames, packageName, fieldPrefixes);

生成的文件

9bd6577651f9bd8ff7412c1b2c60c4b4.png

我们将生成的文件拷贝到工程里

7349c1a59e981387e9be64bb20ac02e9.png

测试

@Autowiredprivate MPUserRepository mpUserRepository;@Testpublic void testSelectList() {    List<UserEntity> list = mpUserRepository.list();    list.forEach(userEntity -> log.info(userEntity.toString()));}

运行:

8557f533d758d88aa8aadcc4745d86cb.png

工程链接

代码生成器:mybatis-plus-code-generator:https://github.com/fengwenyi/mybatis-plus-code-generator

示 例 工 程:study-spring-boot-mybatis-plus:https://github.com/fengwenyi/study-spring-boot/tree/master/spring-boot-mybatis-plus

文章来源:https://blog.csdn.net/weixin_39640090/article/details/111200836
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:https://dhexx.cn/news/show-16683.html

相关文章

java-资源管理器try-with-resource

在java编程中会遇到很多关闭资源的问题&#xff0c;但是&#xff0c;往往我们的关闭不能百分百正确&#xff0c;所以java7中出现了新的资源管理器方法try-with-resource&#xff0c;这是一项重要的改进&#xff0c;因为没人能再手动关闭资源时做到100%正确&#xff0c;有人在想…

android如何保护intent参数,android 使用application解决intent 页面跳转传参限制

实现原理&#xff0c;把参数保存到application里面&#xff0c;到另一个页面再从application获取。实现方式&#xff1a;1.首先创建BaseApp.javaimport android.app.Application;import com.example.test.BaseActivity.JumpPara;public class BaseApp extends Application {pri…

【smarty项目源码】模拟smarty模版文件的解析过程

[php] view plaincopy <?php class MyMiniSmarty{ //模版文件的存放路径 var $template_dir"./templates/"; //编译文件的存放路径 &#xff0c;编译文件的名称格式暂定为&#xff1a;com_对应的tpl.php var $complie_dir"./templates…

SQL Server 2008 R2 跟踪标志

SQL Server 2008 R2 跟踪标志 原文:SQL Server 2008 R2 跟踪标志跟踪标志用于临时设置特定服务器的特征或关闭特定行为。例如&#xff0c;如果启动 SQL Server 的一个实例时设置了跟踪标志 3205&#xff0c;将禁用磁带机的硬件压缩。跟踪标志经常用于诊断性能问题&#xff0c;或…

win32获取当前运行程序的内存地址_运行于微软Windows 10X上的Win32应用将受到诸多限制...

IT之家2月18日消息 IT之家先前曾报道微软Windows 10X将支持大部分Win32应用&#xff0c;但最新消息显示&#xff0c;运行于新系统的Win32应用将受到诸多限制。据报道Windows 10X系统将使用容器来运行应用程序&#xff0c;包括Win32、UWP和PWA&#xff0c;以此降低系统受到恶意软…

mac共享文件android,Mac如何访问安卓系统共享文件夹的方法

Mac系统与安卓系统的差异导致了很多网友都无法完成两个系统间互相访问共享文件夹的操作&#xff0c;对办公等造成了很多的困扰&#xff0c;那么到底Mac系统如何访问WIN系统的共享文件夹呢?今天学习啦小编就为大家带来了Mac如何访问安卓系统共享文件夹的解决方法&#xff0c;希…

iOS开发宝典:String用法大全

一、NSString 创建字符串。 NSString *astring "This is a String!"; 创建空字符串&#xff0c;给予赋值。 NSString *astring [[NSString alloc] init]; astring "This is a String!"; NSLog("astring:%",astring); string releas…

code[vs]3301 Square words

暴力枚举最长公共子序列 #include <iostream> #include <cstring>using namespace std;int dp[510][510]; int n;int lcs(char * a, char * b, int lena, int lenb) {memset(dp, 0, sizeof(dp));for (int i 1; i < lena; i){for (int j 1; j < lenb; j){//…

python try except continue_python中 try、except、finally 的执行顺序

i in finally : 2 test4Return : 1 test3和test4得到的结论&#xff1a; 在except和try中遇到return时&#xff0c;会锁定return的值&#xff0c;然后跳转到finally中&#xff0c;如果finally中没有return语句&#xff0c;则finally执行完毕之后仍返回原return点&#xff0c;将…

android dumpsys wifi,Android Shell命令之dumpsys部分功能简介

adb简称 android debug bridge, 通过adb我们获得了查看android手机的另一个窗口。可以通过它看到一些内部的东西。dumpsys则是其中一个很强大的工具。作用跟名字说的有点像&#xff0c;查看系统信息。android下的每一个系统组件都有一个名字&#xff0c;如window, surfaceFinge…