博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis逆向工程配置文件怎么再偷懒(懒出天际)
阅读量:4639 次
发布时间:2019-06-09

本文共 3702 字,大约阅读时间需要 12 分钟。

使用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);        List
tables = 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到逆向工程配置文件里面即可

 

转载于:https://www.cnblogs.com/Libinkai/p/10417042.html

你可能感兴趣的文章
ef linq 中判断实体中是否包含某集合
查看>>
章三 链表
查看>>
Solution for Concurrent number of AOS' for this application exceeds the licensed number
查看>>
CSE 3100 Systems Programming
查看>>
IntelliJ IDEA 的Project structure说明
查看>>
Java Security(JCE基本概念)
查看>>
Linux Supervisor的安装与使用入门
查看>>
创建 PSO
查看>>
JasperReport报表设计4
查看>>
项目活动定义 概述
查看>>
团队冲刺04
查看>>
我的Python分析成长之路8
查看>>
泛型在三层中的应用
查看>>
SharePoint2010 -- 管理配置文件同步
查看>>
.Net MVC3中取得当前区域的名字(Area name)
查看>>
获得屏幕像素以及像素密度
查看>>
int与string转换
查看>>
adb命令 判断锁屏
查看>>
推荐一个MacOS苹果电脑系统解压缩软件
查看>>
1035等差数列末项计算
查看>>