使用mybatis逆向工程时,需要在逆向工程配置文件那里指定要对那些表进行逆向工程,如果数据表很多的话,一个一个地写有点麻烦,为什么不自动生成这些XML字段呢
(我的需求是,将数据表首字母大写,然后下划线去掉,再把下划线的后一个字母大写,如evaluation_data对应的PO为EvaluationData)
首先连接数据库
1 //获取数据库连接 2 public class JdbcConnection { 3 private static String DRIVER = "com.mysql.jdbc.Driver"; 4 private static String URL = "jdbc:mysql://localhost:3306/mates_db"; 5 private static String USERNAME = "root"; 6 private static String PASSWORD = "123456"; 7 8 public static Connection getConnection(){ 9 try {10 Class.forName(DRIVER);11 return DriverManager.getConnection(URL,USERNAME,PASSWORD);12 } catch (ClassNotFoundException e) {13 e.printStackTrace();14 } catch (SQLException e) {15 e.printStackTrace();16 }17 return null;18 }19 }
1 //数据库操作工具类 2 public class SqlHelper { 3 private Connection con = null; 4 5 private PreparedStatement pstmt = null; 6 7 public SqlHelper() { 8 } 9 10 public SqlHelper(Connection con) {11 this.con = con;12 }13 14 public int executeUpdate(String sql, String... attributes) throws SQLException {15 System.out.println("sql executeUpdate...");16 pstmt = this.con.prepareStatement(sql);17 if (attributes!=null){18 for (int i = 0;i
//XML配置文件的那个父标签public class XMLFather { private String tableName; private String domainObjectName; public String getTableName() { return tableName; } public void setTableName(String tableName) { this.tableName = tableName; } public String getDomainObjectName() { return domainObjectName; } public void setDomainObjectName(String domainObjectName) { this.domainObjectName = domainObjectName; }}
//XML配置文件的子标签,设置逆向工程属性之类的public class XMLProperties { private String useActualColumnNames; public String getUseActualColumnNames() { return useActualColumnNames; } public void setUseActualColumnNames(String useActualColumnNames) { this.useActualColumnNames = useActualColumnNames; }}
//主角来啦public class MybatisGeneratorHelper { //主方法,获取指定数据库的所有数据表 @Test public void main() throws SQLException, ClassNotFoundException { SqlHelper sqlHelper = new SqlHelper(JdbcConnection.getConnection()); String sql = "SHOW TABLES"; ResultSet resultSet = sqlHelper.executeQuery(sql,null); Listtables = new ArrayList (); while (resultSet.next()){ tables.add(resultSet.getString(1)); } sqlHelper.close(); generator(tables); } //对各张表进行操作 public void generator(List tables){ for (String table : tables){ XMLFather xmlFather = new XMLFather(); xmlFather.setTableName(table); xmlFather.setDomainObjectName(tableNameTranslate(table)); XMLProperties xmlProperties = new XMLProperties(); xmlProperties.setUseActualColumnNames("true"); System.out.println(XMLGenerator(xmlFather,xmlProperties)); } } //将数据库的首字母大写,并去掉下划线,改为驼峰风格 public String tableNameTranslate(String tableName){ char[] newWords = new char[tableName.length()]; char[] words = tableName.toCharArray(); newWords[0] = (char) (words[0]-32); for (int i = 1,len = words.length,k=1;i "); if (xmlProperties.getUseActualColumnNames()!=null){ stringBuilder.append(" "); } stringBuilder.append(""); return stringBuilder.toString(); }}
将控制台打印出来的东西copy到逆向工程配置文件里面即可