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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值