日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java mybatis 代码生成器_Java MyBatis-Plus 代码生成器

發(fā)布時間:2023/12/15 java 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java mybatis 代码生成器_Java MyBatis-Plus 代码生成器 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

pom.xml的依賴

org.projectlombok

lombok

true

com.baomidou

mybatis-plus-boot-starter

3.3.2

mysql

mysql-connector-java

com.baomidou

mybatis-plus-generator

3.3.2

org.apache.velocity

velocity-engine-core

2.2

核心代碼

package com.example.test5.support;

import com.baomidou.mybatisplus.annotation.DbType;

import com.baomidou.mybatisplus.annotation.FieldFill;

import com.baomidou.mybatisplus.annotation.IdType;

import com.baomidou.mybatisplus.generator.AutoGenerator;

import com.baomidou.mybatisplus.generator.InjectionConfig;

import com.baomidou.mybatisplus.generator.config.*;

import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;

import com.baomidou.mybatisplus.generator.config.po.TableFill;

import com.baomidou.mybatisplus.generator.config.po.TableInfo;

import com.baomidou.mybatisplus.generator.config.rules.DateType;

import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;

import com.baomidou.mybatisplus.generator.config.rules.IColumnType;

import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.io.File;

import java.util.ArrayList;

import java.util.List;

public class class2 {

private static String projectPath = null;

public static void main(String[] args) {

projectPath = System.getProperty("user.dir");

AutoGenerator autoGenerator = new AutoGenerator();

autoGenerator.setGlobalConfig(configGlobalConfig());

autoGenerator.setDataSource(configDataSource());

autoGenerator.setStrategy(configStrategyConfig());

autoGenerator.setPackageInfo(configPackageConfig());

autoGenerator.setCfg(customerConfig());

autoGenerator.execute();

}

/**

* 自定義生成文件配置

*

* @return

*/

private static InjectionConfig customerConfig() {

InjectionConfig config = new InjectionConfig() {

@Override

public void initMap() {

}

};

List files = new ArrayList();

files.add(new FileOutConfig("/templates/template.java.vm") {

@Override

public String outputFile(TableInfo tableInfo) {

String expand = "c:/pms/expand";

String entityFile = String.format((expand + File.separator + "%s" + ".java"), tableInfo.getControllerName());

return entityFile;

}

});

config.setFileOutConfigList(files);

return config;

}

/**

* 全局配置

*

* @return

*/

private static GlobalConfig configGlobalConfig() {

GlobalConfig config = new GlobalConfig();

// 是否支持AR模式

config.setActiveRecord(true)

// 作者

.setAuthor("test@company.com")

// 生成路徑

// .setOutputDir(projectPath + "/src/main/java/")

.setOutputDir("c:/pms/src/main/java/")

// 文件覆蓋

.setFileOverride(true)

// 主鍵策略

.setIdType(IdType.AUTO)

// 設(shè)置生成的service接口的名字的首字母是否為I,例如IEmployeeService

.setServiceName("%sService")

//生成基本的resultMap

.setBaseResultMap(true)

//生成基本的SQL片段

.setBaseColumnList(true)

//生成后打開文件夾

.setOpen(false).setDateType(DateType.ONLY_DATE);

return config;

}

/**

* 數(shù)據(jù)源配置

*

* @return

*/

private static DataSourceConfig configDataSource() {

DataSourceConfig dsConfig = new DataSourceConfig();

// 設(shè)置數(shù)據(jù)庫類型

dsConfig.setDbType(DbType.MYSQL)

.setDriverName("com.mysql.cj.jdbc.Driver")

.setUrl("jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8")

.setUsername("root")

.setPassword("root")

.setTypeConvert(new MySqlTypeConvert() {

// 自定義數(shù)據(jù)庫表字段類型轉(zhuǎn)換【可選】

@Override

public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {

System.out.println("轉(zhuǎn)換類型:" + fieldType);

if (fieldType.toLowerCase().contains("tinyint")) {

return DbColumnType.INTEGER;

}

return super.processTypeConvert(globalConfig, fieldType);

}

});

return dsConfig;

}

/**

* 策略配置

*

* @return

*/

private static StrategyConfig configStrategyConfig() {

StrategyConfig stConfig = new StrategyConfig();

List tableFillList = new ArrayList();

//如 每張表都有一個創(chuàng)建時間、修改時間

//而且這基本上就是通用的了,新增時,創(chuàng)建時間和修改時間同時修改

//修改時,修改時間會修改,

//雖然像Mysql數(shù)據(jù)庫有自動更新幾只,但像ORACLE的數(shù)據(jù)庫就沒有了,

//使用公共字段填充功能,就可以實現(xiàn),自動按場景更新了。

//如下是配置

TableFill sysCreateTime = new TableFill("create_time", FieldFill.INSERT);

TableFill sysUpdateTime = new TableFill("update_time", FieldFill.UPDATE);

TableFill sysCreateBy = new TableFill("create_by", FieldFill.INSERT);

TableFill sysUpdateBy = new TableFill("update_by", FieldFill.UPDATE);

tableFillList.add(sysCreateTime);

tableFillList.add(sysUpdateTime);

tableFillList.add(sysCreateBy);

tableFillList.add(sysUpdateBy);

// 全局大寫命名

stConfig.setCapitalMode(true)

// 指定表名 字段名是否使用下劃線

//.setDbColumnUnderline(true)

// 數(shù)據(jù)庫表映射到實體的命名策略

.setNaming(NamingStrategy.underline_to_camel)

//.setTablePrefix("tbl_")

// 生成的表

.setInclude(new String[]{

"pms_brand"

})

.setEntityBooleanColumnRemoveIsPrefix(false)

// 自定義實體,公共字段

.setTableFillList(tableFillList);

return stConfig;

}

/**

* 包名策略配置

* @return

*/

private static PackageConfig configPackageConfig() {

PackageConfig pkConfig = new PackageConfig();

pkConfig.setParent("com.example.test5.pms")

//dao

.setMapper("dao")

//service

.setService("service")

//controller

.setController("controller")

.setEntity("entity")

//mapper.xml

.setXml("mapper");

return pkConfig;

}

}

模板

#foreach($field in $!{table.fields})

#if($!{field.name}!=$!{cfg.tenantColumn})

{

label: "$!{field.comment}",

prop: "$!{field.propertyName}",

rules: [{

required: true,

message: "請輸入$!{field.comment}",

trigger: "blur"

}]

},

#end

#end

.

總結(jié)

以上是生活随笔為你收集整理的java mybatis 代码生成器_Java MyBatis-Plus 代码生成器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。