Hibernate4+Spring JPA+SpringMVC+Volecity搭建web应用(一)

时间:2024-04-20 10:06:51
pom.xml配置

<dependencies>
<!-- hibernate begin -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.0.Final</version>
<exclusions>
<exclusion>
<artifactId>javax.el-api</artifactId>
<groupId>javax.el</groupId>
</exclusion>
<exclusion>
<artifactId>javax.el</artifactId>
<groupId>org.glassfish.web</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- hibernate end -->
<!-- spring begin -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<!-- spring end --> <!-- servlet begin -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
<!-- servlet end -->
<!-- jstl begin -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- jstl end -->
<!-- druid begin -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.15</version>
</dependency>
<!-- druid end -->
<!-- apache commons begin -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>20030825.184428</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>20030825.183949</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency>
<!-- apache commons end -->
<!--logger begin -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<!-- logger end --> <!-- JSON begin -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- JSON end --> <!-- mysqlDriver begin -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<!-- mysqlDriver end --> </dependencies> db.properties配置 db.driverClass=com.mysql.jdbc.Driver
db.jdbcUrl=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8
db.user=root
db.password=root
db.initialSize=1
db.minIdle=1
db.maxActive=20
db.maxWait=60000
db.timeBetweenEvictionRunsMillis=60000
db.minEvictableIdleTimeMillis=300000
db.poolPreparedStatements=true
db.maxPoolPreparedStatementPerConnectionSize=20 log4j.properties配置 #spring
log4j.rootLogger=INFO,stdout,other
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH:mm:ss}] %l %m %n
log4j.appender.other=org.apache.log4j.RollingFileAppender
log4j.appender.other.File=${webapp.root}/WEB-INF/logs/log.log
log4j.appender.other.MaxFileSize=1024KB
log4j.appender.other.MaxBackupIndex=1000
og4j.appender.other.Threshold=INFO
log4j.appender.other.layout=org.apache.log4j.PatternLayout
log4j.appender.other.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH:mm:ss}] %l %t %m %n
#hibernate
log4j.logger.org.hibernate=other
log4j.logger.org.hibernate.hql.ast.AST=other
log4j.logger.org.hibernate.SQL=other
log4j.logger.org.hibernate.type=other
log4j.logger.org.hibernate.cache=other
log4j.logger.org.hibernate.transaction=other
log4j.logger.org.hibernate.jdbc=other Spring.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
default-lazy-init="true"> <!-- 包扫描 -->
<context:component-scan base-package="cn.bdqn.smvc" />
<context:annotation-config /> <!-- 向Spring容器中加载properties文件 -->
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean> <!-- JPA整合 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="packagesToScan">
<array>
<value>cn.bdqn.smvc.entity</value>
</array>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean> <!-- 指定使用jpa -->
<jpa:repositories base-package="cn.bdqn.smvc"
transaction-manager-ref="transactionManager"
entity-manager-factory-ref="entityManagerFactory" /> <!-- 将hibernate所有工作交由JPA来实现 -->
<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> <!-- 配置事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean> <!-- 事务处理过程使用注解来完成 -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" /> <!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="db.driverClass"/><propertyname="url"value="{db.jdbcUrl}" />
<property name="username" value="db.user"/><propertyname="password"value="{db.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="db.initialSize"/><propertyname="minIdle"value="{db.minIdle}" />
<property name="maxActive" value="db.maxActive"/><!−−配置获取连接等待超时的时间−−><propertyname="maxWait"value="{db.maxWait}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="db.timeBetweenEvictionRunsMillis"/><!−−配置一个连接在池中最小生存的时间,单位是毫秒−−><propertyname="minEvictableIdleTimeMillis"value="{db.minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="SELECT 'x' from dual" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="db.poolPreparedStatements"/><propertyname="maxPoolPreparedStatementPerConnectionSize"value="{db.maxPoolPreparedStatementPerConnectionSize}" />
</bean> <!-- 启动对@AspectJ(面向切面)注解的支持 -->
<aop:aspectj-autoproxy />
</beans>