帮我写一份使用DS注解同时操作两个数据库的完整Java代码
时间: 2024-03-11 13:46:54 浏览: 144
好的,以下是一份使用@DS注解同时操作两个数据库的Java代码:
```
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private OrderMapper orderMapper;
@DS("db1")
public List<User> getUsers() {
return userMapper.getUsers();
}
@DS("db2")
public List<Order> getOrders() {
return orderMapper.getOrders();
}
@DS("db1")
public void addUser(User user) {
userMapper.addUser(user);
}
@DS("db2")
public void addOrder(Order order) {
orderMapper.addOrder(order);
}
}
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user")
List<User> getUsers();
@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
void addUser(User user);
}
@Mapper
public interface OrderMapper {
@Select("SELECT * FROM order")
List<Order> getOrders();
@Insert("INSERT INTO order(name, price) VALUES(#{name}, #{price})")
void addOrder(Order order);
}
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "db1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource db1DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "db2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource db2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "dynamicDataSource")
public DataSource dynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
Map<Object, Object> dataSourceMap = new HashMap<>();
dataSourceMap.put("db1", db1DataSource());
dataSourceMap.put("db2", db2DataSource());
dynamicDataSource.setTargetDataSources(dataSourceMap);
dynamicDataSource.setDefaultTargetDataSource(db1DataSource());
return dynamicDataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dynamicDataSource());
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/**/*.xml"));
return sessionFactory.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(sqlSessionFactory());
}
}
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DbContextHolder.getDbType();
}
}
public class DbContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
public static void setDbType(String dbType) {
contextHolder.set(dbType);
}
public static String getDbType() {
return contextHolder.get();
}
public static void clearDbType() {
contextHolder.remove();
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface DS {
String value() default "db1";
}
```
在这份代码中,我们使用了@DS注解来标记不同的方法需要使用哪个数据库。同时,我们也实现了动态数据源的功能,使得我们可以同时操作两个不同的数据库。
需要注意的是,在使用@DS注解时,我们需要保证注解的值与DataSourceConfig中配置的dataSourceMap的Key值相同,否则将会出现数据源找不到的问题。
另外,我们还需要在每个请求结束后手动清空DbContextHolder中的DbType,以免影响下一次请求。
阅读全文