码迷,mamicode.com
首页 > 编程语言 > 详细

struts2+hibernate-jpa+Spring+maven 整合(2)

时间:2014-06-07 21:51:12      阅读:518      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

之前的一篇已经讲到了 spring 与struts2 的整合, 其实对于struts2+hibernate-jpa+Spring 之间的整合的文章已经相当多了,

也相当成熟了,只要不是各自的版本不兼容之外,其他的几乎没啥问题, 不行mybatis那样是不是的冒点让人头疼的事情来

下面修改pom.xml ,把hibernate 的jar 关联进来;

bubuko.com,布布扣
<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>msshj</groupId>
    <artifactId>msshj</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name />
    <description />
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.16</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.10.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.10.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>


        <dependency>
            <groupId>com.alibaba.druid</groupId>
            <artifactId>druid-wrapper</artifactId>
            <version>0.2.9</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
 
        <!-- commons -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.1</version>
        </dependency>
        
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version>
        </dependency>

        <!-- J2EE COMMONS -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.annotation</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.ejb</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.servlet</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <charset>UTF-8</charset>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
bubuko.com,布布扣

添加spring-hibernate-jpa.xml的配置

bubuko.com,布布扣
 <?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"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"

    xsi:schemaLocation="http://www.springframework.org/schema/beans   
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
          http://www.springframework.org/schema/context   
          http://www.springframework.org/schema/context/spring-context-3.0.xsd   
          http://www.springframework.org/schema/aop   
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
          http://www.springframework.org/schema/tx    
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/cache 
          http://www.springframework.org/schema/cache/spring-cache-3.0.xsd">

    <!-- 配置实体管理对象 p:persistenceUnitManager-ref="persistenceUnitManager" -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:/META-INF/persistence.xml" />
        <property name="dataSource" ref="dataSource" />
        <!-- 选配属性 -->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="MYSQL" />
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="databasePlatform" value="${hibernate.dialect}" />
            </bean>
        </property>
    </bean>

    <!--可屏蔽 <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager" 
        p:default-data-source-ref="dataSource" p:persistence-xml-locations="classpath*:META-INF/persistence.xml"> 
        <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/> 
        </property> </bean> -->
    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="txManager"
        proxy-target-class="true" />
    <!-- 定义使用事务的方法 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable" />
            <tx:method name="append*" propagation="REQUIRED"
                rollback-for="Throwable" />
            <tx:method name="insert*" propagation="REQUIRED"
                rollback-for="Throwable" />
            <tx:method name="save*" propagation="REQUIRED"
                rollback-for="Throwable" />
            <tx:method name="update*" propagation="REQUIRED"
                rollback-for="Throwable" />
            <tx:method name="modify*" propagation="REQUIRED"
                rollback-for="Throwable" />
            <tx:method name="edit*" propagation="REQUIRED"
                rollback-for="Throwable" />
            <tx:method name="delete*" propagation="REQUIRED"
                rollback-for="Throwable" />
            <tx:method name="remove*" propagation="REQUIRED"
                rollback-for="Throwable" />
            <tx:method name="repair" propagation="REQUIRED"
                rollback-for="Throwable" />
            <tx:method name="delAndRepair" propagation="REQUIRED"
                rollback-for="Throwable" />
            <tx:method name="get*" propagation="SUPPORTS" rollback-for="Throwable" />
            <tx:method name="find*" propagation="SUPPORTS"
                rollback-for="Throwable" />
            <tx:method name="load*" propagation="SUPPORTS"
                rollback-for="Throwable" />
            <tx:method name="search*" propagation="SUPPORTS"
                rollback-for="Throwable" />
            <tx:method name="datagrid*" propagation="SUPPORTS"
                rollback-for="Throwable" />
            <tx:method name="*" propagation="SUPPORTS" rollback-for="Throwable" />
        </tx:attributes>
    </tx:advice>
    <!-- 定义 切入点,使事务在它的切入点内有效  -->
<aop:config>
<aop:pointcut expression="within(com.sai..*.service.impl.*)" id="TXpointcut1" />
<aop:advisor pointcut-ref="TXpointcut1" advice-ref="txAdvice" />
</aop:config> </beans>
bubuko.com,布布扣

添加spring-datasource.xml

bubuko.com,布布扣
<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- JNDI方式配置数据源 -->
    <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
        <property name="jndiName" value="${jndiName}"></property> </bean> -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:dataSourceConfig.properties</value>
            </list>
        </property>
    </bean>
    <!-- 配置数据源 -->
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="${jdbc_username}" />
        <property name="password" value="${jdbc_password}" />

        <!-- 初始化连接大小 -->
        <property name="initialSize" value="1" />
        <!-- 连接池最大使用连接数量 -->
        <property name="maxActive" value="20" />
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="20" />
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="1" />
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="60000" />

        <!-- <property name="poolPreparedStatements" value="true" /> <property 
            name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->

        <property name="validationQuery" value="${validationQuery}" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="25200000" />

        <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandoned" value="true" />
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="1800" />
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="true" />

        <!-- 监控数据库 -->
        <!-- <property name="filters" value="stat" /> -->
        <property name="filters" value="mergeStat" />
    </bean>
</beans>
bubuko.com,布布扣

最后修改 web.xml

bubuko.com,布布扣
<?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">
    <!-- contextConfigLocation -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/spring-*.xml</param-value>
    </context-param>

    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- OpenEntityManagerInViewFilter -->
    <filter>
        <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <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>*.cy</url-pattern>
    </filter-mapping>
    <display-name></display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
bubuko.com,布布扣

最后一点META-INF/persistence.xml 必须要有!

应为我是选择在自定义文件中配置数据源, 所以,这里面的内容必须为空,你也可以选择在这里配置你的数据源.但是可配置的属性比较少,但是胜在方便

bubuko.com,布布扣
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">

    <persistence-unit name="webUnit" transaction-type="RESOURCE_LOCAL">
    <!-- <properties> -->

    <!-- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" 
        /> -->

    <!-- <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/sshe?useUnicode=true&amp;characterEncoding=UTF-8" 
        /> -->
    <!-- <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" 
        /> -->

    <!-- <property name="hibernate.connection.username" value="root" /> -->
    <!-- <property name="hibernate.connection.password" value="123123" /> -->

    <!-- <property name="hibernate.max_fetch_depth" value="3" /> -->
    <!-- <property name="hibernate.jdbc.fetch_size" value="20" /> -->
    <!-- <property name="hibernate.jdbc.batch_size" value="10" /> -->

    <!-- <property name="hibernate.show_sql" value="true" /> -->
    <!-- <property name="hibernate.format_sql" value="true" /> -->

    <!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->

    <!-- <property name="hibernate.timeBetweenEvictionRunsMillis" -->
    <!-- value="12000" /> -->
    <!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒 -->
    <!-- <property name="hibernate.minEvictableIdleTimeMillis" value="1200" 
        /> -->
    <!-- </properties> -->
    </persistence-unit>

</persistence>
bubuko.com,布布扣

 

整合到此结束, 启动即用,不再废话了!

 

 

 

 

 

struts2+hibernate-jpa+Spring+maven 整合(2),布布扣,bubuko.com

struts2+hibernate-jpa+Spring+maven 整合(2)

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/jorcen/p/3774813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!