JavaFxcontroller中用@Autowired@Resource注入为空,是因为JavaFxcontroller没有交给spring的ioc管理

解决方案

使用一个静态工具类,对mapper进行管理/注入,并提供静态方法

工具类 MapperUtil.java

@Component
public class MapperUtil implements Serializable {
@Autowired
private UserMapper userMapper;

public static MapperUtil mapperUtil;

@PostConstruct
public void init(){
mapperUtil=this;
mapperUtil.userMapper=this.userMapper;
}

// 添加用户
public static void addUser(User u){
mapperUtil.userMapper.addUser(u);
}
}

使用

@FXML
protected void onLoginButtonClick() throws Exception {
// 此处省略
User loginUser = MapperUtil.findByName(name.getText());
if(loginUser==null){
// 此处省略
}
else{
// 此处省略
}
}

参考链接

springboot+javafx+mybatis在controller中注入mapper为空-CSDN博客