`
jingliankui123
  • 浏览: 104801 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

spring配置文件 和测类

    博客分类:
  • java
阅读更多

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

 
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://10.0.0.58:3306/HereOnLine"/>
  <property name="username" value="root"/>
  <property name="password" value="soho2010here"/>
  <!--
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://10.50.8.196:3306/HereOnLine"/>
  <property name="username" value="hereonline"/>
  <property name="password" value="000000"/>
  <property name="url" value="jdbc:mysql://192.168.8.199:3306/HereOnLine"/>
  <property name="username" value="soho"/>
  <property name="password" value="000000"/>
   -->
 
 </bean>

 
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">false</prop>
    <prop key="hibernate.current_session_context_class">thread</prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
    <!-- ************************************************ -->
       <value>cn/com/sohocat/pojo/HoAdministrator.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoGroup.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoMenu.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoRole.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoUser.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoAdminGroup.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoGroupRole.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoUserRole.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoLanguageCode.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoCorpusZhEn.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoTerminologyZhEn.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoTermCategory.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoAnnouncement.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoVisitStatistics.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoScore.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoScoreRecord.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoFeedBack.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoDownloadStatistics.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoTrafficStatistics.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoHistoryStatistics.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoUserSohoVip.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoUserSohoTeam.hbm.xml</value>
       <value>cn/com/sohocat/pojo/HoUserSohoVipTeam.hbm.xml</value>
   </list>
  </property>
 </bean>
  
  <!-- ********************************************************************************* -->
 <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://10.0.0.58:3306/SOHOCat"/>
  <property name="username" value="root"/>
  <property name="password" value="soho2010here"/>
  
 </bean>
 
 <bean id="sessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource2"/>
  <property name="hibernateProperties">  
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">false</prop>
    <prop key="hibernate.current_session_context_class">thread</prop>
   </props>
  </property>
  
  <property name="mappingResources">
   <list>    
       <value>cn/com/sohocat/pojo/GlossaryChecked.hbm.xml</value>
       <value>cn/com/sohocat/pojo/CorporaChecked.hbm.xml</value>
       <value>cn/com/sohocat/pojo/CorporaEsEn.hbm.xml</value>
       <value>cn/com/sohocat/pojo/GlossaryEnEs.hbm.xml</value>
   </list>
  </property>
 </bean>
 
 
 <!-- ********************************************************************************* -->
  
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"/>
 </bean>
 
 <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="transactionAttributes"> 
     <props>
      <prop key="add*">PROPAGATION_REQUIRED</prop>
      <prop key="insert*">PROPAGATION_REQUIRED</prop>
      <prop key="modify*">PROPAGATION_REQUIRED</prop>
      <prop key="execute*">PROPAGATION_REQUIRED</prop>
      <prop key="remove*">PROPAGATION_REQUIRED</prop>
      <prop key="delete*">PROPAGATION_REQUIRED</prop>
      <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
     </props>
    </property>
   </bean>
 
 <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">
   <list>
    <value>i*</value>
   </list>
  </property>
  <property name="interceptorNames">
   <list>
    <value>transactionInterceptor</value>
   </list>
  </property>
  <property name="proxyTargetClass" value="true"/>
 </bean>
 
 
 <!--
 <bean id="audienceAdvice" class="cn.com.sohocat.aspectj.Audience" />切面 -->
 <!--
   <aop:config proxy-target-class="true">
    <aop:aspect ref="audienceAdvice">
     <aop:pointcut id="performance" expression="execution(* cn.com.sohocat.api.*.*(..))"/> 切点
     <aop:before method="adviceBefore" pointcut-ref="performance"/>
     <aop:after method="adviceAfter" pointcut-ref="performance"/>
     <aop:after-throwing method="adviceFailure" pointcut-ref="performance"/>
    </aop:aspect>
   </aop:config>
  -->
 
 
 </beans>

 

测式类

/**
 * @此方法描述的是:
 * @Dec 9, 2009
 */
package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.sohocat.api.IHoAdmin;
import cn.com.sohocat.api.IHoAnnouncement;
import cn.com.sohocat.api.IHoLanguage;
import cn.com.sohocat.api.IHoUser;
import cn.com.sohocat.mail.IMail;
import cn.com.sohocat.pojo.HoAnnouncement;
import cn.com.sohocat.util.BaseDao;
import cn.com.sohocat.util.BeanHelp;

/**
 * @author blaiu
 * @Dec 9, 2009
 * @
 */
public class Test extends BaseDao{

    private static ApplicationContext  ac = null;
    static{
      String[] locations = {"applicationContext.xml", "applicationContext-common.xml"};
      ac = new ClassPathXmlApplicationContext(locations);
    }
   
    public void kk() {
     queryByHQL("from HoRole");
    }
   
    public static void main(String[] args) throws Exception{
     
     IMail iMail = (IMail) BeanHelp.getBean("iMail");
     IHoUser iHoUser = (IHoUser) BeanHelp.getBean("iHoUser");
     IHoAdmin iHoAdmin = (IHoAdmin) BeanHelp.getBean("iHoAdmin");
     IHoLanguage iHoLanguage = (IHoLanguage) BeanHelp.getBean("iHoLanguage");
     IHoAnnouncement iHoAnnouncement = (IHoAnnouncement) BeanHelp.getBean("iHoAnnouncement");
     
  HoAnnouncement a = iHoAnnouncement.queryHoByAnnouncementId("1");
        String mesg = "2010-02-22";
       
  
  boolean bol = iHoAnnouncement.updateHoAnnouncement(a);
//  if(bol){
//   BuildNewHtml build = new BuildNewHtml();
//   build.removeFile(a);
//   build.init2(a);
//   build.init();
//  }
  System.out.println(bol);
    }
}

 

有事物

BaseDao.class

/**
 * @此方法描述的是:
 * @Nov 12, 2009
 */
package cn.com.sohocat.util;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * @author blaiu
 * @Nov 12, 2009
 * @
 */

public class BaseDao extends HibernateDaoSupport {
 
 protected Log log = LogFactory.getLog(this.getClass());
 
 /**
  *
  * 此方法描述的是:获得当前Hibernate 中的session
  * @return
  * Session
  */
 public Session getHibernateSession() {
  SessionFactory sessionFactory = this.getHibernateTemplate().getSessionFactory();
  sessionFactory.getStatistics().logSummary();//调用logSummary() 在日志中记录(info级别)其总结
  return sessionFactory.getCurrentSession();
 }
 
 /**
  *
  * 此方法描述的是:新增一个新的对象实体
  * @param object
  * void
  */
 public boolean insert(Object object) {
  Session session = this.getHibernateSession();
  try {
   session.save(object);
   return true;
  } catch (HibernateException e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  *
  * 此方法描述的是:删除一个对象实体
  * @param object
  * void
  */
 public void delete (Object object) {
  Session session = this.getHibernateSession();
  session.delete(object);
 }
 
 /**
  *
  * 此方法描述的是:通过一组id, 批量删除对象实体
  * @param ids
  * @param objectClass
  * void
  */
 public void deleteByIds (String[] ids, Class objectClass) {
  Session session = this.getHibernateSession();
  for (int i=0; i<ids.length; i++) {
   Object object = session.get(objectClass, Integer.valueOf(ids[i]));
   session.delete(object);
  }
 }
 
 
 
 /**
  *
  * 此方法描述的是:更新一个对象实体
  * @param object
  * @throws HibernateException
  * void
  */
 public boolean update (Object object) {
  Session session = this.getHibernateSession();
  try {
   session.update(object);
   return true;
  } catch (HibernateException e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  *
  * 此方法描述的是:更新一个对象实体
  * @param object
  * @throws HibernateException
  * void
  */
 public boolean update (Object object1, Object object2) {
  Session session = this.getHibernateSession();
  try {
   session.update(object1);
   session.update(object2);
   return true;
  } catch (HibernateException e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  *
  * 此方法描述的是:更新一个对象实体
  * @param object
  * @throws HibernateException
  * void
  */
 public boolean saveOrUpdate (Object object) {
  Session session = this.getHibernateSession();
  try {
   session.saveOrUpdate(object);
   return true;
  } catch (HibernateException e) {
   e.printStackTrace();
   return false;
  }
 }
 
 
 /**
  *
  * 此方法描述的是:批量更新对象实体
  * @param list
  * void
  */
 public void update (List list) {
  int count = 0;
  Session session = this.getHibernateSession();
  for (int i=0; i<list.size(); i++) {
   count++;
   Object object = list.get(i);
   session.save(object);
   if (count%20 == 0) {
    session.flush();
    session.evict(object);
   }
  }
 }
 
 /**
  *
  * 此方法描述的是:hibernate SQL 查询
  * @param HQL
  * @return
  * List
  */
 public List queryByHQL (String HQL) {
  Session session = this.getHibernateSession();
  return session.createQuery(HQL).list();
 }
 
 /**
  *
  * 此方法描述的是:sql 查询
  * @param SQL
  * @return
  * List
  */
 public List queryBySQL (String SQL) {
  Session session = this.getHibernateSession();
  return session.createSQLQuery(SQL).list();
 }
 
 /**
  *
  * 此方法描述的是:sql 查询
  * @param SQL
  * @return
  * List
  */
 public List queryBySQL (String SQL, Class objectClass) {
  Session session = this.getHibernateSession();
  return session.createSQLQuery(SQL).addEntity(objectClass).list();
 }
 
 /**
  *
  * 此方法描述的是:按Id查询
  * @param id
  * @param objectClass
  * @return
  * Object
  */
 public Object queryById (String id, Class objectClass ) {
  Session session = this.getHibernateSession();
  return session.get(objectClass, id);
 }
 
 /**
  *
  * 此方法描述的是:查询指定两个时间之间的记录
  * @param HQL
  * @param startDate
  * @param endDate
  * @return
  * List
  */
 public List queryByDate (String HQL, Date startDate, Date endDate) {
  Session session = this.getHibernateSession();
  return session.createQuery(HQL).setDate(0, startDate).setDate(2, endDate).list();
 }
 
 /**
  *
  * 此方法描述的是:范围时间内的查询
  * @param HQL
  * @param date
  * @return
  * List
  */
 public List queryByDate (String HQL, Date date) {
  Session session = this.getHibernateSession();
  return session.createQuery(HQL).setDate(0, date).list();
 }
 
 /**
  *
  * 此方法描述的是:查询当前时间下前count条记录
  * @param HQL
  * @param count
  * @return
  * List
  */
 public List queryByHQL (String HQL, int count) {
  Session session = this.getHibernateSession();
  return session.createQuery(HQL).setMaxResults(count).setFirstResult(0).list();
 }
 
 /**
  *
  * 此方法描述的是:执行增,删,改SQL语句
  * @param SQL
  * @return
  * boolean
  */
 public boolean executeSQL (String SQL) {
  Session session = this.getHibernateSession();
  try {
   return !session.connection().createStatement().execute(SQL);
  } catch (HibernateException e) {
   e.printStackTrace();
   return false;
  } catch (SQLException e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  *
  * 此方法描述的是:查询记录总数
  * @param HQL
  * @return
  * int
  */
 public int getCount(String HQL) {
  Session session = this.getHibernateSession();
  try {
   String HQL_rows = "select count(*) " + HQL.substring(HQL.indexOf("from"));
   return Integer.valueOf(session.createQuery(HQL_rows).list().get(0).toString());
  } catch (HibernateException e) {
   e.printStackTrace();
   return -1;
  }
 }
 
 /**
  *
  * 此方法描述的是:查询记录总数
  * @param HQL
  * @param date
  * @return
  * int
  */
 public int getCount(String HQL, Date date) {
  Session session = this.getHibernateSession();
  try {
   String HQL_rows = "select count(*) " + HQL.substring(HQL.indexOf("from"));
   return Integer.valueOf(session.createQuery(HQL_rows).setDate(0, date).list().get(0).toString());
  } catch (HibernateException e) {
   e.printStackTrace();
   return -1;
  }
 }
 
 
 
 /**
  *
  * 此方法描述的是:分页查询
  * @param HQL
  * @param firstResult
  * @param maxResults
  * @return
  * List
  */ 
 public List findPageResult(String HQL, int firstResult, int maxResults){
  Session session = this.getHibernateSession();
  try{
   return session.createQuery(HQL).setFirstResult(firstResult).setMaxResults(maxResults).list();
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }
 }
 
 /**
  *
  * 此方法描述的是:分页查询
  * @param HQL
  * @param firstResult
  * @param maxResults
  * @return
  * List
  */ 
 
 public List findPageResultBySQL(String SQL,int firstResult,int maxResults,Class class1){
  Session session = this.getHibernateSession();
  try{
   return session.createSQLQuery(SQL).addEntity(class1).setFirstResult(firstResult-1).setMaxResults(maxResults).list();
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }
 }
 
 public List findPageResultBySQL(String SQL,int firstResult,int maxResults){
  Session session = this.getHibernateSession();
  try{
   return session.createSQLQuery(SQL).setFirstResult(firstResult-1).setMaxResults(maxResults).list();
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }
 }
 
 public int updateByHQL(String HQL){
  Session session = this.getHibernateSession();
  try{
   return session.createQuery(HQL).executeUpdate();
  } catch (HibernateException e) {
   e.printStackTrace();
   return -1;
  }
 }
 
 
 /**
  *
  * 此方法描述的是:按Id查询
  * @param id
  * @param objectClass
  * @return
  * Object
  */
 public Object queryById (int id, Class objectClass ) {
  Session session = this.getHibernateSession();
  return session.get(objectClass, id);
 }
 
 /**
  *
  * 此方法描述的是:按Id查询
  * @param id
  * @param objectClass
  * @return
  * Object
  */
 public Object queryById (long id, Class objectClass ) {
  Session session = this.getHibernateSession();
  return session.get(objectClass, id);
 }
 
 
 /**
  * @Title: query
  * @Description: 按指定SQL 查询
  * @param SQL SQL中指定自己需要返回的column
  * @param columnSize SQL中指定column的大小
  * @return Map<Object,Object> 返回map,map的key为指定列加序号,Hashtable不容许重复的key,所以用key加序号来标识列
  * @throws
  * @version 创建时间:Feb 3, 2010 9:55:02 AM
  */
 public Map<Object, Object> query(String SQL, int columnSize){
  Map<Object, Object> items = new Hashtable<Object, Object>();  
  PreparedStatement pStmt = null;  
     ResultSet rs = null;
     //标识不同列的Id
     int j = 0;
  try {
   pStmt = this.getHibernateSession().connection().prepareStatement(SQL);  
   rs = pStmt.executeQuery();
   while(rs.next()){
             for(int i = 1; i <= columnSize; i++)   {
                 items.put(rs.getMetaData().getColumnName(i)+ j, rs.getObject(i) == null ? "" : rs.getString(i).toString());
                 //log.debug("1--->"+rs.getMetaData().getColumnName(i));
                 //log.debug("2--->"+rs.getObject(i) == null ? "" : rs.getString(i).toString());
             }
             j++;
            }
  } catch (HibernateException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  } finally{
   try {
    rs.close();
    pStmt.close();
    rs = null;
    pStmt = null;
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  return items;
 }

  
}

 

没有事物的BaseDao.class

package cn.com.hereonline.admin.dao;

 

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.com.hereonline.admin.bean.HoDictionary;
import cn.com.hereonline.admin.bean.expand.Page;
import cn.com.hereonline.util.PageUtil;


public class BaseDAO extends HibernateDaoSupport{

 //获得session
 public Session getHibernateSession() {
  //return this.getHibernateTemplate().getSessionFactory().getCurrentSession();
  return this.getHibernateTemplate().getSessionFactory().openSession();
  
 }
 //新增
 public boolean insert(Object entity) {
  Session session = this.getHibernateSession();
  try {
   Transaction tr = session.beginTransaction();
   session.save(entity);
   tr.commit();
   return true;
  } catch (HibernateException e) {
   e.printStackTrace();
   return false;
  }finally{
   session.close();
  }
  
 }
 
 
 public boolean executeSQL (String SQL){
  Session session = this.getHibernateSession();
  try {
   Transaction tr = session.beginTransaction();
   boolean bb = session.connection().createStatement().execute(SQL);
   tr.commit();
   return bb;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }finally{
   session.close();
  }
 }
 
 
 //新增根据id查询
 public Object queryById(String id, Class entityClass) {
  Session session = this.getHibernateSession();
  try {
   return session.get(entityClass,id);
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }finally{
   session.close();
  }
 }
 //删除对象
 public void delete(Object entity) {
  Session session = this.getHibernateSession();
  try {
   session.delete(entity);
  } catch (HibernateException e) {
   e.printStackTrace();
  }finally{
   session.close();
  }
 }
 
 
 public List queryArea(String HQL, String language){
  Session session = this.getHibernateSession();
  try {
   session.enableFilter("language").setParameter("myFilterParam", language);
   return session.createQuery(HQL).list();
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }finally{
   session.close();
  }
 }
 
 
 public boolean delete(String id, Class entityClass) {
  Session session = this.getHibernateSession();
  try {
   Transaction tr = session.beginTransaction();
   Object entity = session.get(entityClass, Integer.valueOf(id));
   session.delete(entity);
   tr.commit();
   return true;
  } catch (HibernateException e) {
   e.printStackTrace();
   return false;
  }finally{
   session.close();
  }
 }
 
 
 //根据SQL语句来删除对象
 public void delete(String sql) {
  Session session = this.getHibernateSession();
  try {
    Query query = session.createQuery(sql);
    query.executeUpdate();
  } catch (HibernateException e) {
   e.printStackTrace();
  }finally{
   session.close();
  }
 }
 //使用HQL语句删除数据 
 public void deleteByHQL(String hql){ 
  Session session = this.getHibernateSession();
    
  try {
   Query query = session.createQuery(hql) ; 
      query.executeUpdate() ; 
     
  } catch (HibernateException e) {
   e.printStackTrace();
  }finally{
   session.close();
  }
 }
 
 //使用HQL语句删除数据 
 public boolean deleteByHQLReturn(String hql){ 
  Session session = this.getHibernateSession();
  
  try {
   Query query = session.createQuery(hql) ; 
   query.executeUpdate() ; 
   return true;
  } catch (HibernateException e) {
   e.printStackTrace();
   return false;
  }finally{
   session.close();
  }
 }
 
 //更新对象
 public void update(Object entity) {
  Session session = this.getHibernateSession();
  try {
   session.update(entity);
   session.flush();
  } catch (HibernateException e) {
   e.printStackTrace();
  }finally{
   session.close();
  }
 }
 
 //根据SQL语句来更新对象
 public void update(String sql,int i) {
  Session session = this.getHibernateSession();
  try {
   Query query = session.createQuery(sql);
    query.executeUpdate();
  } catch (HibernateException e) {
   e.printStackTrace();
  }finally{
   session.close();
  }
 }
 //根据HQL 查询
 
 //根据SQL语句来更新对象
 public void insertBySQL(String sql) {
  Session session = this.getHibernateSession();
  try {
   Query query = session.createQuery(sql);
   query.executeUpdate();
  } catch (HibernateException e) {
   e.printStackTrace();
  }finally{
   session.close();
  }
 }
 //根据HQL 查询
 public List queryByHQL(String HQL) {
  Session session = this.getHibernateSession();
  try {   
   return session.createQuery(HQL).setMaxResults(10000).list();
  } catch (Exception e) {
   e.printStackTrace();
   return null;
  }
  finally{
   session.close();
  }
 }
 
 
 
 public void getName(String HQL) {
  Session session = this.getHibernateSession();
  try {
   session.createQuery(HQL).executeUpdate();
  } catch (HibernateException e) {
   e.printStackTrace();
  }finally{
   session.close();
  }
 }
 
 //根据id查询对象
 public Object queryById(long id, Class entityClass) {
  Session session = this.getHibernateSession();
  try {
   return session.get(entityClass,id);
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }finally{
   session.close();
  }
 }
 //根据id查询对象
 public Object queryById(int id, Class entityClass) {
  Session session = this.getHibernateSession();
  try {
   return session.get(entityClass,id);
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }finally{
   session.close();
  }
 }
 
 public List queryByHQL(String hsql, int count) {
  Session session = this.getHibernateSession();
  try {
   return session.createQuery(hsql).setMaxResults(count).setFirstResult(0).list();
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }finally{
   session.close();
  }
 }
 
 public List queryByHQL(String hql, Page page) {
  Session session = this.getHibernateSession();
  if(page.getTotalRecords() == 0){
   String hqlTemp = "select count(id) " + hql;
   long start = System.currentTimeMillis();
   int totalRecords=((Number)session.createQuery(hqlTemp).iterate().next()).intValue();
   System.out.println("totalRecords:" + totalRecords + ",time:" + (System.currentTimeMillis()-start)/1000);
   if(totalRecords == 0){
    return new ArrayList();
   }
   page.setTotalRecords(totalRecords);
  }
  
  PageUtil.createPage(page, page.getTotalRecords());
  try {
   long start = System.currentTimeMillis();
   List ls = session.createQuery(hql).setFirstResult(page.getBeginIndex()).setMaxResults(page.getEveryPage()).list();
   System.out.println("ls.size:" + ls.size() + ",time:" + (System.currentTimeMillis()-start)/1000);
   return ls;
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }finally{
   session.close();
  }
 }
 
 public List queryBySQL(String sql) {
  Session session = this.getHibernateSession();
  try {
   Query query1 = session.createSQLQuery(sql);
   return query1.list();
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }finally{
   session.close();
  }
 }
 
 public boolean updateBySQL(String sql) {
  Session session = this.getHibernateSession();
  Transaction tr = session.beginTransaction();
   try {
    boolean b = session.connection().createStatement().execute(sql);
    tr.commit();
    return true;
   } catch (SQLException e) {
    e.printStackTrace();
    tr.rollback();
    return false;
   }finally{
   session.close();
   }
 }
 
 public List queryBySQL(String sql, Class entityclass1) {
  Session session = this.getHibernateSession();
  try {
   Query query1 = session.createSQLQuery(sql).addEntity(entityclass1);
   return query1.list();
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  } finally {
   session.close();
  }
 }
 
 public List queryBySQL(String sql, Class entityclass1, int count) {
  Session session = this.getHibernateSession();
  try {
   Query query1 = session.createSQLQuery(sql).addEntity(entityclass1).setFirstResult(0).setMaxResults(count);
   return query1.list();
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  } finally {
   session.close();
  }
 }
 
 //根据sql语句查询返回单个属性值
 public int queryHQL(String hsql) {
  Session session = this.getHibernateSession();
  try {
   System.out.println("***basedao **session**11*"+session);
   List list = session.createQuery(hsql).list();
   System.out.println("***basedao **session**22*"+session);
   return (Integer)list.get(0);
  } catch (HibernateException e) {
   e.printStackTrace();
   return 1;
  }finally{
   session.close();
  }
 }
 
 public Object queryByName(String name, Class entityClass) {
  Session session = this.getHibernateSession();
  try {
   return session.get(entityClass,name);
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }finally{
   session.close();
  }
 }
 //hibernate分页,根据(HQL ,起始位置,和返回的记录数)
 public List queryByHQL(String HQL, int firstCount, int maxCount) {
  Session session = this.getHibernateSession();
  try {
   return session.createQuery(HQL).setFirstResult(firstCount).setMaxResults(maxCount).list();
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }finally{
   session.close();
  }
 }
 
 //hibernate分页,根据(HQL ,起始位置,和返回的记录数,日期条件)
 public List queryByHQL(String HQL, int firstCount, int maxCount, Date date) {
  Session session = this.getHibernateSession();
  try {
   return session.createQuery(HQL).setDate(0, date).setFirstResult(firstCount).setMaxResults(maxCount).list();
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }finally{
   session.close();
  }
 }
 //hibernate分页,根据(HQL ,起始位置,和返回的记录数,日期条件,日期条件)
 public List queryByHQL(String HQL, int firstCount, int maxCount, Date startDate, Date endDate) {
  Session session = this.getHibernateSession();
  try {
   return session.createQuery(HQL)
        .setDate(0, startDate)
        .setDate(1, endDate)
        .setFirstResult(firstCount)
        .setMaxResults(maxCount)
        .list();
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  }finally{
   session.close();
  }
 }
 
 /**
  *
  * 此方法描述的是:过滤语言检索
  * @version 创建时间:2009-8-17 下午02:36:09
  */
 public List queryFilter(String HQL, String language) {
  Session session = this.getHibernateSession();
  try {
   session.enableFilter("language")
       .setParameter("myFilterParam",language);
   return session.createQuery(HQL).list();
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  } finally {
   session.close();
  }
 }
 /**
  *
  * 此方法描述的是:过滤语言检索
  * @version 创建时间:2009-8-17 下午02:36:09
  */
 public List queryFilter(String HQL, String language,int maxCount) {
  Session session = this.getHibernateSession();
  try {
   session.enableFilter("language")
       .setParameter("myFilterParam",language);
   return session.createQuery(HQL).setMaxResults(maxCount).list();
  } catch (HibernateException e) {
   e.printStackTrace();
   return null;
  } finally {
   session.close();
  }
 }

}

分享到:
评论

相关推荐

    S变换+Sockwell R G , Mansinha L , Lowe R P . Localization of the complex spectrum: the S transformJ

    s变换用的高斯窗函数( 高斯窗是指数窗的一种,它也无负的旁瓣,而且没有旁瓣波动,因而不回引起计算谱中假的极大值或极小值,而且高斯窗频率窗函数的主瓣比指数窗的主瓣窄,分辨率比指数窗有所提高。

    2021科大讯飞车辆贷违预测大赛冠军源码+全部资料.zip

    2021科大讯飞车辆贷违预测大赛冠军源码+全部资料.zip [资源说明] 1、该项目是团队成员近期最新开发,代码完整,资料齐全,含设计文档等 2、上传的项目源码经过严格测试,功能完善且能正常运行,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的高校学生、教师、科研工作者、行业从业者下载使用,可借鉴学习,也可直接作为毕业设计、课程设计、作业、项目初期立项演示等,也适合小白学习进阶,遇到问题不懂就问,欢迎交流。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 5、不懂配置和运行,可远程教学 欢迎下载,学习使用!

    AI图像处理工具包-一键抠图、背景切换、旧照片修复、人像漫画化、视频卡通化(Python+OpenCV+Dlib+TensorFlow).zip

    AI图像处理工具包-一键抠图、背景切换、旧照片修复、人像漫画化、视频卡通化(Python+OpenCV+Dlib+TensorFlow).zip [资源说明] 1、该项目是团队成员近期最新开发,代码完整,资料齐全,含设计文档等 2、上传的项目源码经过严格测试,功能完善且能正常运行,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的高校学生、教师、科研工作者、行业从业者下载使用,可借鉴学习,也可直接作为毕业设计、课程设计、作业、项目初期立项演示等,也适合小白学习进阶,遇到问题不懂就问,欢迎交流。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 5、不懂配置和运行,可远程教学 欢迎下载,学习使用!

    基于java+springboot+vue+mysql的远程教育网站设计与实现.docx

    基于java+springboot+vue+mysql的远程教育网站设计与实现.docx

    springboot005学生心理咨询评估系统(源码+数据库+论文+PPT+包调试+一对一指导)

    毕业设计资料,计算机毕业设计,源码,毕业论文,毕业答辩,答辩PPT,Java毕业设计,php毕业设计,ASP.NET毕业设计,毕业指导,计算机作业,php作业,java作业,ASP.NET作业,编程作业,管理系统,网站,app,毕业设计学习,Java学习,php学习,ASP.NET学习,java课程,php课程,ASP.NET课程,答辩技巧,SQLSERVER数据库,Mysql数据库,jdbc,SSM框架,SpringBoot框架,Html5,小程序

    蓝牙串口助手,可以连接HC-05等蓝牙模块,实现单片机设备与手机通讯,安卓手机,蓝牙调试助手,具有按键功能!

    蓝牙串口助手,可以连接HC-05等蓝牙模块,实现单片机设备与手机通讯,安卓手机,蓝牙调试助手,具有按键功能!

    TriLib-2-Model-Loading-Package-2.3.7.unitypackage

    TriLib 2 是一个跨平台的运行时 3D 模型导入器

    “人力资源+大数据+薪酬报告+涨薪调薪”

    人力资源+大数据+薪酬报告+涨薪调薪,在学习、工作生活中,越来越多的事务都会使用到报告,通常情况下,报告的内容含量大、篇幅较长。那么什么样的薪酬报告才是有效的呢?以下是小编精心整理的调薪申请报告,欢迎大家分享。相信老板看到这样的报告,一定会考虑涨薪的哦。

Global site tag (gtag.js) - Google Analytics