Java反射实现类似Hibernate的通用增删改查

package com.zsw.util.service;

import java.sql.Connection;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import java.lang.reflect.*;

import java.sql.*;

public class DBCommonFunction<T> {

private T ts;

public T getTs() {

return ts;

}

public void setTs(T ts) {

this.ts = ts;

}

public DBCommonFunction(T ts) {

this.ts = ts;

}

public void getClasses() {

Object object = ts.getClass();

System.out.println(getTs().getClass().getName());

try {

Class s = Class.forName(getTs().getClass().getName());

object = s.newInstance(); // 利用T类型 创建一个新的实例

System.out.println((T) object);

} catch (Exception e) {

e.printStackTrace();

}

}

// 获得T的参数并赋值

public void getParam() {

try {

Class s = Class.forName(getTs().getClass().getName());

Object o = s.newInstance();

for (Field f : o.getClass().getDeclaredFields()) {

if (f.getType().getSimpleName().equals("String")) {

f.set(o, 2 + "");

} else {

f.set(o, 1);

}

System.out.println(f);

}

System.out.println((T) o);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 获取表名

*/

private String getTblName() {

return getTs().getClass().getSimpleName() + "s";

}

/**

* 获取表中的字段

*/

@SuppressWarnings("finally")

private String getPram() {

String pram = "";

try {

Class s = Class.forName(getTs().getClass().getName());

Object o = s.newInstance();

Field[] fs = o.getClass().getDeclaredFields();

for (int i = 1; i < fs.length; i++) {

Field f = fs[i];

if (fs.length - 1 == i)

pram += f.getName();

else

pram += f.getName() + ",";

}

} catch (Exception e) {

e.printStackTrace();

} finally {

return pram;

}

}

/**

* 获取传递进的类型的值

*/

private String getValues(T t) {

Field[] fs = t.getClass().getDeclaredFields();

String ret = "";

try {

Class.forName(t.getClass().getName());


for (int i = 1; i < fs.length; i++) {

if (fs.length - 1 == i)

if (fs[i].getType().getSimpleName().equals("String")) {

ret += "'"+fs[i].get(t).toString()+"'";

} else {

ret += fs[i].get(t);

}

else {

if (fs[i].getType().getSimpleName().equals("String")) {

ret += "'"+fs[i].get(t)+"',";


} else {

ret += fs[i].get(t)+",";

}

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

return ret;

}

}


public List<T> findAll(){

List<T> list = new ArrayList<T>();

Connection conn = PageService.getConn();

PreparedStatement pstmt = null;

ResultSet rs = null;

try {

pstmt = conn.prepareStatement("select * from "+getTblName());

rs = pstmt.executeQuery();

while (rs.next()) {

Class s = Class.forName(getTs().getClass().getName());

Object o = s.newInstance();

for (Field f : o.getClass().getDeclaredFields()) {

f.set(o,rs.getObject(f.getName()));

}
list.add((T)o);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

return list;

}

}


/**

* 通用的添加

*/

public boolean Add(T t) {

String sql = "insert into " + getTblName() + " (" + getPram()

+ ") values (" + getValues(t) + ")";

Connection conn = PageService.getConn();

PreparedStatement pstmt = null;

int count = 0;

try {

pstmt = conn.prepareStatement(sql);

count = pstmt.executeUpdate();

} catch (Exception e) {

e.printStackTrace();

} finally{

try {

pstmt.close();

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}
}


return count > 0?true:false;

}

}

你可能感兴趣的:(Hibernate)