6.dubbo常用的xml配置有哪些_SpringMVC和SpringBoot整合mybatis以及多数据源配置

1.SpringMVC整合MyBatis及多数据源配置

1.1pom.xml文件引入mybatis配置

    mysql      mysql-connector-java      ${mysql.version}org.mybatis      mybatis      ${mybatis-version}org.mybatis      mybatis-spring      ${mybatis-spring-version}com.mchange      c3p0      ${c3p0.version}

1.2 /resources目录添加mybatis-config.xml配置文件以及mybatis目录

dd95a8fe5c6850460ea9b13fca3902c2.png
6504e5a7b2cdc14ce01768b17ab4deaf.gif

这个工程我配置了两个数据源,所以mybatis目录下的Mapper文件根据不同数据源分别放在不同目录下,这样编译扫描Mapper文件。

mybatis-config.xml内容如下:

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

Mapper.xml文件和Dao类要一一对应

Mapper.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>        select channel,name from tb_channel where deleted = 0 and length(name) > 0 and p_id = 91        ORDER BY id    

namespace要指定对应的Dao类。Dao类内容如下:

package com.test.dao.second;import com.test.domain.ProductChannel;import org.apache.ibatis.annotations.Mapper;import java.util.List;/** * Created by on 2017/12/27. */@Mapperpublic interface ChannelDao {    List getChannelList();}

1.3 spring-context.xml配置数据源

SpringMVC工程当中数据源的配置是写在spring-context.xml文件当中的。

    

其中${admin-jdbc.url}这些参数是从jdbc.properties配置文件中读取的,具体的配置在我的文章《SpringMVC和SpringBoot开发环境、生产环境的多环境配置》中有详细介绍。

SpringMVC的多数据源配置很简单,只要按照上面的方式再创建一组bean就行了,如下所示:

2.SpringBoot整合Mybatis及多数据源配置

SpringBoot对各种常用软件的整合做了很多很好的封装,SpringBoot整合MyBatis网上有很多教程,这里只介绍一下多数据源的SpringBoot配置。

2.1 application.properties配置不同数据源的连接配置

60887ca2e70180ce21306c6d15df57c7.png
1fc9f7d0c55e220381af20b8a0b70152.gif

Mapper文件和Dao类同样根据数据源进行分组

970314b0460913d17774412c0b44fe5a.png
1fc9f7d0c55e220381af20b8a0b70152.gif

2.2 创建数据源配置类

不同数据源创建不同的配置类,如下

3696ffa7345ab9ce47f81b885370cfc8.png
1fc9f7d0c55e220381af20b8a0b70152.gif

配置类内容如下:

package com.xxxx.xxx.datasource;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.context.annotation.PropertySource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;/** * Created by on 17/7/17. */@Configuration@MapperScan(basePackages = "com.xxxx.xxx.dao.source", sqlSessionTemplateRef  = "fromSqlSessionTemplate")@PropertySource("classpath:application.properties")public class SourceDataSourceConfig {    @Bean(name = "fromDataSource")    @ConfigurationProperties(prefix = "jdbcFrom")    @Primary    public DataSource fromDataSource() {        return DataSourceBuilder.create().build();    }    @Bean(name = "fromSqlSessionFactory")    @Primary    public SqlSessionFactory fromSqlSessionFactory(@Qualifier("fromDataSource") DataSource dataSource) throws Exception {        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();        bean.setDataSource(dataSource);        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/source/*Mapper.xml"));        return bean.getObject();    }    @Bean(name = "fromTransactionManager")    @Primary    public DataSourceTransactionManager fromTransactionManager(@Qualifier("fromDataSource") DataSource dataSource) {        return new DataSourceTransactionManager(dataSource);    }    @Bean(name = "fromSqlSessionTemplate")    @Primary    public SqlSessionTemplate fromSqlSessionTemplate(@Qualifier("fromSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {        return new SqlSessionTemplate(sqlSessionFactory);    }}第二个数据源的配置类同上,只是bean的名称要取不一样的。SpringMVC和SpringBoot整合mybatis以及多数据源配置就讲解到这里,更多精彩文章敬请期待。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值