SpringBoot 多數據源配置

一、配置 application.propertiesjava

[TIPS:多數據源配置指定主庫]  經過@primary指定 , 也可spring.datasource.primary.*屬性指定mysql

#Mysql配置
#Mysql
spring.datasource.one.driverClassName = com.mysql.jdbc.Driver
#LOC
spring.datasource.one.url = jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8
spring.datasource.one.username=root
spring.datasource.one.password=root
#多數據源配置  [多數據源配置須要指定主庫]
spring.datasource.two.driverClassName = com.mysql.jdbc.Driver
spring.datasource.two.url = jdbc:mysql://localhost:3306/springboot4more?useUnicode=true&characterEncoding=utf-8
spring.datasource.two.username = root
spring.datasource.two.password = root

#mybatis xml方式配置屬性
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

二、配置文件git

@Configuration
@MapperScan(basePackages = "com.manjixx.mybatis.mapper.mapperone" , sqlSessionTemplateRef = "sqlSessionTemplateOne")
public class DataSourceOneConfig {

    @Bean(name = "dataSourceOne")
    @ConfigurationProperties(prefix = "spring.datasource.one")
    @Primary
    public DataSource dataSourceOne(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactoryOne")
    @Primary
    public SqlSessionFactory sqlSessionFactoryOne(@Qualifier("dataSourceOne") DataSource dataSource) throws  Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/mapperone/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "transactionManagerOne")
    @Primary
    public DataSourceTransactionManager transactionManagerOne(@Qualifier("dataSourceOne") DataSource dataSource){
        return  new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sqlSessionTemplateOne")
    @Primary
    public SqlSessionTemplate sqlSessionTemplateOne(@Qualifier("sqlSessionFactoryOne") SqlSessionFactory sqlSessionFactory) throws  Exception{
        return  new SqlSessionTemplate(sqlSessionFactory);

    }

}

三、Mapper 編寫github

package com.manjixx.mybatis.mapper.mapperone;

import com.manjixx.mybatis.domain.User;
import java.util.List;

/**
 * Created by manji on 2017/8/18.
 */
//@Mapper
//@Component
public interface UserMapperOne {

    List<User> findAll();
    User findOne(Long id);
    void save(User user);
    void update(User user);
    void delete(Long id);
}

四、編寫對應的Xml文件spring

<?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.manjixx.mybatis.mapper.mapperone.UserMapperOne" >
    <resultMap id="BaseResultMap" type="com.manjixx.mybatis.domain.User" >
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="userName" property="userName" jdbcType="VARCHAR" />
        <result column="nick_name" property="nickName" jdbcType="VARCHAR" />
        <result column="age" property="age" jdbcType="INTEGER" />
        <result column="passWord" property="passWord" jdbcType="VARCHAR" />
    </resultMap>

    <sql id="Base_Column_List" >
        id, userName,nick_name,age,passWord
    </sql>

    <select id="findAll" resultMap="BaseResultMap"  >
        SELECT
        <include refid="Base_Column_List" />
        FROM user
    </select>

    <select id="findOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT
        <include refid="Base_Column_List" />
        FROM user
        WHERE id = #{id}
    </select>

    <insert id="save" parameterType="com.manjixx.mybatis.domain.User" >
        INSERT INTO
        user
        (userName,nick_name,age,passWord)
        VALUES
        (#{userName}, #{nickName}, #{age} , #{passWord})
    </insert>

    <update id="update" parameterType="com.manjixx.mybatis.domain.User" >
        UPDATE
        user
        SET
        <if test="userName != null">userName = #{userName},</if>
        <if test="nickName != null">nick_name = #{nickName},</if>
        <if test="age != null">age = #{age},</if>
        <if test="passWord != null">passWord = #{passWord},</if>
        WHERE
        id = #{id}
    </update>

    <delete id="delete" parameterType="java.lang.Long" >
        DELETE FROM
        user
        WHERE
        id =#{id}
    </delete>

</mapper>

五、編寫Service 或者 直接注入Mapper 到Controller 中 測試sql

代碼地址:https://github.com/manjixx/SpringBoot/tree/master/mybatisspringboot

相關文章
相關標籤/搜索