三、 Spring JDBC(了解)

Spring框架提供 对jdbc的进行了封装,可以操作 JdbcTemplate模板完成crud操作,JdbcTemplate 有点像 apache 提供的 dbutils

1. 开发步骤:

  • 添加 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>
  • 添加数据源
 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
 </dependency>
  • 创建jdbcTemplate 模板实例,就可以快乐的玩,一般该模板通过ioc 容器帮助完成创建的工程

  • 配置 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>


<!-- 配置数据源 -->
<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.实现

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 提供的声明式事务管理

2.1 事务管理方式

  • 编码式事务管理
1.connection.setTranscation(false);
2.connection.beginTransction();

3.try   {
 操作的业务代码
}catch(exception e ) {
 rollback();
}
4.connection.commit();
  • 声明式事务管理:通过配置spring aop来完成事务的管理
<!-- 配置 spring 提供的 事务管理器(切面) -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 配置 spring 事务管理的详情 -->
<tx:advice transaction-manager="transactionManager" id="myAdiver">
    <tx:attributes>
        <!-- isolation : 事务的隔离级别
             propagation : 事务的传播性 :事务的边界
             一般 全用默认
         -->
        <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 文件,到内存 -->
<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>


<!-- 配置数据源 -->
<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 提供的 事务管理器(切面) -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 开启注解事务驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>


</beans>

3.Struts2、Mybatis、Spring 整合

Spring 框架整合应用程序每层的组件(统一 Spring 进行 管理) 核心,Spring 框架提供了相应的整合方案

3.1 Spring 整合 Struts2

  • 配置依赖 pom 文件
    <dependencies>
        <!-- 配置 struts2 相关的依赖 -->
        <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 相关的依赖 -->
        <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 模块提供了启动 spring 容器的 监听器 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <!-- spring end -->
    </dependencies>
  • 编写 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="/">
        <!-- 注意,当前的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相关的依赖 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相关的依赖 end -->
  • 编写 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 文件,到内存 -->
<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 数据源 -->
<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 接口的代理实现类 -->
<mybatis-spring:scan base-package="com.xingxue.s2sm.mapper"/>

<!--  =============以下是配置事务管理=============== -->

<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 相关的依赖以及 Hiberante 的依赖 (POM 文件)
 <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.5.Final</version>
        </dependency>
        <!--Spring 相关的依赖-->
        <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>
        <!-- 关键 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 提供了一个 LocalSessionFactoryBean 来完成 SessionFactory 对象创建,并把该对象纳入 容器 管理-->
    <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"
    />



    <!-- 配置数据源 -->
    <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 事务切面类-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
         p:sessionFactory-ref="sessionFactory"
    />

    <!--开启事务注解驱动-->
    <tx:annotation-driven/>
</beans>

4.1 Spring 整合 Struts2

  • Struts2 相关的依赖 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 相关的依赖-->
        <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>
        <!-- 关键 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 相关的依赖-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.14.1</version>
        </dependency>

        <!--提供一个 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 提供了一个 LocalSessionFactoryBean 来完成 SessionFactory 对象创建,并把该对象纳入 容器 管理-->
    <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"
    />



    <!-- 配置数据源 -->
    <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 事务切面类-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
         p:sessionFactory-ref="sessionFactory"
    />

    <!--开启事务注解驱动-->
    <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>

    <!-- 通过 spring 提供的一个监听器 来启动 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 容器获取 动作类,不能写完整路径 -->
       <action name="reg" class="userController" method="reg">
           <result>
               /WEB-INF/views/ok.jsp
           </result>
       </action>
   </package>
</struts>

5. Struts2、Spring、JPA(Hibernate) 整合

  • pom 文件:在以上的整合中导入
  <!-- 整合 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 提供了一个 LocalContainerEntityManagerFactoryBean 来完成 entityManagerFactory 对象创建,并把该对象纳入 容器 管理-->
    <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 的实现 为Hibernate-->
    <bean id="jpaVendorAdapter"
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    </bean>


    <!-- 配置数据源 -->
    <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 事务切面类-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
         p:entityManagerFactory-ref="entityManagerFactory"
    />
    <!--开启事务注解驱动-->
    <tx:annotation-driven/>
</beans>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容