Spring中静态方法调用Mapper,Service

Spring中静态方法调用Mapper,Service

1. 业务需求

业务开发中,经常会在工具类中通过mybatis的mapper或者service接口操作数据库,但是工具类中的方法我们一般采用静态static方式,而Mapper或者service接口为非静态方法,所以会存在冲突,静态方法不能调用非静态方法。

2. 问题描述

1.直接使用静态方法调用mapper直接爆红变异不通过提示异常:
Non-static field 'categoryMapper' cannot be referenced from a static context.

2.使用非静态方法调用mapper,编译通过,但是调用异常:java.lang.NullPointerException: null
发现注入为null,其实并不是,只是被static方法“清空”了。

3. 解决方案

直接使用静态方法调用mapper
1.为该类添加注解@Component,将其加载到spring容器中;
2.将该类定义为静态属性,private static CommonUtils commonUtils;
3.添加初始化方法,并为初始化方法添加注解@PostConstruct 注解作用是会在类加在是执行init方法
@PostConstruct
public void init() {
     commonUtils = this;
     commonUtils.categoryMapper = this.categoryMapper;
}
4.通过该静态属性调用mapper接口,使用 commonUtils.categoryMapper.selectAll();

4. 相关代码

/**
 * 静态工具类
 *
 * @author zrj
 * @since 2021/7/29
 **/
// 第一步,注入
@Component
public class CommonUtils {

    // 第二步,定义静态属性
    private static CommonUtils commonUtils;

    @Resource
    private CategoryMapper categoryMapper;

    // 第三步,初始化构造方法
    @PostConstruct
    public void init() {
        commonUtils = this;
        commonUtils.categoryMapper = this.categoryMapper;
    }

    /**
     * 1.直接使用静态方法调用mapper直接爆红变异不通过提示异常:
     * Non-static field 'categoryMapper' cannot be referenced from a static context
     */
    public static void getMapperStatic() {
        //categoryMapper.selectAll();
    }

    /**
     * 2.使用非静态方法调用mapper,编译通过,但是调用异常:java.lang.NullPointerException: null
     * 发现注入为null,其实并不是,只是被static方法“清空”了。
     */
    public void getMapperNoStatic() {
        List<Category> categoryList = categoryMapper.selectAll();
        System.out.println(categoryList);
    }

    /**
     * 3.直接使用静态方法调用mapper
     * 1)为该类添加注解@Component,将其加载到spring容器中;
     * 2)将该类定义为静态属性,private static CommonUtils commonUtils;
     * 3)添加初始化方法,并为初始化方法添加注解@PostConstruct 注解作用是会在类加在是执行init方法
     * @ PostConstruct
     * public void init() {
     * commonUtils = this;
     * commonUtils.categoryMapper = this.categoryMapper;
     * }
     * 4)通过该静态属性调用mapper接口,使用 commonUtils.categoryMapper.selectAll();
     */
    public static void getMapperInitStatic() {
        // 第四步,调用
        List<Category> categoryList = commonUtils.categoryMapper.selectAll();
        System.out.println(categoryList);
    }
}

Spring Boot,可以通过两种方式来获取MyBatisMapper对象。一种方式是通过@Autowired注解来自动装配Mapper对象,另一种方式是通过静态方法来手动获取Mapper对象。 首先,使用@Autowired注解的方法可以实现自动装配,简化了获取Mapper对象的过程,代码如下: ```java @Autowired private MyMapper myMapper; // 使用myMapper对象进行数据库操作 ``` 在上述代码,通过@Autowired注解将MyMapper接口的实现类自动注入到myMapper对象,然后就可以直接使用myMapper对象进行数据库操作,无需手动获取。 另一种方式是通过静态方法来手动获取Mapper对象,代码如下: ```java @Configuration @MapperScan("com.example.mapper") // 指定Mapper接口所在的包 public class MyMapperConfig { private static MyMapper myMapper; @Autowired public void setMyMapper(MyMapper myMapper) { MyMapperConfig.myMapper = myMapper; } public static MyMapper getMyMapper() { return myMapper; } } ``` 在上述代码,通过@Configuration注解将该类标记为配置类,并通过@MapperScan注解指定Mapper接口所在的包。然后通过@Autowired注解将MyMapper接口的实现类自动注入到静态的myMapper对象,在静态的getMyMapper方法返回该对象。 这样,在其他地方调用MyMapperConfig.getMyMapper()方法即可获取到Mapper对象,然后就可以使用Mapper对象进行数据库操作。 以上就是通过静态方法获取MyBatis Mapper对象的方式,可以根据具体需求选择适合的方式来获取Mapper对象。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值