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);
}
}
@Testpublic void test1() {
User u1 = new User(1L,"A");
service.add(u1);
}
控制台输出
数据库确没数据,又不报错。
回答
程序结束的太早了,数据库插入式需要时间的,不信你最后sleep下
以上是 spring boot环境,在service里调用线程,线程执行一条sql插入语句,执行Test测试无报错,但数据库没数据 的全部内容, 来源链接: utcz.com/a/29762.html