使用MyBatis Generator自动生成代码,提高开发效率
package com.ideabobo;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import com.alibaba.fastjson.JSON;
import com.ideabobo.util.Common;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.DatabaseMetaData;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* 使用MyBatis Generator自动生成代码示例
*/
public class StartGenerator {
public static void main(String[] args) {
createMappingTables();
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
String genCfg = '/generatorConfig.xml';
File configFile = new File(StartGenerator.class.getResource(genCfg)
.getFile());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
try {
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
try {
myBatisGenerator.generate(null);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 创建数据库表映射
*/
public static void createMappingTables() {
Connection con = null;
try {
// 使用properties对象加载输入流
// 获取key对应的value值
String driver = Common.getProperty('spring.datasource.driver-class-name');// 'com.mysql.jdbc.Driver';
String url = Common.getProperty('spring.datasource.url');// 'jdbc:mysql://localhost:3306/samp_db';
String username = Common.getProperty('spring.datasource.username'); // 'root';
String password = Common.getProperty('spring.datasource.password');// '';
Class.forName(driver); // classLoader,加载对应驱动
con = (Connection) DriverManager.getConnection(url, username,
password);
} catch (Exception e) {
e.printStackTrace();
}
try {
DatabaseMetaData meta = (DatabaseMetaData) con.getMetaData();
ResultSet rs = meta.getTables(null, null, null,
new String[] { 'TABLE' });
ArrayList<String> tables = new ArrayList<>();
while (rs.next()) {
String tableName = rs.getString(3);
tables.add(tableName);
System.out.println('表名:' + rs.getString(3));
//System.out.println('表所属用户名:' + rs.getString(2));
System.out.println('------------------------------');
}
con.close();
String filePackage = Common.getProperty('tablemappingpackage');
String servicePackage = Common.getProperty('servicemappingpackage').replaceAll('\.', '\\')+'\\';
String packagePath = System.getProperty('user.dir')+'\\src\\main\\java\\'+filePackage.replaceAll('\.', '\\')+'\\';
String servicePackagePath = System.getProperty('user.dir')+'\\src\\main\\java\\'+servicePackage;
String cfilepath = 'Dbtablemapping.java';
String path = packagePath+cfilepath;
File file = new File(path);
File packageDir = new File(packagePath);
File serviceDir = new File(servicePackagePath);
if(packageDir.exists()){
if(packageDir.isDirectory()){
File[] modelFiles = packageDir.listFiles();
for(File mfile:modelFiles){
if(mfile.getName().equals('Dbtablemapping.java')||mfile.getName().equals('Dbservice.java')){
continue;
}else{
mfile.delete();
}
}
}
}
if(serviceDir.exists()){
if(serviceDir.isDirectory()){
File[] modelFiles = serviceDir.listFiles();
for(File mfile:modelFiles){
if(mfile.getName().equals('DatabaseService.java')){
continue;
}else{
mfile.delete();
}
}
}
}
StringBuilder sb = new StringBuilder();
sb.append('package com.ideabobo.model;\r\n');
sb.append('import com.alibaba.fastjson.JSON;\r\n');
sb.append('public class Dbtablemapping {\r\n');
sb.append(' public static Object parseStringModel(String value, String table) {\r\n');
sb.append(' Object object = null;\r\n');
sb.append(' switch (table) {\r\n');
//sb.append('');//拼装
for(String table:tables){
String caseStr = ' case \''+table+'\': object = JSON.parseObject(value, '+getModeNameByTable(table)+'.class); break;\r\n';
sb.append(caseStr);
}
sb.append(' }\r\n');
sb.append(' return object;\r\n');
sb.append('}\r\n');
sb.append('public static Object getModelByTable(String table) {\r\n');
sb.append(' Object object = null;\r\n');
sb.append(' switch (table) {\r\n');
for(String table:tables){
String caseStr = ' case \''+table+'\': object = new '+getModeNameByTable(table)+'(); break;\r\n';
sb.append(caseStr);
}
sb.append(' }\r\n');
sb.append(' return object;\r\n');
sb.append(' }\r\n');
sb.append('}\r\n');
if(!file.exists()){
System.out.println(file.getAbsolutePath());
file.createNewFile();
}
Common.str2File(file.getAbsolutePath(), sb.toString());
String mappingfilepath = System.getProperty('user.dir')+'\\src\\main\\resources\\mapper\\';
File mfile = new File(mappingfilepath);
if(mfile.exists() && mfile.isDirectory()){
File[] files = mfile.listFiles();
for(File mapperfile:files){
String mapfilename = mapperfile.getName();
if(!mapfilename.equals('DatabaseMapper.xml')){
mapperfile.delete();
}
}
}
} catch (Exception e) {
try {
con.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 根据表名获取模型名称
* @param tableName 表名
* @return 模型名称
*/
public static String getModeNameByTable(String tableName){
String tablePre = Common.getProperty('tableprefix');
String modelName = tableName;
if(tablePre!=null && !tablePre.equals('')){
modelName = modelName.replaceAll(tablePre, '');
}
modelName = Common.toUpperCaseFirstOne(modelName);
return modelName;
}
}
代码解释:
这段Java代码使用MyBatis Generator来自动生成MyBatis项目所需的代码,包括:
- 读取数据库表结构信息: 连接数据库,获取所有数据表的名称。
- 生成实体类: 根据表名和配置信息,生成对应的Java实体类,例如User.java。
- 生成Mapper接口: 生成操作数据库的Mapper接口,例如UserMapper.java。
- 生成XML映射文件: 生成SQL语句与Java代码之间的映射文件,例如UserMapper.xml。
代码优势:
- 提高开发效率: 自动生成代码,减少手动编写代码的工作量,提高开发效率。
- 减少出错率: 自动生成的代码结构规范,减少手动编写代码可能出现的错误。
- 易于维护: 修改数据库表结构后,只需重新运行代码生成工具,即可更新相关代码,方便维护。
使用方法:
- 配置
generatorConfig.xml文件,包括数据库连接信息、要生成的表名等。 - 运行
StartGenerator类的main方法。 - 生成的代码会输出到指定目录。
总结:
使用MyBatis Generator可以极大地简化MyBatis项目的开发流程,提高开发效率和代码质量。
原文地址: https://www.cveoy.top/t/topic/jn0O 著作权归作者所有。请勿转载和采集!