昨天看了看注解,于是简单模拟一下hibernate
整个包:
先来看注解Table.java,主要是把实体类和表名联系起来
package annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;//自定义的Annotation@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface Table { String name() default "";}
然后是实体类Model.java,用刚写好的注解@Table配置一下
package annotation;//实体类@Table(name = "_model") //用annotation配置public class Model { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
SessionFactory主要是读取注解配置(通过反射很easy),然后把实体类放入容器中
package annotation;import java.io.File;import java.util.HashMap;import java.util.Map;//annotation配置文件的解读public class SessionFactory { private MaptableMap = new HashMap (); private static SessionFactory sf = new SessionFactory(); private SessionFactory() { } public static SessionFactory config() throws Exception { // 读取包名为annotation下所有class File f = new File(System.getProperty("user.dir") + "/bin/annotation"); File[] files = f.listFiles(); for (int i = 0; i < files.length; i++) { String fileName = files[i].getName(); // 载入class Class clazz = Class.forName("annotation." + fileName.substring(0, fileName.indexOf("."))); // 检查该类是否加有Table注解 Table t = (Table) clazz.getAnnotation(Table.class); // 加入Table注解的放入容器中 if (t != null) { sf.getTableMap().put(clazz, t.name()); } } return sf; } public Session openSession() { return new Session(tableMap); } public Map getTableMap() { return tableMap; } public void setTableMap(Map tableMap) { this.tableMap = tableMap; }}
session类主要负责数据库连接和sql语句处理
package annotation;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.sql.Connection;import java.sql.DriverManager;import java.util.HashMap;import java.util.Map;//负责数据库连接,包括生成及执行sql语句public class Session { private MaptableMap = new HashMap (); public Session(Map tableMap) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } this.tableMap = tableMap; } // 储存 public void save(Object o) throws Exception { Class clazz = o.getClass(); String tableName = tableMap.get(clazz); // 表列名 String keys = ""; // 值 String vals = ""; Field[] fields = clazz.getDeclaredFields(); for (Field f : fields) { // 构造列名 keys += f.getName() + ","; // 得到get**方法,得到列的对应值 String ms = "get" + Character.toUpperCase(f.getName().charAt(0)) + f.getName().substring(1); Method m = clazz.getDeclaredMethod(ms); Object obj = m.invoke(o, null); if (obj instanceof Integer) { vals += obj + ","; } if (obj instanceof String) { vals += "'" + obj + "'" + ","; } } keys = keys.substring(0, keys.length() - 1); vals = vals.substring(0, vals.length() - 1); String sql = createSaveSQL(tableName, keys, vals); executeSQL(sql); } private void executeSQL(String sql) throws Exception { Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost/test", "root", "lj123456"); conn.createStatement().execute(sql); conn.close(); } // 建sql语句 private String createSaveSQL(String tableName, String keys, String vals) { String sql = "insert into " + tableName + " (" + keys + ") values (" + vals + ")"; return sql; }}
Main.java 来测试一下代码
package annotation;public class Main { public static void main(String[] args) throws Exception { SessionFactory sf = SessionFactory.config(); Session session = sf.openSession(); Model m = new Model(); m.setId(1); m.setName("first"); session.save(m); }}
结果:
O啦,暂时没有模拟hibernate其他注解,但原理一样,就偷偷懒呗.