Spring框架提供 對(duì)jdbc的進(jìn)行了封裝憔四,可以操作 JdbcTemplate模板完成crud操作第租,JdbcTemplate 有點(diǎn)像 apache 提供的 dbutils
1. 開(kāi)發(fā)步驟:
- 添加 spring-jdbc 的包措拇,配置pom文件中依賴spring-jdbc
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xingxue.spring</groupId>
<artifactId>spring-jdbcttemplete</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
- 添加數(shù)據(jù)源
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
創(chuàng)建jdbcTemplate 模板實(shí)例,就可以快樂(lè)的玩,一般該模板通過(guò)ioc 容器幫助完成創(chuàng)建的工程
配置 beans.xml 文件
<bean id="userDao" class="com.xingxue.spring.dao.UserDao">
<property name="template" ref="template"></property>
</bean>
<!-- 配置 JdbcTemplate jdbc模板 -->
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置數(shù)據(jù)源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="url" value="jdbc:mysql:///xingxue24"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
2.實(shí)現(xiàn)
public class UserDao {
private JdbcTemplate template;
public void add(User entity){
//ctrl + shift + x (y)
String sql = "INSERT INTO TBL_USER (NAME,BIRTH)VALUES(?,?)";
template.update(sql, entity.getName(),entity.getBirth());
}
public void delete(Serializable id){
String sql = "DELETE FROM TBL_USER WHERE ID = ?";
template.update(sql, id);
}
public void update(User entity){
String sql = "UPDATE TBL_USER SET NAME = ?,BIRTH = ? WHERE ID = ?";
template.update(sql, entity.getName(),entity.getBirth(),entity.getId());
}
public User getById(Serializable id){
String sql = "SELECT ID,NAME,BIRTH FROM TBL_USER WHERE ID = ?";
return template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
}
public List<User> getAll() {
String sql = "SELECT ID,NAME,BIRTH FROM TBL_USER ";
return template.query(sql, new BeanPropertyRowMapper<User>(User.class));
}
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
}
2. Spring 提供的聲明式事務(wù)管理
2.1 事務(wù)管理方式
- 編碼式事務(wù)管理
1.connection.setTranscation(false);
2.connection.beginTransction();
3.try {
操作的業(yè)務(wù)代碼
}catch(exception e ) {
rollback();
}
4.connection.commit();
- 聲明式事務(wù)管理:通過(guò)配置spring aop來(lái)完成事務(wù)的管理
<!-- 配置 spring 提供的 事務(wù)管理器(切面) -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置 spring 事務(wù)管理的詳情 -->
<tx:advice transaction-manager="transactionManager" id="myAdiver">
<tx:attributes>
<!-- isolation : 事務(wù)的隔離級(jí)別
propagation : 事務(wù)的傳播性 :事務(wù)的邊界
一般 全用默認(rèn)
-->
<tx:method name="query*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 配置 spring aop -->
<aop:config>
<aop:pointcut expression="execution(* com.xingxue..service.*.*(..))" id="myPoint"/>
<!-- 通知的建議配置 -->
<aop:advisor advice-ref="myAdiver" pointcut-ref="myPoint"/>
</aop:config>
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 讀取外部的 properties 文件,到內(nèi)存 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="userService" class="com.xingxue.spring.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="com.xingxue.spring.dao.UserDao">
<property name="template" ref="template"></property>
</bean>
<!-- 配置 JdbcTemplate jdbc模板 -->
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置數(shù)據(jù)源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="url" value="jdbc:mysql:///xingxue24"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<!-- 配置 spring 提供的 事務(wù)管理器(切面) -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 開(kāi)啟注解事務(wù)驅(qū)動(dòng) -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
3.Struts2慎宾、Mybatis丐吓、Spring 整合
Spring 框架整合應(yīng)用程序每層的組件(統(tǒng)一 Spring 進(jìn)行 管理) 核心,Spring 框架提供了相應(yīng)的整合方案
3.1 Spring 整合 Struts2
- 配置依賴 pom 文件
<dependencies>
<!-- 配置 struts2 相關(guān)的依賴 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.33</version>
</dependency>
<!-- struts2 提供了 整合 spring 的插件包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.33</version>
</dependency>
<!-- struts2 end -->
<!-- spring 相關(guān)的依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<!-- spring web 模塊提供了啟動(dòng) spring 容器的 監(jiān)聽(tīng)器 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<!-- spring end -->
</dependencies>
- 編寫(xiě) Spring 配置文件 (applicationContext.xml) 趟据、 Struts2 的配置文件
- applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.xingxue"></context:component-scan>
</beans>
- struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="p1" extends="struts-default" namespace="/">
<!-- 注意券犁,當(dāng)前的Action 組件從 spring 容器中取 -->
<action name="/user" class="userController" method="login">
<result name="ok">
/ok.jsp
</result>
</action>
</package>
</struts>
- 配置 web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<!-- struts2 中央控制器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3.2 Spring 整合 Mybatis
- 配置依賴 pom 文件
<!-- mybatis相關(guān)的依賴 start -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<!-- mybatis 整合 spring的依賴插件 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mybatis相關(guān)的依賴 end -->
- 編寫(xiě) Spring 配置文件 (applicationContext.xml)、SqlMapConfig.xml
- applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 讀取外部的 properties 文件之宿,到內(nèi)存 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<context:component-scan base-package="com.xingxue"></context:component-scan>
<!-- SqlSessionFactory 工廠交給容器管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/xingxue/s2sm/mapper/*Mapper.xml"></property>
</bean>
<!-- 配置 druid 數(shù)據(jù)源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
</bean>
<!-- 配置 mapper 接口的代理實(shí)現(xiàn)類 -->
<mybatis-spring:scan base-package="com.xingxue.s2sm.mapper"/>
<!-- =============以下是配置事務(wù)管理=============== -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4. Struts2族操、Spring、Hiberante 整合
4.1 Spring 整合 Hibernate
- 配置 Spring 相關(guān)的依賴以及 Hiberante 的依賴 (POM 文件)
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
</dependency>
<!--Spring 相關(guān)的依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<!-- 關(guān)鍵 orm 模塊的包 整合 hibernate 依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<!-- 其他可選依賴-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0</version>
</dependency>
</dependencies>
- Spring 整合的 配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="com.xingxue"/>
<context:property-placeholder location="jdbc.properties"/>
<util:properties id="hibernatePro">
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</util:properties>
<!--spring 提供了一個(gè) LocalSessionFactoryBean 來(lái)完成 SessionFactory 對(duì)象創(chuàng)建比被,并把該對(duì)象納入 容器 管理-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:mappingLocations="com/xingxue/s2sh/domain/*.hbm.xml"
p:hibernateProperties-ref="hibernatePro"
/>
<!-- 配置數(shù)據(jù)源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
p:url="${jdbc.url}"
p:driverClassName="${jdbc.driver}"
p:username="${jdbc.user}"
p:password="${jdbc.password}"
/>
<!--配置 spring 事務(wù)切面類-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"
/>
<!--開(kāi)啟事務(wù)注解驅(qū)動(dòng)-->
<tx:annotation-driven/>
</beans>
4.1 Spring 整合 Struts2
- Struts2 相關(guān)的依賴 pom 文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xingxue.s2sh</groupId>
<artifactId>s2sh</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
</dependency>
<!--Spring 相關(guān)的依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<!-- 關(guān)鍵 orm 模塊的包 整合 hibernate 依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<!-- spring 整合 struts2 配置 web 模塊-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<!-- struts2 相關(guān)的依賴-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.14.1</version>
</dependency>
<!--提供一個(gè) struts2 整合 Spring 的整合插件-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.14.1</version>
</dependency>
<!-- 其他可選依賴-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0</version>
</dependency>
<!-- servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<!-- jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
- Spring 的配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="com.xingxue"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<util:properties id="hibernatePro">
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</util:properties>
<!--spring 提供了一個(gè) LocalSessionFactoryBean 來(lái)完成 SessionFactory 對(duì)象創(chuàng)建色难,并把該對(duì)象納入 容器 管理-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:mappingLocations="classpath:com/xingxue/s2sh/domain/*.hbm.xml"
p:hibernateProperties-ref="hibernatePro"
/>
<!-- 配置數(shù)據(jù)源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
p:url="${jdbc.url}"
p:driverClassName="${jdbc.driver}"
p:username="${jdbc.user}"
p:password="${jdbc.password}"
/>
<!--配置 spring 事務(wù)切面類-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"
/>
<!--開(kāi)啟事務(wù)注解驅(qū)動(dòng)-->
<tx:annotation-driven/>
</beans>
- web.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!-- 通過(guò) spring 提供的一個(gè)監(jiān)聽(tīng)器 來(lái)啟動(dòng) spring 容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
- struts.xml 配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="p1" extends="struts-default" namespace="/">
<!-- 記住一定要從 spring 容器獲取 動(dòng)作類笤妙,不能寫(xiě)完整路徑 -->
<action name="reg" class="userController" method="reg">
<result>
/WEB-INF/views/ok.jsp
</result>
</action>
</package>
</struts>
5. Struts2召衔、Spring、JPA(Hibernate) 整合
- pom 文件:在以上的整合中導(dǎo)入
<!-- 整合 JPA 的依賴包 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
</dependency>
- Spring 配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="com.xingxue"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<util:properties id="hibernatePro">
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</util:properties>
<!--spring 提供了一個(gè) LocalContainerEntityManagerFactoryBean 來(lái)完成 entityManagerFactory 對(duì)象創(chuàng)建,并把該對(duì)象納入 容器 管理-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="com.xingxue.s2sh.domain"
p:jpaProperties-ref="hibernatePro"
p:jpaVendorAdapter-ref="jpaVendorAdapter"
/>
<!--配置 jpa 的實(shí)現(xiàn) 為Hibernate-->
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</bean>
<!-- 配置數(shù)據(jù)源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
p:url="${jdbc.url}"
p:driverClassName="${jdbc.driver}"
p:username="${jdbc.user}"
p:password="${jdbc.password}"
/>
<!--配置 spring 事務(wù)切面類-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"
/>
<!--開(kāi)啟事務(wù)注解驅(qū)動(dòng)-->
<tx:annotation-driven/>
</beans>