
1,utils类上面添加@Component注解
2,@Autowired注入需要使用的mapper对象,注意不是静态的
3,创建一个静态的这个utils的对象 private static Utils utils;
4,创建一个init方法,添加@PostConstruct注解,方法内容如下
@Component
public class Utils {
@Autowired
private Mapper Mapper;
@Autowired
private Mapper Mapper;
private static Utils Utils;
@PostConstruct
public void init(){
Utils = this;
Utils.Mapper = this.busiTabMapper;
Utils.Mapper = tMapper;
}
public static void test(Integer id){
try {
List list =Utils.Mapper.selectById(id);
①,静态方法直接调用非静态方法和非静态属性是报错的,但是对象中的就不会去检查。所以这个类需要一个自己的对象layoutNumUtils。
②,如果直接用这个new出的对象调用mapper会出现报null,因为这个mapper是spring控制的springbeen,由spring控制他的创建生命周期等。所以做了一个init方法,并且加上了@PostConstruct,表示在服务器启动的时候运行init。init的工作就是layoutNumUtils = this;把spring创建的springbeen赋给对象。 这样我就有对象,不会报静态调用的错,而且这个对象是springbeen,可以调用mapper这个springbeen。(要想调用springbeen只能本身也是springbeen)欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)