mybatis学习笔记2 -- springboot 多数据源

来源:互联网 发布:数据库系统中分量 编辑:程序博客网 时间:2024/06/03 18:13

springboot 目前已经是spring 世界的新宠儿,由于其简单的配置和启动方式,受到了越来越多的关注,在学习mybatis之前, lz先搭建boot的多数据源,springboot崇尚去xml化,意思就是尽量的springboot 的config 来取代xml, 个人觉得, 无论是config 还是xml都是一个表现形式而已,各有千秋。

废话少说,下面将进行springboot 对多数据源的配置
定义两个数据库 order , shop 下面针对两个数据库进行 数据源配置

package smaug.service.provider.dbConfig;import com.alibaba.druid.pool.DruidDataSource;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import smaug.service.config.dbConfig.DBConfig;import javax.sql.DataSource;/** * Created by naonao on 17/7/16. */@Configuration@MapperScan(basePackages = OrderDBConfig.MAPPER_PACKAGE, sqlSessionFactoryRef = "")public class OrderDBConfig {    /**     * mybatis 实体类     */    public static final String MAPPER_PACKAGE = "smaug.service.provider.mapper.orders";    /**     * sql xml     */    public static final String MAPPER_LOCATION = "classpath:mybatis/orders/*.xml";    @Autowired    private DBConfig dbConfig;    @Bean(name = "queuesDBSource")    public DataSource queuesDBSource() {        DruidDataSource dataSource = new DruidDataSource();        dataSource.setDriverClassName(dbConfig.getDriverClassName());        dataSource.setUrl(dbConfig.getQueuesUrl());        dataSource.setUsername(dbConfig.getQueuesUserName());        dataSource.setPassword(dbConfig.getQueuesPassword());        dataSource.setEnable(true);        dataSource.setMaxActive(1000);        return dataSource;    }    @Bean(name = "queuesTransactionManager")    public DataSourceTransactionManager queuesTransactionManager() {        return new DataSourceTransactionManager(queuesDBSource());    }    @Bean(name = "queuesSqlSessionFactory")    public SqlSessionFactory queuesSqlSessionFactory(@Qualifier("queuesDBSource") DataSource queuesDBSource)            throws Exception {        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();        sessionFactory.setDataSource(queuesDBSource);        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()                .getResources(OrderDBConfig.MAPPER_LOCATION));        return sessionFactory.getObject();    }}
package smaug.service.provider.dbConfig;import com.alibaba.druid.pool.DruidDataSource;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import smaug.service.config.dbConfig.DBConfig;import javax.sql.DataSource;/** * Created by naonao on 17/8/5. */@Configurationpublic class ShopDBConfig {    /**     * mapper 地址     */    public static final String MAPPER_PACKAGE = "smaug.service.provider.mapper.shops";    public static final String XML_PACKAGE = "classpath:mybatis/shops/*.xml";    @Autowired    private DBConfig dbConfig;    @Bean(name = "shopsDBSource")    @Primary    public DataSource shopsDBSource() {        DruidDataSource dataSource = new DruidDataSource();        dataSource.setDriverClassName(dbConfig.getDriverClassName());        dataSource.setUrl(dbConfig.getShopUrl());        dataSource.setUsername(dbConfig.getShopUserName());        dataSource.setPassword(dbConfig.getShopPassword());        dataSource.setEnable(true);        dataSource.setMaxActive(1000);        return dataSource;    }    @Bean(name = "shopsTransactionManager")    @Primary    public DataSourceTransactionManager shopsTransactionManager() {        return new DataSourceTransactionManager(shopsDBSource());    }    @Bean(name = "shopsSqlSessionFactory")    @Primary    public SqlSessionFactory shopsSqlSessionFactory(@Qualifier("shopsDBSource") DataSource shopsDBSource)            throws Exception {        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();        sessionFactory.setDataSource(shopsDBSource);        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()                .getResources(ShopDBConfig.XML_PACKAGE));        return sessionFactory.getObject();    }}

然后我们看一下 mapper 和 sql
mapper

package smaug.service.provider.mapper.orders;import smaug.service.dataEntity.entity.orders.SmaugOrderEntity;import java.util.List;import java.util.Map;public interface SuamgOrderEntityMapper {    List<SmaugOrderEntity> selectOrderList(Map<String, Object> map);}

原谅我为了简洁将数据库实体类没有帖进来

<select id="selectOrderList" resultMap="BaseResultMap" parameterType="map">        SELECT * FROM QueuingTable WHERE 1=1        <if test="userId!=null">            AND UserID = #{userId}        </if>    </select>

嗯嗯呃,到了这里 写个demo测试一下吧

编写一个 impl

@Override    public DataResult<OrderListResponse> orderList(OrderListRequest request) {        DataResult<OrderListResponse> result = new DataResult<>();        List<OrderListItem> items = orderListHandler.orderList(request.getParams());        OrderListResponse response = new OrderListResponse();        response.setData(items);        response.setMsg("请求成功");        result.setData(response);        return result;    }

启动springBoot 类 或者直接写个test

原创粉丝点击