本文主要介紹如何對Spring、MyBatis、springmvc進行整合,給出一系列的配置文件,併進行了圖片上傳簡單示例。 ...
一、思路:
(一) Dao層:
1、 SqlMapConfig.xml,空文件即可。需要文件頭。
2、 applicationContext_dao.xml。
a) 資料庫連接池
b) SqlSessionFactory對象,需要spring和mybatis整合包下的。
c) 配置mapper文件掃描器。
(二)Service層:
1、applicationContext_service.xml包掃描器,掃描@service註解的類。
2、applicationContext_trans.xml配置事務。
(三)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>ssm_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 24 <junit.version>4.12</junit.version> 25 <spring.version>4.3.10.RELEASE</spring.version> 26 <servlet-api.version>2.5</servlet-api.version> 27 <mybatis.version>3.2.8</mybatis.version> 28 <mybatis.spring.version>1.2.2</mybatis.spring.version> 29 <pagehelper.version>4.0.0</pagehelper.version> 30 <javassist.version>3.11.0.GA</javassist.version> 31 <mysql.version>5.1.32</mysql.version> 32 <druid.version>1.0.9</druid.version> 33 <commons-fileupload.version>1.3.1</commons-fileupload.version> 34 <commons-lang3.version>3.3.2</commons-lang3.version> 35 <commons-io.version>1.3.2</commons-io.version> 36 <jackson.version>2.8.9</jackson.version> 37 <jstl.version>1.2</jstl.version> 38 <jsp-api.version>2.0</jsp-api.version> 39 <fastjson.version>1.2.28</fastjson.version> 40 <slf4j.version>1.6.4</slf4j.version> 41 <lombok.version>1.16.20</lombok.version> 42 </properties> 43 44 <dependencies> 45 <!-- Apache工具組件 --> 46 <dependency> 47 <groupId>org.apache.commons</groupId> 48 <artifactId>commons-lang3</artifactId> 49 <version>${commons-lang3.version}</version> 50 </dependency> 51 <dependency> 52 <groupId>org.apache.commons</groupId> 53 <artifactId>commons-io</artifactId> 54 <version>${commons-io.version}</version> 55 </dependency> 56 <!-- Jackson Json處理工具包 --> 57 <dependency> 58 <groupId>com.fasterxml.jackson.core</groupId> 59 <artifactId>jackson-databind</artifactId> 60 <version>${jackson.version}</version> 61 </dependency> 62 63 <!--alibaba json處理工具包--> 64 <dependency> 65 <groupId>com.alibaba</groupId> 66 <artifactId>fastjson</artifactId> 67 <version>${fastjson.version}</version> 68 </dependency> 69 <!-- 日誌處理 --> 70 <dependency> 71 <groupId>org.slf4j</groupId> 72 <artifactId>slf4j-log4j12</artifactId> 73 <version>${slf4j.version}</version> 74 </dependency> 75 <!--實體類get/set註解--> 76 <dependency> 77 <groupId>org.projectlombok</groupId> 78 <artifactId>lombok</artifactId> 79 <version>${lombok.version}</version> 80 <scope>provided</scope> 81 </dependency> 82 <!--單元測試--> 83 <dependency> 84 <groupId>junit</groupId> 85 <artifactId>junit</artifactId> 86 <version>${junit.version}</version> 87 </dependency> 88 89 <!-- Spring --> 90 <dependency> 91 <groupId>org.springframework</groupId> 92 <artifactId>spring-context</artifactId> 93 <version>${spring.version}</version> 94 </dependency> 95 <dependency> 96 <groupId>org.springframework</groupId> 97 <artifactId>spring-beans</artifactId> 98 <version>${spring.version}</version> 99 </dependency> 100 <dependency> 101 <groupId>org.springframework</groupId> 102 <artifactId>spring-webmvc</artifactId> 103 <version>${spring.version}</version> 104 </dependency> 105 <dependency> 106 <groupId>org.springframework</groupId> 107 <artifactId>spring-jdbc</artifactId> 108 <version>${spring.version}</version> 109 </dependency> 110 <dependency> 111 <groupId>org.springframework</groupId> 112 <artifactId>spring-aspects</artifactId> 113 <version>${spring.version}</version> 114 </dependency> 115 <dependency> 116 <groupId>org.springframework</groupId> 117 <artifactId>spring-jms</artifactId> 118 <version>${spring.version}</version> 119 </dependency> 120 <dependency> 121 <groupId>org.springframework</groupId> 122 <artifactId>spring-context-support</artifactId> 123 <version>${spring.version}</version> 124 </dependency> 125 <dependency> 126 <groupId>org.springframework</groupId> 127 <artifactId>spring-test</artifactId> 128 <version>${spring.version}</version> 129 </dependency> 130 <!--jsp相關--> 131 <dependency> 132 <groupId>javax.servlet</groupId> 133 <artifactId>servlet-api</artifactId> 134 <version>${servlet-api.version}</version> 135 <scope>provided</scope> 136 </dependency> 137 <!-- 文件上傳組件 --> 138 <dependency> 139 <groupId>commons-fileupload</groupId> 140 <artifactId>commons-fileupload</artifactId> 141 <version>${commons-fileupload.version}</version> 142 </dependency> 143 <!-- Mybatis --> 144 <dependency> 145 <groupId>org.mybatis</groupId> 146 <artifactId>mybatis</artifactId> 147 <version>${mybatis.version}</version> 148 </dependency> 149 <dependency> 150 <groupId>org.mybatis</groupId> 151 <artifactId>mybatis-spring</artifactId> 152 <version>${mybatis.spring.version}</version> 153 </dependency> 154 <!--MyBatis分頁插件--> 155 <dependency> 156 <groupId>com.github.pagehelper</groupId> 157 <artifactId>pagehelper</artifactId> 158 <version>${pagehelper.version}</version> 159 </dependency> 160 <!-- MySql --> 161 <dependency> 162 <groupId>mysql</groupId> 163 <artifactId>mysql-connector-java</artifactId> 164 <version>${mysql.version}</version> 165 </dependency> 166 <!-- 連接池 --> 167 <dependency> 168 <groupId>com.alibaba</groupId> 169 <artifactId>druid</artifactId> 170 <version>${druid.version}</version> 171 </dependency> 172 173 </dependencies> 174 175 <build> 176 <finalName>ssm_template</finalName> 177 <!--設置mapper.xml配置與mapper類處於同個包下,否則編譯時候無法生成到同一目錄下,會報BindingException--> 178 <resources> 179 <resource> 180 <directory>src/main/java</directory> 181 <includes> 182 <include>**/*.xml</include> 183 </includes> 184 </resource> 185 <resource> 186 <directory>src/main/resources</directory> 187 </resource> 188 </resources> 189 190 <plugins> 191 <!-- 配置Tomcat7插件 --> 192 <plugin> 193 <groupId>org.apache.tomcat.maven</groupId> 194 <artifactId>tomcat7-maven-plugin</artifactId> 195 <version>2.2</version> 196 <configuration> 197 <uriEncoding>UTF-8</uriEncoding> 198 </configuration> 199 </plugin> 200 <!-- 配置打包時跳過測試 --> 201 <plugin> 202 <groupId>org.apache.maven.plugins</groupId> 203 <artifactId>maven-surefire-plugin</artifactId> 204 <version>2.12.4</version> 205 </plugin> 206 </plugins> 207 </build> 208 209 </project>
(二)applicationContext.xml的配置
1、applicationContext_dao.xml(資料庫)的配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 9 10 11 <!-- dao層 資料庫連接部分--> 12 <!-- 載入配置文件 --> 13 <context:property-placeholder location="classpath:properties/db.properties" /> 14 15 <!-- 資料庫連接池 --> 16 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> 17 <property name="driverClassName" value="${jdbc.driver}" /> 18 <property name="url" value="${jdbc.url}" /> 19 <property name="username" value="${jdbc.username}" /> 20 <property name="password" value="${jdbc.password}"/> 21 <!-- 連接池的最大資料庫連接數 --> 22 <property name="maxActive" value="10" /> 23 <!-- 最大空閑數 --> 24 <property name="maxIdle" value="5" /> 25 </bean> 26 <!-- 配置SqlSessionFactory單例 --> 27 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 28 <!-- 配置數據源 --> 29 <property name="dataSource" ref="dataSource" /> 30 <!-- 載入mybatis核心配置文件 --> 31 <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /> 32 <!--別名包掃描--> 33 <property name="typeAliasesPackage" value="com.cenobitor.pojo"/> 34 35 </bean> 36 <!-- 動態代理dao配置,包掃描(推薦方式)--> 37 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 38 <!-- 支持多包配置,多包時用,或者;分隔 --> 39 <property name="basePackage" value="com.cenobitor.mapper" /> 40 </bean> 41 </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=
2、applicationContext_service.xml(service掃描)的配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 9 10 <!-- 配置service掃描 --> 11 <context:component-scan base-package="com.cenobitor.service"/> 12 13 </beans>
3、applicationContext_trans.xml(事務)的配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 8 http://www.springframework.org/schema/aop 9 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 12 13 <!-- 事務管理器 --> 14 <bean id