欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jfinal 生成实体类

程序员文章站 2022-06-14 23:06:02
...
jfinal根据Generator生成器生成实体类:
package com.ysd.test;
import javax.sql.DataSource;

import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.generator.Generator;
import com.jfinal.plugin.druid.DruidPlugin;
import com.ysd.entity.ImsChatRoomcashsumLast;

public class _JFinalDemoGenerator {
	public static DataSource getDataSource() {
		//PropKit.use("a_little_config.txt");
		DruidPlugin druidPlugin = new DruidPlugin("jdbc:mysql://47.94.172.202/phpweizan", "temp1", "asdfghjkl");
		druidPlugin.start();
		ActiveRecordPlugin activeRecord = new ActiveRecordPlugin(druidPlugin);
		activeRecord.addMapping("ims_chat_roomcashsum_last", "Id",ImsChatRoomcashsumLast.class);
		//activeRecord.addMapping("Askers", "userId",Askers.class);
		
		return druidPlugin.getDataSource();
	}
	
	public static void main(String[] args) {
		// base model 所使用的包名
		String baseModelPackageName = "com.ysd.entity";
		// base model 文件保存路径
		String baseModelOutputDir = PathKit.getWebRootPath() + "/src/main/java/com/ysd/entity";
		
		// model 所使用的包名 (MappingKit 默认使用的包名)
		String modelPackageName = "com.ysd.entity";
		// model 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径)
		String modelOutputDir = PathKit.getWebRootPath() + "/src/main/java/com/ysd/entity";
		
		
		// 创建生成器
		//Generator generator = new Generator(getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir);
		//generator.setMetaBuilder(new _MetaBuilder(getDataSource()));
		// 设置是否生成链式 setter 方法
		//generator.setGenerateChainSetter(false);
		// 添加不需要生成的表名
		//generator.addExcludedTable("ims_chat_roomcashsum_last");
		
		// 设置是否在 Model 中生成 dao 对象
		//generator.setGenerateDaoInModel(true);
		// 设置是否生成链式 setter 方法
		//generator.setGenerateChainSetter(true);
		// 设置是否生成字典文件
		//generator.setGenerateDataDictionary(false);
		// 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user",移除前缀 "osc_"后生成的model名为 "User"而非 OscUser
		//generator.setRemovedTableNamePrefixes("t_");
		// 生成
		//generator.generate();
	}
}

jfinal不依靠Generator生成单独实体类

package com.ysd.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

public class reflectBean {
	private Connection connection;
	private PreparedStatement UserQuery;
	/*mysql url的连接字符串*/
	private static String url = "jdbc:mysql://47.94.172.202/phpweizan?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true";
	//账号
	private static String user = "temp1";
	//密码
	private static String password = "asdfghjkl";
	private Vector<String> vector = new Vector<String>();
	//mysql jdbc的java包驱动字符串
	private String driverClassName = "com.mysql.jdbc.Driver";
	//数据库中的表名
	String table = "core_admin";
	//数据库的列名称
	private String[] colnames; // 列名数组
	//列名类型数组  
	private String[] colTypes;
	public reflectBean(){
		try {//驱动注册
			Class.forName(driverClassName);
			if (connection == null || connection.isClosed())
				//获得链接
				connection = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException ex) {
				ex.printStackTrace();
				System.out.println("Oh,not");
			} catch (SQLException e) {
				e.printStackTrace();
				System.out.println("Oh,not");
			}
	}
	
	public Connection getConnection() {
		return connection;
	}
	public void setConnection(Connection connection) {
		this.connection = connection;
	}
 
	public void doAction(){
		String sql = "select * from "+table;
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			//获取数据库的元数据 
			ResultSetMetaData metadata = statement.getMetaData();
			//数据库的字段个数
			int len = metadata.getColumnCount();
			//字段名称
			colnames = new String[len+1];
			//字段类型 --->已经转化为java中的类名称了
			colTypes = new String[len+1];
			for(int i= 1;i<=len;i++){
				//System.out.println(metadata.getColumnName(i)+":"+metadata.getColumnTypeName(i)+":"+sqlType2JavaType(metadata.getColumnTypeName(i).toLowerCase())+":"+metadata.getColumnDisplaySize(i));
				//metadata.getColumnDisplaySize(i);
				colnames[i] = metadata.getColumnName(i); //获取字段名称
				colTypes[i] = sqlType2JavaType(metadata.getColumnTypeName(i)); //获取字段类型	
				System.out.println(sqlType2JavaType(metadata.getColumnTypeName(i)));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	/*
	 * mysql的字段类型转化为java的类型*/
	private String sqlType2JavaType(String sqlType) {  
        if(sqlType.equalsIgnoreCase("bit")){  
            return "boolean";  
        }else if(sqlType.equalsIgnoreCase("tinyint")){  
            return "byte";  
        }else if(sqlType.equalsIgnoreCase("smallint")){  
            return "short";  
        }else if(sqlType.equalsIgnoreCase("int")){  
            return "Integer";  
        }else if(sqlType.equalsIgnoreCase("bigint")){  
            return "long";  
        }else if(sqlType.equalsIgnoreCase("float")){  
            return "float";  
        }else if(sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric")   
                || sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money")   
                || sqlType.equalsIgnoreCase("smallmoney")){  
            return "double";  
        }else if(sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char")   
                || sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar")   
                || sqlType.equalsIgnoreCase("text")){  
            return "String";  
        }else if(sqlType.equalsIgnoreCase("datetime") ||sqlType.equalsIgnoreCase("date")){  
            return "Date";  
        }else if(sqlType.equalsIgnoreCase("image")){  
            return "Blod";  
        }  
          
        return null;  
    }
	/*获取整个类的字符串并且输出为java文件
	 * */
	public  StringBuffer getClassStr(){
		//输出的类字符串
		StringBuffer str = new StringBuffer("");
		//获取表类型和表名的字段名
		this.doAction();
		//校验
		if(null == colnames && null == colTypes) return null;
		//拼接
		str.append("public class "+table+" {\r\n");
		//拼接属性
		for(int index=1; index < colnames.length ; index++){
			str.append(getAttrbuteString(colnames[index],colTypes[index]));
		}
		//拼接get,Set方法		
		for(int index=1; index < colnames.length ; index++){
			str.append(getGetMethodString(colnames[index],colTypes[index]));
			str.append(getSetMethodString(colnames[index],colTypes[index]));
		}
		str.append("}\r\n");
		//输出到文件中
		File file = new File(table+".java");
		BufferedWriter write = null;
 
		try {
			write = new BufferedWriter(new FileWriter(file));
			write.write(str.toString());
			write.close();
		} catch (IOException e) {
 
			e.printStackTrace();
			if (write != null)
				try {
					write.close();
				} catch (IOException e1) {			
					e1.printStackTrace();
				}
		}
		return str;
	}
	/*
	 * 获取字段字符串*/
	public StringBuffer getAttrbuteString(String name, String type) {
		if(!check(name,type)) {
			System.out.println("类中有属性或者类型为空");
			return null;
		};
		String format = String.format("    private %s %s;\n\r", new String[]{type,name});
		return new StringBuffer(format);
	}
	/*
	 * 校验name和type是否合法*/
	public boolean check(String name, String type) {
		if("".equals(name) || name == null || name.trim().length() ==0){
			return false;
		}
		if("".equals(type) || type == null || type.trim().length() ==0){
			return false;
		}
		return true;
		
	}
	/*
	 * 获取get方法字符串*/
	private StringBuffer getGetMethodString(String name, String type) {
		if(!check(name,type)) {
			System.out.println("类中有属性或者类型为空");
			return null;
		};
		String Methodname = "get"+GetTuoFeng(name);
		String format = String.format("    public %s %s(){\n\r", new Object[]{type,Methodname});
		format += String.format("        return this.%s;\r\n", new Object[]{name});
		format += "    }\r\n";
		return new StringBuffer(format);
	}
	//将名称首字符大写
	private String GetTuoFeng(String name) {
		name = name.trim();
		if(name.length() > 1){
			name = name.substring(0, 1).toUpperCase()+name.substring(1);
		}else
		{
			name = name.toUpperCase();
		}
		return name;
	}
	/*
	 * 获取字段的get方法字符串*/
	private Object getSetMethodString(String name, String type) {
		/*if(!check(name,type)) {
			System.out.println("类中有属性或者类型为空");
			return null;
		};*/
		String Methodname = "set"+GetTuoFeng(name);
		String format = String.format("    public void %s(%s %s){\n\r", new Object[]{Methodname,type,name});
		format += String.format("        this.%s = %s;\r\n", new Object[]{name,name});
		format += "    }\r\n";
		return new StringBuffer(format);
	}
 
	public static void main(String[] args) {
		reflectBean bean = new reflectBean();
		System.err.println(bean.getClassStr());
	}
}

还有手写底层生成数据库中model,和dao的,找不到了,下次更新。。。。。