前言 今天複習一下SpringMVC+Hibernate的搭建,本來想著將Spring-Security許可權控制框架也映入其中的,但是發現內容太多了,Spring-Security的就留在下一篇吧,這篇主要搭建SpringMVC4.1.4和Hibernate4.3.8,之前也打了好多SpringMV ...
前言
今天複習一下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="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 請求超時時間 --> <property name="checkoutTimeout" value="30000" /> <!-- 每60秒檢查所有連接池中的空閑連接。預設值: 0,不檢查 --> <property name="idleConnectionTestPeriod" value="30" /> <!-- 連接資料庫連接池最大空閑時間 --> <property name="maxIdleTime" value="30" /> <!-- 連接池初始化連接數 --> <property name="initialPoolSize" value="5" /> <property name="minPoolSize" value="5" /> <property name="maxPoolSize" value="20" /> <!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。預設值: 3 --> <property name="acquireIncrement" value="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="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