一般工具类方法都是静态的,不能直接使用@Autowired注入mapper
可以通过以下方法解决
1.创建一个非静态的 Bean 来持有 Mapper
import com.ruoyi.project.jmreport.mapper.WangXiaoMapper;
import com.ruoyi.project.system.mapper.SysConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MapperHolder {
@Autowired
private SysConfigMapper sysConfigMapper;
@Autowired
private WangXiaoMapper wangXiaoMapper;
public SysConfigMapper getSysConfigMapper() {
return sysConfigMapper;
}
public WangXiaoMapper getWangXiaoMapper() {
return wangXiaoMapper;
}
}
2.在工具类中使用 ApplicationContext 获取 Mapper
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@Component
public class QuanGuoPingTaiUtil implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
context = applicationContext;
}
public static SysConfigMapper getSysConfigMapper() {
return context.getBean(MapperHolder.class).getSysConfigMapper();
}
public static WangXiaoMapper getWangXiaoMapper() {
return context.getBean(MapperHolder.class).getWangXiaoMapper();
}
//调用的方法
public static String test() {
SysConfigMapper sysConfigMapper = getSysConfigMapper();
}
}