package noticeDATA;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import oracle.net.aso.p;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.CriteriaSpecification;
public class DataProvice {
private Configuration cfg = null;
private SessionFactory sf = null;
// 优化底层
private Session se = null;
private Query query=null;
//构造,采用构造浪费资源,采用线层比较好
public DataProvice() {
// 初始化配置对象
cfg = new Configuration().configure();
// 服务注册器
StandardServiceRegistry sr = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build();
sf = cfg.buildSessionFactory(sr);
// 优化底层
se = sf.openSession();
}
//参数化
private List<SqlParameter> parameter=new LinkedList<SqlParameter>();
public List<SqlParameter> getParameter() {
return parameter;
}
public void setParameter(List<SqlParameter> parameter) {
this.parameter = parameter;
}
public SessionFactory gethSession() {
return sf;
}
//参数化
public void setParam(){
for (SqlParameter sp : parameter) {
query.setParameter(sp.getParamName(), sp.getParamValue());
}
}
//清空参数的方法
public void clearParam(){
parameter.removeAll(getParameter());
}
// 添加,修改,删除
public int excuteNon(Object m, Boolean isDelete) {
int flag = 0;
Session se = sf.openSession();
Transaction transaction = se.beginTransaction();
try {
if (isDelete) {
se.delete(m);
} else {
se.saveOrUpdate(m);
}
flag = 1;
transaction.commit();
} catch (HibernateException he) {
he.printStackTrace();
flag = -1;
transaction.rollback();
}
return flag;
}
// 泛型,可用与展现列表
/**
* @param t
* 实体类
* @param sql
* @return 泛型列表(集合)
* @throws Exception
* @throws IllegalAccessException
*/
public <T> List<T> getList(Class<T> t, String sql) throws Exception,IllegalAccessException {
List<T> lm = new LinkedList<T>();
SessionFactory sf = new DataProvice().gethSession();
Session se = sf.openSession();
//SQLQuery query = se.createSQLQuery(sql).addEntity(t);
query = se.createSQLQuery(sql).addEntity(t);
setParam();
@SuppressWarnings("rawtypes")
Iterator it = query.list().iterator();
while (it.hasNext()) {
T m = (T) it.next();
lm.add(m);
}
return lm;
}
//视图(泛型)
public <T> List<T> getView(String sql, Class<T> clzz) {
//SQLQuery sqlQuery = se.createSQLQuery(sql);
query = se.createSQLQuery(sql);
//转换为hibernate键值对
//sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
setParam();
//遍历键值对,一个map是一个字段的键值对,字段名,字段值
@SuppressWarnings("unchecked")
//List<Map<String, Object>> list = (List<Map<String, Object>>) sqlQuery.list();
List<Map<String, Object>> list = (List<Map<String, Object>>) query.list();
List<T> result = new ArrayList<T>();
try {
//通过类获取类的信息,给props
PropertyDescriptor[] props = Introspector.getBeanInfo(clzz).getPropertyDescriptors();
for (Map<String, Object> map : list) {
T t = clzz.newInstance();
for (Entry<String, Object> entry : map.entrySet()) {
//得到字段名
String attrName = entry.getKey().toLowerCase();
for (PropertyDescriptor prop : props) {
if (!attrName.equals(prop.getName())) {
continue;
}
//通过类找到属性的方法在java中属性也是方法,.net不是,引用java.lang.reflct反射
Method method = prop.getWriteMethod();
//得到字段值,反射是通过类反射属性
method.invoke(t, entry.getValue());
}
}
result.add(t);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 用于修改
public <T> T getEntity(Class<T> t, String sql) throws Exception,IllegalAccessException {
SessionFactory sf = new DataProvice().gethSession();
Session se = sf.openSession();
//SQLQuery query = se.createSQLQuery(sql).addEntity(t);
query = se.createSQLQuery(sql).addEntity(t);
setParam();
// 用参数时用:stu_id
// query.setParameter(:stu_id, arg1);
@SuppressWarnings("rawtypes")
Iterator it = query.list().iterator();
while (it.hasNext()) {
T m = (T) it.next();
return m;
}
return null;
}
//该方法用于取总数
public Integer excuteScaler(String sql){
Integer flag=0;
//SQLQuery query=se.createSQLQuery(sql);
query=se.createSQLQuery(sql);
setParam();
@SuppressWarnings("rawtypes")
Iterator it = query.list().iterator();
while(it.hasNext()){
flag=Integer.parseInt(it.next().toString());
}
return flag;
}
//批量删除
public Integer deleteData(String sql){
Integer flag=0;
Transaction transaction = se.beginTransaction();
try {
flag=se.createSQLQuery(sql).executeUpdate();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
}
transaction.commit();
return flag;
}
public void closeDbResource() {
if (se.isOpen()) {
se.close();
}
sf.close();
}
}
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import oracle.net.aso.p;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.CriteriaSpecification;
public class DataProvice {
private Configuration cfg = null;
private SessionFactory sf = null;
// 优化底层
private Session se = null;
private Query query=null;
//构造,采用构造浪费资源,采用线层比较好
public DataProvice() {
// 初始化配置对象
cfg = new Configuration().configure();
// 服务注册器
StandardServiceRegistry sr = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build();
sf = cfg.buildSessionFactory(sr);
// 优化底层
se = sf.openSession();
}
//参数化
private List<SqlParameter> parameter=new LinkedList<SqlParameter>();
public List<SqlParameter> getParameter() {
return parameter;
}
public void setParameter(List<SqlParameter> parameter) {
this.parameter = parameter;
}
public SessionFactory gethSession() {
return sf;
}
//参数化
public void setParam(){
for (SqlParameter sp : parameter) {
query.setParameter(sp.getParamName(), sp.getParamValue());
}
}
//清空参数的方法
public void clearParam(){
parameter.removeAll(getParameter());
}
// 添加,修改,删除
public int excuteNon(Object m, Boolean isDelete) {
int flag = 0;
Session se = sf.openSession();
Transaction transaction = se.beginTransaction();
try {
if (isDelete) {
se.delete(m);
} else {
se.saveOrUpdate(m);
}
flag = 1;
transaction.commit();
} catch (HibernateException he) {
he.printStackTrace();
flag = -1;
transaction.rollback();
}
return flag;
}
// 泛型,可用与展现列表
/**
* @param t
* 实体类
* @param sql
* @return 泛型列表(集合)
* @throws Exception
* @throws IllegalAccessException
*/
public <T> List<T> getList(Class<T> t, String sql) throws Exception,IllegalAccessException {
List<T> lm = new LinkedList<T>();
SessionFactory sf = new DataProvice().gethSession();
Session se = sf.openSession();
//SQLQuery query = se.createSQLQuery(sql).addEntity(t);
query = se.createSQLQuery(sql).addEntity(t);
setParam();
@SuppressWarnings("rawtypes")
Iterator it = query.list().iterator();
while (it.hasNext()) {
T m = (T) it.next();
lm.add(m);
}
return lm;
}
//视图(泛型)
public <T> List<T> getView(String sql, Class<T> clzz) {
//SQLQuery sqlQuery = se.createSQLQuery(sql);
query = se.createSQLQuery(sql);
//转换为hibernate键值对
//sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
setParam();
//遍历键值对,一个map是一个字段的键值对,字段名,字段值
@SuppressWarnings("unchecked")
//List<Map<String, Object>> list = (List<Map<String, Object>>) sqlQuery.list();
List<Map<String, Object>> list = (List<Map<String, Object>>) query.list();
List<T> result = new ArrayList<T>();
try {
//通过类获取类的信息,给props
PropertyDescriptor[] props = Introspector.getBeanInfo(clzz).getPropertyDescriptors();
for (Map<String, Object> map : list) {
T t = clzz.newInstance();
for (Entry<String, Object> entry : map.entrySet()) {
//得到字段名
String attrName = entry.getKey().toLowerCase();
for (PropertyDescriptor prop : props) {
if (!attrName.equals(prop.getName())) {
continue;
}
//通过类找到属性的方法在java中属性也是方法,.net不是,引用java.lang.reflct反射
Method method = prop.getWriteMethod();
//得到字段值,反射是通过类反射属性
method.invoke(t, entry.getValue());
}
}
result.add(t);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 用于修改
public <T> T getEntity(Class<T> t, String sql) throws Exception,IllegalAccessException {
SessionFactory sf = new DataProvice().gethSession();
Session se = sf.openSession();
//SQLQuery query = se.createSQLQuery(sql).addEntity(t);
query = se.createSQLQuery(sql).addEntity(t);
setParam();
// 用参数时用:stu_id
// query.setParameter(:stu_id, arg1);
@SuppressWarnings("rawtypes")
Iterator it = query.list().iterator();
while (it.hasNext()) {
T m = (T) it.next();
return m;
}
return null;
}
//该方法用于取总数
public Integer excuteScaler(String sql){
Integer flag=0;
//SQLQuery query=se.createSQLQuery(sql);
query=se.createSQLQuery(sql);
setParam();
@SuppressWarnings("rawtypes")
Iterator it = query.list().iterator();
while(it.hasNext()){
flag=Integer.parseInt(it.next().toString());
}
return flag;
}
//批量删除
public Integer deleteData(String sql){
Integer flag=0;
Transaction transaction = se.beginTransaction();
try {
flag=se.createSQLQuery(sql).executeUpdate();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
}
transaction.commit();
return flag;
}
public void closeDbResource() {
if (se.isOpen()) {
se.close();
}
sf.close();
}
}