原文地址: https://www.cnblogs.com/Jeffscnblog/p/10072483.htmlhtml
通常上,咱們會使用數據庫錶轉換爲javabean、dao、或是mapper,就叫逆向工程。作項目時通常也是先設計數據庫,再進行系統開發,因此通常使用逆向工程。java
但我這邊因爲工做臨時的須要,須要將javabean轉換爲建表語句和Mybatis的Mapper文件,因而上網搜了一下,大部分是作一個工具類進行bean解析輸出的。sql
根據自身項目命名設計要求,將javabean小駝峯命名法轉換爲數據庫表字段下劃線命名法,簡化一個例子以下,供參考數據庫
package com.util; import java.io.IOException; import java.lang.reflect.Field; /** * Created by Jeff on 2018/12/5. */ public class GenerateSqlMapperUtil { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { generateSqlMapper(com.entity.Test.class, null); } public static void generateSqlMapper(Class obj,String tableName) throws IOException{ Field[] fields = obj.getDeclaredFields(); String param = null; String cameCaseColumn = null; String underScoreCaseColumn = null; StringBuilder sql = new StringBuilder(); if(tableName==null||tableName.equals("")){ // 未傳代表默認用類名 tableName = obj.getName().substring(obj.getName().lastIndexOf(".")+1); } /** * 如下部分生成建表Sql */ sql.append("drop table if exists ").append(tableName).append(";\r\n"); sql.append("create table ").append(tableName).append("( \r\n"); boolean firstId = true; for(Field f : fields){ cameCaseColumn = f.getName(); underScoreCaseColumn = cameCaseColumn; for(int i = 0; i < cameCaseColumn.length(); i++) if(Character.isUpperCase(cameCaseColumn.charAt(i))) // 將javabean中小駝峯命名變量的「大寫字母」轉換爲「_小寫字母」 underScoreCaseColumn = cameCaseColumn.substring(0, i) + '_' + cameCaseColumn.substring(i, i+1).toLowerCase() + cameCaseColumn.substring(i+1, cameCaseColumn.length()); sql.append(underScoreCaseColumn).append(" "); param = f.getType().getTypeName(); if(param.equals("java.lang.Integer")){ sql.append("INTEGER"); }else{ // 根據須要自行修改 sql.append("VARCHAR(20)"); } if(firstId){ // 默認第一個字段爲ID主鍵 sql.append(" PRIMARY KEY AUTO_INCREMENT"); firstId = false; } sql.append(",\n"); } sql.delete(sql.lastIndexOf(","), sql.length()).append("\n)ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;\r\n"); System.out.println(sql); /** * 如下部分生成Mapper */ firstId = true; StringBuilder mapper = new StringBuilder(); StringBuilder resultMap = new StringBuilder(); StringBuilder insert = new StringBuilder(); StringBuilder insertValues = new StringBuilder(); StringBuilder update = new StringBuilder(); StringBuilder updateWhere = new StringBuilder(); StringBuilder delete = new StringBuilder(); StringBuilder deleteWhere = new StringBuilder(); mapper.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"); mapper.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\r\n"); mapper.append("<mapper namespace=\"com..mapper.").append(tableName).append("Mapper\">\r\n"); resultMap.append(" <resultMap id=\"Base").append(tableName).append("\" type=\"").append(obj.getName()).append("\">\r\n"); insert.append(" <insert id=\"save").append(tableName).append("\" parameterType=\"").append(obj.getName()).append("\">\r\n"); insert.append(" INSERT INTO ").append(tableName.toLowerCase()).append(" ("); update.append(" <update id=\"update").append(tableName).append("\" parameterType=\"").append(obj.getName()).append("\">\r\n"); update.append(" UPDATE ").append(tableName.toLowerCase()).append(" SET "); delete.append(" <delete id=\"delete").append(tableName).append("\" parameterType=\"int\">\r\n"); delete.append(" DELETE FROM ").append(tableName.toLowerCase()); for(Field f : fields){ cameCaseColumn = f.getName(); underScoreCaseColumn = cameCaseColumn; for(int i = 0; i < cameCaseColumn.length(); i++) if(Character.isUpperCase(cameCaseColumn.charAt(i))) // 將javabean中小駝峯命名變量的「大寫字母」轉換爲「_小寫字母」 underScoreCaseColumn = cameCaseColumn.substring(0, i) + '_' + cameCaseColumn.substring(i, i+1).toLowerCase() + cameCaseColumn.substring(i+1, cameCaseColumn.length()); resultMap.append(" "); if(firstId){ resultMap.append("<id column=\"").append(underScoreCaseColumn).append("\" property=\"").append(cameCaseColumn).append("\" jdbcType=\""); updateWhere.append(" WHERE ").append(underScoreCaseColumn).append(" = #{").append(cameCaseColumn).append("}\r\n"); deleteWhere.append(" WHERE ").append(underScoreCaseColumn).append(" = #{").append(cameCaseColumn).append("}\r\n"); firstId = false; } else { resultMap.append("<result column=\"").append(underScoreCaseColumn).append("\" property=\"").append(cameCaseColumn).append("\" jdbcType=\""); insert.append(underScoreCaseColumn).append(", "); insertValues.append("#{").append(cameCaseColumn).append("},"); update.append(underScoreCaseColumn).append(" = #{").append(cameCaseColumn).append("}, "); } param = f.getType().getTypeName(); if(param.equals("java.lang.Integer")){ resultMap.append("INTEGER\" />\r\n"); }else{ // 根據須要自行修改 resultMap.append("VARCHAR\" />\r\n"); } } resultMap.append(" </resultMap>\r\n"); insert.delete(insert.lastIndexOf(","), insert.length()).append(")\r\n"); insertValues.delete(insertValues.lastIndexOf(","), insertValues.length()); insert.append(" VALUES ("); insert.append(insertValues).append(")\r\n"); insert.append(" </insert>\r\n"); update.delete(update.lastIndexOf(","), update.length()).append("\r\n"); update.append(updateWhere); update.append(" </update>\r\n"); delete.append(deleteWhere); delete.append(" </delete>\r\n"); mapper.append(resultMap).append(insert).append(update).append(delete); mapper.append("</mapper>"); System.out.println(mapper); } }