首页 > 代码库 > Maven搭建SpringMVC+Hibernate项目详解 【转】
Maven搭建SpringMVC+Hibernate项目详解 【转】
前言
今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这篇主要搭建SpringMVC4.1.4和Hibernate4.3.8,之前也打了好多SpringMVC的,这部分已经非常的熟悉了,毕竟业开发过一年多SpringMVC的,这次持久层采用Hibernate,数据源采用c3p0,数据库暂采用MySQL,主要是想复习一下Hibernate。搭建SpringMVC+Hibernate的框架的思路如下:
1、设计数据库:设计好表结构,最好符合3NF,采用Hibernate tools将设计好的表自动生成对应的实体entity。
1、创建Maven项目,按需映入Maven包依赖。
2、搭建Spring:配置Spring对控件层Bean的注入。
3、搭建Hibernate:配置数据源,配置SessionFactory,配置事务,以及二级缓存ehcache的配置。
4、测试Spring+Hibernate的框架搭建,写单元测试JUnit,测试事务配置等。
5、映入SpringMVC:配置SpringMVC配置信息。
6、配置web.xml容器
7、测试三个框架的整合:Maven编译打包部署服务器,测试。
1、数据库设计
设计一个权限、角色、用户的表。自然如果一个用户可以有多个角色,一个角色多个用户所拥有;一个角色拥有多个权限,一个权限对应多个角色。所以按照数据库表结构设计,符合3NF的话我们需要5张表进行存在。表的具体信息就不列出了(可以下在源码,里面还有数据库),表关系如下:
这里面ID才用到是java的UUID(36位)
2、创建Maven项目,编写pom.xml文件
创建一个Maven的webapp项目,编写pom.xml文件,引入所需的包依赖,我这里就将所需的全部的映入了。
pom.xml文件内容如下:
<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>org.andy.sh</groupId> <artifactId>springmvc_hibernate_demo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springmvc_hibernate_demo Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.1.4.RELEASE</spring.version> <hibernate.version>4.3.8.Final</hibernate.version> <jackson.version>2.5.0</jackson.version> </properties> <dependencies> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- 使用SpringMVC需配置 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- 关系型数据库整合时需配置 如hibernate jpa等 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>${hibernate.version}</version> </dependency> <!-- 二级缓存ehcache --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.9.0</version> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- mysql连接 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <!-- c3p0数据源 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5-pre10</version> </dependency> <!-- json --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!-- aop --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>springmvc_hibernate_demo</finalName> <plugins> <!-- Run the JUnit unit tests in an isolated classloader --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.2</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <webXml>src/main/webapp/WEB-INF/web.xml</webXml> </configuration> </plugin> <!-- generate java doc --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.9.1</version> <configuration> <javadocDirectory>target/javadoc</javadocDirectory> <reportOutputDirectory>target/javadoc</reportOutputDirectory> <charset>UTF-8</charset> <encoding>UTF-8</encoding> <docencoding>UTF-8</docencoding> <show>private</show> </configuration> </plugin> <!-- 部署至本机 --> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.0</version> <configuration> <container> <containerId>tomcat6x</containerId> <home>D:\WebServer\apache-tomcat-6.0.39</home> </container> <configuration> <type>existing</type> <home>D:\WebServer\apache-tomcat-6.0.39</home> </configuration> </configuration> </plugin> </plugins> </build> </project>
3、引入Spring配置
编写配置文件,配置文件放在src/main/resources资源目录下(下同)。
项目所需的配置信息config.properties
#application configs #jdbc c3p0 config jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/work?useUnicode=true&characterEncoding=utf-8 jdbc.username = root jdbc.password = 12345 #hibernate config hibernate.dialect = org.hibernate.dialect.MySQLDialect hibernate.show_sql = true hibernate.format_sql = false hibernate.hbm2ddl.auto = update hibernate.cache.use_second_level_cache = true hibernate.cache.use_query_cache = true hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactory hibernate.cache.provider_configuration_file_resource_path = ehcache.xml
3.1、spring配置
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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:config.properties"/> <!-- 扫描service自动注入为bean --> <context:component-scan base-package="org.andy.work.service.impl,org.andy.work.dao.impl" /> </beans>
上面spring配置了对应包下组件bean自动注入管理,我们需要将dao,service的实现类交由spring管理,如(@Repository,@Service)。
3.2、引入日志文件配置Log4j
log4j.properties的配置文件如下:
### set log levels ### log4j.rootLogger = INFO , C , D , E ### console ### log4j.appender.C = org.apache.log4j.ConsoleAppender log4j.appender.C.Target = System.out log4j.appender.C.layout = org.apache.log4j.PatternLayout log4j.appender.C.layout.ConversionPattern = [springmvc_hibernate_demo][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n ### log file ### log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = ../logs/springmvc_hibernate_demo.log log4j.appender.D.Append = true log4j.appender.D.Threshold = INFO log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = [springmvc_hibernate_demo][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n ### exception ### log4j.appender.E = org.apache.log4j.DailyRollingFileAppender log4j.appender.E.File = ../logs/springmvc_hibernate_demo_error.log log4j.appender.E.Append = true log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern = [sspringmvc_hibernate_demo][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n
4、Hibernate配置
Hibernate的配置主要包括:配置数据源c3p0,配置SessionFactory,配置事务管理器,配置事务管理,其spring-hibernate.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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"> <!-- 配置数据源 c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="http://www.mamicode.com/${jdbc.driver}" /> <property name="jdbcUrl" value="http://www.mamicode.com/${jdbc.url}" /> <property name="user" value="http://www.mamicode.com/${jdbc.username}" /> <property name="password" value="http://www.mamicode.com/${jdbc.password}" /> <!-- 请求超时时间 --> <property name="checkoutTimeout" value="http://www.mamicode.com/30000" /> <!-- 每60秒检查所有连接池中的空闲连接。默认值: 0,不检查 --> <property name="idleConnectionTestPeriod" value="http://www.mamicode.com/30" /> <!-- 连接数据库连接池最大空闲时间 --> <property name="maxIdleTime" value="http://www.mamicode.com/30" /> <!-- 连接池初始化连接数 --> <property name="initialPoolSize" value="http://www.mamicode.com/5" /> <property name="minPoolSize" value="http://www.mamicode.com/5" /> <property name="maxPoolSize" value="http://www.mamicode.com/20" /> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3 --> <property name="acquireIncrement" value="http://www.mamicode.com/5" /> </bean> <!-- 配置hibernate的SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 注入数据源 相关信息看源码 --> <property name="dataSource" ref="dataSource" /> <!-- hibernate配置信息 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <!-- 开启二级缓存 ehcache --> <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop> <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop> <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop> <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.cache.provider_configuration_file_resource_path} </prop> </props> </property> <!-- 扫描hibernate注解配置的entity --> <property name="packagesToScan" value="http://www.mamicode.com/org.andy.work.entity" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置事务增强处理Bean,指定事务管理器 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <!-- 配置详细事务处理语义 --> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="load*" propagation="SUPPORTS" read-only="true" /> <!-- 其他采用默认事务方式 --> <tx:method name="*" /> </tx:attributes> </tx:advice> <!-- Spring aop事务管理 --> <aop:config> <!-- 配置切入点 --> <aop:pointcut id="transactionPointcut" expression="execution(* org.andy.work.service..*Impl.*(..))" /> <!-- 指定在txAdvice切入点应用txAdvice事务增强处理 --> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> </beans>
上面配置了二级缓存,ehcache,相关的具体信息查看上一篇Hibernate二级缓存以及ehcache的搭建配置,下面是二级缓存的配置。
4.1、ehcache.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <diskStore path="D:/ehcache" /> <!-- DefaultCache setting. --> <defaultCache maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="1000000" overflowToDisk="true" memoryStoreEvictionPolicy="LRU"> </defaultCache> <!-- Special objects setting. --> <cache name="org.andy.work.entity.AcctUser" maxElementsInMemory="2" memoryStoreEvictionPolicy="LRU" eternal="true" diskPersistent="false" overflowToDisk="false" maxElementsOnDisk="1000000" /> </ehcache>
ok,到此Spring+SpringMVC+Hibernate搭建完毕。
框架/平台构成:
Maven+Springmvc + Mybatis + Shiro(权限)+ Tiles(模板) +ActiveMQ(消息队列) + Rest(服务) + WebService(服务)+ EHcache(缓存) + Quartz(定时调度)+ Html5(支持PC、IOS、Android)
用户权限系统:
组织结构:角色、用户、用户组、组织机构;权限点:页面、方法、按钮、数据权限、分级授权
项目管理新体验:
快速出原型系统、组件树、版本控制、模块移植、协同开发、实时监控、发布管理
可持续集成:
所有组件可移植、可定制、可扩充,开发成果不断积累,形成可持续发展的良性循环
支持平台平台:
Windows XP、Windows 7 、Windows 10 、 Linux 、 Unix
服务器容器:
Tomcat 5/6/7 、Jetty、JBoss、WebSphere 8.5
Maven搭建SpringMVC+Hibernate项目详解 【转】