日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

mybatis逆向工程配置文件怎么再偷懒(懒出天际)

發布時間:2023/11/27 生活经验 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 {//主方法,獲取指定數據庫的所有數據表
    @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);}//對各張表進行操作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到逆向工程配置文件里面即可

?

轉載于:https://www.cnblogs.com/Libinkai/p/10417042.html

總結

以上是生活随笔為你收集整理的mybatis逆向工程配置文件怎么再偷懒(懒出天际)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。