LK接着上面的,来总结一下springboot与数据源的操作,毕竟做web数据库是少不了的,今天就springboot和mysql整合做一总结: 1. springboot+mybaties整合之注解形式 2. springboot+mybaties整合之xml形式 3. springboot+mybatiesplus+driud数据源整合 4. springboot+mybatiesplus+driud多数据源配置
resource目录下新建一个UserMapper.xml文件夹
配置文件中配置xml mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.yrz.model spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver dao层实现 @Mapper public interface UserMapper { List<User> getAllUser(); User getOne(Long id); void insert(User user); void update(User user); void delete(Long id); }此处使用@Mapper注解,在启动类上就不用在引入dao层包了。 源码请在https://github.com/TDYRZ/spring-/tree/master/springboot-mybites-xml上获取
具体代码实现
配置文件(mysql下两个数据源 db1 db2) server: port: 8080 spring: aop: proxy-target-class: true auto: true datasource: druid: # 数据库 1 db1: url: jdbc:mysql://localhost:3306/db1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver initialSize: 5 minIdle: 5 maxActive: 20 # 数据库 2 db2: url: jdbc:mysql://localhost:3306/db2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver initialSize: 5 minIdle: 5 maxActive: 20 重点来看看切面类 切点是放在service层,当调用service的方法时切换数据源。 判断方法上是否有数据库切换注解,有的话直接使用注解。 @Component @Aspect public class DataSourceSwitchAspect { @Pointcut("execution(* com.yrz.service.db1.*.*(..))") private void db1Aspect() { } @Pointcut("execution(* com.yrz.service.db2.*.*(..))") private void db2Aspect() { } /** * 执行service方法之前先切换数据源到db1 * @Title: db1 * @Description: TODO(这里用一句话描述这个方法的作用) * @param @param joinPoint 设定文件 * @author:yrz * @data: 2019年5月21日 下午4:48:06 * @return void 返回类型 * @throws */ @Before( "db1Aspect()" ) public void db1(JoinPoint joinPoint) { System.out.println("切换到db1 数据源..."); setDataSource(joinPoint,DBTypeEnum.db1); } /** * 执行service方法之前先切换数据源到db2 * @Title: db1 * @Description: TODO(这里用一句话描述这个方法的作用) * @param @param joinPoint 设定文件 * @author:yrz * @data: 2019年5月21日 下午4:48:06 * @return void 返回类型 * @throws */ @Before("db2Aspect()" ) public void db2 (JoinPoint joinPoint) { System.out.println("切换到db2 数据源..."); setDataSource(joinPoint,DBTypeEnum.db2); } /** * 添加注解方式,如果有注解优先注解,没有则按传过来的数据源配置 * @param joinPoint * @param dbTypeEnum */ private void setDataSource(JoinPoint joinPoint, DBTypeEnum dbTypeEnum) { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); DataSourceSwitch dataSourceSwitch = methodSignature.getMethod().getAnnotation(DataSourceSwitch.class); if (Objects.isNull(dataSourceSwitch) || Objects.isNull(dataSourceSwitch.value())) { DbContextHolder.setDbType(dbTypeEnum); }else{ System.out.println("根据注解来切换数据源,注解值为:"+dataSourceSwitch.value()); switch (dataSourceSwitch.value().getValue()) { case "db1": DbContextHolder.setDbType(DBTypeEnum.db1); break; case "db2": DbContextHolder.setDbType(DBTypeEnum.db2); break; default: DbContextHolder.setDbType(dbTypeEnum); } } } } 不同数据源注入容器 ** * @Author yrz */ @Configuration @MapperScan({"com.yrz.mapper*"}) public class MybatisPlusConfig { /** * mybatis-plus分页插件<br> * 文档:http://mp.baomidou.com<br> */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); //paginationInterceptor.setLocalPage(true);// 开启 PageHelper 的支持 return paginationInterceptor; } //注入“db1”数据源 @Bean(name = "db1") @ConfigurationProperties(prefix = "spring.datasource.druid.db1" ) public DataSource db1 () { return DruidDataSourceBuilder.create().build(); } //注入“db2”数据源 @Bean(name = "db2") @ConfigurationProperties(prefix = "spring.datasource.druid.db2" ) public DataSource db2 () { return DruidDataSourceBuilder.create().build(); } /** * 动态数据源配置 * @return */ @Bean @Primary public DataSource multipleDataSource (@Qualifier("db1") DataSource db1, @Qualifier("db2") DataSource db2 ) { DynamicDataSource dynamicDataSource = new DynamicDataSource(); Map< Object, Object > targetDataSources = new HashMap<>(); targetDataSources.put(DBTypeEnum.db1.getValue(), db1 ); targetDataSources.put(DBTypeEnum.db2.getValue(), db2); dynamicDataSource.setTargetDataSources(targetDataSources); dynamicDataSource.setDefaultTargetDataSource(db1);//默认数据源 return dynamicDataSource; } //配置mybaties @Bean("sqlSessionFactory") public SqlSessionFactory sqlSessionFactory() throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); sqlSessionFactory.setDataSource(multipleDataSource(db1(),db2())); MybatisConfiguration configuration = new MybatisConfiguration(); configuration.setJdbcTypeForNull(JdbcType.NULL); configuration.setMapUnderscoreToCamelCase(true); configuration.setCacheEnabled(false); sqlSessionFactory.setConfiguration(configuration); sqlSessionFactory.setPlugins(new Interceptor[]{ //PerformanceInterceptor(),OptimisticLockerInterceptor() paginationInterceptor() }); sqlSessionFactory.setGlobalConfig(globalConfiguration()); return sqlSessionFactory.getObject(); } //全局配置 @Bean public GlobalConfiguration globalConfiguration() { GlobalConfiguration conf = new GlobalConfiguration(new LogicSqlInjector()); conf.setLogicDeleteValue("-1"); conf.setLogicNotDeleteValue("1"); conf.setIdType(0); // conf.setMetaObjectHandler(new MyMetaObjectHandler()); conf.setDbColumnUnderline(true); conf.setRefresh(true); return conf; } }测试结果: 项目源码 https://github.com/TDYRZ/spring-/tree/master/springboot-mybitesplus-druidMultipledatasources
至此 springboot整合mybaties历经简单到多数据源配置,因为LK一直使用mybaties所以以此作为实例,如果有兴趣可以使用jpa实现。