springmvc+mybatis多数据源配置,AOP注解动态切换数据源
springmvc与springboot没多大区别,springboot一个jar包配置几乎包含了所有springmvc,也不需要繁琐的xml配置,springmvc需要配置多种jar包,需要繁琐的xml配置,当然springmvc也可以使用java类来配置,但这种感觉没有xml配置来的直观。
下面是springmvc+mybatis动态多数据源完整代码:
数据源配置db.properties
1 2 #datasource.driver=com.mysql.jdbc.Driver 3 4 #datasource.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver 5 6 #datasource.url=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=db1 7 8 #数据源1 9 10 datasource1.driver=com.mysql.jdbc.Driver 11 12 datasource1.url=jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false 13 14 datasource1.username=root 15 16 datasource1.password=root 17 18 #数据源2 19 20 datasource2.driver=com.mysql.jdbc.Driver 21 22 datasource2.url=jdbc:mysql://127.0.0.1:3306/db2?useUnicode=true&characterEncoding=utf-8&useSSL=false 23 24 datasource2.username=root 25 26 datasource2.password=root 27 28 #通用配置 29 30 jdbc.initialSize=5 31 32 jdbc.minIdle=5 33 34 jdbc.maxIdle=20 35 36 jdbc.maxActive=100 37 38 jdbc.maxWait=100000 39 40 jdbc.defaultAutoCommit=false 41 42 jdbc.removeAbandoned=true 43 44 jdbc.removeAbandonedTimeout=600 45 46 jdbc.testWhileIdle=true 47 48 jdbc.timeBetweenEvictionRunsMillis=60000 49 50 jdbc.numTestsPerEvictionRun=20
springmvc配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 9 xmlns:aop="http://www.springframework.org/schema/aop" 10 11 xmlns:context="http://www.springframework.org/schema/context" 12 13 xmlns:mvc="http://www.springframework.org/schema/mvc" 14 15 xsi:schemaLocation=" 16 17 http://www.springframework.org/schema/beans 18 19 http://www.springframework.org/schema/beans/spring-beans.xsd 20 21 http://www.springframework.org/schema/tx 22 23 http://www.springframework.org/schema/tx/spring-tx.xsd 24 25 http://www.springframework.org/schema/context 26 27 http://www.springframework.org/schema/context/spring-context.xsd 28 29 http://www.springframework.org/schema/mvc 30 31 http://www.springframework.org/schema/mvc/spring-mvc.xsd 32 33 http://www.springframework.org/schema/aop 34 35 http://www.springframework.org/schema/aop/spring-aop.xsd "> 36 37 38 39 <!--使用spring注解支持@RequestMapping, @Controller--> 40 41 <mvc:annotation-driven /> 42 43 <!-- 扫描包下面spring注解,多个包以逗号分隔 --> 44 45 <context:component-scan base-package="com.ss"/> 46 47 <!-- 使用annotation自动注册bean,并保证@Required,@Autowired的属性被注入 --> 48 49 <context:component-scan base-package="com.ss.controller"> 50 51 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 52 53 </context:component-scan> 54 55 56 57 <!-- 将无法mapping到Controller的path交给default servlet handler处理 --> 58 59 <mvc:default-servlet-handler /> 60 61 62 63 <!-- 定义页面文件的位置 --> 64 65 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 66 67 <property name="prefix" value="/WEB-INF/views/" /> 68 69 <property name="suffix" value=".html" /> 70 71 </bean> 72 73 74 75 <!-- 限制文件上传大小 76 77 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 78 79 <property name="defaultEncoding" value="UTF-8" /> 80 81 <property name="maxUploadSize" value="5242880" /> 82 83 </bean> 84 85 --> 86 87 88 89 <!-- 让spring 去读取指定路径下的资源文件 注:maven下的pom.xml已经加载了资源文件了 --> 90 91 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 92 93 <property name="locations" value="classpath:db.properties"/> 94 95 </bean> 96 97 98 99 <!-- 数据源1--> 100 101 <bean id="datasource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 102 103 <property name="driverClassName" value="${datasource1.driver}"/> 104 105 <property name="url" value="${datasource1.url}"/> 106 107 <property name="username" value="${datasource1.username}"/> 108 109 <property name="password" value="${datasource1.password}"/> 110 111 <property name="initialSize" value="${jdbc.initialSize}"/> 112 113 <property name="minIdle" value="${jdbc.minIdle}"/> 114 115 <property name="maxIdle" value="${jdbc.maxIdle}"/> 116 117 <property name="maxActive" value="${jdbc.maxActive}"/> 118 119 <property name="maxWait" value="${jdbc.maxWait}"/> 120 121 <property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/> 122 123 <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/> 124 125 <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/> 126 127 <property name="testWhileIdle" value="${jdbc.testWhileIdle}"/> 128 129 <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/> 130 131 <property name="numTestsPerEvictionRun" value="${jdbc.numTestsPerEvictionRun}"/> 132 133 <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/> 134 135 </bean> 136 137 <!-- 数据源2--> 138 139 <bean id="datasource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 140 141 <property name="driverClassName" value="${datasource2.driver}"/> 142 143 <property name="url" value="${datasource2.url}"/> 144 145 <property name="username" value="${datasource2.username}"/> 146 147 <property name="password" value="${datasource2.password}"/> 148 149 <property name="initialSize" value="${jdbc.initialSize}"/> 150 151 <property name="minIdle" value="${jdbc.minIdle}"/> 152 153 <property name="maxIdle" value="${jdbc.maxIdle}"/> 154 155 <property name="maxActive" value="${jdbc.maxActive}"/> 156 157 <property name="maxWait" value="${jdbc.maxWait}"/> 158 159 <property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/> 160 161 <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/> 162 163 <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/> 164 165 <property name="testWhileIdle" value="${jdbc.testWhileIdle}"/> 166 167 <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/> 168 169 <property name="numTestsPerEvictionRun" value="${jdbc.numTestsPerEvictionRun}"/> 170 171 <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/> 172 173 </bean> 174 175 176 177 <!-- 配置动态配置数据源 --> 178 179 <bean id ="dynamicDataSource" class= "com.ss.config.DynamicDataSource"> 180 181 <!-- 默认使用dataSource1的数据源 --> 182 183 <property name ="defaultTargetDataSource" ref="datasource1"></property> 184 185 <property name ="targetDataSources"> 187 <map key-type ="java.lang.String"> 189 <entry key= "datasource1" value-ref="datasource1"></entry> 191 <entry key= "datasource2" value-ref="datasource2"></entry> 193 </map> 195 </property> 199 </bean> 203 <!-- mybatis事物相关配置 --> 204 205 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 206 207 <!-- 指定数据源——这里为动态多数据源 --> 208 209 <property name="dataSource" ref="dynamicDataSource" /> 210 211 <!-- 指定mybatis-config.xml --> 212 213 <property name="configLocation" value="classpath:mybatis-config.xml"></property> 214 215 <!-- 指定实体类包 --> 216 217 <property name="typeAliasesPackage" value="com.ss.model"/> 218 219 <!-- 指定dao层对应的sql配置 --> 220 221 <property name="mapperLocations" value="classpath:mapper/*.xml" /> 222 223 </bean> 224 225 <!-- 扫描包下所有以@Mapper标识的接口--> 226 227 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 228 229 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 230 231 <property name="basePackage" value="com.ss.dao" /> 232 233 </bean> 239 <!-- 事务管理 --> 240 241 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 242 243 <property name="dataSource" ref="dynamicDataSource"></property> 244 245 </bean> 246 247 <!-- 使用声明式事务 --> 248 249 <tx:annotation-driven transaction-manager="txManager" /> 253 <!-- 指定什么方法开始事务 attributes下面指定什么样的方法注入事务 --> 254 255 <tx:advice id="txAdvice" transaction-manager="txManager"> 256 257 <tx:attributes> 258 259 <tx:method name="add*" propagation="REQUIRED"/> 260 261 <tx:method name="save*" propagation="REQUIRED"/> 262 263 <tx:method name="del*" propagation="REQUIRED"/> 264 265 <tx:method name="insert*" propagation="REQUIRED"/> 266 267 <tx:method name="update*" propagation="REQUIRED"/> 268 269 <tx:method name="modify*" propagation="REQUIRED"/> 270 271 <tx:method name="*" read-only="true"/> 272 273 </tx:attributes> 274 275 </tx:advice> 276 277 278 279 <!-- 开启注解配置 --> 280 281 <context:annotation-config /> 285 <!-- 开启注解Aop --> 286 287 <aop:aspectj-autoproxy proxy-target-class="true"/> 291 <!-- 配置数据库注解aop --> 292 293 <bean id="dataSourceAspect" class="com.ss.config.DynamicDataSourceAspect" /> 298 299 <aop:config> 300 301 <!-- 302 303 切割点:pointcut 304 305 第一个*:任意返回值 306 307 第二个*:类名(任意类) 308 309 第三个*:方法名(任意方法) 310 311 (..):0-n参数,类型任意 312 313 --> 314 315 <aop:pointcut id="pointCut" expression="execution(* com.ss.service.*.*(..))" /> 316 317 <!--切割注入事物 ,植入事务:advisor--> 318 319 <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/> 320 321 <!-- 动态数据源切割 322 323 <aop:aspect id="dynamicAspect" ref="dataSourceAspect"> 324 325 <aop:after pointcut-ref="pointCut" method="afterSwitchDS"/> 326 327 <aop:before pointcut-ref="pointCut" method="beforeSwitchDS"/> 328 329 </aop:aspect> 330 331 --> 332 333 </aop:config> 337 </beans>
mybatis配置
1 2 <?xml version="1.0" encoding="UTF-8" ?> 3 4 <!DOCTYPE configuration 5 6 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 7 8 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 9 10 <configuration> 11 12 <properties> 13 14 <property name="dialectClass" value="com.eliteams.quick4j.core.feature.orm.dialect.MySql5Dialect"/> 15 16 </properties> 17 18 19 20 <!-- 配置mybatis的缓存,延迟加载等等一系列属性 --> 21 22 <settings> 23 24 <!-- 全局映射器启用缓存 --> 25 26 <setting name="cacheEnabled" value="true"/> 27 28 <!-- 查询时,关闭关联对象即时加载以提高性能 --> 29 30 <setting name="lazyLoadingEnabled" value="true"/> 31 32 <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 --> 33 34 <setting name="multipleResultSetsEnabled" value="true"/> 35 36 <!-- 允许使用列标签代替列名 --> 37 38 <setting name="useColumnLabel" value="true"/> 39 40 <!-- 不允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 --> 41 42 <setting name="useGeneratedKeys" value="false"/> 43 44 <!-- 给予被嵌套的resultMap以字段-属性的映射支持 FULL,PARTIAL --> 45 46 <setting name="autoMappingBehavior" value="PARTIAL"/> 47 48 <!-- 对于批量更新操作缓存SQL以提高性能 BATCH,SIMPLE --> 49 50 <!-- <setting name="defaultExecutorType" value="BATCH" /> --> 51 52 <!-- 数据库超过25000秒仍未响应则超时 --> 53 54 <!-- <setting name="defaultStatementTimeout" value="25000" /> --> 55 56 <!-- Allows using RowBounds on nested statements --> 57 58 <setting name="safeRowBoundsEnabled" value="false"/> 59 60 <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. --> 61 62 <setting name="mapUnderscoreToCamelCase" value="true"/> 63 64 <!-- MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT 65 66 local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. --> 67 68 <setting name="localCacheScope" value="SESSION"/> 69 70 <!-- Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values 71 72 like NULL, VARCHAR or OTHER. --> 73 74 <setting name="jdbcTypeForNull" value="OTHER"/> 75 76 <!-- Specifies which Object's methods trigger a lazy load --> 77 78 <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> 79 80 <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 --> 81 82 <setting name="aggressiveLazyLoading" value="false"/> 83 84 85 86 </settings> 87 88 <!-- 89 90 <typeAliases> 91 92 <package name="com.ss.dao"/> 93 94 </typeAliases> 95 96 --> 97 98 <!--<plugins>--> 99 100 <!--<plugin interceptor="com.eliteams.quick4j.core.feature.orm.mybatis.PaginationResultSetHandlerInterceptor"/>--> 101 102 <!--<plugin interceptor="com.eliteams.quick4j.core.feature.orm.mybatis.PaginationStatementHandlerInterceptor"/>--> 103 104 <!--</plugins>--> 105 106 </configuration>
数据源切换保存类
1 package com.ss.config; 2 3 4 5 /** 6 7 * Created by pure on 2018-05-06. 8 9 */ 10 11 public class DataSourceContextHolder { 12 13 /** 14 15 * 默认数据源 16 17 */ 18 19 public static final String DEFAULT_DS = "datasource1"; 20 21 22 23 private static final ThreadLocal<String> contextHolder = new ThreadLocal<>(); 24 25 26 27 // 设置数据源名 28 29 public static void setDB(String dbType) { 30 31 System.out.println("切换到{"+dbType+"}数据源"); 32 33 contextHolder.set(dbType); 34 35 } 36 37 38 39 // 获取数据源名 40 41 public static String getDB() { 42 43 //return (contextHolder.get()); 44 45 if(contextHolder.get()==null){ 46 47 return DEFAULT_DS; 48 49 }else{ 50 51 return (contextHolder.get()); 52 53 } 54 55 } 56 57 58 59 // 清除数据源名 60 61 public static void clearDB() { 62 63 contextHolder.remove(); 64 65 } 66 67 }
动态数据源类
1 package com.ss.config; 2 3 4 5 import org.springframework.core.annotation.Order; 6 7 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; 8 9 10 11 /** 12 13 * Created by pure on 2018-05-08. 14 15 */ 16 17 @Order(2) 18 19 public class DynamicDataSource extends AbstractRoutingDataSource { 20 21 @Override 22 23 protected Object determineCurrentLookupKey() { 24 25 System.out.println("当前数据源为"+DataSourceContextHolder.getDB()); 26 27 return DataSourceContextHolder.getDB(); 28 29 } 30 31 }
自定义注解
1 package com.ss.config; 2 3 4 5 import java.lang.annotation.*; 6 7 8 9 /** 10 11 * 自定义注解 12 13 */ 14 15 @Retention(RetentionPolicy.RUNTIME) 16 17 @Target({ElementType.METHOD}) 18 19 @Documented 20 21 public @interface DS { 22 23 String value() default "datasource1"; 24 25 }
注解Aop切割类
1 package com.ss.config; 2 3 4 5 import org.aspectj.lang.JoinPoint; 6 7 import org.aspectj.lang.annotation.After; 8 9 import org.aspectj.lang.annotation.Aspect; 10 11 import org.aspectj.lang.annotation.Before; 12 13 import org.aspectj.lang.reflect.MethodSignature; 14 15 import org.springframework.core.annotation.Order; 16 17 import org.springframework.stereotype.Component; 18 19 20 21 import java.lang.reflect.Method; 22 23 24 25 /** 26 27 * 自定义注解 + AOP的方式实现数据源动态切换。 28 29 * Created by pure on 2018-05-06. 30 31 */ 32 33 @Order(1) 34 35 @Aspect 36 37 //@Component 38 39 public class DynamicDataSourceAspect { 40 41 /* 42 43 //使用DS注解动作之后清除 44 45 @After("@annotation(DS)") 46 47 public void afterSwitchDS(JoinPoint point){ 48 49 System.out.println("清除当前数据源"+DataSourceContextHolder.getDB()); 50 51 DataSourceContextHolder.clearDB(); 52 53 } 54 55 //*/ 56 57 //使用DS注解动态切换 58 59 @Before("@annotation(DS)") 60 61 public void beforeSwitchDS(JoinPoint point){ 62 63 //获得当前访问的class 64 65 Class<?> className = point.getTarget().getClass(); 66 67 //获得访问的方法名 68 69 String methodName = point.getSignature().getName(); 70 71 //得到方法的参数的类型 72 73 Class[] argClass = ((MethodSignature)point.getSignature()).getParameterTypes(); 74 75 String dataSource = DataSourceContextHolder.DEFAULT_DS; 76 77 try { 78 79 // 得到访问的方法对象 80 81 Method method = className.getMethod(methodName, argClass); 82 83 // 判断是否存在@DS注解 84 85 if (method.isAnnotationPresent(DS.class)) { 86 87 DS annotation = method.getAnnotation(DS.class); 88 89 // 取出注解中的数据源名 90 91 dataSource = annotation.value(); 92 93 } 94 95 } catch (Exception e) { 96 97 e.printStackTrace(); 98 99 } 100 101 // 切换数据源 102 103 DataSourceContextHolder.setDB(dataSource); 104 105 } 106 107 108 109 }
Mapper映射xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 4 5 6 7 <mapper namespace="com.ss.dao.UserDao"> 8 9 <!-- 查询所有user --> 10 11 <select id="getAllUser" resultType="java.util.Map"> 12 13 select * from user 14 15 </select> 16 17 </mapper>
dao层
1 package com.ss.dao; 2 3 import org.apache.ibatis.annotations.Mapper; 4 5 import org.apache.ibatis.annotations.Param; 6 7 import java.util.List; 8 9 import java.util.Map; 10 11 12 /** 13 14 * dao层 15 16 * Created by pure on 2018-05-06. 17 18 */ 19 20 @Mapper 21 22 public interface UserDao { 23 24 //使用xml配置形式查询 25 26 public List<Map> getAllUser(); 27 28 }
service层
1 package com.ss.service; 2 3 import com.ss.config.DS; 4 5 import com.ss.dao.UserDao; 6 7 import org.springframework.beans.factory.annotation.Autowired; 8 9 import org.springframework.stereotype.Service; 10 11 import java.util.List; 12 13 import java.util.Map; 14 15 /** 16 17 * service层 18 19 * Created by pure on 2018-05-06. 20 21 */ 22 23 @Service 24 25 public class UserService { 26 27 @Autowired 28 29 private UserDao userDao; 30 31 //使用数据源1查询 32 33 @DS("datasource1") 34 35 public List<Map> getAllUser1(){ 36 37 return userDao.getAllUser(); 38 39 } 40 41 //使用数据源2查询 42 43 @DS("datasource2") 44 45 public List<Map> getAllUser2(){ 46 47 return userDao.getAllUser(); 48 49 } 50 51 }
controller层
1 package com.ss.controller; 2 3 import com.ss.config.DataSourceContextHolder; 4 5 import com.ss.service.UserService; 6 7 import org.springframework.beans.factory.annotation.Autowired; 8 9 import org.springframework.stereotype.Controller; 10 11 import org.springframework.web.bind.annotation.RequestMapping; 12 13 import org.springframework.web.bind.annotation.ResponseBody; 14 15 import java.util.List; 16 17 import java.util.Map; 18 19 /** 20 21 * Created by pure on 2018-05-08. 22 23 */ 24 25 @Controller 26 27 @RequestMapping("/user") 28 29 public class UserController { 30 31 32 @Autowired 33 34 private UserService userService; 35 36 37 @RequestMapping(value = "/getDb1AllUser") 38 39 @ResponseBody 40 41 public List<Map> getDb1AllUser() { 42 43 List<Map> list = userService.getAllUser1(); 44 45 return list; 46 47 } 48 49 50 @RequestMapping(value = "/getDb2AllUser") 51 52 @ResponseBody 53 54 public List<Map> getDb2AllUser() { 55 56 List<Map> list = userService.getAllUser2(); 57 58 return list; 59 60 } 61 62 }
maven配置pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" 4 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 7 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 8 9 <modelVersion>4.0.0</modelVersion> 10 11 12 <groupId>com.springmvc</groupId> 13 14 <artifactId>ss1</artifactId> 15 16 <version>1.0-SNAPSHOT</version> 17 18 <packaging>war</packaging> 19 20 21 <properties> 22 23 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 24 25 </properties> 26 27 <dependencies> 28 29 30 31 <!-- jsp页面jstl相关标签包 --> 32 33 <dependency> 34 35 <groupId>javax.servlet</groupId> 36 37 <artifactId>jstl</artifactId> 38 39 <version>1.2</version> 40 41 </dependency> 42 43 <dependency> 44 45 <groupId>taglibs</groupId> 46 47 <artifactId>standard</artifactId> 48 49 <version>1.1.2</version> 50 51 </dependency> 52 53 <dependency> 54 55 <groupId>javax.servlet.jsp</groupId> 56 57 <artifactId>jsp-api</artifactId> 58 59 <version>2.2</version> 60 61 </dependency> 62 63 64 <!-- springMvc相关架包maven配置 --> 65 66 <dependency> 67 68 <groupId>org.springframework</groupId> 69 70 <artifactId>spring-context</artifactId> 71 72 <version>4.3.9.RELEASE</version> 73 74 </dependency> 75 76 <dependency> 77 78 <groupId>org.springframework</groupId> 79 80 <artifactId>spring-core</artifactId> 81 82 <version>4.3.9.RELEASE</version> 83 84 </dependency> 85 86 <dependency> 87 88 <groupId>org.springframework</groupId> 89 90 <artifactId>spring-beans</artifactId> 91 92 <version>4.3.9.RELEASE</version> 93 94 </dependency> 95 96 <dependency> 97 98 <groupId>org.springframework</groupId> 99 100 <artifactId>spring-web</artifactId> 101 102 <version>4.3.9.RELEASE</version> 103 104 </dependency> 105 106 <dependency> 107 108 <groupId>org.springframework</groupId> 109 110 <artifactId>spring-webmvc</artifactId> 111 112 <version>4.3.9.RELEASE</version> 113 114 </dependency> 115 116 <dependency> 117 118 <groupId>org.springframework</groupId> 119 120 <artifactId>spring-aop</artifactId> 121 122 <version>4.3.9.RELEASE</version> 123 124 </dependency> 125 126 <dependency> 127 128 <groupId>org.springframework</groupId> 129 130 <artifactId>spring-test</artifactId> 131 132 <version>4.3.9.RELEASE</version> 133 134 <scope>test</scope> 135 136 </dependency> 137 138 <dependency> 139 140 <groupId>org.springframework</groupId> 141 142 <artifactId>spring-context-support</artifactId> 143 144 <version>4.3.9.RELEASE</version> <!-- 4.3.9.RELEASE 3.1.3.RELEASE--> 145 146 </dependency> 147 148 <dependency> 149 150 <groupId>commons-dbcp</groupId> 151 152 <artifactId>commons-dbcp</artifactId> 153 154 <version>1.4</version> 155 156 </dependency> 157 158 <dependency> 159 160 <groupId>org.springframework</groupId> 161 162 <artifactId>spring-jdbc</artifactId> 163 164 <version>4.3.9.RELEASE</version> 165 166 </dependency> 167 168 <dependency> 169 170 <groupId>org.springframework</groupId> 171 172 <artifactId>spring-orm</artifactId> 173 174 <version>4.3.9.RELEASE</version> 175 176 </dependency> 177 178 179 <dependency> 180 181 <groupId>org.aspectj</groupId> 182 183 <artifactId>aspectjrt</artifactId> 184 185 <version>1.8.10</version> 186 187 </dependency> 188 189 <dependency> 190 191 <groupId>org.aspectj</groupId> 192 193 <artifactId>aspectjweaver</artifactId> 194 195 <version>1.8.10</version> 196 197 </dependency> 198 199 200 <!-- mybatis相关配置架包 --> 201 202 <dependency> 203 204 <groupId>org.mybatis</groupId> 205 206 <artifactId>mybatis</artifactId> 207 208 <version>3.4.4</version> 209 210 </dependency> 211 212 <!-- spring-mybatis整合包 --> 213 214 <dependency> 215 216 <groupId>org.mybatis</groupId> 217 218 <artifactId>mybatis-spring</artifactId> 219 220 <version>1.3.1</version> 221 222 </dependency> 223 224 225 <!-- mysql连接包 --> 226 227 <dependency> 228 229 <groupId>mysql</groupId> 230 231 <artifactId>mysql-connector-java</artifactId> 232 233 <version>5.1.42</version> 234 235 </dependency> 236 237 238 <dependency> 239 240 <groupId>com.google.code.gson</groupId> 241 242 <artifactId>gson</artifactId> 243 244 <version>2.6.2</version> 245 246 </dependency> 247 248 249 250 <dependency> 251 252 <groupId>log4j</groupId> 253 254 <artifactId>log4j</artifactId> 255 256 <version>1.2.17</version> 257 258 </dependency> 259 260 </dependencies> 261 262 263 264 <build> 265 266 <plugins> 267 268 <plugin> 269 270 <artifactId>maven-compiler-plugin</artifactId> 271 272 <version>3.1</version> 273 274 <configuration> 275 276 <source>1.7</source> 277 278 <target>1.7</target> 279 280 </configuration> 281 282 </plugin> 283 284 </plugins> 285 286 </build> 287 288 </project>
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <web-app xmlns="http://java.sun.com/xml/ns/javaee" 4 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 7 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 8 9 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 10 11 version="3.0"> 12 13 <context-param> 14 15 <param-name>contextConfigLocation</param-name> 16 17 <param-value> 18 19 classpath:/spring-mvc.xml 20 21 </param-value><!-- classpath*:/spring-mvc.xml,classpath*:spring-quartz.xml --> 22 23 </context-param> 24 25 <listener> 26 27 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 28 29 </listener> 30 31 32 33 <!-- Spring --> 34 35 <servlet> 36 37 <servlet-name>springServlet</servlet-name> 38 39 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 40 41 <init-param> 42 43 <param-name>contextConfigLocation</param-name> 44 45 <param-value>classpath:/spring-mvc.xml</param-value> 46 47 </init-param> 48 49 <load-on-startup>1</load-on-startup> 50 51 </servlet> 52 53 <servlet-mapping> 54 55 <servlet-name>springServlet</servlet-name> 56 57 <url-pattern>/</url-pattern> 58 59 </servlet-mapping> 60 61 62 63 <filter> 64 65 <filter-name>encodingFilter</filter-name> 66 67 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 68 69 <init-param> 70 71 <param-name>encoding</param-name> 72 73 <param-value>UTF-8</param-value> 74 75 </init-param> 76 77 <init-param> 78 79 <param-name>forceEncoding</param-name> 80 81 <param-value>true</param-value> 82 83 </init-param> 84 85 </filter> 86 87 <filter-mapping> 88 89 <filter-name>encodingFilter</filter-name> 90 91 <url-pattern>/*</url-pattern> 92 93 </filter-mapping> 94 95 96 97 <welcome-file-list> 98 99 <welcome-file>index.jsp</welcome-file> 100 101 </welcome-file-list> 102 103 </web-app>
项目结构
运行结果:
代码下载:https://download.csdn.net/download/xiaosheng_papa/10417516