java basedao封装_Hibernate4 一个baseDao的封装,包含一些通用的增删改查方法

import java.io.Serializable;

import java.lang.annotation.Annotation;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.lang.reflect.Modifier;

import java.sql.CallableStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Types;

import java.util.ArrayList;

import java.util.Collection;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.persistence.Id;

import javax.persistence.Transient;

import org.hibernate.Query;

import org.hibernate.SQLQuery;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.transform.Transformers;

import org.springframework.orm.hibernate4.SessionFactoryUtils;

/**@version 20151231

* SuperBaseDaoImpl中主要实现和数据库相关的操作逻辑,不涉及和视图层或控制层的一些操作;

* 其它dao可以继承此dao然后扩展;

* @author tingfeng

* 1.get时主要返回单个对象;

* 2.find使用的hql语句进行操作,主要返回list;

*/

@SuppressWarnings("unchecked")

public abstract class SuperBaseDaoImpl{

private Session session;

public abstract SessionFactory getSessionFactory();

/**

* 返回当前的Session,如果为null,返回SessionFactory的CurrentSession;

* @return

*/

public Session getCurrentSession() {

if(session!=null)

return session;

return this.getSessionFactory().getCurrentSession();

}

/**

* 此时获取当前session,不是从SessionFactory中取Session,可能为null;

* 如果需要从SessionFactory中取,用getCurrentSession()方法;

* @return

*/

public Session getBeanSession() {

return session;

}

public void setSession(Session session) {

this.session = session;

}

/**

* 返回存储此对象的主键

*/

public  Serializable save(T o){

return this.getCurrentSession().save(o);

}

public  void saveByCollection(Collection collection){

for(T t:collection){

this.save(t);

}

}

public  void update(T o) {

this.getCurrentSession().update(o);

}

public  void saveOrUpdate(T o) {

this.getCurrentSession().saveOrUpdate(o);

}

/**更新一个实体中指定的字段

* 这里columnNames和columnsValues的名字和值得顺序必须一致;

* @param t

* @param columnNames

* @param columnValues

* @throws InvocationTargetException

* @throws IllegalArgumentException

* @throws IllegalAccessException

* @throws SecurityException

* @throws NoSuchFieldException

*/

public  void updateByColumns(T t,List columnNames,List> columnValues) throws NoSuchFieldException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{

String tableNameString=t.getClass().getSimpleName();

String hqlString="update "+tableNameString+" table_ set ";

for(int i=0;i

hqlString+=columnNames.get(i)+"="+columnValues.get(i);

}

hqlString+=" where table_."+this.getPkName(t.getClass())+"="+this.getPkValue(t).toString();

this.executeHql(hqlString);

}

/**更新一个实体中除开指定的字段之外的字段

* 这里columnNames和columnsValues的名字和值得顺序必须一致;

* @param t

* @param columnNames

* @param columnValues

* @throws InvocationTargetException

* @throws IllegalArgumentException

* @throws IllegalAccessException

* @throws SecurityException

* @throws NoSuchFieldException

*/

public  void updateByExceptColumns(T t,List exceptColumnNames,List> columnValues) throws NoSuchFieldException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{

List columnNames=this.getEntityColumnNames(t.getClass(), exceptColumnNames);

String tableNameString=t.getClass().getSimpleName();

String hqlString="update "+tableNameString+" table_ set ";

for(int i=0;i

hqlString+=columnNames.get(i)+"="+columnValues.get(i);

}

hqlString+=" where table_."+this.getPkName(t.getClass())+"="+this.getPkValue(t).toString();

this.executeHql(hqlString);

}

public  T get(Class c, Serializable id) {

return (T) this.getCurrentSession().get(c,id);

}

public  T get(String hql) {

return this.get(hql, null);

}

public  T get(String hql, Map params) {

Query q = this.getCurrentSession().createQuery(hql);

this.setParameterToQuery(q, params);

List l = q.list();

if (l != null && l.size() > 0) {

return l.get(0);

}

return null;

}

public  void delete(T o) {

this.getCurrentSession().delete(o);

}

/**

* 从数据库中找出此id对象并删除

* @param entityClass

* @param id

*/

public  void delete(Class entityClass, PK id) {

getCurrentSession().delete(get(entityClass, id));

}

/**hql语句,"delete from "+tableName+" where "+columnName+" in ("+columnValues+")"

*  用in语句删除指定表中,包含有指定值得指定列的记录;

* @param tableName

* @param columnName

* @param columnValues 如1,3,4这种in语句参数需要的内容

* @throws Exception

*/

public void deleteByColumns(String tableName,String columnName,String columnValues) throws Exception {

if(com.tingfeng.sql.utils.SqlUtils.sqlValidate(columnValues)){

throw new Exception("列名字数据中包含sql关键字!");

}

String hql="delete from "+tableName+" where "+columnName+" in ("+columnValues+")";

this.executeHql(hql);

}

/**

* hql语句,"delete from "+tableName+" where "+columnName+" in ("+columnValues+")"

* 用in语句删除指定表中,包含有指定值得指定列的记录;

* @param tableName

* @param columnName

* @param columnValues 一个参数值的集合

*/

public void deleteByColumns(String tableName,String columnName,Collection> columnValues) {

String hql="delete from "+tableName+" where "+columnName+" in (:columnValues)";

Map params=new HashMap();

params.put("columnValues",columnValues);

this.executeHql(hql,params);

}

/**

* 如果有id并存在于数据库中,则更新,否则保存

* @param model

*/

public  void merge(T model) {

getCurrentSession().merge(model);

}

public  List findList(String hql) {

Query q = this.getCurrentSession().createQuery(hql);

return q.list();

}

public  List findList(String hql, Map params) {

Query q = this.getCurrentSession().createQuery(hql);

this.setParameterToQuery(q, params);

return q.list();

}

/**

*

* @param hql

* @param topCount 返回前topCount条记录

* @return

*/

public  List findTopList(String hql, int topCount) {

// 获取当前页的结果集

Query query = this.getCurrentSession().createQuery(hql);

query.setFirstResult(0);

if(topCount<0) topCount=0;

query.setMaxResults(topCount);

return  query.list();

}

/**

* 用hql语句,得到当前表的所有记录

* @param tableName

* @return

*/

public  List findAll(String tableName){

String hqlString="select * from "+tableName;

return this.findList(hqlString);

}

/**

*

* @param hql

* @param params

* @param page 当前页码

* @param rows 每页显示的记录数量

* @return

*/

public  List findList(String hql, Map params, int page, int rows) {

Query q = this.getCurrentSession().createQuery(hql);

this.setParameterToQuery(q, params);

if(page<1) page=1;

if(rows<0) rows=0;

return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();

}

public  List findList(String hql, int page, int rows) {

return this.findList(hql, null, page,rows);

}

public Long getCountByHql(String hql) {

Query q = this.getCurrentSession().createQuery(hql);

return (Long) q.uniqueResult();

}

public Long getCountByHql(String hql, Map params) {

Query q = this.getCurrentSession().createQuery(hql);

this.setParameterToQuery(q, params);

return (Long) q.uniqueResult();

}

/**

* 根据HQL语句返回一个值,如分布获取总页数

*/

public Object getCountByHql(String hql, Object... params) {

Query query = getCurrentSession().createQuery(hql);

this.setParameterToQuery(query, params);

return query.uniqueResult();

}

/**

* 根据HQL语句,获得查找总记录数的HQL语句 如: select ... from Orgnization o where o.parent is

* null 经过转换,可以得到: select count(*) from Orgnization o where o.parent is null

* @param hql

* @return

*/

protected String getCountQuery(String hql) {

int index = hql.toLowerCase().indexOf("from");

int last = hql.toLowerCase().indexOf("order by");

if (index != -1) {

if (last != -1) {

return "select count(*) " + hql.substring(index, last);

}

return "select count(*) " + hql.substring(index);

}

return null;

}

public  int executeHql(String hql) {

Query q = this.getCurrentSession().createQuery(hql);

return q.executeUpdate();

}

public int executeHql(String hql, Map params) {

Query q = this.getCurrentSession().createQuery(hql);

this.setParameterToQuery(q, params);

return q.executeUpdate();

}

public int executeHql(String hql,Object... objects) {

Query q = this.getCurrentSession().createQuery(hql);

this.setParameterToQuery(q, objects);

return q.executeUpdate();

}

/**

*

* @param hql

* @param objects 参数,其顺序应该和?占位符一一对应

* @return

*/

public int executeHql(String hql,List> objects) {

Query q = this.getCurrentSession().createQuery(hql);

this.setParameterToQuery(q, objects);

return q.executeUpdate();

}

/**

* @param q

* @param params 当前支持普通对象,数组,集合三种类型的参数

*/

protected void setParameterToQuery(Query q,Map params){

if (params != null && !params.isEmpty()) {

for (String key : params.keySet()) {

if(params.get(key) instanceof Object[]){

Object[] objs=(Object[]) params.get(key);

q.setParameterList(key, objs);

}else if(params.get(key) instanceof Collection>){

Collection> collection=(Collection>) params.get(key);

q.setParameterList(key, collection);

}else{

q.setParameter(key, params.get(key));

}

}

}

}

/**

* @param q

* @param params 当前支持普通对象,不支持集合与数组

*/

protected void setParameterToQuery(Query q,Object... params){

if (params != null && params.length>0) {

for (int i=0;i

Object object=params[i];

q.setParameter(i,object);

}

}

}

/**

* @param q

* @param params 当前支持普通对象,不支持集合与数组

*/

protected void setParameterToQuery(Query q,List> params){

if (params != null && !params.isEmpty()) {

for (int i=0;i

Object object=params.get(i);

q.setParameter(i,object);

}

}

}

/****************************************************************

******* 上面是和hql相关的操作,下面是和sql相关的操作****************

****************************************************************/

public  T getCountBySql(String sql) {

return this.getCountBySql(sql,new HashMap());

}

/**

* 根据SQL语句返回一个值,如分布获取总页数

*/

public  T getCountBySql(String sql, Object... params) {

Query query = getCurrentSession().createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

this.setParameterToQuery(query, params);

return (T) query.uniqueResult();

}

/**

* 根据SQL语句返回一个值,如分布获取总页数

*/

public  T getCountBySql(String sql,Map params) {

Query query = getCurrentSession().createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

this.setParameterToQuery(query, params);

return (T) query.uniqueResult();

}

public List> findListBySql(String sql) {

return this.findListBySql(sql, new HashMap());

}

public List> findListBySql(String sql,Map params) {

SQLQuery query = this.getCurrentSession().createSQLQuery(sql);

query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

this.setParameterToQuery(query, params);

return query.list();

}

/**

* 根据SQL语句返回一个集合

*/

public List> findListBySql(String sql, Object... params) {

Query query = getCurrentSession().createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

this.setParameterToQuery(query, params);

return query.list();

}

/**

*调用存储过程

*/

public  List execProc(String hql) {

Query q = this.getCurrentSession().getNamedQuery(hql);

return q.list();

}

/**

function: 执行原生态的sql语句,添加、删除、修改语句

* @createDate 2010-8-2 下午05:33:42

* @author hoojo

* @param sql

*            将要执行的sql语句

* @return int

* @throws Exception

*/

public int executeBySql(String sql) throws Exception {

try {

return this.getCurrentSession().createSQLQuery(sql).executeUpdate();

} catch (Exception e) {

throw new RuntimeException(e);

}

}

public  List callProcedure(String procString, List params) throws Exception {

ResultSet rs = null;

CallableStatement stmt = null;

try {

stmt = (CallableStatement)SessionFactoryUtils.getDataSource(this.getSessionFactory()).getConnection()

.prepareCall(procString);

if (params != null) {

int idx = 1;

for (Object obj : params) {

if (obj != null) {

stmt.setObject(idx, obj);

} else {

stmt.setNull(idx, Types.NULL);

}

idx++;

}

}

rs = stmt.executeQuery();

List list = new ArrayList();

ResultSetMetaData md = rs.getMetaData(); // 得到结果集(rs)的结构信息,比如字段数、字段名等

int columnCount = md.getColumnCount(); // 返回此 ResultSet 对象中的列数

Map rowData = new HashMap();

while (rs.next()) {

rowData = new HashMap(columnCount);

for (int i = 1; i <= columnCount; i++) {

rowData.put(md.getColumnName(i), rs.getObject(i));

}

list.add(rowData);

}

return list;

} catch (SQLException e) {

e.printStackTrace();

throw new Exception("调用存储过程的时候发生错误[sql = " + procString + "]", e);

} finally {

rs.close();

stmt.close();

}

}

/**

* 返回此类的列的属性名称,不包含静态属性和Transient

* @param entity

* @return

*/

private List getEntityColumnNameList(Class> cls){

List list=new ArrayList();

Class> clazz=cls;

Field[] fs=clazz.getDeclaredFields();

String filedName=null;

for(Field field:fs){

boolean isStatic = Modifier.isStatic(field.getModifiers());

if(isStatic)

continue;

field.setAccessible(true);

filedName=field.getName();

Annotation[] as=field.getAnnotations();

boolean isTransaction=false;

for(int i=0;i

Annotation a=as[i];

if(a instanceof Transient){

isTransaction=true;

break;

}

}

if(!isTransaction){

list.add(filedName);

}

}

return list;

}

/**

* 得到除开指定名称的属性列

*/

protected List getEntityColumnNames(Class> cls,String... exceptCoulumns){

List nameList=getEntityColumnNameList(cls);

if(exceptCoulumns!=null){

for(String s:exceptCoulumns){

nameList.remove(s);

}

}

return nameList;

}

/**

* 得到除开指定名称的属性列

*/

protected List getEntityColumnNames(Class> cls,List exceptCoulumns){

List nameList=getEntityColumnNameList(cls);

if(exceptCoulumns!=null){

for(String s:exceptCoulumns){

nameList.remove(s);

}

}

return nameList;

}

/**

* 获取主键名称

*

* @return 没有逐渐则返回null;

*/

private String getPkName(Class> cls) {

String pkname=null;

// 标注在getter方法上

Method[] methods = cls.getDeclaredMethods();

for (Method method : methods) {

if (method.getName().startsWith("get")) {

if (method.isAnnotationPresent(Id.class)) {

String temp = method.getName().replaceAll("^get", "");

// 将第一个字母变成小写

pkname = this.firstLetterToLower(temp);

break;

}

}

}

if(pkname==null){

Field[] fields=cls.getDeclaredFields();

for(Field field:fields){

if(field.isAnnotationPresent(Id.class)){

return field.getName();

}

}

}

return pkname;

}

private Object getPkValue(Object t) throws NoSuchFieldException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{

Field field=t.getClass().getField(this.getPkName(t.getClass()));

try {

Method method = t.getClass().getMethod("get"+ this.firstLetterToLower(field.getName()));// 此方法不需要参数,如:getName(),getAge()

return method.invoke(t);

} catch (NoSuchMethodException e) {

return field.get(t);

}

}

private  String firstLetterToLower(String srcString) {

StringBuilder sb = new StringBuilder();

sb.append(Character.toLowerCase(srcString.charAt(0)));

sb.append(srcString.substring(1));

return sb.toString();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的BaseDao类,封装了增加、删除、查询、更新四个方法: ``` import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BaseDao { // 定义数据库连接对象 private Connection conn = null; // 构造方法,初始化数据库连接对象 public BaseDao() { conn = DBUtil.getConnection(); } // 增加数据方法 public int add(String sql, Object... params) { int count = 0; PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(sql); // 设置占位符参数 for (int i = 0; i < params.length; i++) { pstmt.setObject(i + 1, params[i]); } // 执行增加操作 count = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.closeAll(null, pstmt, null); } return count; } // 删除数据方法 public int delete(String sql, Object... params) { int count = 0; PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(sql); // 设置占位符参数 for (int i = 0; i < params.length; i++) { pstmt.setObject(i + 1, params[i]); } // 执行删除操作 count = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.closeAll(null, pstmt, null); } return count; } // 查询数据方法 public ResultSet query(String sql, Object... params) { ResultSet rs = null; PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(sql); // 设置占位符参数 for (int i = 0; i < params.length; i++) { pstmt.setObject(i + 1, params[i]); } // 执行查询操作 rs = pstmt.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } return rs; } // 更新数据方法 public int update(String sql, Object... params) { int count = 0; PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(sql); // 设置占位符参数 for (int i = 0; i < params.length; i++) { pstmt.setObject(i + 1, params[i]); } // 执行更新操作 count = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.closeAll(null, pstmt, null); } return count; } } ``` 在使用该类时,可以通过继承的方式来实现具体的操作,例如: ``` public class UserDao extends BaseDao { // 添加用户 public int addUser(User user) { String sql = "insert into user(username, password, age) values(?,?,?)"; return super.add(sql, user.getUsername(), user.getPassword(), user.getAge()); } // 删除用户 public int deleteUser(int id) { String sql = "delete from user where id=?"; return super.delete(sql, id); } // 查询用户 public User getUser(int id) { User user = null; String sql = "select * from user where id=?"; ResultSet rs = super.query(sql, id); try { if (rs.next()) { user = new User(); user.setId(rs.getInt("id")); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); user.setAge(rs.getInt("age")); } } catch (SQLException e) { e.printStackTrace(); } return user; } // 更新用户 public int updateUser(User user) { String sql = "update user set username=?, password=?, age=? where id=?"; return super.update(sql, user.getUsername(), user.getPassword(), user.getAge(), user.getId()); } } ``` 这样,我们就可以通过继承BaseDao类来实现具体的增、删、查、改操作了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值