首页 > 代码库 > java 根据实体类创建映射文件

java 根据实体类创建映射文件

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;

import com.mohe.model.entity.Teach;

/**
 * 根据实体类生成映射文件
 * 
 * @author dxm
 * 
 */
public class ObjectToMapping {

	/**
	 * 将实体类转换为映射文件
	 * 
	 * @param cls
	 *            实体类
	 * @param file
	 *            映射文件存放的根目录
	 * @param id
	 *            主键字段名
	 * @param identity
	 *            主键是否为自动表示列
	 * @return
	 * @throws IOException
	 */
	public static boolean convert(Class cls, File file, String id, boolean identity) throws IOException {

		if (null == cls || file == null) {
			return false;
		}

		if (!file.exists()) {
			file.mkdirs();
		}

		file = new File(file.getAbsolutePath() + "\\" + cls.getSimpleName() + ".hbm.xml");
		if (file.exists()) {
			file.delete();
		}

		String str = "";

		FileOutputStream fs = new FileOutputStream(file);
		fs.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".getBytes("UTF-8"));
		fs.write("<!DOCTYPE hibernate-mapping PUBLIC\n".getBytes("UTF-8"));
		fs.write("\"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"\n".getBytes("UTF-8"));
		fs.write("\"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\n".getBytes("UTF-8"));

		str = "<hibernate-mapping package=\"" + cls.getPackage().getName() + "\">\n";
		fs.write(str.getBytes("UTF-8"));

		str = "\t<class name=\"" + cls.getSimpleName() + "\" table=\"t_" + cls.getSimpleName().toLowerCase() + "\">\n\n";
		fs.write(str.getBytes("UTF-8"));
		fs.write("\t\t<cache usage=\"read-write\" />\n\n".getBytes("UTF-8"));

		Field[] fls = cls.getDeclaredFields();
		Field[] sfls = cls.getSuperclass().getDeclaredFields();

		// 加载父类
		for (Field f : sfls) {

			if (f.getName().equals("o00000") || f.getName().equals("serialVersionUID")) {
				continue;
			}

			if (f.getName().equals(id)) {
				str = "\t\t<id name=\"" + id + "\" type=\"" + f.getType().getName() + "\">\n";
				fs.write(str.getBytes("UTF-8"));

				str = "\t\t\t<column name=\"" + id + "\" />\n";
				fs.write(str.getBytes("UTF-8"));

				if (identity) {
					fs.write("\t\t\t<generator class=\"identity\"></generator>\n".getBytes("UTF-8"));
				}

				fs.write("\t\t</id>\n\n".getBytes("UTF-8"));
				continue;
			}

			str = "\t\t<property name=\"" + f.getName() + "\" type=\"" + f.getType().getName() + "\">\n";
			fs.write(str.getBytes("UTF-8"));

			str = "\t\t\t<column name=\"" + f.getName() + "\" />\n";
			fs.write(str.getBytes("UTF-8"));
			fs.write("\t\t</property>\n\n".getBytes("UTF-8"));
		}

		// 加载当前类
		for (Field f : fls) {

			// 过滤特殊属性
			if (f.getName().equals("serialVersionUID")) {
				continue;
			}

			// 主键
			if (f.getName().equals(id)) {
				str = "\t\t<id name=\"" + id + "\" type=\"" + f.getType().getName() + "\">\n";
				fs.write(str.getBytes("UTF-8"));

				str = "\t\t\t<column name=\"" + id + "\" />\n";
				fs.write(str.getBytes("UTF-8"));

				if (identity) {
					fs.write("\t\t\t<generator class=\"identity\"></generator>\n".getBytes("UTF-8"));
				}

				fs.write("\t\t</id>\n\n".getBytes("UTF-8"));
				continue;
			}

			// 多对一的关系
			if (f.getType().getClassLoader() != null) {
				// str = "\t\t<many-to-one name=\"" + f.getName() +
				// "\" class=\"" + f.getType().getSimpleName() + "\" column=\""
				// + f.getName() + "\"></many-to-one>\n\n";
				// fs.write(str.getBytes("UTF-8"));
				continue;
			}

			// 一对多的关系
			if (f.getType().isAssignableFrom(List.class) || f.getType().isAssignableFrom(Set.class)) {
				// fs.write("<set name=\""+f.getName()+"\">".getBytes("UTF-8"));
				// fs.write("<key column=\"俩表中的外键字段\"></key>".getBytes("UTF-8"));
				// fs.write("<one-to-many class=\"A表所在的包名+A表的名\"></one-to-many>".getBytes("UTF-8"));
				// fs.write("</set>".getBytes("UTF-8"));
				continue;
			}

			str = "\t\t<property name=\"" + f.getName() + "\" type=\"" + f.getType().getName() + "\">\n";
			fs.write(str.getBytes("UTF-8"));

			str = "\t\t\t<column name=\"" + f.getName() + "\" />\n";
			fs.write(str.getBytes("UTF-8"));
			fs.write("\t\t</property>\n\n".getBytes("UTF-8"));
		}

		fs.write("\t</class>\n".getBytes("UTF-8"));
		fs.write("</hibernate-mapping>".getBytes("UTF-8"));
		return false;
	}

	public static void main(String[] args) throws IOException {
		ObjectToMapping.convert(Teach.class, new File("E:\\金山快盘\\工作\\eclipse-j2ee\\WebTest\\src\\com\\mohe\\model\\mapping1\\"), "timeKey", true);
	}
}

java 根据实体类创建映射文件