MybatisCodeGenerator解析
程序员文章站
2022-07-01 19:53:06
...
github:https://github.com/Cukuyo/MybatisCodeGenerator
本项目是基于Jdk 1.8和maven开发的一款自动生成Mybatis Po层、dao层、mapper层、service层、serviceImpl层的工具,自动生成的代码则是我平常使用的编码习惯和规范,如果有问题或者发现BUG请提交一份Issue,如果你能赋予它更多的功能和更强的性能则更好
这个项目其实大部分都是苦力活,没什么技术含量,基本就是读取指定数据库数据表的字段,然后往不同文件写不同数据
唯一有点技术含量的就是读取数据库
连接
pom.xml加上mysql依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>LATEST</version>
</dependency>
代码里写上
/**
* 数据库连接对象
*/
private Connection connection;
/**
* SQL执行对象
*/
private Statement statement;
public void connect() throws SQLException {
//2018年10月14日:应用程序不再需要显式地加载JDBC驱动程序使用forname ()。
//现有的项目目前加载JDBC驱动程序使用forname()将继续没有修改
// try {
// //指定JDBC连接时调用的DriverManager,注意:
// //1、不要导入com.mysql.cj.jdbc.*,否则你会有麻烦的!
// //2、newInstance()调用是针对一些损坏的Java实现的解决方案
// Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
// } catch (Exception e) {
// e.printStackTrace();
// throw new SQLException(e.getMessage());
// }
String url = "jdbc:mysql://" + ip + ":" + port;
connection = DriverManager.getConnection(url, user, password);
statement = connection.createStatement();
查询有哪些数据库
这里直接用查sql的方式,不走驱动的api
public List<String> getDatabases() throws SQLException {
ResultSet resultSet = statement.executeQuery("SELECT SCHEMA_NAME FROM information_schema.SCHEMATA");
List<String> dbNameList = new LinkedList<>();
while (resultSet.next()) {
dbNameList.add(resultSet.getString("SCHEMA_NAME"));
}
resultSet.close();
return dbNameList;
}
查询库下有哪些表
也是直接走sql
public List<Table> getTables(String dbName) throws SQLException {
ResultSet resultSet = statement.executeQuery(
"SELECT TABLE_NAME,TABLE_COMMENT " +
"FROM information_schema.TABLES " +
"WHERE TABLE_SCHEMA=" + '\'' + dbName + '\'');
List<Table> tableList = new LinkedList<>();
while (resultSet.next()) {
Table table = new Table();
.....................
tableList.add(table);
}
resultSet.close();
return tableList;
}
查询表的字段属性
走sql
public List<Column> getColumns(String dbName, String tableName) throws SQLException {
ResultSet resultSet = statement.executeQuery(
"SELECT COLUMN_NAME,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT " +
"FROM information_schema.COLUMNS " +
"WHERE TABLE_SCHEMA=" + '\'' + dbName + '\'' + " AND TABLE_NAME=" + '\'' + tableName + '\'');
List<Column> columnList = new LinkedList<Column>();
while (resultSet.next()) {
Column column = new Column();
...............
columnList.add(column);
}
resultSet.close();
return columnList;
}
导出mybatis代码
导出
FileOutputStream fileOutputStream = new FileOutputStream(path);
OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, ExportInfo.CHARSET_NAME);
writer.write();
writer.write();
.................//老夫就一个字,写!
writer.close();
踩的坑
1、获取到字段的属性了,那么得知道它对应的java类型,这是个苦力活,不信你瞧
https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-type-conversions.html和JdbcTypeSearch这个类,全是一个字一个字敲的
2、写dao层你会发现需要po层的包名,写service层需要dao层和po层的包名,写serviceImpl层需要dao、service、po三层的包名,类似的坑还有一两个,所以我索性全放在一个ExportInfo里,然后传给每一个导出的函数,你看
.........................................................
..........................
private String poPackageName;
private String poClassName;
private String poPackageClassName;
private String poVariableName;
private String poFileName;
private String daoPackageName;
private String daoClassName;
private String daoPackageClassName;
private String daoVariableName;
private String daoFileName;
private String servicePackageName;
private String serviceClassName;
private String servicePackageClassName;
private String serviceVariableName;
private String serviceFileName;
private String serviceImplPackageName;
private String serviceImplClassName;
private String serviceImplPackageClassName;
private String serviceImplVariableName;
private String serviceImplFileName
...........................................
........................