标签:mysql snapshot nap 自动 ide 工具 完整 param https
@
idea 选择 file -> new project -> maven
后面一直next,注意maven plugin 工程的命名最好是 xxx-maven-plugin.
下面写maven插件必须的依赖。
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.1</version>
</dependency>
因为要依赖mybatis-plus-generator来生成mapper等文件,所以还需要添加下面等依赖。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.1</version>
</dependency>
@Mojo( name = "mybatis-generate",
defaultPhase = LifecyclePhase.PROCESS_SOURCES)
public class MyMojo extends AbstractMojo {
/**
* 要生成的表名.
*/
@Parameter(defaultValue = "${tables}", required = true)
private String tables;
/**
* 数据库连接
*/
@Parameter(defaultValue = "${url}", required = true)
private String url;
/**
* 驱动名称
*/
@Parameter(defaultValue = "${driverName}", required = true)
private String driverName;
/**
* 用户名称
*/
@Parameter(defaultValue = "${username}", required = true)
private String username;
/**
* 密码
*/
@Parameter(defaultValue = "${password}", required = true)
private String password;
/**
* 父包名
*/
@Parameter(defaultValue = "${parentModuleName}", required = true)
private String parentModuleName;
/**
* 密码
*/
@Parameter(defaultValue = "${author}", required = false)
private String author;
private static final String DEFAULT_AUTHOR = "SYSTEM GENERATE";
private Log log = getLog();
public void execute() throws MojoExecutionException {
if(this.isNullOrEmpty(tables)){
getLog().error("tables is null or empty");
throw new MojoExecutionException("tables is null or empty");
}
String[] tableArr = tables.split(",");
if(Objects.isNull(tableArr) || tableArr.length <= 0){
getLog().error("tables is null or empty");
throw new MojoExecutionException("tables is null or empty");
}
AutoGenerator mpg = new AutoGenerator();
//1、全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor(this.isNullOrEmpty(author) ? DEFAULT_AUTHOR : author);
gc.setOpen(false);
gc.setFileOverride(true);
gc.setServiceName("I%sService");
gc.setBaseResultMap(true);
mpg.setGlobalConfig(gc);
//2、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(url);
dsc.setDriverName(driverName);
dsc.setUsername(username);
dsc.setPassword(password);
mpg.setDataSource(dsc);
// 3、包配置
PackageConfig pc = new PackageConfig();
pc.setParent(parentModuleName);
mpg.setPackageInfo(pc);
// 4、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setInclude(tableArr);
mpg.setStrategy(strategy);
//5、执行
mpg.execute();
}
public boolean isNullOrEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
}
maven install到本地或者deploy到远程,然后在需要使用的项目pom.xml文件中添加依赖。
<plugins>
<plugin>
<groupId>org.example</groupId>
<artifactId>mybatisplus-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<tables>sp_user</tables>
<url>
jdbc:mysql://127.0.0.1:3306/myshop?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
</url>
<driverName>com.mysql.cj.jdbc.Driver</driverName>
<username>root</username>
<password>a123456</password>
<parentModuleName>com.lkb.myshop</parentModuleName>
<author>lkb</author>
</configuration>
</plugin>
</plugins>
这个插件很简陋,在很多地方可以完善,例如:数据库信息的获取方面,生成文件的选择方面等等。后续优化完成后再完善一波。
完整代码 请查看
源码链接
标签:mysql snapshot nap 自动 ide 工具 完整 param https
原文地址:https://www.cnblogs.com/catlkb/p/14632276.html