您当前的位置: 首页 >  spring

小志的博客

暂无认证

  • 0浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

springboot 项目普通类中调用mapper或service接口(utils包下的类封装方法时调用mapper或service接口)

小志的博客 发布时间:2020-04-23 09:31:38 ,浏览量:0

1、整体步骤如下:
1、该类使用@Component注解

2、添加一个你需要用到的类的静态字段

3、创建一个初始化方法,贴上@PostConstruct 标签,用于注入bean

4、创建方法调用mapper或service接口

5、最后直接在普通类中调用即可
2、普通类中调用mapper接口中的新增员工方法的示例代码如下:
package com.rf.utils;

import com.rf.entity.Employee;
import com.rf.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: json数据解析工具类
 * @author: xiaozhi
 * @create: 2020-04-20 14:23
 */
 //第一步:使用@Component注解
@Component
public class JsonParse {

    @Autowired
    EmployeeMapper employeeMapper;
    
    //第二步:添加一个你需要用到的类的静态字段
    public static JsonParse jsonParse;
    
	//第三步:创建一个初始化方法,引入@PostConstruct注解,用于注入bean
    @PostConstruct
    public void init(){
        jsonParse = this ;
        //第四步:调用mapper或service接口
        jsonParse.employeeMapper = this.employeeMapper;
    }
	//你要封装的方法
    public static void  getJsonParse(String jsondata){
        List list=new ArrayList();
        Employee employee=null;
        for(int i=0;i            
关注
打赏
1661269038
查看更多评论
0.0543s