• 13.SpringBoot项目之Service层


    SpringBoot项目之Service层

    JavaEE三层架构

    为了项目维护方便,为了项目开发便利。
    
    • 1
    三层架构功能
    控制器层:controller方便和前端数据进行交互
    业务层:service处理各种业务
    持久化层:mapper和数据库进行数据交互

    01

    抽取出service层

    按照这个层级目录输入
    02

    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import org.springframework.stereotype.Service;
    
    /**
    * 专门处理股票的各种业务
    * (不包含请求和响应,因为请求和响应是控制器处理)
    */
    @Service
    public class StockService {
    
       /**
        * 处理股票基本信息的json字符串
        * @param json
        */
       public void stockBasicJsonInfoHandler(String json){
           //5、JSON转换
           JSONObject jsonObject = JSONObject.parseObject(json);
           //获取请求id(测试用)
           String requestId = jsonObject.getString("request_id");
           //获取本次的所有数据
           JSONObject data = jsonObject.getJSONObject("data");
           //获取表头(数组)
           JSONArray fields = data.getJSONArray("fields");
           //fields.fori
           for (int i = 0; i < fields.size(); i++) {
               System.out.print(fields.get(i)+"\t\t");
           }
           System.out.println();
           //获取表格内部数据
           JSONArray items = data.getJSONArray("items");
           for (int i = 0; i < items.size(); i++) {
               JSONArray jsonArray = items.getJSONArray(i);
               //展示这一行的数据
               for (int j = 0; j < jsonArray.size(); j++) {
                   System.out.print(jsonArray.get(j)+"\t\t");
               }
               System.out.println();
           }
       }
    /**
    * 从CSV文件中读取股票信息
    */
    public void stockBasicInfoCSVHandler() {
       File file;
       try {
           file = ResourceUtils.getFile("classpath:csvdata/stock_basic.csv");
       } catch (FileNotFoundException e) {
           throw new RuntimeException(e);
       }
       try (
               FileReader reader = new FileReader(file);
       ) {
           CSVReader csvReader = new CSVReader(reader);
           List<String[]> list = csvReader.readAll();
           for (String[] arr : list) {
               //展示某一行的数据
               for (String s : arr) {
                   System.out.print(s + "\t\t");
               }
               System.out.println();
           }
       } catch (FileNotFoundException e) {
           throw new RuntimeException(e);
       } catch (IOException e) {
           throw new RuntimeException(e);
       } catch (CsvException e) {
           throw new RuntimeException(e);
       }
    }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    控制器层:
    在前面创建的MyController2类中输入

    @RestController
    @RequestMapping("/my2")
    public class MyController2 {
       //将RestTemplate工具导入到当前控制器中
       @Autowired
       RestTemplate restTemplate;
       @Autowired
       StockService stockService;
     //中间其他案例代码省略.......
    /**
        * fastjson的转换展示(带有service)
        */
       @RequestMapping("/r6")
       public void r6(){
           //1、封装map参数
           HashMap<String, String> map = new HashMap<>();
           map.put("api_name","stock_basic");
           map.put("token","1d4f7ff4545ece6b15812c50d2e179271e4b8c597e68744f6cbbc662");
           //2、设置请求头信息
           HttpHeaders httpHeaders = new HttpHeaders();
           httpHeaders.setContentType(MediaType.APPLICATION_JSON);
           //3、封装头实体内容
           HttpEntity<Map> entity = new HttpEntity<>(map, httpHeaders);
           //4、传递信息
           String json = restTemplate.postForObject("http://api.tushare.pro", entity, String.class);
           //5、调用业务层,处理并展示json数据
           stockService.stockBasicJsonInfoHandler(json);
       }
    /**
    * openCSV
    */
    @RequestMapping("/r7")
    public void r7() {
       stockService.stockBasicInfoCSVHandler();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    Controller控制器层: 所有和请求响应有关的代码,都在该层
    Service业务层:所有和请求响应无关的代码,放在业务层

  • 相关阅读:
    机器学习基础(5)—— 模型选择之性能度量
    synchronized代码块使用练习
    【二进制和位运算】二、认识时间复杂度
    短视频简单无脑玩法,播放量10w+的藏头诗玩法,操作思路分享给你!
    list,dict使用方法
    思科设备BGP配置命令
    外汇天眼:芝加哥商品交易所 的 ADNV 在 10 月下跌12%!
    玉米社:SEM竞价推广投放之提升转化操作办法
    Spark 6:Spark SQL DataFrame
    Nginx那些事儿2
  • 原文地址:https://blog.csdn.net/m0_58503202/article/details/133806735