Hello你們好,本章咱們集成generator自動生成model,xml,dao功能 。有問題能夠聯繫我mr_beany@163.com。另求各路大神指點,感謝
ps:最近這幾天在忙着搬家,更新的速度和回消息的速度可能不會特別及時,見諒java
因爲咱們上篇文章講過使用通用mapper,因此這裏咱們除了須要添加mybatis-generator
以外還須要添加通用mapper提供的generator
依賴,其次咱們還須要引入兩個經常使用jar包mysql
<!--代碼生成器-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-generator</artifactId>
<version>1.0.0</version>
</dependency>
<!--經常使用庫依賴 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>複製代碼
CREATE TABLE `system_log` (
`id` varchar(32) NOT NULL,
`description` varchar(50) DEFAULT NULL COMMENT '日誌信息描述',
`method` varchar(20) DEFAULT NULL COMMENT '方法名稱',
`log_type` varchar(10) DEFAULT NULL COMMENT '日誌類型 0是正常,1是異常',
`request_ip` varchar(30) DEFAULT NULL COMMENT '請求的ip',
`exception_code` varchar(50) DEFAULT NULL COMMENT '異常錯誤碼',
`exception_detail` varchar(255) DEFAULT NULL COMMENT '異常詳情',
`params` varchar(1000) DEFAULT NULL COMMENT '請求參數',
`user_id` varchar(32) DEFAULT NULL COMMENT '請求的用戶id',
`create_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系統日誌表';複製代碼
建立core→constant→ProjectConstantgit
package com.example.demo.core.constant;
public class ProjectConstant {
// 項目基礎包名稱
public static final String BASE_PACKAGE = "com.example.demo";
// Model所在包
public static final String MODEL_PACKAGE = BASE_PACKAGE + ".model";
// Mapper所在包
public static final String MAPPER_PACKAGE = BASE_PACKAGE + ".dao";
// Service所在包
public static final String SERVICE_PACKAGE = BASE_PACKAGE + ".service";
// ServiceImpl所在包
public static final String SERVICE_IMPL_PACKAGE = SERVICE_PACKAGE + ".impl";
// Controller所在包
public static final String CONTROLLER_PACKAGE = BASE_PACKAGE + ".controller";
// Mapper插件基礎接口的徹底限定名
public static final String MAPPER_INTERFACE_REFERENCE = BASE_PACKAGE + ".core.universal.Mapper";
}複製代碼
package com.example.demo;
import com.example.demo.core.constant.ProjectConstant;
import com.google.common.base.CaseFormat;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.util.ArrayList;
import java.util.List;
/**
* @Description: 代碼生成器,根據數據表名稱生成對應的Model、Mapper簡化開發。
* @author 張瑤
* @date 2018/4/23 20:28
*/
public class CodeGenerator {
// JDBC配置,請修改成你項目的實際配置
private static final String JDBC_URL = "jdbc:mysql://localhost:3333/demo";
private static final String JDBC_USERNAME = "root";
private static final String JDBC_PASSWORD = "123456";
private static final String JDBC_DIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
private static final String JAVA_PATH = "src/main/java"; // java文件路徑
private static final String RESOURCES_PATH = "src/main/resources";// 資源文件路徑
/**
* genCode("輸入表名","輸入自定義Model名稱");
* 若是想建立全部表,請輸入"%"
* @param args
*/
public static void main(String[] args) {
genCode("system_log");
}
/**
* 經過數據表名稱生成代碼,Model 名稱經過解析數據表名稱得到,下劃線轉大駝峯的形式。 如輸入表名稱 "t_user_detail" 將生成
* TUserDetail、TUserDetailMapper、TUserDetailService ...
*
* @param tableNames
* 數據表名稱...
*/
public static void genCode(String... tableNames) {
for (String tableName : tableNames) {
genCode(tableName, null);
}
}
/**
* 經過數據表名稱,和自定義的 Model 名稱生成代碼 如輸入表名稱 "t_user_detail" 和自定義的 Model 名稱 "User"
* 將生成 User、UserMapper、UserService ...
*
* @param tableName
* 數據表名稱
* @param modelName
* 自定義的 Model 名稱
*/
public static void genCode(String tableName, String modelName) {
genModelAndMapper(tableName, modelName);
}
public static void genModelAndMapper(String tableName, String modelName) {
Context context = getContext();
JDBCConnectionConfiguration jdbcConnectionConfiguration = getJDBCConnectionConfiguration();
context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
PluginConfiguration pluginConfiguration = getPluginConfiguration();
context.addPluginConfiguration(pluginConfiguration);
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = getJavaModelGeneratorConfiguration();
context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = getSqlMapGeneratorConfiguration();
context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
JavaClientGeneratorConfiguration javaClientGeneratorConfiguration =getJavaClientGeneratorConfiguration();
context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
TableConfiguration tableConfiguration = new TableConfiguration(context);
tableConfiguration.setTableName(tableName);
tableConfiguration.setDomainObjectName(modelName);
context.addTableConfiguration(tableConfiguration);
List<String> warnings;
MyBatisGenerator generator;
try {
Configuration config = new Configuration();
config.addContext(context);
config.validate();
boolean overwrite = true;
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
warnings = new ArrayList<>();
generator = new MyBatisGenerator(config, callback, warnings);
generator.generate(null);
} catch (Exception e) {
throw new RuntimeException("生成Model和Mapper失敗", e);
}
if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) {
throw new RuntimeException("生成Model和Mapper失敗:" + warnings);
}
if (StringUtils.isEmpty(modelName)){
modelName = tableNameConvertUpperCamel(tableName);
}
System.out.println(modelName + ".java 生成成功");
System.out.println(modelName + "Mapper.java 生成成功");
System.out.println(modelName + "Mapper.xml 生成成功");
}
private static Context getContext(){
Context context = new Context(ModelType.FLAT);
context.setId("Potato");
context.setTargetRuntime("MyBatis3Simple");
context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
return context;
}
private static JDBCConnectionConfiguration getJDBCConnectionConfiguration(){
JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
jdbcConnectionConfiguration.setConnectionURL(JDBC_URL);
jdbcConnectionConfiguration.setUserId(JDBC_USERNAME);
jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD);
jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME);
return jdbcConnectionConfiguration;
}
private static PluginConfiguration getPluginConfiguration(){
PluginConfiguration pluginConfiguration = new PluginConfiguration();
pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");
pluginConfiguration.addProperty("mappers", ProjectConstant.MAPPER_INTERFACE_REFERENCE);
return pluginConfiguration;
}
private static JavaModelGeneratorConfiguration getJavaModelGeneratorConfiguration(){
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
javaModelGeneratorConfiguration.setTargetProject(JAVA_PATH);
javaModelGeneratorConfiguration.setTargetPackage(ProjectConstant.MODEL_PACKAGE);
javaModelGeneratorConfiguration.addProperty("enableSubPackages","true");
javaModelGeneratorConfiguration.addProperty("trimStrings","true");
return javaModelGeneratorConfiguration;
}
private static SqlMapGeneratorConfiguration getSqlMapGeneratorConfiguration(){
SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
sqlMapGeneratorConfiguration.setTargetProject(RESOURCES_PATH);
sqlMapGeneratorConfiguration.setTargetPackage("mapper");
return sqlMapGeneratorConfiguration;
}
private static JavaClientGeneratorConfiguration getJavaClientGeneratorConfiguration(){
JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
javaClientGeneratorConfiguration.setTargetProject(JAVA_PATH);
javaClientGeneratorConfiguration.setTargetPackage(ProjectConstant.MAPPER_PACKAGE);
javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
return javaClientGeneratorConfiguration;
}
private static String tableNameConvertUpperCamel(String tableName) {
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName.toLowerCase());
}
}複製代碼
在CodeGenerator
中右鍵rungithub
ok,建立成功sql
碼雲地址: gitee.com/beany/mySpr…
apache
GitHub地址: github.com/MyBeany/myS…api
寫文章不易,如對您有幫助,請幫忙點下starbash
集成generator自動生成model,xml,dao功能已完成,後續功能接下來陸續更新,有問題能夠聯繫我mr_beany@163.com。另求各路大神指點,感謝你們。mybatis