mybatis逆向工程配置文件怎么再偷懒(懒出天际)
使用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<attributes.length;i++){ 19 pstmt.setString(i+1,attributes[i]); 20 } 21 } 22 int res = pstmt.executeUpdate(); 23 return res; 24 } 25 26 public ResultSet executeQuery(String sql,String... attributes) throws SQLException { 27 System.out.println("sql executeQuery..."); 28 pstmt = this.con.prepareStatement(sql); 29 if (attributes!=null){ 30 for (int i = 0;i<attributes.length;i++){ 31 pstmt.setString(i+1,attributes[i]); 32 } 33 } 34 ResultSet resultSet = pstmt.executeQuery(); 35 return resultSet; 36 } 37 38 public void close() throws SQLException { 39 if (pstmt!=null){ 40 pstmt.close(); 41 } 42 if (con!=null){ 43 this.con.close(); 44 } 45 } 46 }
//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); List<String> tables = new ArrayList<String>(); while (resultSet.next()){ tables.add(resultSet.getString(1)); } sqlHelper.close(); generator(tables); } //对各张表进行操作 public void generator(List<String> 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<len;i++){ if (words[i]=='_'){ newWords[k] = (char)(words[i+1]-32); i++; k++; }else { newWords[k] = words[i]; k++; } } return new String(newWords); } //XML拼接方法 public String XMLGenerator(XMLFather xmlFather,XMLProperties xmlProperties){ StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("<table tableName=\""+xmlFather.getTableName()+"\" domainObjectName=\""+xmlFather.getDomainObjectName()+"\">"); if (xmlProperties.getUseActualColumnNames()!=null){ stringBuilder.append("<property name=\"useActualColumnNames\" value=\""+xmlProperties.getUseActualColumnNames()+"\"/>"); } stringBuilder.append("</table>"); return stringBuilder.toString(); } }
将控制台打印出来的东西copy到逆向工程配置文件里面即可