mybatis逆向工程配置文件怎么再偷懒(懒出天际)
生活随笔
收集整理的這篇文章主要介紹了
mybatis逆向工程配置文件怎么再偷懒(懒出天际)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用mybatis逆向工程時(shí),需要在逆向工程配置文件那里指定要對(duì)那些表進(jìn)行逆向工程,如果數(shù)據(jù)表很多的話,一個(gè)一個(gè)地寫(xiě)有點(diǎn)麻煩,為什么不自動(dòng)生成這些XML字段呢
(我的需求是,將數(shù)據(jù)表首字母大寫(xiě),然后下劃線去掉,再把下劃線的后一個(gè)字母大寫(xiě),如evaluation_data對(duì)應(yīng)的PO為EvaluationData)
首先連接數(shù)據(jù)庫(kù)
1 //獲取數(shù)據(jù)庫(kù)連接 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 //數(shù)據(jù)庫(kù)操作工具類 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配置文件的那個(gè)父標(biāo)簽 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配置文件的子標(biāo)簽,設(shè)置逆向工程屬性之類的 public class XMLProperties {private String useActualColumnNames;public String getUseActualColumnNames() {return useActualColumnNames;}public void setUseActualColumnNames(String useActualColumnNames) {this.useActualColumnNames = useActualColumnNames;} }
//主角來(lái)啦 public class MybatisGeneratorHelper {//主方法,獲取指定數(shù)據(jù)庫(kù)的所有數(shù)據(jù)表 @Testpublic 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);}//對(duì)各張表進(jìn)行操作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));}}//將數(shù)據(jù)庫(kù)的首字母大寫(xiě),并去掉下劃線,改為駝峰風(fēng)格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();} }
將控制臺(tái)打印出來(lái)的東西copy到逆向工程配置文件里面即可
?
轉(zhuǎn)載于:https://www.cnblogs.com/Libinkai/p/10417042.html
總結(jié)
以上是生活随笔為你收集整理的mybatis逆向工程配置文件怎么再偷懒(懒出天际)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 期货里的内盘和外盘有什么区别?
- 下一篇: 变动信息