三演侯、 Spring JDBC(了解)

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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鸠蚪,一起剝皮案震驚了整個(gè)濱河市蘸鲸,隨后出現(xiàn)的幾起案子窑多,更是在濱河造成了極大的恐慌遥巴,老刑警劉巖值桩,帶你破解...
    沈念sama閱讀 212,383評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡霎奢,警方通過(guò)查閱死者的電腦和手機(jī)户誓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)幕侠,“玉大人帝美,你說(shuō)我怎么就攤上這事∥钏叮” “怎么了悼潭?”我有些...
    開(kāi)封第一講書(shū)人閱讀 157,852評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)舞箍。 經(jīng)常有香客問(wèn)我舰褪,道長(zhǎng),這世上最難降的妖魔是什么疏橄? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,621評(píng)論 1 284
  • 正文 為了忘掉前任占拍,我火速辦了婚禮,結(jié)果婚禮上捎迫,老公的妹妹穿的比我還像新娘晃酒。我一直安慰自己,他們只是感情好立砸,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,741評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布掖疮。 她就那樣靜靜地躺著,像睡著了一般颗祝。 火紅的嫁衣襯著肌膚如雪浊闪。 梳的紋絲不亂的頭發(fā)上恼布,一...
    開(kāi)封第一講書(shū)人閱讀 49,929評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音搁宾,去河邊找鬼折汞。 笑死,一個(gè)胖子當(dāng)著我的面吹牛盖腿,可吹牛的內(nèi)容都是我干的爽待。 我是一名探鬼主播,決...
    沈念sama閱讀 39,076評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼翩腐,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼鸟款!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起茂卦,我...
    開(kāi)封第一講書(shū)人閱讀 37,803評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤何什,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后等龙,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體处渣,經(jīng)...
    沈念sama閱讀 44,265評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,582評(píng)論 2 327
  • 正文 我和宋清朗相戀三年蛛砰,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了罐栈。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,716評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡泥畅,死狀恐怖荠诬,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情涯捻,我是刑警寧澤浅妆,帶...
    沈念sama閱讀 34,395評(píng)論 4 333
  • 正文 年R本政府宣布,位于F島的核電站障癌,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏辩尊。R本人自食惡果不足惜涛浙,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,039評(píng)論 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望摄欲。 院中可真熱鬧轿亮,春花似錦、人聲如沸胸墙。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,798評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)迟隅。三九已至但骨,卻和暖如春励七,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背奔缠。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,027評(píng)論 1 266
  • 我被黑心中介騙來(lái)泰國(guó)打工掠抬, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人校哎。 一個(gè)月前我還...
    沈念sama閱讀 46,488評(píng)論 2 361
  • 正文 我出身青樓两波,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親闷哆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子腰奋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,612評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容