项目目录
版本
代码
package com.example.demo.mybatiscodegenerate;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
public class CodeGenerator {
public static void main(String[] args) {
// 创建AutoGenerator
AutoGenerator generator = new AutoGenerator();
// 创建全局配置实体
GlobalConfig globalConfig = new GlobalConfig();
// 这里的各项具体设置可以去GlobalConfig这个类里查用处
globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java"); // 输出目录
globalConfig.setOpen(false); // 生成后是否打开目录
globalConfig.setFileOverride(true); // 设置是否覆盖原有文件
globalConfig.setBaseColumnList(true);
globalConfig.setBaseResultMap(true);
globalConfig.setIdType(IdType.INPUT);
globalConfig.setMapperName("%sDao"); //这个设置会将接口由原来的mpper转为dao
generator.setGlobalConfig(globalConfig); // 将设置应用到generator上
// 创建数据源配置实体,用于自动生成实体类
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/db_name"); // 设置数据源url
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver"); // 设置数据库驱动
dataSourceConfig.setUsername("user_name"); // 设置用户名
dataSourceConfig.setPassword("passworld"); // 设置密码
generator.setDataSource(dataSourceConfig); // 将设置应用到generator上
// 创建数据表策略实体
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setInclude("table_name"); // 设置需要生成的表,与exclude二选一配置
strategyConfig.setEntityLombokModel(false); // 设置实体类是否使用lombok
strategyConfig.setEntitySerialVersionUID(false); // 设置实体类是否添加SerialVersionUID
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
generator.setStrategy(strategyConfig); // 将设置应用到generator上
// 包配置
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.example.demo.mybatiscodegenerate");// 设置父包名
generator.setPackageInfo(packageConfig);// 将设置应用到generator上
// 模板设置
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.disable(TemplateType.CONTROLLER, TemplateType.SERVICE); // 不生成指定的模板
generator.setTemplate(templateConfig); // 将设置应用到generator上
// 开始生成
generator.execute();
}
}
MybatisGenerator.xml
MyBatisGeneratorRun.clss
package com.mybatis.mbgenerate;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class MyBatisGeneratorRun {
public static void main(String[] args) throws Exception {
MyBatisGeneratorRun app = new MyBatisGeneratorRun();
System.out.println(app.getClass().getResource("/").getPath());
app.generator();
System.out.println(System.getProperty("user.dir"));
}
public void generator() throws Exception {
List warnings = new ArrayList();
boolean overwrite = true;
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("MybatisGenerator.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(resourceAsStream);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
for (String warning : warnings) {
System.out.println(warning);
}
}
}
MyCommentGenerator.class //中文注释
package com.mybatis.mbgenerate;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.XmlElement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Set;
public class MyCommentGenerator implements CommentGenerator{
private Properties properties;
private Properties systemPro;
private boolean suppressDate;
private boolean suppressAllComments;
private String currentDateStr;
public MyCommentGenerator() {
super();
properties = new Properties();
systemPro = System.getProperties();
suppressDate = false;
suppressAllComments = false;
currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
}
@Override
public void addConfigurationProperties(Properties properties) {
// TODO Auto-generated method stub
}
@Override
public void addFieldComment(Field field,IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn)
{
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
field.addJavaDocLine(sb.toString().replace("\n", " "));
field.addJavaDocLine(" */");
}
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
// TODO Auto-generated method stub
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
field.addJavaDocLine(sb.toString().replace("\n", " "));
field.addJavaDocLine(" */");
}
@Override
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// TODO Auto-generated method stub
}
@Override
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
// TODO Auto-generated method stub
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerClass.addJavaDocLine(sb.toString());
sb.setLength(0);
sb.append(" * @author ");
sb.append(systemPro.getProperty("user.name"));
sb.append(" ");
sb.append(currentDateStr);
// addJavadocTag(innerClass, markAsDoNotDelete);
innerClass.addJavaDocLine(" */");
}
@Override
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
}
@Override
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
// TODO Auto-generated method stub
}
@Override
public void addGetterComment(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
// TODO Auto-generated method stub
if (suppressAllComments) {
return;
}
method.addJavaDocLine("/**");
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
sb.setLength(0);
sb.append(" * @return ");
sb.append(introspectedColumn.getActualColumnName());
sb.append(" ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
// addJavadocTag(method, false);
method.addJavaDocLine(" */");
}
@Override
public void addSetterComment(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
// TODO Auto-generated method stub
if (suppressAllComments) {
return;
}
method.addJavaDocLine("/**");
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
Parameter parm = method.getParameters().get(0);
sb.setLength(0);
sb.append(" * @param ");
sb.append(parm.getName());
sb.append(" ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
// addJavadocTag(method, false);
method.addJavaDocLine(" */");
}
@Override
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
// TODO Auto-generated method stub
}
@Override
public void addJavaFileComment(CompilationUnit compilationUnit) {
// TODO Auto-generated method stub
}
@Override
public void addComment(XmlElement xmlElement) {
// TODO Auto-generated method stub
}
@Override
public void addRootComment(XmlElement rootElement) {
// TODO Auto-generated method stub
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
Set imports) {
// TODO Auto-generated method stub
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn, Set imports) {
// TODO Auto-generated method stub
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
Set imports) {
// TODO Auto-generated method stub
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn, Set imports) {
// TODO Auto-generated method stub
}
@Override
public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable,
Set imports) {
// TODO Auto-generated method stub
}
}
xml方式配置好之后,运行
MyBatisGeneratorRun的main