<kbd id='H69bMxpjXQ8sjda'></kbd><address id='H69bMxpjXQ8sjda'><style id='H69bMxpjXQ8sjda'></style></address><button id='H69bMxpjXQ8sjda'></button>

              <kbd id='H69bMxpjXQ8sjda'></kbd><address id='H69bMxpjXQ8sjda'><style id='H69bMxpjXQ8sjda'></style></address><button id='H69bMxpjXQ8sjda'></button>

                      <kbd id='H69bMxpjXQ8sjda'></kbd><address id='H69bMxpjXQ8sjda'><style id='H69bMxpjXQ8sjda'></style></address><button id='H69bMxpjXQ8sjda'></button>

                              <kbd id='H69bMxpjXQ8sjda'></kbd><address id='H69bMxpjXQ8sjda'><style id='H69bMxpjXQ8sjda'></style></address><button id='H69bMxpjXQ8sjda'></button>

                                      <kbd id='H69bMxpjXQ8sjda'></kbd><address id='H69bMxpjXQ8sjda'><style id='H69bMxpjXQ8sjda'></style></address><button id='H69bMxpjXQ8sjda'></button>

                                              <kbd id='H69bMxpjXQ8sjda'></kbd><address id='H69bMxpjXQ8sjda'><style id='H69bMxpjXQ8sjda'></style></address><button id='H69bMxpjXQ8sjda'></button>

                                                      <kbd id='H69bMxpjXQ8sjda'></kbd><address id='H69bMxpjXQ8sjda'><style id='H69bMxpjXQ8sjda'></style></address><button id='H69bMxpjXQ8sjda'></button>

                                                              <kbd id='H69bMxpjXQ8sjda'></kbd><address id='H69bMxpjXQ8sjda'><style id='H69bMxpjXQ8sjda'></style></address><button id='H69bMxpjXQ8sjda'></button>

                                                                  太阳城娱乐城_SpringBoot ( 七 ) :springboot + mybatis 大都据源最简办理方
                                                                  作者:太阳城娱乐城 浏览:8159  发布日期:2017-12-10

                                                                  (七)-springboot+mybatis大都据源最简办理方案.html

                                                                  提及大都据源,一样平常都来办理那些题目呢,主从模式可能营业较量伟大必要毗连差异的分库来支持营业。我们项目是后者的模式,网上找了许多,多半是按照jpa来做大都据源办理方案,要不就是老的spring大都据源办理方案,尚有的是操作aop动态切换,感受有点小伟大,着实我只是想找一个简朴的大都据支持罢了,折腾了两个小时清算出来,供各人参考。

                                                                  设置文件

                                                                  pom包就不贴了较量简朴该依靠的就依靠,首要是数据库这边的设置:

                                                                  mybatis.config-locations=classpath:mybatis/mybatis-config.xml

                                                                  spring.datasource.test1.driverClassName = com.mysql.jdbc.Driver

                                                                  spring.datasource.test1.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8

                                                                  spring.datasource.test1.username = root

                                                                  spring.datasource.test1.password = root

                                                                  spring.datasource.test2.driverClassName = com.mysql.jdbc.Driver

                                                                  spring.datasource.test2.url = jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8

                                                                  spring.datasource.test2.username = root

                                                                  spring.datasource.test2.password = root

                                                                  一个test1库和一个test2库,个中test1位主库,在行使的进程中必需拟定主库,否则会报错。

                                                                  数据源设置

                                                                  @Configuration

                                                                  @MapperScan(basePackages = "com.neo.mapper.test1", sqlSessionTemplateRef = "test1SqlSessionTemplate")

                                                                  public class DataSource1Config {

                                                                  @Bean(name = "test1DataSource")

                                                                  @ConfigurationProperties(prefix = "spring.datasource.test1")

                                                                  @Primary

                                                                  public DataSource testDataSource() {

                                                                  return DataSourceBuilder.create().build();

                                                                  }

                                                                  @Bean(name = "test1SqlSessionFactory")

                                                                  @Primary

                                                                  public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {

                                                                  SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

                                                                  bean.setDataSource(dataSource);

                                                                  bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));

                                                                  return bean.getObject();

                                                                  }

                                                                  @Bean(name = "test1TransactionManager")

                                                                  @Primary

                                                                  public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {

                                                                  return new DataSourceTransactionManager(dataSource);

                                                                  }

                                                                  @Bean(name = "test1SqlSessionTemplate")

                                                                  @Primary

                                                                  public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {

                                                                  return new SqlSessionTemplate(sqlSessionFactory);

                                                                  }

                                                                  }

                                                                  最要害的处所就是这块了,一层一层注入,先建设DataSource,在建设SqlSessionFactory在建设事宜,最后包装到SqlSessionTemplate中。个中必要拟定分库的mapper文件地点,以及分库到层代码。

                                                                  @MapperScan(basePackages = "com.neo.mapper.test1", sqlSessionTemplateRef = "test1SqlSessionTemplate")

                                                                  这块的注解就是指明白扫描dao层,而且给dao层注入指定的SqlSessionTemplate。全部@Bean都必要凭证定名指定正确。

                                                                  dao层和xml层

                                                                  dao层和xml必要凭证库来分在差异的目次,,好比:test1库dao层在com.neo.mapper.test1包下,test2库在com.neo.mapper.test1。

                                                                  public interface User1Mapper {

                                                                  List<UserEntity> getAll();

                                                                  UserEntity getOne(Long id);

                                                                  void insert(UserEntity user);

                                                                  void update(UserEntity user);

                                                                  void delete(Long id);

                                                                  }

                                                                  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.neo.mapper.test1.User1Mapper" >

                                                                  <resultMap type="com.neo.entity.UserEntity" >

                                                                  <id column="id" property="id" jdbcType="BIGINT" />

                                                                  <result column="userName" property="userName" jdbcType="VARCHAR" />

                                                                  <result column="passWord" property="passWord" jdbcType="VARCHAR" />

                                                                  <result column="user_sex" property="userSex" javaType="com.neo.enums.UserSexEnum"/>

                                                                  <result column="nick_name" property="nickName" jdbcType="VARCHAR" />

                                                                  </resultMap>

                                                                  <sql >

                                                                  id, userName, passWord, user_sex, nick_name

                                                                  </sql>

                                                                  <select resultMap="BaseResultMap" >

                                                                  SELECT

                                                                  <include refid="Base_Column_List" />

                                                                  FROM users

                                                                  </select>

                                                                  <select parameterType="java.lang.Long" resultMap="BaseResultMap" >

                                                                  SELECT

                                                                  <include refid="Base_Column_List" />

                                                                  FROM users

                                                                  WHERE id = #{id}

                                                                  </select>

                                                                  <insert parameterType="com.neo.entity.UserEntity" >

                                                                  INSERT INTO

                                                                  users

                                                                  (userName,passWord,user_sex)

                                                                  VALUES

                                                                  (#{userName}, #{passWord}, #{userSex})

                                                                  </insert>

                                                                  <update parameterType="com.neo.entity.UserEntity" >

                                                                  UPDATE

                                                                  users

                                                                  SET

                                                                  <if test="userName != null">userName = #{userName},</if>

                                                                  <if test="passWord != null">passWord = #{passWord},</if>

                                                                  nick_name = #{nickName}

                                                                  WHERE

                                                                  id = #{id}

                                                                  </update>

                                                                  <delete parameterType="java.lang.Long" >

                                                                  DELETE FROM

                                                                  users

                                                                  WHERE

                                                                  id =#{id}

                                                                  </delete>

                                                                  </mapper>

                                                                  测试

                                                                  测试可以行使SpringBootTest,也可以放到Controller中,这里只贴Controller层的行使。

                                                                  @RestController

                                                                  public class UserController {

                                                                  @Autowired

                                                                  private User1Mapper user1Mapper;

                                                                  @Autowired

                                                                  private User2Mapper user2Mapper;

                                                                  @RequestMapping("/getUsers")

                                                                  public List<UserEntity> getUsers() {

                                                                  List<UserEntity> users=user1Mapper.getAll();

                                                                  return users;

                                                                  }

                                                                  @RequestMapping("/getUser")

                                                                  public UserEntity getUser(Long id) {

                                                                  UserEntity user=user2Mapper.getOne(id);

                                                                  return user;

                                                                  }

                                                                  @RequestMapping("/add")

                                                                  public void save(UserEntity user) {

                                                                  user2Mapper.insert(user);

                                                                  }

                                                                  @RequestMapping(value="update")

                                                                  public void update(UserEntity user) {

                                                                  user2Mapper.update(user);

                                                                  }

                                                                  @RequestMapping(value="/delete/{id}")

                                                                  public void delete(@PathVariable("id") Long id) {

                                                                  user1Mapper.delete(id);