关闭

我的Spring框架+Hibernate应用

标签: hibernatespring框架importdatestring
2282人阅读 评论(2) 收藏 举报
分类:

package **.dao.hibernate;

 

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Date;

import java.util.List;

import java.util.Map;

 

import org.apache.log4j.Logger;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

 

import  **.dao.EJEntityDAO;

import **.domain.EJEntity;

import **.domain.SysParameter;

import **.service.SysParameterService;

import **.utils.SpringContextLoader;

import **.utils.StringUtil;

 

public class HibernateEJEntityDAO extends BaseHibernateDAO implements

  EJEntityDAO {

 private Logger logger = Logger.getLogger(this.getClass().getName());

 

 public void insertEJ(EJEntity entity) {

  //向表中插入一条新记录

  this.getHibernateTemplate().save(entity);

 }

 

 public EJEntity getEJByID(String id) {

  //通过主键查找对应的单个实体 

  String hql = "select ej from EJEntity ej where ej.id=:id2";

  List list = this.getHibernateTemplate().findByNamedParam(hql,"id2",id);

  if(list.size()>0){

   return (EJEntity)list.get(0);

  }

  return null;

 }

 

 public List getEJByPostDate(String omh_gl_post_date) {

  //查找omh_gl_post_date即主机会计日期对应的所有记录项

  String hql = " from EJEntity where gl_post_date=:date";

  List list = this.getHibernateTemplate().findByNamedParam(hql,"date",omh_gl_post_date);

  return list;

 }

 

 public List getEJBeforePostDate(String omh_gl_post_date) {

  // TODO Auto-generated method stub

  String hql = "select ej from EJEntity ej where ej.gl_post_date<:date";

  List list = this.getHibernateTemplate().findByNamedParam(hql,"date",omh_gl_post_date);

  return list;

 }

 

 /**

  * 采用hibernatecriteria查询,可以灵活的设置查询条件

  * @author tianbo

  */

 public List getEJByParams(Map map){

  String sql =  "from EJEntity where 1=1 ";

  List nameList = new ArrayList();

  List object = new ArrayList();

 

     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S"); 

  String str = StringUtil.getStrFormMap(map,"fromDate");

  if(!str.equals("")){

   try {

    object.add(sdf.parse(str + "  00:00:00.0"));

    sql = sql + " and transaction_date>=:fromDate";

    nameList.add("fromDate");

   } catch (ParseException e) {

    e.printStackTrace();

   }

  }

 

  str = StringUtil.getStrFormMap(map,"toDate");

  if(!str.equals("")){

   try {

    object.add(sdf.parse(str + "   23:59:59.0"));

    sql = sql + " and transaction_date<:toDate";

    nameList.add("toDate");

   } catch (ParseException e) {

    e.printStackTrace();

   }

  }

 

  str = StringUtil.getStrFormMap(map,"userId");

  if(!str.equals("")){

   sql += " and user_id=:userId";

   nameList.add("userId");

   object.add(str);

  }

 

//  如果已经给出交易代号,则交易类别查询条件可以忽略!!

  str = StringUtil.getStrFormMap(map,"tracCode");

  if(!str.equals("")){

   sql += " and trans_code=:tracCode";

   nameList.add("tracCode");

   object.add(str);

  }else{

   str = StringUtil.getStrFormMap(map,"tracType");

   if(!str.equals("")){//未给出交易代号,但给出了交易类别,

//   按交易类别查询,即是交易代号的前两位是否等于交易类别。

    sql += " and trans_code like :tracType";

    nameList.add("tracType");

    object.add(str+"%"); // %表示任意长度任意字符串的通配符

   }

  }

 

  if(!StringUtil.getStrFormMap(map,"stime").equals("00:00") || !StringUtil.getStrFormMap(map,"etime").equals("00:00")){

   str = StringUtil.getStrFormMap(map,"stime");

   if(!str.equals("")){

    sql += " and actual_time>=:stime";

    nameList.add("stime");

    object.add(str);

   }

  

   str = StringUtil.getStrFormMap(map,"etime");

   if(!str.equals("")){

    sql += " and actual_time<=:etime";

    nameList.add("etime");

    object.add(str);

   }

  }

 

//  处理  是否曾经打印

//  str = StringUtil.getStrFormMap(map,"hasPrint");

//  if(!str.equals("")){

//   sql += " and " ;  

//  }

 

  str = StringUtil.getStrFormMap(map,"accountNo");

  if(!str.equals("")){

   sql += " and account_num=:accountNo";

   nameList.add("accountNo");

   object.add(str);

  }

 

  str = StringUtil.getStrFormMap(map,"lowAmount");

  if(!str.equals("")){

   try{

    object.add(new Double(Double.parseDouble(str)) );

    sql += " and amount>=:lowAmount";

    nameList.add("lowAmount");

   }catch(NumberFormatException ex){

    ex.printStackTrace();

   }

  }

 

  str = StringUtil.getStrFormMap(map,"highAmount");

  if(!str.equals("")){

   try{

    object.add(new Double(Double.parseDouble(str)) );

    sql += " and amount<=:highAmount";

    nameList.add("highAmount");

   }catch(NumberFormatException ex){

    ex.printStackTrace();

   }

  }

 

  str = StringUtil.getStrFormMap(map,"currencyType");

  if(!str.equals("")){

   sql += " and currency=:currencyType";

   nameList.add("currencyType");

   object.add(str);

  }

 

  str = StringUtil.getStrFormMap(map,"certId");

  if(!str.equals("")){

   sql += " and cert_num=:certId";

   nameList.add("certId");

   object.add(str);

  }

 

  str = StringUtil.getStrFormMap(map,"ischeckInOut");

  if(!str.equals("")){

   sql += " and dc_flag=:ischeckInOut";

   nameList.add("ischeckInOut");

   object.add(str);

  }

 

  str = StringUtil.getStrFormMap(map,"order");

  if(str.equals("") || str.equals("2")){ // 缺省和取值=2时为逆序

   sql += " order by id desc";  

  }

 

  String[] names = new String[nameList.size()];

  for(int i=0; i<nameList.size();i++){

   names[i] = (String)nameList.get(i); 

  } 

 

  if(names.length<=0)

   return getHibernateTemplate().find(sql);

  else

   return getHibernateTemplate().findByNamedParam(sql,names,object.toArray());  

 }

 

 public int removeEJ(EJEntity delEntity) {

  this.getHibernateTemplate().delete(delEntity); 

  return 1;

 }

 

 /**

  * 考虑到一个月的日志数据量必然很大,用自己的事务处理策略和批处理删除。

  * 摒弃了原先逐个调用this.getHibernateTemplate().delete(EJEntity)的低效方法。

  * @author tianbo

  */

 public int clearEJNow() { 

  SysParameterService service = (SysParameterService)SpringContextLoader.getBean("sysParameterService");

  SysParameter param = service.getParameterByCode("0002");

  int interval = 1;

  try {

   interval = Integer.parseInt(param.getParameterValue());

  } catch (NumberFormatException e) {

   e.printStackTrace();

  }

  logger.info("clear ej intervals"+interval);

        Calendar now = Calendar.getInstance();

        now.add(Calendar.DATE,-interval);   

  Date date = new Date(now.getTimeInMillis());

 

        String sdate = new SimpleDateFormat("yyyy-MM-dd").format(date);

        logger.info("all records before "+sdate+" will be cleared.");

 

  SessionFactory factory = this.getSessionFactory();

  Session session = factory.openSession();

  Transaction tx = session.beginTransaction();

  String hql = "delete from EJEntity where gl_post_date<:date";       

 

  int deletedEntities = session.createQuery( hql )

                            .setString( "date", sdate )

                            .executeUpdate();

        tx.commit();

        session.close();

        return deletedEntities;

 }

 

}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:152100次
    • 积分:2171
    • 等级:
    • 排名:第17470名
    • 原创:50篇
    • 转载:47篇
    • 译文:1篇
    • 评论:30条
    最新评论