bean.xml
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- dbcp数据源bean -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=db_sx"/>
<property name="username" value="sa"/>
<property name="password" value="sql2008"/>
</bean>
<!-- Hibernate SessionFactory Bean -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>com/syyyy/lu/sx/model/Teacher.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="format_sql">true</prop>
<prop key="show_sql">true</prop>
<prop key="cache.use_query_cache">true</prop>
<prop key="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
<prop key="cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
<bean id="sessionFactoryBean" class="com.syyyy.lu.util.HibernateSession">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<context:component-scan base-package="com.syyyy.test"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
获取Session对象工具类:HibernateSession.java
package com.syyyy.lu.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HibernateSession {
private SessionFactory sessionFactory;
public static Session session;
public static Session getSession(){
if(session==null){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
HibernateSession hibernateSession=(HibernateSession)ctx.getBean("sessionFactoryBean");
SessionFactory sessionFactory = hibernateSession.getSessionFactory();
session = sessionFactory.getCurrentSession();
return session;
}else{
return session;
}
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public static void setSession(Session session) {
HibernateSession.session = session;
}
}
测试类:test.java
package com.syyyy.test;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.syyyy.lu.sx.model.Teacher;
import com.syyyy.lu.util.HibernateSession;
public class test{
@Test
public void mytest(){
Session session= HibernateSession.getSession();
session.beginTransaction();
List<Teacher> list = (List<Teacher>)session.createQuery("from Teacher").list();
System.out.println(list.size());
}
}