1、配置 application.properties
[TIPS:多数据源配置指定主库] 通过@primary指定 , 也可spring.datasource.primary.*属性指定
#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
2、配置文件
@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); } }
3、Mapper 编写
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); }
4、编写对应的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.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>
5、编写Service 或者 直接注入Mapper 到Controller 中 测试
代码地址:https://github.com/manjixx/SpringBoot/tree/master/mybatis