springmvc+mybatis多数据源配置,AOP注解动态切换数据源

springmvc与springboot没多大区别,springboot一个jar包配置几乎包含了所有springmvc,也不需要繁琐的xml配置,springmvc需要配置多种jar包,需要繁琐的xml配置,当然springmvc也可以使用java类来配置,但这种感觉没有xml配置来的直观。

springboot+mybatis多数据源看这里

下面是springmvc+mybatis动态多数据源完整代码:

数据源配置db.properties

 #datasource.driver=com.mysql.jdbc.Driver

 #datasource.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver

 #datasource.url=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=db1

 #数据源1

 datasource1.driver=com.mysql.jdbc.Driver

 datasource1.url=jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false

 datasource1.username=root

 datasource1.password=root

 #数据源2

 datasource2.driver=com.mysql.jdbc.Driver

 datasource2.url=jdbc:mysql://127.0.0.1:3306/db2?useUnicode=true&characterEncoding=utf-8&useSSL=false

 datasource2.username=root

 datasource2.password=root

 #通用配置

 jdbc.initialSize=5

 jdbc.minIdle=5

 jdbc.maxIdle=20

 jdbc.maxActive=100

 jdbc.maxWait=100000

 jdbc.defaultAutoCommit=false

 jdbc.removeAbandoned=true

 jdbc.removeAbandonedTimeout=600

 jdbc.testWhileIdle=true

 jdbc.timeBetweenEvictionRunsMillis=60000

 jdbc.numTestsPerEvictionRun=20

springmvc配置

 <?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"

 xmlns:context="http://www.springframework.org/schema/context"

 xmlns:mvc="http://www.springframework.org/schema/mvc"

 xsi:schemaLocation="

 http://www.springframework.org/schema/beans

 http://www.springframework.org/schema/beans/spring-beans.xsd

 http://www.springframework.org/schema/tx

 http://www.springframework.org/schema/tx/spring-tx.xsd

 http://www.springframework.org/schema/context

 http://www.springframework.org/schema/context/spring-context.xsd

 http://www.springframework.org/schema/mvc

 http://www.springframework.org/schema/mvc/spring-mvc.xsd

 http://www.springframework.org/schema/aop

 http://www.springframework.org/schema/aop/spring-aop.xsd ">

 <!--使用spring注解支持@RequestMapping, @Controller-->

 <mvc:annotation-driven />

 <!-- 扫描包下面spring注解,多个包以逗号分隔 -->

 <context:component-scan base-package="com.ss"/>

 <!-- 使用annotation自动注册bean,并保证@Required,@Autowired的属性被注入 -->

 <context:component-scan base-package="com.ss.controller">

 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

 </context:component-scan>

 <!-- 将无法mapping到Controller的path交给default servlet handler处理 -->

 <mvc:default-servlet-handler />

 <!-- 定义页面文件的位置 -->

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

 <property name="prefix" value="/WEB-INF/views/" />

 <property name="suffix" value=".html" />

 </bean>

 <!-- 限制文件上传大小

 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

 <property name="defaultEncoding" value="UTF-8" />

 <property name="maxUploadSize" value="5242880" />

 </bean>

 -->

 <!-- 让spring 去读取指定路径下的资源文件 注:maven下的pom.xml已经加载了资源文件了 -->

 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

 <property name="locations" value="classpath:db.properties"/>

 </bean>

 <!-- 数据源1-->

 <bean id="datasource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

 <property name="driverClassName" value="${datasource1.driver}"/>

 <property name="url" value="${datasource1.url}"/>

 <property name="username" value="${datasource1.username}"/>

 <property name="password" value="${datasource1.password}"/>

 <property name="initialSize" value="${jdbc.initialSize}"/>

 <property name="minIdle" value="${jdbc.minIdle}"/>

 <property name="maxIdle" value="${jdbc.maxIdle}"/>

 <property name="maxActive" value="${jdbc.maxActive}"/>

 <property name="maxWait" value="${jdbc.maxWait}"/>

 <property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/>

 <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>

 <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>

 <property name="testWhileIdle" value="${jdbc.testWhileIdle}"/>

 <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>

 <property name="numTestsPerEvictionRun" value="${jdbc.numTestsPerEvictionRun}"/>

 <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>

 </bean>

 <!-- 数据源2-->

 <bean id="datasource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

 <property name="driverClassName" value="${datasource2.driver}"/>

 <property name="url" value="${datasource2.url}"/>

 <property name="username" value="${datasource2.username}"/>

 <property name="password" value="${datasource2.password}"/>

 <property name="initialSize" value="${jdbc.initialSize}"/>

 <property name="minIdle" value="${jdbc.minIdle}"/>

 <property name="maxIdle" value="${jdbc.maxIdle}"/>

 <property name="maxActive" value="${jdbc.maxActive}"/>

 <property name="maxWait" value="${jdbc.maxWait}"/>

 <property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/>

 <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>

 <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>

 <property name="testWhileIdle" value="${jdbc.testWhileIdle}"/>

 <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>

 <property name="numTestsPerEvictionRun" value="${jdbc.numTestsPerEvictionRun}"/>

 <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>

 </bean>

 <!-- 配置动态配置数据源 -->

 <bean id ="dynamicDataSource" class= "com.ss.config.DynamicDataSource">

 <!-- 默认使用dataSource1的数据源 -->

 <property name ="defaultTargetDataSource" ref="datasource1"></property>

 <property name ="targetDataSources">
<map key-type ="java.lang.String">
<entry key= "datasource1" value-ref="datasource1"></entry>
<entry key= "datasource2" value-ref="datasource2"></entry>
</map>
</property>
</bean>
<!-- mybatis事物相关配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定数据源——这里为动态多数据源 --> <property name="dataSource" ref="dynamicDataSource" /> <!-- 指定mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- 指定实体类包 --> <property name="typeAliasesPackage" value="com.ss.model"/> <!-- 指定dao层对应的sql配置 --> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!-- 扫描包下所有以@Mapper标识的接口--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.ss.dao" /> </bean>
<!-- 事务管理 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dynamicDataSource"></property> </bean> <!-- 使用声明式事务 --> <tx:annotation-driven transaction-manager="txManager" />
<!-- 指定什么方法开始事务 attributes下面指定什么样的方法注入事务 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="modify*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 开启注解配置 --> <context:annotation-config />
<!-- 开启注解Aop --> <aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 配置数据库注解aop --> <bean id="dataSourceAspect" class="com.ss.config.DynamicDataSourceAspect" /> <aop:config> <!-- 切割点:pointcut 第一个*:任意返回值 第二个*:类名(任意类) 第三个*:方法名(任意方法) (..):0-n参数,类型任意 --> <aop:pointcut id="pointCut" expression="execution(* com.ss.service.*.*(..))" /> <!--切割注入事物 ,植入事务:advisor--> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/> <!-- 动态数据源切割 <aop:aspect id="dynamicAspect" ref="dataSourceAspect"> <aop:after pointcut-ref="pointCut" method="afterSwitchDS"/> <aop:before pointcut-ref="pointCut" method="beforeSwitchDS"/> </aop:aspect> --> </aop:config>
</beans>

mybatis配置

 <?xml version="1.0" encoding="UTF-8" ?>

 <!DOCTYPE configuration

 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

 "http://mybatis.org/dtd/mybatis-3-config.dtd">

 <configuration>

 <properties>

 <property name="dialectClass" value="com.eliteams.quick4j.core.feature.orm.dialect.MySql5Dialect"/>

 </properties>

 <!-- 配置mybatis的缓存,延迟加载等等一系列属性 -->

 <settings>

 <!-- 全局映射器启用缓存 -->

 <setting name="cacheEnabled" value="true"/>

 <!-- 查询时,关闭关联对象即时加载以提高性能 -->

 <setting name="lazyLoadingEnabled" value="true"/>

 <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->

 <setting name="multipleResultSetsEnabled" value="true"/>

 <!-- 允许使用列标签代替列名 -->

 <setting name="useColumnLabel" value="true"/>

 <!-- 不允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->

 <setting name="useGeneratedKeys" value="false"/>

 <!-- 给予被嵌套的resultMap以字段-属性的映射支持 FULL,PARTIAL -->

 <setting name="autoMappingBehavior" value="PARTIAL"/>

 <!-- 对于批量更新操作缓存SQL以提高性能 BATCH,SIMPLE -->

 <!-- <setting name="defaultExecutorType" value="BATCH" /> -->

 <!-- 数据库超过25000秒仍未响应则超时 -->

 <!-- <setting name="defaultStatementTimeout" value="25000" /> -->

 <!-- Allows using RowBounds on nested statements -->

 <setting name="safeRowBoundsEnabled" value="false"/>

 <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. -->

 <setting name="mapUnderscoreToCamelCase" value="true"/>

 <!-- 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

 local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. -->

 <setting name="localCacheScope" value="SESSION"/>

 <!-- 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

 like NULL, VARCHAR or OTHER. -->

 <setting name="jdbcTypeForNull" value="OTHER"/>

 <!-- Specifies which Object's methods trigger a lazy load -->

 <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>

 <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->

 <setting name="aggressiveLazyLoading" value="false"/>

 </settings>

 <!--

 <typeAliases>

 <package name="com.ss.dao"/>

 </typeAliases>

 -->

 <!--<plugins>-->

 <!--<plugin interceptor="com.eliteams.quick4j.core.feature.orm.mybatis.PaginationResultSetHandlerInterceptor"/>-->

 <!--<plugin interceptor="com.eliteams.quick4j.core.feature.orm.mybatis.PaginationStatementHandlerInterceptor"/>-->

 <!--</plugins>-->

 </configuration>

数据源切换保存类

 package com.ss.config;

 /**

 * Created by pure on 2018-05-06.

 */

 public class DataSourceContextHolder {

 /**

 * 默认数据源

 */

 public static final String DEFAULT_DS = "datasource1";

 private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

 // 设置数据源名

 public static void setDB(String dbType) {

 System.out.println("切换到{"+dbType+"}数据源");

 contextHolder.set(dbType);

 }

 // 获取数据源名

 public static String getDB() {

 //return (contextHolder.get());

 if(contextHolder.get()==null){

 return DEFAULT_DS;

 }else{

 return (contextHolder.get());

 }

 }

 // 清除数据源名

 public static void clearDB() {

 contextHolder.remove();

 }

 }

动态数据源类

 package com.ss.config;

 import org.springframework.core.annotation.Order;

 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

 /**

 * Created by pure on 2018-05-08.

 */

 @Order(2)

 public class DynamicDataSource extends AbstractRoutingDataSource {

 @Override

 protected Object determineCurrentLookupKey() {

 System.out.println("当前数据源为"+DataSourceContextHolder.getDB());

 return DataSourceContextHolder.getDB();

 }

 }

自定义注解

 package com.ss.config;

 import java.lang.annotation.*;

 /**

 * 自定义注解

 */

 @Retention(RetentionPolicy.RUNTIME)

 @Target({ElementType.METHOD})

 @Documented

 public @interface DS {

 String value() default "datasource1";

 }

注解Aop切割类

 package com.ss.config;

 import org.aspectj.lang.JoinPoint;

 import org.aspectj.lang.annotation.After;

 import org.aspectj.lang.annotation.Aspect;

 import org.aspectj.lang.annotation.Before;

 import org.aspectj.lang.reflect.MethodSignature;

 import org.springframework.core.annotation.Order;

 import org.springframework.stereotype.Component;

 import java.lang.reflect.Method;

 /**

 * 自定义注解 + AOP的方式实现数据源动态切换。

 * Created by pure on 2018-05-06.

 */

 @Order(1)

 @Aspect

 //@Component

 public class DynamicDataSourceAspect {

 /*

 //使用DS注解动作之后清除

 @After("@annotation(DS)")

 public void afterSwitchDS(JoinPoint point){

 System.out.println("清除当前数据源"+DataSourceContextHolder.getDB());

 DataSourceContextHolder.clearDB();

 }

 //*/

 //使用DS注解动态切换

 @Before("@annotation(DS)")

 public void beforeSwitchDS(JoinPoint point){

 //获得当前访问的class

 Class<?> className = point.getTarget().getClass();

 //获得访问的方法名

 String methodName = point.getSignature().getName();

 //得到方法的参数的类型

 Class[] argClass = ((MethodSignature)point.getSignature()).getParameterTypes();

 String dataSource = DataSourceContextHolder.DEFAULT_DS;

 try {

 // 得到访问的方法对象

 Method method = className.getMethod(methodName, argClass);

 // 判断是否存在@DS注解

 if (method.isAnnotationPresent(DS.class)) {

 DS annotation = method.getAnnotation(DS.class);

 // 取出注解中的数据源名

 dataSource = annotation.value();

 }

 } catch (Exception e) {

 e.printStackTrace();

 }

 // 切换数据源

 DataSourceContextHolder.setDB(dataSource);

 }

 }

Mapper映射xml

 <?xml version="1.0" encoding="UTF-8" ?>

 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

 <mapper namespace="com.ss.dao.UserDao">

 <!-- 查询所有user -->

 <select id="getAllUser" resultType="java.util.Map">

 select * from user

 </select>

 </mapper>

dao层

 package com.ss.dao;

 import org.apache.ibatis.annotations.Mapper;

 import org.apache.ibatis.annotations.Param;

 import java.util.List;

 import java.util.Map;

 /**

 * dao层

 * Created by pure on 2018-05-06.

 */

 @Mapper

 public interface UserDao {

 //使用xml配置形式查询

 public List<Map> getAllUser();

 }

service层

 package com.ss.service;

 import com.ss.config.DS;

 import com.ss.dao.UserDao;

 import org.springframework.beans.factory.annotation.Autowired;

 import org.springframework.stereotype.Service;

 import java.util.List;

 import java.util.Map;

 /**

 * service层

 * Created by pure on 2018-05-06.

 */

 @Service

 public class UserService {

 @Autowired

 private UserDao userDao;

 //使用数据源1查询

 @DS("datasource1")

 public List<Map> getAllUser1(){

 return userDao.getAllUser();

 }

 //使用数据源2查询

 @DS("datasource2")

 public List<Map> getAllUser2(){

 return userDao.getAllUser();

 }

 }

controller层

 package com.ss.controller;

 import com.ss.config.DataSourceContextHolder;

 import com.ss.service.UserService;

 import org.springframework.beans.factory.annotation.Autowired;

 import org.springframework.stereotype.Controller;

 import org.springframework.web.bind.annotation.RequestMapping;

 import org.springframework.web.bind.annotation.ResponseBody;

 import java.util.List;

 import java.util.Map;

 /**

 * Created by pure on 2018-05-08.

 */

 @Controller

 @RequestMapping("/user")

 public class UserController {

 @Autowired

 private UserService userService;

 @RequestMapping(value = "/getDb1AllUser")

 @ResponseBody

 public List<Map> getDb1AllUser() {

 List<Map> list = userService.getAllUser1();

 return list;

 }

 @RequestMapping(value = "/getDb2AllUser")

 @ResponseBody

 public List<Map> getDb2AllUser() {

 List<Map> list = userService.getAllUser2();

 return list;

 }

 }

maven配置pom.xml

 <?xml version="1.0" encoding="UTF-8"?>

 <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/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>

 <groupId>com.springmvc</groupId>

 <artifactId>ss1</artifactId>

 <version>1.0-SNAPSHOT</version>

 <packaging>war</packaging>

 <properties>

 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

 </properties>

 <dependencies>

 <!-- jsp页面jstl相关标签包 -->

 <dependency>

 <groupId>javax.servlet</groupId>

 <artifactId>jstl</artifactId>

 <version>1.2</version>

 </dependency>

 <dependency>

 <groupId>taglibs</groupId>

 <artifactId>standard</artifactId>

 <version>1.1.2</version>

 </dependency>

 <dependency>

 <groupId>javax.servlet.jsp</groupId>

 <artifactId>jsp-api</artifactId>

 <version>2.2</version>

 </dependency>

 <!-- springMvc相关架包maven配置 -->

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-context</artifactId>

 <version>4.3.9.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-core</artifactId>

 <version>4.3.9.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-beans</artifactId>

 <version>4.3.9.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-web</artifactId>

 <version>4.3.9.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-webmvc</artifactId>

 <version>4.3.9.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-aop</artifactId>

 <version>4.3.9.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-test</artifactId>

 <version>4.3.9.RELEASE</version>

 <scope>test</scope>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-context-support</artifactId>

 <version>4.3.9.RELEASE</version> <!-- 4.3.9.RELEASE 3.1.3.RELEASE-->

 </dependency>

 <dependency>

 <groupId>commons-dbcp</groupId>

 <artifactId>commons-dbcp</artifactId>

 <version>1.4</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-jdbc</artifactId>

 <version>4.3.9.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-orm</artifactId>

 <version>4.3.9.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.aspectj</groupId>

 <artifactId>aspectjrt</artifactId>

 <version>1.8.10</version>

 </dependency>

 <dependency>

 <groupId>org.aspectj</groupId>

 <artifactId>aspectjweaver</artifactId>

 <version>1.8.10</version>

 </dependency>

 <!-- mybatis相关配置架包 -->

 <dependency>

 <groupId>org.mybatis</groupId>

 <artifactId>mybatis</artifactId>

 <version>3.4.4</version>

 </dependency>

 <!-- spring-mybatis整合包 -->

 <dependency>

 <groupId>org.mybatis</groupId>

 <artifactId>mybatis-spring</artifactId>

 <version>1.3.1</version>

 </dependency>

 <!-- mysql连接包 -->

 <dependency>

 <groupId>mysql</groupId>

 <artifactId>mysql-connector-java</artifactId>

 <version>5.1.42</version>

 </dependency>

 <dependency>

 <groupId>com.google.code.gson</groupId>

 <artifactId>gson</artifactId>

 <version>2.6.2</version>

 </dependency>

 <dependency>

 <groupId>log4j</groupId>

 <artifactId>log4j</artifactId>

 <version>1.2.17</version>

 </dependency>

 </dependencies>

 <build>

 <plugins>

 <plugin>

 <artifactId>maven-compiler-plugin</artifactId>

 <version>3.1</version>

 <configuration>

 <source>1.7</source>

 <target>1.7</target>

 </configuration>

 </plugin>

 </plugins>

 </build>

 </project>

web.xml

 <?xml version="1.0" encoding="UTF-8"?>

 <web-app xmlns="http://java.sun.com/xml/ns/javaee"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

 version="3.0">

 <context-param>

 <param-name>contextConfigLocation</param-name>

 <param-value>

 classpath:/spring-mvc.xml

 </param-value><!-- classpath*:/spring-mvc.xml,classpath*:spring-quartz.xml -->

 </context-param>

 <listener>

 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

 </listener>

 <!-- Spring -->

 <servlet>

 <servlet-name>springServlet</servlet-name>

 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

 <init-param>

 <param-name>contextConfigLocation</param-name>

 <param-value>classpath:/spring-mvc.xml</param-value>

 </init-param>

 <load-on-startup>1</load-on-startup>

 </servlet>

 <servlet-mapping>

 <servlet-name>springServlet</servlet-name>

 <url-pattern>/</url-pattern>

 </servlet-mapping>

 <filter>

 <filter-name>encodingFilter</filter-name>

 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

 <init-param>

 <param-name>encoding</param-name>

 <param-value>UTF-8</param-value>

 </init-param>

 <init-param>

 <param-name>forceEncoding</param-name>

 <param-value>true</param-value>

 </init-param>

 </filter>

 <filter-mapping>

 <filter-name>encodingFilter</filter-name>

 <url-pattern>/*</url-pattern>

 </filter-mapping>

 <welcome-file-list>

 <welcome-file>index.jsp</welcome-file>

 </welcome-file-list>

 </web-app>

项目结构

springmvc+mybatis多数据源配置,AOP注解动态切换数据源

运行结果:

springmvc+mybatis多数据源配置,AOP注解动态切换数据源

springmvc+mybatis多数据源配置,AOP注解动态切换数据源

代码下载:https://download.csdn.net/download/xiaosheng_papa/10417516

上一篇:[转]SpringMVC+ Mybatis 配置多数据源 + 手动切换数据源


下一篇:Outlook~设置