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>

项目结构

运行结果:

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

springmvc+mybatis多数据源配置,AOP注解动态切换数据源的更多相关文章

  1. Springboot+Mybatis AOP注解动态切换数据源

    在开发中因需求在项目中需要实现多数据源(虽然项目框架是SpringCloud,但是因其中只是单独的查询操作,觉得没必要开发一个项目,所以采用多数据源来进行实现) 1.在配置文件中创建多个数据连接配置 ...

  2. AOP获取方法注解实现动态切换数据源

    AOP获取方法注解实现动态切换数据源(以下方式尚未经过测试,仅提供思路) ------ 自定义一个用于切换数据源的注解: package com.xxx.annotation; import org. ...

  3. Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法

    一.开篇 这里整合分别采用了Hibernate和MyBatis两大持久层框架,Hibernate主要完成增删改功能和一些单一的对象查询功能,MyBatis主要负责查询功能.所以在出来数据库方言的时候基 ...

  4. 在使用 Spring Boot 和 MyBatis 动态切换数据源时遇到的问题以及解决方法

    相关项目地址:https://github.com/helloworlde/SpringBoot-DynamicDataSource 1. org.apache.ibatis.binding.Bind ...

  5. Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源方法

    一.开篇 这里整合分别采用了Hibernate和MyBatis两大持久层框架,Hibernate主要完成增删改功能和一些单一的对象查询功能,MyBatis主要负责查询功能.所以在出来数据库方言的时候基 ...

  6. Spring+Mybatis动态切换数据源

    功能需求是公司要做一个大的运营平台: 1.运营平台有自身的数据库,维护用户.角色.菜单.部分以及权限等基本功能. 2.运营平台还需要提供其他不同服务(服务A,服务B)的后台运营,服务A.服务B的数据库 ...

  7. Spring AOP动态切换数据源

    现在稍微复杂一点的项目,一个数据库也可能搞不定,可能还涉及分布式事务什么的,不过由于现在我只是做一个接口集成的项目,所以分布式就先不用了,用Spring AOP来达到切换数据源,查询不同的数据库就可以 ...

  8. Spring + Mybatis 项目实现动态切换数据源

    项目背景:项目开发中数据库使用了读写分离,所有查询语句走从库,除此之外走主库. 最简单的办法其实就是建两个包,把之前数据源那一套配置copy一份,指向另外的包,但是这样扩展很有限,所有采用下面的办法. ...

  9. 学习笔记_J2EE_SSM_01_spring+springMVC+Mybatis整合_XML配置示例

    spring+springMVC+Mybatis整合_XML配置示例 1.概述 spring+springMVC+Mybatis整合  XML配置方式 1.1 测试环境说明 名称 版本 备注 操作系统 ...

随机推荐

  1. Sql Server (MSSQLSERVER) 服务无法启动

    北京的冬天特别干燥,大清早的一同事就和服务器擦出了爱的火花,结果没想到竟导致服务器无法开机了,这可尴尬了,代码可都在服务器上托管着呢,一会还等着上线呢,必须得修啊.他们说是主板坏了,就另外找了一台电脑 ...

  2. zookeeper 集群简单搭建,以及Error contacting service,It is probably not running问题解决

    第一步:现在http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.9/ 下载一个gz包,然后解压.当然,zookeeper 需要在java 的环境 ...

  3. Spring-RabbitMQ实现商品的同步(后台系统)

    1.配置rabbitMQ 需要把以上配置文件加载到spring容器,在appliacationContext.xml中添加如下内容: 注意:无需配置监听,因为服务器端(生产者只需要将消息发送到交换机即 ...

  4. Bzoj 1566: [NOI2009]管道取珠(DP)

    1566: [NOI2009]管道取珠 Time Limit: 20 Sec Memory Limit: 650 MB Submit: 1558 Solved: 890 [Submit][Status ...

  5. dashucoding记录2019.6.8

    WordPress网站 网址: https://cn.wordpress.org/ 阿里云市场 https://market.aliyun.com/products/53616009?spm=a2c4 ...

  6. Myeclipse10 安装 破解 激活(含安装包,激活软件 免费)

    参考博客: https://www.cnblogs.com/gzhnan/articles/9536275.html 注意 安装完成之后一定不要点 Launch Myeclipse 下载地址: htt ...

  7. kvm 学习(三)存储池

    创建kvm存储池 1.查看系统已经存储的存储池 [root@runstone ~ ::]#virsh pool-list Name State Autostart ------------------ ...

  8. Tkinter 之CheckButton复选框标签

    一.参数说明 语法 作用 Checkbutton(root,text='xxxx') 复选框显示的文本 Checkbutton(root,variable=id) 通过变量的值确定哪些复选框被选中 C ...

  9. 线程sleep方法的demo详解

    sleep:超时等待指定时间,时间到了之后,重新回到就绪状态,抢到CPU资源后,立马进入运行状态: package com.roocon.thread.t1; public class NewThre ...

  10. LUA upvalues

    1 upvalue概念 upvalue:嵌套函数的外部函数的局部变量 function func(a) <== 这个函数返回值是一个函数 return function () a = a + 1 ...