Struts2+Hibernate4+Spring4的開發環境主要指的是使用Spring的IoC來控制Struts2中的Action和Hibernate的SessionFactory,因此搭建的兩個方面分別是Spring管理Struts和Spring管理Hibernate。Spring管理Struts的時候我們需要一個struts2-spring-plugin包,有了這個包,struts.xml中填寫的action的class就不再是真實的class文件,而是applicationContext.xml中的一個bean的id,而這個bean才真正代表着action。Spring管理Hibernate的時候,我們不再需要Hibernate.cfg.xml,相應的連接處數據源配置直接作爲bean寫在applicationContext.xml中。此外SessionFactory也作爲bean存在。Entity類作爲SessionFactory的屬性存在。不多說,現在就來看一看吧。
pox文件中需要的repository依賴
struts2-spring-plugin:2.3.24.1
hibernate-core:5.02.Final
struts-core:2.3.24.1
c3p0:0.9.1.2
spring-orm:4.0.3.RELEASE
mysql-connector-jave:5.1.6
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Archetype Created Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> </web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.i18n.encoding" value="GBK"/> <constant name="struts.devMode" value="true"/> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <package name="cao" extends="struts-default"> <action name="login" class="loginAction"> <result name="success">/WEB-INF/success.jsp</result> <result name="error">/WEB-INF/error.jsp</result> </action> </package> </struts>
applicationContext.xml
<?xml version="1.0" encoding="GBK"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- ************************hiberante******************************** --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="com.mysql.jdbc.Driver" p:jdbcUrl="jdbc:mysql://localhost/cmdb" p:user="root" p:password="cm123" p:maxPoolSize="40" p:minPoolSize="2" p:initialPoolSize="2" p:maxIdleTime="30"/> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" p:dataSource-ref="dataSource"> <!-- annotatedClasses is showed in this list --> <property name="annotatedClasses"> <list> <!-- write all of your pojo class--> <value>com.pojo.Users</value> </list> </property> <!-- 定義Hibernate SessionFactory的屬性 --> <property name="hibernateProperties"> <props> <!-- 指定Hibernate的連接方言 --> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5InnoDBDialect </prop> <!--是否根據Hiberante映射創建數據表 --> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <!-- *************下面的配置就是正常的Spring的Bean的配置了***************** --> <bean id="dao" class="com.pojo.Dao" p:sessionFactory-ref="sessionFactory"/> <bean id="judge" class="com.judge" p:dao-ref="dao"/> <bean id="loginAction" class="com.LoginAction" scope="prototype" p:j-ref="judge" /> </beans>
在存在依賴的類中,我們需要將依賴object看做是一個屬性,然後設置set跟get方法。在需要調用SessionFactory的類中,一樣道理,將SessionFactory看做是屬性。然後使用如下代碼得到SessionFactory。
Session sess=getSessionFactory().openSession(); Transaction tx=sess.beginTransaction(); Users u=new Users(); u.setLevel(4); sess.save(u); tx.commit();