1)属性定义:AutoGeneratorProp
package cn.hg.biz.mybatis.plus.generator;
import com.baomidou.mybatisplus.annotation.DbType;
import lombok.Data;
/**
* 生成属性配置
* @author hgSuper
* @date 2020-11-19
*/
@Data
public class AutoGeneratorProp {
/**
* 作者
*/
private String author;
/**
* 本地输出路径
*/
private String outputDir;
/**
* 数据库连接
* eg:jdbc:mysql://localhost:3306/hg_demo?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=UTC
*/
private String dbUrl;
private String dbUserName;
private String dbPwd;
/**
* 父级包名
*/
private String parentPackage;
/**
* 表
*/
private String[] tables;
/**
* 删除表前缀
*/
private String tablePrefix;
private DbType dbType;
private String driverName;
public void oracle() {
this.dbType = DbType.ORACLE;
this.driverName = "oracle.jdbc.OracleDriver";
}
public void mysql() {
this.dbType = DbType.MYSQL;
this.driverName = "com.mysql.cj.jdbc.Driver";
//this.driverName = "com.mysql.jdbc.Driver";
}
public void sqlite() {
this.dbType = DbType.SQLITE;
this.driverName = "org.sqlite.JDBC";
}
}
2)生成器: MyBatisPlusGenerator
package cn.hg.biz.mybatis.plus.generator;
import com.baomidou.mybatisplus.annotation.IdType;
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.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
public class MyBatisPlusGenerator {
private static AutoGeneratorProp buildProp() {
AutoGeneratorProp autoGeneratorProp = new AutoGeneratorProp();
// autoGeneratorProp.mysql();
autoGeneratorProp.sqlite();
autoGeneratorProp.setAuthor("hgSuper");
//autoGeneratorProp.setDbUrl("jdbc:mysql://localhost:3306/niubi345_eladmin?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=UTC");
//autoGeneratorProp.setDbUrl("jdbc:mysql://localhost:3306/hg-solr?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
autoGeneratorProp.setDbUrl("jdbc:sqlite:f:/g/java/sqlite/databases/hg.db");
// jdbc:sqlite::resource:sqlite.db
//autoGeneratorProp.setDbUrl("jdbc:mysql://localhost:3306/hg_demo?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
//autoGeneratorProp.setDbUrl("jdbc:sqlite:hg.db");
//autoGeneratorProp.setDbUserName("hg-solr");
//autoGeneratorProp.setDbUserName("root");
//autoGeneratorProp.setDbPwd("DnfZwE@Zz6QbAYjR4!ND");
//autoGeneratorProp.setDbPwd("root");
autoGeneratorProp.setOutputDir("d://tmp");
autoGeneratorProp.setParentPackage("hg.demo.sqlite.system.demo");
autoGeneratorProp.setTablePrefix("t_");
String[] tables = {"t_demo"};
autoGeneratorProp.setTables(tables);
return autoGeneratorProp;
}
public static void main(String[] args) {
AutoGeneratorProp autoGeneratorProp = buildProp();
autoGenerator(autoGeneratorProp);
}
/**
* 自动生成
* @param autoGeneratorProp
*/
public static void autoGenerator(AutoGeneratorProp autoGeneratorProp) {
System.out.println("------------------");
System.out.println("--------开始生成----------");
AutoGenerator autoGenerator = initAutoGenerator(autoGeneratorProp);
autoGenerator.execute();
System.out.println("--------生成完成----------");
System.out.println("------------------");
}
/**
* 初始化-生成器
*/
private static AutoGenerator initAutoGenerator(AutoGeneratorProp autoGeneratorProp) {
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setGlobalConfig(initGlobalConfig(autoGeneratorProp));
autoGenerator.setDataSource(iniDataSourceConfig(autoGeneratorProp));
autoGenerator.setStrategy(initStrategyConfig(autoGeneratorProp));
autoGenerator.setPackageInfo(initPackageConfig(autoGeneratorProp));
return autoGenerator;
}
/**
* 初始化-全局配置
* @return
*/
public static GlobalConfig initGlobalConfig(AutoGeneratorProp autoGeneratorProp) {
GlobalConfig config = new GlobalConfig();
//config.setAuthor("hgSuper");
config.setAuthor(autoGeneratorProp.getAuthor());
config.setBaseResultMap(false);
// 生成路径
//config.setOutputDir("D:\\tmp");
config.setOutputDir(autoGeneratorProp.getOutputDir());
config.setIdType(IdType.INPUT);
config.setDateType(DateType.ONLY_DATE);
config.setFileOverride(true);
return config;
}
/**
* 初始化-包配置
* @return
*/
private static PackageConfig initPackageConfig(AutoGeneratorProp autoGeneratorProp) {
//String parentPackage = "cn.hgsuper.financial.subject";
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent(autoGeneratorProp.getParentPackage());
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
packageConfig.setMapper("mapper");
packageConfig.setXml("mapper.xml");
packageConfig.setController("controller");
packageConfig.setEntity("entity");
return packageConfig;
}
/**
* 初始化-数据库连接
* @return
*/
private static DataSourceConfig iniDataSourceConfig(AutoGeneratorProp autoGeneratorProp) {
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(autoGeneratorProp.getDbType());
dataSourceConfig.setUrl(autoGeneratorProp.getDbUrl());
dataSourceConfig.setUsername(autoGeneratorProp.getDbUserName());
dataSourceConfig.setPassword(autoGeneratorProp.getDbPwd());
dataSourceConfig.setDriverName(autoGeneratorProp.getDriverName());
return dataSourceConfig;
}
/**
* 初始化-生成策略
* @return
*/
private static StrategyConfig initStrategyConfig(AutoGeneratorProp autoGeneratorProp) {
// 表名
//String[] tables = new String[] {"hg_financial_subject"};
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setCapitalMode(true)
.setEntityLombokModel(true)
// 删除前缀
//.setTablePrefix("hg_")
.setTablePrefix(autoGeneratorProp.getTablePrefix())
.setNaming(NamingStrategy.underline_to_camel)
.setInclude(autoGeneratorProp.getTables());
return strategyConfig;
}
}
3)说明
说明1: 这个源码是从项目中抽离出来的,删除了业务代码,如果依赖中报错了,已经是缺依赖,加入依赖即可
说明2: 核心生成代码已经在了,所以一定难不倒牛批的你
Original: https://blog.csdn.net/myloverisxin/article/details/119317985
Author: hgSuper
Title: 【Java】SQLite | MybatisPlus自动生成 | 报错(near “show“: syntax error)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/816610/
转载文章受原作者版权保护。转载请注明原作者出处!