spring+hibernate+struts整合(1)
生活随笔
收集整理的這篇文章主要介紹了
spring+hibernate+struts整合(1)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://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"/> 數(shù)據(jù)庫鏈接<property name="username" value="sa"/><property name="password" value="123456"/><!-- 連接池啟動時的初始值 --><property name="initialSize" value="1"/><!-- 連接池的最大值 --><property name="maxActive" value="500"/><!-- 最大空閑值.當(dāng)經(jīng)過一個高峰時間后,連接池可以慢慢將已經(jīng)用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 --><property name="maxIdle" value="2"/><!-- 最小空閑值.當(dāng)空閑的連接數(shù)少于閥值時,連接池就會預(yù)申請去一些連接,以免洪峰來時來不及申請 --><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.SQLServerDialecthibernate.hbm2ddl.auto=updatehibernate.show_sql=falsehibernate.format_sql=falsehibernate.cache.use_second_level_cache=truehibernate.cache.use_query_cache=falsehibernate.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接口,并實現(xiàn)它。
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;@BeforeClasspublic static void setUpBeforeClass() throws Exception {ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");userservice=(userService) ac.getBean("userService");}@Testpublic 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
原因:在實現(xiàn)的地方,沒有加入事務(wù)
解決辦法:在對應(yīng)方法上面加@Transactional
?
到此,spring和hibernate整合成功
轉(zhuǎn)載于:https://www.cnblogs.com/shenghaishiweini/p/3331593.html
總結(jié)
以上是生活随笔為你收集整理的spring+hibernate+struts整合(1)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 东风本田思铭车头是本田标志,而方向盘不是
- 下一篇: 最常用的13条mysql语句