- 浏览: 104825 次
- 性别:
- 来自: 天津
文章分类
最新评论
-
sqz10200:
...
urlrewrite 路径重写 -
侯上校:
Very Good !!!学习了……
tomcat jndi -
Rocket076:
简明易懂,学习了
java读取Excel -
captmjc:
myreligion 写道
引用(5)、record_buf ...
mysql性能优化的参数简介 -
mathgl:
xiaoZ5919 写道引用
(4)、max_connecti ...
mysql性能优化的参数简介
<?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();
}
}
}
发表评论
-
算法的Java实现
2011-06-02 17:05 848引用: http://www.java3z.com/cwbw ... -
页面表格成Execl
2011-06-02 14:47 965ExcleBuild.java//生成Execl所要的类 ... -
数据连接池
2010-05-26 16:53 676数据库 -
(mysql,sqlserver,orcal)数据连接
2010-05-14 10:44 952<?xml version="1.0" ... -
汉语转拼音
2010-05-12 08:12 819//汉语转拼音的方法 protected String ... -
dom和xas解析xml
2010-04-14 17:37 847xas 解析import java.io.FileIn ... -
数据连接池源理
2010-03-15 14:31 1993近年来,随着Internet/Intr ... -
jdbc DBUtils
2010-03-15 14:09 1104package jing.lian.kui.util; ... -
jdbc连mysql数据库
2010-02-23 15:10 903package cn.com.sohocat.util; ... -
读xml
2010-02-23 15:08 859/** * @#ConstantDAO.java * ... -
java 报表
2010-02-23 15:06 1282JavaReport-V3-Enterprise-Releas ... -
根椐ip得到地址
2010-02-23 14:59 681把QQWry.Dat放到WebRoot/WEB-INF下 就 ... -
MD5加密
2010-02-23 14:52 681package com.here.web.common; ... -
java图片验证码
2010-02-23 14:51 890package com.here.web.common; ... -
处理中文时的乱码问题
2010-02-23 14:49 750最近在做一个类似于百度的提示功能,可是遇到了dwr给后台传值是 ... -
jspsmart.upload
2010-02-23 14:48 975上传图1.jsp <table id=&q ... -
添加cookie最近浏览过的5职位
2010-02-23 14:47 774关键字: cookie if(document.all){ ... -
fckeditor 编辑器 web.xml配置
2010-02-23 14:44 809<servlet> <ser ... -
log4j.properties 配置
2010-02-23 14:43 739## log4j.rootLogger=error, cons ... -
cookie
2010-02-23 14:41 818public class Cookies { public ...
相关推荐
接下来,一旦检测到Spring配置文件发生变化,我们需要重新加载配置文件。这可以通过Spring的`ApplicationContext`的`refresh()`方法来实现。`refresh()`会重新初始化Bean工厂,读取新的配置信息,并更新所有Bean的...
Spring配置文件的详细理解和灵活运用是每个Spring开发者的基础技能。它们提供了对应用程序结构的清晰控制,使得代码更加易于维护和测试。通过深入学习和实践,你将能够更好地驾驭Spring框架,实现高效的企业级开发。
这个配置文件用于告诉Struts如何使用Spring管理Action,通常通过`<struts:action>`标签指定Action的类,然后通过Spring的id引用该类。 在大型项目中,为了提高可维护性和可扩展性,配置文件通常会被分解成多个部分...
总的来说,Spring提供了多种方式来读取和管理配置文件,无论是类路径还是文件系统,甚至是跨环境的配置管理,都提供了灵活的解决方案。通过`ClassPathXmlApplicationContext`和`FileSystemXmlApplicationContext`,...
标题"hibernate+spring配置文件"指出了我们需要关注的重点,即如何将这两个框架协同工作。首先,我们需要在Spring的配置文件中引入Hibernate的相关bean,通常命名为`applicationContext.xml`。这个文件是Spring的IoC...
### Spring配置文件:整理与总结Spring中XML配置的最佳实践 #### 概述 Spring框架作为一个强大的Java应用框架,在企业级应用开发中占据了重要的地位。它为普通的Java对象(Plain Old Java Objects, POJOs)提供了...
在Spring Boot应用中,多模块配置文件的读取是一个重要的实践,它有助于提高代码的可维护性和模块化。本文将详细探讨如何在Spring Boot的多模块项目中管理和使用不同的配置文件,以实现低耦合的设计。 首先,了解...
本压缩包文件围绕"spring配置文件"展开,旨在帮助用户更好地理解和使用Spring框架的配置。 首先,Spring配置文件通常指的是XML格式的`applicationContext.xml`,这是Spring容器的主配置文件,用于定义bean的实例、...
整合SSH涉及到的主要配置文件有`struts2-spring-plugin.xml`、`spring-context.xml`以及Hibernate的相关配置文件(如`hibernate.cfg.xml`)。`struts2-spring-plugin.xml`配置Struts2与Spring的集成,确保Action类由...
在Spring中,配置文件通常采用XML格式,但随着版本的升级,也支持了基于注解的配置和Java配置类。在"applicationContext.xml"中,我们可以看到bean的声明,例如: ```xml ``` 这里,`id`属性是bean的唯一标识,`...
**Spring2 整合 DWR:将DWR配置融入Spring配置文件** Direct Web Remoting (DWR) 是一个开源的JavaScript库,它允许在浏览器和服务器之间进行实时、安全的双向通信,使得Web应用程序能够拥有类似桌面应用的交互性。...
1. **Mapper接口**:定义数据库操作的方法,与XML配置文件或注解对应。 2. **SQL映射文件**:编写具体的SQL语句,可以动态化处理,支持复杂的查询需求。 3. **MyBatis-Spring整合**:使MyBatis与Spring无缝集成,...
spring在英文里有春天、弹簧、跳跃和泉眼的意思。Spring 也表示是一个开源框架,是为了解决企业应用程序开发复杂性由Rod Johnson创建的。框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时...
在Spring 3.0框架中,配置文件是管理和组织应用程序行为的关键部分。Properties文件则常用于存储应用程序的配置参数,如数据库连接字符串、系统环境变量等。本篇将详细讲解如何在Spring 3.0的配置文件中加载...
redis-context.xml redis和spring配置,需要引用到自己项目的spring配置文件里面 redis.properties redis的配置项,ip一级连接池参数,注意:redis默认没有密码,所以我的spring配置文件把密码注释掉了,如果有密码...
Spring Junit单元测试加载配置文件失败问题解决方法 在软件开发过程中,单元测试是一个非常重要的步骤,它可以帮助开发者检测代码中的错误、提高代码的可靠性和稳定性。Spring Junit是一个常用的单元测试框架,它...
Hibernate通过XML配置文件或注解将Java对象(实体类)与数据库表进行映射,实现了数据的自动持久化。开发者可以通过简单的对象操作完成复杂的数据库操作,极大地提高了开发效率。 **Spring** 是一个全面的企业级...