2024/03/06(2024春季)

今日在软件学习上所花的时间大概就3小时多左右。

今日的编写的代码行数算上数据库大概300多行。

发表博客一篇。

今天主要学习了springboot中的三层架构加上分层解藕,使用ioc容器管理和DI分配来完成项目在运行时的对象的实例化对象的填充。

package com.itheima.springbootwebquickstart.controller;

import com.itheima.springbootwebquickstart.dao.impl.EmpDaoA;
import com.itheima.springbootwebquickstart.pojo.Emp;
import com.itheima.springbootwebquickstart.pojo.Result;
import com.itheima.springbootwebquickstart.service.EmpService;
import com.itheima.springbootwebquickstart.service.impl.EmpServiceA;
import com.itheima.springbootwebquickstart.utils.XmlParserUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
//controller层接受请求响应数据
@RestController
public class EmpController {
//    private EmpService empService=new EmpServiceA();
    //耦合性太强需要解耦
    @Resource(name="empServiceB")//jdk提供的

//    @Qualifier("empServiceA")//指定装配的类型
//    @Autowired
    private EmpService empService;


    @RequestMapping("/listEmp")
    public Result list()
    {
//        DAO层
//        String file=this.getClass().getClassLoader().getResource("emp.xml").getFile();
//        System.out.println(file);
//        //从emp.xml中获取数据并解析
//        List<Emp> empList=XmlParserUtils.parse(file, Emp.class);

//        Service层
//        empList.stream().forEach(emp->{
//            String gender=emp.getGender();
//            if("1".equals("gender"))
//            {
//                emp.setGender("男");
//            } else if ("2".equals(gender)) {
//                emp.setGender("女");
//            }
//            String job=emp.getJob();
//            if("1".equals(job))
//            {
//                emp.setGender("讲师");
//            } else if ("2".equals(job)) {
//                emp.setGender("班主任");
//            } else if ("3".equals(job)) {
//                emp.setJob("就业指导");
//            }
//        });




        //响应数据
        List<Emp> empList= empService.listEmp();
        return Result.success(empList);
    }
}
package com.itheima.springbootwebquickstart.service;

import com.itheima.springbootwebquickstart.pojo.Emp;

import java.util.List;
//service进行逻辑处理
public interface EmpService {
    public List<Emp> listEmp();
}
package com.itheima.springbootwebquickstart.service.impl;

import com.itheima.springbootwebquickstart.dao.EmpDao;
import com.itheima.springbootwebquickstart.dao.impl.EmpDaoA;
import com.itheima.springbootwebquickstart.pojo.Emp;
import com.itheima.springbootwebquickstart.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

//@Component
@Service
public class EmpServiceA implements EmpService {
//    private EmpDao empDao=new EmpDaoA();
    //耦合性太强需要解耦

    @Autowired//运行时IOC容器会提供该类型的bean对象
    private EmpDao empDao;
    @Override
    public List<Emp> listEmp() {
        List<Emp> empList=empDao.listEmp();
        empList.stream().forEach(emp->{
            String gender=emp.getGender();
            if("1".equals(gender))
            {
                emp.setGender("男");
            } else if ("2".equals(gender)) {
                emp.setGender("女");
            }
            String job=emp.getJob();
            if("1".equals(job))
            {
                emp.setJob("讲师");
            } else if ("2".equals(job)) {
                emp.setJob("班主任");
            } else if ("3".equals(job)) {
                emp.setJob("就业指导");
            }
        });
        return empList;
    }
}

大概就是使用@Component让该类于容器管理,然后用@Autowired来向接口的别名分配

 

posted @ 2024-03-06 22:31  伐木工熊大  阅读(5)  评论(0编辑  收藏  举报