本文主要介紹spring、SpringDataJpa、springmvc的框架整合。 ...
一、思路:
(一) Dao層與Service層:
applicationContext.xml。
a) 資料庫連接池
b) 整合jpa
c) 配置@service文件掃描器。
d) 配置事務管理管理器,開啟事務註解
e) 配置jpa掃描器
(二)Controller層:
Springmvc.xml
1、包掃描器,掃描@Controller註解的類。
2、配置註解驅動。
3、視圖解析器。
(三)web.xml
1. 配置spring容量監聽器
2. 配置前端控制器
二、構建項目
(一)構建maven項目,添加依賴
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.cenobitor</groupId> 8 <artifactId>ssh_template</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <name>ssm_template Maven Webapp</name> 13 <!-- FIXME change it to the project's website --> 14 <url>http://www.example.com</url> 15 16 <properties> 17 <!--設置編譯的版本使用1.8版本--> 18 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 19 <!-- 和設置編譯插件是一樣的效果--> 20 <maven.compiler.source>1.8</maven.compiler.source> 21 <maven.compiler.target>1.8</maven.compiler.target> 22 23 <junit.version>4.12</junit.version> 24 <spring.version>4.3.10.RELEASE</spring.version> 25 <servlet-api.version>2.5</servlet-api.version> 26 <springdatajpa.version>1.10.4.RELEASE</springdatajpa.version> 27 <hibernate.version>5.0.7.Final</hibernate.version> 28 <javassist.version>3.11.0.GA</javassist.version> 29 <mysql.version>5.1.32</mysql.version> 30 <druid.version>1.0.9</druid.version> 31 <commons-fileupload.version>1.3.1</commons-fileupload.version> 32 <commons-lang3.version>3.3.2</commons-lang3.version> 33 <commons-io.version>1.3.2</commons-io.version> 34 <jackson.version>2.8.9</jackson.version> 35 <jstl.version>1.2</jstl.version> 36 <jsp-api.version>2.0</jsp-api.version> 37 <fastjson.version>1.2.28</fastjson.version> 38 <slf4j.version>1.6.4</slf4j.version> 39 <lombok.version>1.16.20</lombok.version> 40 </properties> 41 42 <dependencies> 43 <!-- Apache工具組件 --> 44 <dependency> 45 <groupId>org.apache.commons</groupId> 46 <artifactId>commons-lang3</artifactId> 47 <version>${commons-lang3.version}</version> 48 </dependency> 49 <dependency> 50 <groupId>org.apache.commons</groupId> 51 <artifactId>commons-io</artifactId> 52 <version>${commons-io.version}</version> 53 </dependency> 54 <!-- Jackson Json處理工具包 --> 55 <dependency> 56 <groupId>com.fasterxml.jackson.core</groupId> 57 <artifactId>jackson-databind</artifactId> 58 <version>${jackson.version}</version> 59 </dependency> 60 61 <!--alibaba json處理工具包--> 62 <dependency> 63 <groupId>com.alibaba</groupId> 64 <artifactId>fastjson</artifactId> 65 <version>${fastjson.version}</version> 66 </dependency> 67 <!-- 日誌處理 --> 68 <dependency> 69 <groupId>org.slf4j</groupId> 70 <artifactId>slf4j-log4j12</artifactId> 71 <version>${slf4j.version}</version> 72 </dependency> 73 <!--實體類get/set註解--> 74 <dependency> 75 <groupId>org.projectlombok</groupId> 76 <artifactId>lombok</artifactId> 77 <version>${lombok.version}</version> 78 <scope>provided</scope> 79 </dependency> 80 <!--單元測試--> 81 <dependency> 82 <groupId>junit</groupId> 83 <artifactId>junit</artifactId> 84 <version>${junit.version}</version> 85 </dependency> 86 87 <!-- Spring --> 88 <dependency> 89 <groupId>org.springframework</groupId> 90 <artifactId>spring-context</artifactId> 91 <version>${spring.version}</version> 92 </dependency> 93 <dependency> 94 <groupId>org.springframework</groupId> 95 <artifactId>spring-beans</artifactId> 96 <version>${spring.version}</version> 97 </dependency> 98 <dependency> 99 <groupId>org.springframework</groupId> 100 <artifactId>spring-webmvc</artifactId> 101 <version>${spring.version}</version> 102 </dependency> 103 <dependency> 104 <groupId>org.springframework</groupId> 105 <artifactId>spring-jdbc</artifactId> 106 <version>${spring.version}</version> 107 </dependency> 108 <dependency> 109 <groupId>org.springframework</groupId> 110 <artifactId>spring-aspects</artifactId> 111 <version>${spring.version}</version> 112 </dependency> 113 <dependency> 114 <groupId>org.springframework</groupId> 115 <artifactId>spring-jms</artifactId> 116 <version>${spring.version}</version> 117 </dependency> 118 <dependency> 119 <groupId>org.springframework</groupId> 120 <artifactId>spring-context-support</artifactId> 121 <version>${spring.version}</version> 122 </dependency> 123 <dependency> 124 <groupId>org.springframework</groupId> 125 <artifactId>spring-test</artifactId> 126 <version>${spring.version}</version> 127 </dependency> 128 <!--jsp相關--> 129 <dependency> 130 <groupId>javax.servlet</groupId> 131 <artifactId>servlet-api</artifactId> 132 <version>${servlet-api.version}</version> 133 <scope>provided</scope> 134 </dependency> 135 <!-- 文件上傳組件 --> 136 <dependency> 137 <groupId>commons-fileupload</groupId> 138 <artifactId>commons-fileupload</artifactId> 139 <version>${commons-fileupload.version}</version> 140 </dependency> 141 142 <!-- spring data jpa 資料庫持久層 --> 143 <dependency> 144 <groupId>org.springframework.data</groupId> 145 <artifactId>spring-data-jpa</artifactId> 146 <version>${springdatajpa.version}</version> 147 </dependency> 148 149 <!-- hibernate 框架 --> 150 <dependency> 151 <groupId>org.hibernate</groupId> 152 <artifactId>hibernate-core</artifactId> 153 <version>${hibernate.version}</version> 154 </dependency> 155 <dependency> 156 <groupId>org.hibernate</groupId> 157 <artifactId>hibernate-entitymanager</artifactId> 158 <version>${hibernate.version}</version> 159 </dependency> 160 161 <!-- MySql --> 162 <dependency> 163 <groupId>mysql</groupId> 164 <artifactId>mysql-connector-java</artifactId> 165 <version>${mysql.version}</version> 166 </dependency> 167 <!-- 連接池 --> 168 <dependency> 169 <groupId>com.alibaba</groupId> 170 <artifactId>druid</artifactId> 171 <version>${druid.version}</version> 172 </dependency> 173 174 </dependencies> 175 176 <build> 177 <finalName>ssh_template</finalName> 178 179 <plugins> 180 <!-- 配置Tomcat7插件 --> 181 <plugin> 182 <groupId>org.apache.tomcat.maven</groupId> 183 <artifactId>tomcat7-maven-plugin</artifactId> 184 <version>2.2</version> 185 <configuration> 186 <uriEncoding>UTF-8</uriEncoding> 187 </configuration> 188 </plugin> 189 <!-- 配置打包時跳過測試 --> 190 <plugin> 191 <groupId>org.apache.maven.plugins</groupId> 192 <artifactId>maven-surefire-plugin</artifactId> 193 <version>2.12.4</version> 194 </plugin> 195 </plugins> 196 </build> 197 198 </project>
(二)applicationContext.xml的配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:tx="http://www.springframework.org/schema/tx" 5 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 8 9 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> 13 14 <!--指定連接池配置--> 15 <!-- dao層 資料庫連接部分--> 16 <!-- 載入配置文件 --> 17 <context:property-placeholder location="classpath:properties/db.properties"/> 18 19 <!-- 配置service掃描 --> 20 <context:component-scan base-package="com.cenobitor.service"/> 21 22 <!-- 資料庫連接池 --> 23 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> 24 <property name="driverClassName" value="${jdbc.driver}"/> 25 <property name="url" value="${jdbc.url}"/> 26 <property name="username" value="${jdbc.username}"/> 27 <property name="password" value="${jdbc.password}"/> 28 <!-- 連接池的最大資料庫連接數 --> 29 <property name="maxActive" value="10"/> 30 <!-- 最大空閑數 --> 31 <property name="maxIdle" value="5"/> 32 </bean> 33 <!-- spring整合JPA --> 34 <bean id="entityManagerFactory" 35 class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 36 <property name="dataSource" ref="dataSource" /> 37 <!--指定JPA掃描的實體類所在的包--> 38 <property name="packagesToScan" value="com.cenobitor.pojo" /> 39 <!-- 指定持久層提供者為Hibernate --> 40 <property name="persistenceProvider"> 41 <bean class="org.hibernate.ejb.HibernatePersistence" /> 42 </property> 43 <property name="jpaVendorAdapter"> 44 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 45 <!-- 自動建表 --> 46 <property name="generateDdl" value="true" /> 47 <property name="database" value="MYSQL" /> 48 <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" /> 49 <property name="showSql" value="true" /> 50 </bean> 51 </property> 52 <property name="jpaDialect"> 53 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> 54 </property> 55 </bean> 56 57 <!-- 配置事務管理器 --> 58 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 59 <property name="entityManagerFactory" ref="entityManagerFactory" /> 60 </bean> 61 62 <!-- 開啟事務註解 --> 63 <tx:annotation-driven transaction-manager="transactionManager" /> 64 <!--指定Spring Data JPA要進行掃描的包,該包中的類框架會自動為其創建代理--> 65 <jpa:repositories base-package="com.cenobitor.dao" /> 66 67 </beans>
1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
3 jdbc.username=root
4 jdbc.password=
(三)web.xml 的配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 6 id="WebApp_ID"