hibernate大多内部使用字节码方式得到对象,我们这里用反射替代。
实体
package shujujiegou;
/**
* Created by lcc on 2017/7/14.
*/
public class Teacher {
public Teacher(){
}
public Teacher(int id, String name, String title) {
this.id = id;
this.name = name;
this.title = title;
}
private int id;
private String name;
private String title;
public int getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
hibernate的session模拟
/**
* Created by lcc on 2017/7/14.
*/
public class SessionMoni {
//我们假设从配置文件里读取到了表名。
private String tableName = "tb_teacher";
//我们假设这里的map是表里列与Teacher字段一一对应,也是从配置文件里读取
private Map<String, String> cfs = new HashMap<String, String>();
private String[] methodName;
public SessionMoni() {
cfs.put("tb_id", "id");
cfs.put("tb_name", "name");
cfs.put("tb_title", "title");
methodName = new String[cfs.size()];
}
public void save(Teacher t) {
Connection connection = getConn();
String sql = createSql();
System.out.println(sql);
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) connection.prepareStatement(sql);
for (int i = 0; i < methodName.length; i++) {
Method method = t.getClass().getMethod(methodName[i]);
Class type = method.getReturnType();
String typeName = type.getSimpleName();
if (typeName.equals("int")) {
Integer value = (Integer) method.invoke(t);
System.out.println(typeName + " " + value);
pstmt.setInt(i + 1, value);
} else if (typeName.equals("String")) {
String value = (String) method.invoke(t);
pstmt.setString(i + 1, value);
System.out.println(typeName + " " + value);
}
}
pstmt.executeUpdate();
pstmt.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private String createSql() {
String str1 = "";
int index = 0;
for (String s : cfs.keySet()) {
str1 = s + "," + str1;
String sm = cfs.get(s);
sm = sm.toUpperCase().charAt(0) + sm.substring(1);
methodName[index] = "get" + sm;
index++;
}
str1 = str1.substring(0, str1.length() - 1);
String str2 = "";
for (int i = 0; i < cfs.size(); i++) {
str2 = "?," + str2;
}
str2 = str2.substring(0, str2.length() - 1);
String sql = "insert into " + tableName + "(" + str1 + ")" + "values (" + str2 + ")";
return sql;
}
/**
* @return
*/
private static Connection getConn() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/samp_db";
String username = "root";
String password = "";
Connection conn = null;
try {
Class.forName(driver); //classLoader,加载对应驱动
conn = (Connection) DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
调用
package shujujiegou;
/**
* Created by lcc on 2017/7/14.
*/
public class TeacherTest {
public static void main(String[] args) {
Teacher t = new Teacher();
t.setId(1);
t.setName("小名");
t.setTitle("中级");
SessionMoni session = new SessionMoni();
session.save(t);
}
}