小浩MR.C
2020-07-03 11:42
采纳率: 100%
浏览 273
已采纳

spring boot环境,在service里调用线程,线程执行一条sql插入语句,执行Test测试无报错,但数据库没数据

  • service层代码
UserService service;

@Autowired
public void setService(UserService service) {
       this.service = service;
 }

@Override
public void add(User user) {
        System.out.println(mapper);
        Thread thread = new Thread(new MyRunnable(mapper, user));
        thread.start();
    }


class MyRunnable implements Runnable {

    UserMapper mapper;
    User user;

    public MyRunnable(UserMapper mapper, User user) {
        this.mapper = mapper;
        this.user = user;
        System.out.println(mapper);
        System.out.println(user);
    }

    @Override
    public void run() {
        mapper.insert(user);
    }
}

@Test
public void test1() {
        User u1 = new User(1L,"A");
        service.add(u1);
}

控制台输出

输出代码

数据库确没数据,又不报错。

2条回答 默认 最新

相关推荐 更多相似问题