该程序为尚学堂马士兵老师讲解,模拟了hibernate的原理,主要应用了字符串拼接,反射知识。
step1,新建数据库
use jd;
create table _student(
_id int(11),
_nage varchar(20),
_age int(11));
step 2 student实体类,再次略过
step3,编写session类,模拟hibernate的实现原理。
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import com.bjsxt.hibernate.model.Student;
public class Session {
static String tableName = "_Student";
static Map<String, String> cfs = new HashMap<String, String>();
static String[] methodNames;
public Session() {
cfs.put("_id", "id");
cfs.put("_name", "name");
cfs.put("_age", "age");
methodNames = new String[cfs.size()];
}
public static void save(Student s) throws Exception {
// TODO Auto-generated method stub
int index = 0;
String sql = createSql();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/jd", "root", "123456");
PreparedStatement ps = conn.prepareStatement(sql);
for (int i = 0; i < methodNames.length; i++) {
Method m = s.getClass().getMethod(methodNames[i]);
Class r = m.getReturnType();
if (r.getName().equals("java.lang.String")) {
String returnValue = (String) m.invoke(s);
ps.setString(i + 1, returnValue);
System.out.println(returnValue);
}
if (r.getName().equals("int")) {
Integer returnValue = (Integer) m.invoke(s);
ps.setInt(i + 1, returnValue);
System.out.println(returnValue);
}
// System.out.println(m.getName()+","+m.getReturnType()+","+r.getName());
}
System.out.println(sql);
ps.executeUpdate();
ps.close();
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String createSql() {
String str2 = "";
String str1 = "";
int index = 0;
for (String s : cfs.keySet()) {//s为_id,_Age,_Name
String v = cfs.get(s);
v = Character.toUpperCase(v.charAt(0)) + v.substring(1);//Id,Name,Age
//System.out.println(v+"涛");
methodNames[index] = "get" + v; // str1+=s+",";//getId,getName,getAge
//System.out.println(s);
//System.out.println(methodNames[index]);
str1 += s + ",";
index++;
System.out.println(str1);
}
str1 = str1.substring(0, str1.length() - 1);
//System.out.println(str1);
for (int i = 0; i < cfs.size(); i++) {
if (i == cfs.size() - 1) {
str2 += "?";
} else {
str2 += "?,";
}
}
//System.out.println(str2);
String sql = "insert into " + tableName + "(" + str1 + ")"
+ " values (" + str2 + ")";
System.out.println(sql+"createSql方法里面");
return sql;
}
}
step4,测试,
import com.bjsxt.hibernate.model.Student;
public class StudentTest {
public static void main(String[] args) throws Exception {
Student s = new Student();
s.setId(10);
s.setName("s1");
s.setAge(1);
Session session = new Session();
session.save(s);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/110101.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...