liulin
2024-08-08 59029ee11e9d1c1f2f07a3d5dc42165777739074
src/test/java/com/lunhan/xxx/host/BasicServiceApplicationTests.java
@@ -1,11 +1,18 @@
package com.lunhan.xxx.host;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.lunhan.xxx.po.UserPO;
import com.lunhan.xxx.repository.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class BasicServiceApplicationTests {
    @Autowired
    private UserMapper userMapper;
//    @Autowired
//    private AdminService service;
@@ -30,4 +37,32 @@
      System.out.println("END.");
    }
    @Test
    void dbTest() {
        // 动态设置数据源上下文
        DynamicDataSourceContextHolder.push("test_db1");
        List<UserPO> list = userMapper.selectList(null);
        UserPO item = new UserPO();
        item.setName("jeff");
        item.setAge(18);
        item.setEmail("");
        int insertRowCount = userMapper.insert(item);
        System.out.println("insertRowCount: " + insertRowCount);
        UserPO find = userMapper.selectById(item.getId());
        UserPO upd = new UserPO();
        upd.setId(item.getId());
        upd.setName("jeff1");
        int updRowCount = userMapper.updateById(item);
        System.out.println("updRowCount: " + updRowCount);
        int delRowCount = userMapper.deleteById(item.getId());
        System.out.println("delRowCount: " + delRowCount);
        // 数据源上下文清空,避免影响后续的数据库操作
        DynamicDataSourceContextHolder.poll();
    }
}