当前位置:首页 > 开发 > 编程语言 > Hibernate > 正文

我的spring3,hibernate3 架构基础抽象类

发表于: 2014-12-26   作者:amazon_cn   来源:转载   浏览:
摘要: BaseService.java ,这个我在工程中使用的抽象基类,spring3,hibernate3的服务层有这个就购了: package com.jhzp.service.base; import java.math.BigDecimal; import java.math.BigInteger; import java.sql.SQLException; import ja
BaseService.java ,这个我在工程中使用的抽象基类,spring3,hibernate3的服务层有这个就购了:

package com.jhzp.service.base;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import ly.tool.utils.ref.ReflectUtils;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.jhzp.domain.Article;
import com.jhzp.domain.base.BaseDomain;
import com.jhzp.domain.constant.DomainPagination;
import com.jhzp.service.info.ConstantService;

public abstract class BaseService {

protected Logger log=Logger.getLogger(getClass());

@Autowired
private SessionFactory sessionFactory;
@Autowired
private SqlMapClient sqlMapClient;
// @Autowired
// private ConstantService cs;

public SessionFactory getSessionFactory() {
return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Session getHibernateSession(){
return this.getSessionFactory().getCurrentSession();
}
protected HibernateTemplate getHibernateTemplate() {
return new HibernateTemplate(this.sessionFactory);
}

protected SqlMapClient getSqlMapClient() {
return sqlMapClient;
}

protected void setSqlMapClient(SqlMapClient sqlMapClient) {
this.sqlMapClient = sqlMapClient;
}
/**
* ibatis 动态参数查询 ,返回的类型要在sql语句及ibatis配置文件中写好
*/
@Transactional(readOnly=true)
public List ilist(String selectId,Map params) throws SQLException{
return this.getSqlMapClient().queryForList(selectId, params);
}

/**
* 20140707 新增通用方法
*/
@Transactional
public void delete(Class clazz,Integer id){
Object obj=this.getHibernateSession().get(clazz,id);
this.getHibernateSession().delete(obj);
}
@Transactional
public void update(BaseDomain domain){
this.getHibernateSession().update(domain);
}
@Transactional(readOnly=true)
public Object getById(Class clazz,Integer id){
return this.getHibernateSession().get(clazz, id);
}
@Transactional(readOnly=true)
public Object loadById(Class clazz,Integer id){
return this.getHibernateSession().load(clazz, id);
}
@Transactional
public void create(BaseDomain domain){
this.getHibernateSession().save(domain);
}

@Transactional(readOnly=true)
public List hlistByParams(Class dclazz,Map<String,Object> params){
log.info("传递的查询参数:"+params);
return this.queryByParam(dclazz, params);
}
@Transactional(readOnly=true)
public List hlistByHqlAndParams(String hql,Map<String,Object> params){
return this.queryByParamAndHql(hql, params);
}
/**
* 查询所所有
*/
// @Transactional(readOnly=true)
// public List hlistAll(Class dclazz,Map<String,Object> params){
// String hql="from "+dclazz.getSimpleName();
// Query query=getHibernateSession().createQuery(hql);
// return query.list();
//
// }
/**
* 分页,多参数查询
*/
@Transactional(readOnly=true)
public List hpage(Integer index,Integer len,Class dclazz,Map<String,Object> params){
if(null==index || 0>index)
index=0;
if(null==len || 0> len)
len=20;
log.info("传递的查询参数:"+params);
String hql="from "+dclazz.getSimpleName();
Query query=this.paramizeQuery(dclazz, params);
query.setFirstResult(index);
query.setMaxResults(len);
// query.
return query.list();
}

// @Transactional(readOnly=true)
// public List hlistByHqlAndParams2(String hql,Map<String,Object> params){
// return this.queryByParamAndHql(getHibernateSession(),hql, params);
// }

/**
* hibernate 动态参数查询 ,查询参数形式只能是: name=:name, 【如果name的值为空,则不传递】
* 其他形式的查询使用自定义hql的查询queryByParamAndHql
*/
private Query paramizeQuery(Class dclazz,Map<String,Object> params){
Session session=this.getHibernateSession();
String hql="from "+dclazz.getSimpleName();
if(null==params){
return session.createQuery(hql);
}
List<String> fnamel=ReflectUtils.listPropNames(dclazz);
Set<String> pnames=params.keySet();
List<String> pnamel=new ArrayList<String>();
Iterator<String> iter=pnames.iterator();
while(iter.hasNext()){
String pname=iter.next();
if(null!=params.get(pname) && fnamel.contains(pname)){
pnamel.add(pname);
}
}

if(pnamel.size()>0){
hql=hql+" where ";
}
StringBuilder sdb=new StringBuilder();
sdb.append(hql);
int count=0;
for(String pname:pnamel){
if(!fnamel.contains(pname)){
log.info(" pname: "+pname+"参数跟属性名不同,返回了,fnamel列表: "+fnamel);
return null;
}

if(count>0){
sdb.append(" and "+ pname+" = :"+pname);
}else
sdb.append(pname+" = :"+pname);
count++;
}
hql=sdb.toString();
log.info("打印生成的hql: "+hql);
Query query=session.createQuery(hql);
for(String pname:pnamel){
Object obj=params.get(pname);
if(obj instanceof String){
query.setParameter(pname, obj);
}else if(obj instanceof BigDecimal){
query.setBigDecimal(pname, ((BigDecimal)obj));
}else if(obj instanceof BigInteger){
query.setBigInteger(pname, ((BigInteger)obj));
}else if(obj instanceof Boolean){
query.setBoolean (pname, ((Boolean)obj));
}else if(obj instanceof Byte){
query.setByte(pname, ((Byte)obj));
}else if(obj instanceof Calendar){
query.setCalendar(pname, ((Calendar)obj));
}else if(obj instanceof Date){
log.info("应用到了"+Date.class);
query.setDate(pname, ((Date)obj));
}else if(obj instanceof Double){
query.setDouble(pname, ((Double)obj));
}else if(obj instanceof Integer){
query.setDouble(pname, ((Integer)obj));
}else if(obj instanceof Locale){
query.setLocale(pname, ((Locale)obj));
}else if(obj instanceof Long){
query.setLong(pname, ((Long)obj));
}else{
query.setParameter(pname, obj);
}

}
return query;
}

protected List queryByParam(Class dclazz,Map<String,Object> params){

return this.paramizeQuery(dclazz, params).list();
}

/**
* 自定义hql,传递动态的参数
*/
protected List queryByParamAndHql(String hql,Map<String,Object> params){
Session session=this.getHibernateSession();
log.info("进入自定义hql,动态参数查询");
Query query=session.createQuery(hql);
for(String pname:params.keySet()){
if(hql.contains(pname)){
Object obj=params.get(pname);
if(obj instanceof String){
query.setParameter(pname, obj);
}else if(obj instanceof BigDecimal){
query.setBigDecimal(pname, ((BigDecimal)obj));
}else if(obj instanceof BigInteger){
query.setBigInteger(pname, ((BigInteger)obj));
}else if(obj instanceof Boolean){
query.setBoolean (pname, ((Boolean)obj));
}else if(obj instanceof Byte){
query.setByte(pname, ((Byte)obj));
}else if(obj instanceof Calendar){
query.setCalendar(pname, ((Calendar)obj));
}else if(obj instanceof Date){
query.setDate(pname, ((Date)obj));
}else if(obj instanceof Double){
query.setDouble(pname, ((Double)obj));
}else if(obj instanceof Integer){
query.setDouble(pname, ((Integer)obj));
}else if(obj instanceof Locale){
query.setLocale(pname, ((Locale)obj));
}else if(obj instanceof Long){
query.setLong(pname, ((Long)obj));
}else{
query.setParameter(pname, obj);
}
}
}

try {
return query.list();
} catch (Exception e) {
e.printStackTrace();
log.error("第二查询出错了。");
return null;
}
}
protected List queryByParamAndHql2(String hql,Map<String,Object> params){
Session session=this.getHibernateSession();
Query query=session.createQuery(hql);
query.setProperties(params);
try {
return query.list();
} catch (Exception e) {
e.printStackTrace();
log.error("第二查询出错了。");
return null;
}
}
}

我的spring3,hibernate3 架构基础抽象类

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号