springboot普通工具类调用service层方法

参考:https://blog.csdn.net/wqc19920906/article/details/80009929

1、注解方式:

@Component 
public class TestUtils {
    @Autowired
    private ItemService itemService;
    
    @Autowired
    private ItemMapper itemMapper;
    
    public static TestUtils testUtils;
    
    @PostConstruct
    public void init() {    
        testUtils = this;
    } 
    
    //utils工具类中使用service和mapper接口的方法例子,用"testUtils.xxx.方法" 就可以了      
    public static void test(Item record){
        testUtils.itemMapper.insert(record);
        testUtils.itemService.queryAll();
    }
}
 

猜你喜欢

转载自blog.csdn.net/qq_37164847/article/details/87859674