首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring配置文件 跟测类

2012-11-19 
spring配置文件 和测类?xml version1.0 encodingUTF-8?beans?xmlnshttp://www.springframework

spring配置文件 和测类

<?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" 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" 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" 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" 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" ref="sessionFactory"/>
?</bean>
?
?<bean id="transactionInterceptor" 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 value="true"/>
?</bean>
?
?
?<!--
?<bean id="audienceAdvice" />切面 -->
?<!--
? ?<aop:config proxy-target-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();
??}
?}

}

热点排行