主要功能: 实现了主表(mysql1)写数据,从表(mysql2)读数据。
使用工具 https://github.com/baomidou/dynamic-datasource-spring-boot-starter
本人:阿里云环境主从数据库已经部署好,看博客的小伙伴可以直接使用下面的yml 配置文件。
想自己部署的参考这篇博客本人 https://zhaideyou.blog.csdn.net/article/details/123821990
创建项目: 引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
spring:
datasource:
dynamic:
primary: master
strict: false
datasource:
master:
url: jdbc:mysql://39.103.170.254:33061/javaboydb
username: root
password: 123
slave:
url: jdbc:mysql://39.103.170.254:33062/javaboydb
username: root
password: 123
@Data
public class User {
private int id;
private String username;
}
@Mapper
public interface UserMapper {
@Insert( "insert into javaboydb.user(username) values (#{username})" )
long addUser(String username);
@Select( "select * from javaboydb.user" )
List<User> getAllUser();
}
public interface UserService {
long addUser(String username);
List<User> getAllUser();
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
@DS( "master" )
public long addUser(String username) {
return userMapper.addUser( username );
}
@Override
@DS( "slave" )
public List<User> getAllUser() {
return userMapper.getAllUser();
}
}
测试:
@Autowired
private UserService userService;
@Test
void testAddUser(){
System.out.println( userService.addUser( "赵六" ) );
}
@Test
void getAllUser(){
List<User> userList = userService.getAllUser();
userList.forEach( System.out::println );
}
使用mybatis-plus 测试
添加依赖,
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
@Mapper
public interface UserMapper2 extends BaseMapper<User> {
}
public interface UserService2 extends IService<User> {
}
@Service
public class UserServiceImpl2 extends ServiceImpl<UserMapper2, User> implements UserService2 {
@Override
@DS( "slave" )
public List<User> list() {
System.out.println("使用读数据库");
return super.list();
}
@Override
@DS( "master" )
public boolean save(User entity) {
return super.save( entity );
}
}
测试
@Test
@DisplayName( "使用mybatis-plus 测试" )
void testAddUser2(){
userService2.save(new User( (int) Math.random(),"j江南一点雨是大佬"));
}
@Test
@DisplayName( "使用mybatis-plus 测试" )
void saveUserTest(){
userService2.list().forEach( System.out::println );
}
自己用数据库连接工具查看数据库。
|