哇,我发现忙起来真的是没有时间来写,最近在学框架,感觉特别有兴趣,对于框架的感激就是又恨又爱的感觉,hibernate,没有研究太深,模拟的写了一点底层的实现,其实就是发射吧,我没有追踪源码去看,就是简单的模拟,哈哈哈,反射出原始类型的没有写现在再追struts的底层,上次我还信誓旦旦的说,年前能写25篇,现在想说我错了,哈哈哈哈
好了,代码就写了一点,都是我猜的,如果有不对的,大神们求带
public class Session {
private static Map<String,String> map = new HashMap<String,String>();
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/demo";
private static final String username = "root";
private static final String password = "0610";
//模拟底层save
public void save(Object obj) throws Exception{
String sql = generateSQL(obj);//调用生成sql语句的方法
System.out.println(sql);//打印sql语句
Class.forName(driver);
Connection conn = (Connection) DriverManager.getConnection(url, username, password);
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
Set<String> keys = map.keySet();
int i = 1;//表示占位符的索引
for (String key : keys) {
//获取属性对应的get方法
Method m = obj.getClass().getMethod(map.get(key));
//通过反射调用get方法获取属性的值,作为占位符的值
ps.setObject(i, m.invoke(obj));
i++;//占位符+1
}
ps.executeUpdate();//指定插入语句
}
/**
* 生成sql语句的方法
* insert into 表名(列名,列名,...) values(?,?,...)
* @param obj
* @return
* @throws SecurityException
* @throws NoSuchMethodException
*/
public static String generateSQL(Object obj) throws Exception{
//获取实体类中所有的属性
Field[] fields = obj.getClass().getDeclaredFields();
//向map中存放键值对:键是属性名,也就是列名;值是属性get方法的名字
for(int i = 0;i < fields.length;i++){
map.put(fields[i].getName(), "get" +fields[i].getName().substring(0, 1).toUpperCase() + fields[i].getName().substring(1));
}
String columns = "";//存储列名
String values = "";//存储?
Set<String> keys = map.keySet();
for (String key : keys) {
columns = columns + key + ",";//拼接列名
values = values + "?,";
}
//截取最后的逗号
if(columns.endsWith(",")){
columns = columns.substring(0, columns.length()-1);
values = values.substring(0,values.length()-1);
}
//获取实体类的表名
String tName = obj.getClass().getSimpleName();
//拼接sql语句
String sql = "insert into " + tName + "(" + columns + ") values(" + values + ")";
return sql;
}
//根据id生成sql语句
public String selectById(Class objClass) throws Exception{
//生成一个对象,
Constructor<?> construction =objClass.getConstructor();
Object object=construction.newInstance();
//获得所有的属性,
Field[] fields = objClass.getDeclaredFields();
for(int i = 0;i < fields.length;i++){
map.put(fields[i].getName(), "set" +fields[i].getName().substring(0, 1).toUpperCase() + fields[i].getName().substring(1));
}
String columns = "";//存储列名
Set<String> keys = map.keySet();
for (String key : keys) {
columns = columns + key + ",";//拼接列名
}
//所有的属性的名字,
if(columns.endsWith(",")){
columns = columns.substring(0, columns.length()-1);
}
//获取实体类的表名
String tName = objClass.getSimpleName();
//拼接sql语句
String sql = " select " + columns + " from "+ tName +" where mid =? ";
return sql;
}
//根据class对象和id进行查询
public Object get(Class<?> objClass,Long id) {
Constructor<?> cons=null;
Object obj=null;
try {
cons = objClass.getConstructor();
obj=cons.newInstance();
String sql = selectById(objClass);//调用生成sql语句的方法
System.out.println(sql);//打印sql语句
Class.forName(driver);
Connection conn = (Connection) DriverManager.getConnection(url, username, password);
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
//执行方法进行获得结果集
ps.setObject(1, id);
//进行查询
ResultSet rs = ps.executeQuery();
while(rs.next()){
//对对象进行封装
Set<String> keys = map.keySet();
for (String key : keys) {
Object o= rs.getObject(key);
Method m = objClass.getMethod(map.get(key),new Class<?>[]{o.getClass()});
//执行对象的set方法
m.invoke(obj,o);
}
}
System.out.println(obj);
} catch (Exception e) {
}
return obj;
}
//根据id进行删除
public void delete(Object obj){
//根据id进行删除
Class objClass=obj.getClass();
String sql="";
//获取对象的id
Field[] fields = objClass.getDeclaredFields();
for(int i=0;i<fields.length;i++){
if("mid".equalsIgnoreCase(fields[i].getName())){
//进行执行删除操作
String name = objClass.getSimpleName();
//获得该属性的值,
String setterName="get" +fields[i].getName().substring(0, 1).toUpperCase() + fields[i].getName().substring(1);
//获得getter的方法
Method method;
try {
method = objClass.getMethod(setterName);
Object id = method.invoke(obj);
sql=" delete from "+name+" where mid= ? ";
System.out.println(sql);
Class.forName(driver);
Connection conn = (Connection) DriverManager.getConnection(url, username, password);
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
//执行方法进行获得结果集
ps.setObject(1, id);
ps.executeUpdate();
System.out.println("删除成功");
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
//删除main方法测试
public static void main(String[] args) {
Session session=new Session();
Object object = session.get(Customer.class, 1L);
session.delete(object);
}
/*//查询main方法进行测试
public static void main(String[] args) {
Session session =new Session();
try {
Object object = session.get(Customer.class, 1L);
} catch (Exception e) {
e.printStackTrace();
}
}*/
//添加的测试
/*public static void main(String[] args) throws Exception {
Session session = new Session();
Customer customer = new Customer();
customer.setMid(1111L);
customer.setName("阿里巴巴");
customer.setAddress("杭州");
session.save(customer);
}
*/
}