代码有些乱,将就能用
生成model效果如下:
package com.core.model;
import com.jfinal.ext.plugin.db.BaseModel;
import com.jfinal.ext.plugin.db.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Db;
@TableBind(tableName = "pdf_log" , pkName = PdfLog._ID)
public class PdfLog extends BaseModel<PdfLog>{
public static final PdfLog dao = new PdfLog();
public static final String LOGID = "logid";
public static final String LOGUNIQUE = "logunique";
public static final String REQUNIQUE = "requnique";
public static final String REQURL = "requrl";
public static final String BACKUPURL = "backupurl";
public static final String SAVEURL = "saveurl";
public static final String FILENAME = "filename";
public static final String LOGSTATUS = "logstatus";
public static final String LOGTIME = "logtime";
public static final String LOGMSG = "logmsg";
public static final String FILEMODIFIED = "filemodified";
public static final String FILESIZE = "filesize";
public static int[] batchInsert(List<PdfLog> list , int size){
return Db.batch("insert into pdf_log ("+LOGID+","+LOGUNIQUE+","+REQUNIQUE+","+REQURL+","+BACKUPURL+","+SAVEURL+","+FILENAME+","+LOGSTATUS+","+LOGTIME+","+LOGMSG+","+FILEMODIFIED+",+"+FILESIZE+") values (?,?,?,?,?,?,?,?,?,?,?,?)", LOGID+","+LOGUNIQUE+","+REQUNIQUE+","+REQURL+","+BACKUPURL+","+SAVEURL+","+FILENAME+","+LOGSTATUS+","+LOGTIME+","+LOGMSG+","+FILEMODIFIED+","+FILESIZE,
list,
size);
}
public PdfLog setLogid(Object value){
return this.set(LOGID,value);
}
public <T> T getLogid(){
return this.get(LOGID);
}
public PdfLog setLogunique(Object value){
return this.set(LOGUNIQUE,value);
}
public <T> T getLogunique(){
return this.get(LOGUNIQUE);
}
public PdfLog setRequnique(Object value){
return this.set(REQUNIQUE,value);
}
public <T> T getRequnique(){
return this.get(REQUNIQUE);
}
public PdfLog setRequrl(Object value){
return this.set(REQURL,value);
}
public <T> T getRequrl(){
return this.get(REQURL);
}
public PdfLog setBackupurl(Object value){
return this.set(BACKUPURL,value);
}
public <T> T getBackupurl(){
return this.get(BACKUPURL);
}
public PdfLog setSaveurl(Object value){
return this.set(SAVEURL,value);
}
public <T> T getSaveurl(){
return this.get(SAVEURL);
}
public PdfLog setFilename(Object value){
return this.set(FILENAME,value);
}
public <T> T getFilename(){
return this.get(FILENAME);
}
public PdfLog setLogstatus(Object value){
return this.set(LOGSTATUS,value);
}
public <T> T getLogstatus(){
return this.get(LOGSTATUS);
}
public PdfLog setLogtime(Object value){
return this.set(LOGTIME,value);
}
public <T> T getLogtime(){
return this.get(LOGTIME);
}
public PdfLog setLogmsg(Object value){
return this.set(LOGMSG,value);
}
public <T> T getLogmsg(){
return this.get(LOGMSG);
}
public PdfLog setFilemodified(Object value){
return this.set(FILEMODIFIED,value);
}
public <T> T getFilemodified(){
return this.get(FILEMODIFIED);
}
public PdfLog setFilesize(Object value){
return this.set(FILESIZE,value);
}
public <T> T getFilesize(){
return this.get(FILESIZE);
}
}
主程序如下:
package com.gen;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.beetl.core.BeetlKit;
import org.junit.Test;
import com.google.common.base.Splitter;
import com.jfinal.common.IOKit;
import com.jfinal.kit.PathKit;
public class CreateModel {
@Test
public void createAll() throws Exception{
List<String> tables = DBConn.getTableNamesByDBName();
for (String table : tables) {
createTable(table);
}
System.out.println("生成model成功!.请查看");
}
@Test
public void createTable() throws Exception{
String tables = "pdf_log";
Iterable<String> strs = Splitter.on(',').split(tables);
for (String object : strs) {
createTable(object);
}
}
private void createTable(String table) throws Exception{
TableMeta tableMeta = new TableMeta();
tableMeta.setPackageName(DBConn.p.getProperty("package"));
Map<String, Object> map = new HashMap<String,Object>();
tableMeta.setTableName(table);
tableMeta.setModelName(toTablName(table));
tableMeta.setColumnsNames(DBConn.getColumnsNamesByTableName(table));
map.put("myModel", tableMeta);
File createFolder = new File("c:/"+DBConn.p.getProperty("package").replace(".", "/"));
createFolder.mkdirs(); //预先创建文件夹,预防没有文件夹而找不到路径
String string = IOKit.readToString(new File(PathKit.getRootClassPath()+"/com/gen/model.btl"));
System.out.println(tableMeta.getPrefix());
BeetlKit.renderTo(string, new FileWriter(new File(createFolder+"/"+tableMeta.getModelName()+".java")), map);
}
private static String toTablName(String str1) {
String str = toUpperCaseTheFristChar(str1);
int x = str.indexOf("_");
if(x != -1){
String z = toUpperCaseTheFristChar(str.substring(x+1));
return str.substring(0,x)+z;
}
return str;
}
public static String toUpperCaseTheFristChar(String str) {
byte[] items = str.getBytes();
items[0] = (byte) ((char) items[0] - 'a' + 'A');
return new String(items);
}
}
获取表信息类
package com.gen;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.jfinal.kit.StrKit;
import com.mysql.jdbc.Statement;
public class DBConn {
public static final Properties p = new Properties();
static {
try {
p.load(DBConn.class.getResourceAsStream("/gen.txt"));
Class.forName(p.getProperty("className"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws IOException, SQLException {
return DriverManager.getConnection(p.getProperty("driverName"),
p.getProperty("userName"), p.getProperty("userPassword"));
}
public static List<String> getTableNamesByDBName() throws SQLException,
IOException {
Statement stame = (Statement) DBConn.getConnection().createStatement();
ResultSet rs = stame.executeQuery("show tables;");
List<String> list = new ArrayList<String>();
while (rs.next()) {
list.add(rs.getString(1));
}
return list;
}
public static List<Column> getColumnsNamesByTableName(String tName) throws SQLException, IOException{
List<Column> list = new ArrayList<Column>();
Statement stame = (Statement) DBConn.getConnection().createStatement();
ResultSet rs = stame.executeQuery("desc "+tName+";");
while (rs.next()) {
Column col = new Column();
col.columnName = rs.getString(1);
col.columnName2 = StrKit.firstCharToUpperCase(col.columnName);
list.add(col);
}
return list;
}
}
记录表信息的bean对象
package com.gen;
import java.util.List;
public class TableMeta {
private String modelName;
private String packageName;
private String tableName;
private List<Column> columnsNames;
private String prefix;
public String getModelName() {
return modelName;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public void setModelName(String modelName) {
this.modelName = modelName;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public List<Column> getColumnsNames() {
return columnsNames;
}
public void setColumnsNames(List<Column> columnsNames) {
this.columnsNames = columnsNames;
int x = columnsNames.get(0).columnName.indexOf("_");
if(x != -1){
setPrefix(columnsNames.get(0).columnName.substring(0,x));
}
}
}
class Column{
public String columnName;
public String columnName2;
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getColumnName2() {
return columnName2;
}
public void setColumnName2(String columnName2) {
this.columnName2 = columnName2;
}
}
beetl模板文件:
package ${myModel.packageName};
import com.jfinal.ext.plugin.db.BaseModel;
import com.jfinal.ext.plugin.db.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Db;
import java.util.List;
@TableBind(tableName = "${myModel.tableName}" , pkName = ${myModel.modelName}.${strutil.toUpperCase(myModel.prefix==null?"")}_ID)
public class ${myModel.modelName} extends BaseModel<${myModel.modelName}>{
public static final ${myModel.modelName} dao = new ${myModel.modelName}();
<%
for(col in myModel.columnsNames){
%>
public static final String ${strutil.toUpperCase(col.columnName)} = "${strutil.toLowerCase(col.columnName)}";
<%
}
%>
public static int[] batchInsert(List<${myModel.modelName}> list , int size){
<%
var fieldKeys='';
var fieldKeys2='';
var fieldVals='';
var x= 0;
for(col in myModel.columnsNames){
if(colLP.index>1){
fieldVals=fieldVals+',';
fieldKeys=fieldKeys+',';
fieldKeys2=fieldKeys2+'+'+'"'+','+'"'+'+';
}
if(colLP.size == colLP.index){
fieldKeys=fieldKeys+'+';
}
fieldKeys=fieldKeys+'"+'+strutil.toUpperCase(col.columnName)+'+"';
fieldVals=fieldVals+'?';
fieldKeys2=fieldKeys2+strutil.toUpperCase(col.columnName);
}
%>
return Db.batch("insert into ${myModel.tableName} (${ fieldKeys}) values (${fieldVals})", ${fieldKeys2},
list,
size);
}
<%
for(col in myModel.columnsNames){
%>
public ${myModel.modelName} set${col.columnName2}(Object value){
return this.set(${strutil.toUpperCase(col.columnName)},value);
}
public <T> T get${col.columnName2}(){
return this.get(${strutil.toUpperCase(col.columnName)});
}
<%
}
%>
}
数据库配置文件:
package=com.core.model
className=com.mysql.jdbc.Driver
driverName=jdbc:mysql://127.0.0.1:3382/iehyou?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
userName=root
userPassword=