spring+hibernate+struts整合(1)
spring+hibernate:整合
步骤1:引入类包
如下图:这里是所有的类包,为后面的struts整合考虑
步骤2:修改web.xml
在web.xml中加入下面的配置
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value>//这个beans.xml是在src目录下建立的文件,具体下面会陈述 </context-param> <!-- 对Spring容器进行实例化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
步骤3:建立实体类并建立hibernate的配置文件
package com.test.model; public class user { private int id; private String username; private String password; private String age; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public void setUsername(String username) { this.username = username; } public String getUsername() { return username; } public void setId(int id) { this.id = id; } public int getId() { return id; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.test.model"> <class name="user" table="[User]"> <cache usage="read-write" region="com.test.model.user"/> <id name="id"> <generator class="identity"/> </id> <property name="username" column="username" not-null="true"/> <property name="password" column="password" not-null="true"/> <property name="age" column="age" not-null="false"/> </class> </hibernate-mapping>
步骤4:建立beans.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/> <property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=Hibernate"/> ////数据库链接 <property name="username" value="sa"/> <property name="password" value="123456"/> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1"/> <!-- 连接池的最大值 --> <property name="maxActive" value="500"/> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2"/> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>com/test/model/user.hbm.xml</value> //////这个是实体类的hibernate配置文件 </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.SQLServerDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=false hibernate.format_sql=false hibernate.cache.use_second_level_cache=true hibernate.cache.use_query_cache=false hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider </value> </property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> <bean id="userService" class="com.test.implement.userServiceBean"/> ///// </beans>
步骤5:定义一个userService接口,并实现它。
package com.test.Interface; import java.util.List; import com.test.model.user; public interface userService { public List<user> getAll(); }
package com.test.implement; import java.util.List; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.transaction.annotation.Transactional; import com.test.Interface.userService; import com.test.model.user; public class userServiceBean implements userService{ @Resource private SessionFactory sessionFactory; @Transactional public List<user> getAll() { return sessionFactory.getCurrentSession().createQuery("from user").list(); } }
步骤6:单元测试
package com.test.test; import java.util.List; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.test.Interface.userService; import com.test.model.user; import junit.framework.TestCase; public class userTest { private static userService userservice; @BeforeClass public static void setUpBeforeClass() throws Exception { ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); userservice=(userService) ac.getBean("userService"); } @Test public void testGetAll() { List<user> users=userservice.getAll(); for (user item : users) { System.out.print("Id:"+item.getId()+"\t"); System.out.print("username:"+item.getUsername()+"\t"); } } }
错误1:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
原因:在实现的地方,没有加入事务
解决办法:在对应方法上面加@Transactional
到此,spring和hibernate整合成功