Spring的注解方式配置bean

eg代码:

package com.qf.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.Dept;
import com.qf.pojo.Employee;
import com.qf.pojo.Post;
import com.qf.pojo.Sign;
import com.qf.service.IDeptService;
import com.qf.service.IEmployeeService;
import com.qf.service.IPostService;

@Controller
public class EmployeeController {
    @Autowired
    private IDeptService deptService;
    @Autowired
    private IPostService postService;
    @Autowired
    private IEmployeeService empService;

    @RequestMapping("/selectEmployee") // 查询所有
    public String getAllEmployee(Model model, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        
        List<Dept> allDeptss = deptService.getAllDeptss();
        List<Post> allPosts = postService.getAllPosts();
        PageHelper.startPage(page, 5);
        List<Employee> allEmployees = empService.getAllEmployees();
        PageInfo<Employee> pageInfo = new PageInfo<>(allEmployees);
        if (pageInfo.getPageNum()!=0) {
            
            if (pageInfo.getPageNum()==pageInfo.getPages()) {
                   pageInfo.setNextPage(pageInfo.getPages());
                }
            
            if (allEmployees!=null) {
                model.addAttribute("jobs", allPosts);
                model.addAttribute("depts", allDeptss);
                model.addAttribute("employees", allEmployees);
                model.addAttribute("pages", pageInfo);
                model.addAttribute("action", "all");
                return "employee/employee";
        }
        
        }
        
        
        return "redirect:/selectEmployee";
    }

    @GetMapping("/addEmployeeJsp") // 添加员工页面跳转显示
    public String addEmployee(Model model) {

        List<Dept> allDeptss = deptService.getAllDeptss();
        List<Post> allPosts = postService.getAllPosts();
        model.addAttribute("jobs", allPosts);
        model.addAttribute("depts", allDeptss);

        return "employee/showAddEmployee";
    }

    @PostMapping("/addEmployeeOnes") // 添加员工
    public String addEmployeeOne(Employee employee, Model model) {
        int addEmployeeOne = empService.addEmployeeOne(employee);
        return "redirect:/selectEmployee";
    }

    @RequestMapping("/selectEmployeeLike") //模糊查询
    public String selectEmployeeOnLike(Model model, HttpServletRequest request,@RequestParam(value = "page", defaultValue = "1") Integer page) {
        String pid = request.getParameter("pid");
        String name = request.getParameter("name");
        String cardId = request.getParameter("cardId");
        String sex = request.getParameter("sex");
        String phone = request.getParameter("phone");
        String did = request.getParameter("did");
        
        HashMap<String, String> map = new HashMap<>();
        map.put("pid", pid);map.put("cardId", cardId);map.put("sex", sex);
        map.put("name", name);map.put("phone", phone);map.put("did", did);
        
        List<Employee> selectEmployeeLike = empService.selectEmployeeLike(map);
        PageHelper.startPage(page, 3);
        List<Dept> allDeptss = deptService.getAllDeptss();
        List<Post> allPosts = postService.getAllPosts();
        
        PageInfo<Employee> pageInfo = new PageInfo<>(selectEmployeeLike);
        if (pageInfo.getPageNum()!=0) {
            model.addAttribute("jobs", allPosts);
            model.addAttribute("depts", allDeptss);
            model.addAttribute("employees", selectEmployeeLike);
            model.addAttribute("pages", pageInfo);
            model.addAttribute("action", "like");
            return "employee/employee";    
        }
        return "redirect:/selectEmployeeLike";
    }
    
    @GetMapping("/updateEmployee") //查询一条 
    public String updateEmployee(Model model,HttpServletRequest  request) {
        
        String eids = request.getParameter("eid");
        int eid=Integer.parseInt(eids);
        
        Employee oneEmp = empService.getOneEmp(eid);
        System.out.println(oneEmp+"********************************");
        model.addAttribute("employee", oneEmp);
        List<Dept> allDeptss = deptService.getAllDeptss();
        List<Post> allPosts = postService.getAllPosts();
        model.addAttribute("jobs", allPosts);
        model.addAttribute("depts", allDeptss);
    
        return "employee/showUpdateEmployee";
    }
    
    @PostMapping("/updateEmployee") //修改 
    public String updateEmployee2(Model model,Employee employee) {
        
        int updateEmployee = empService.updateEmployee(employee);
        
        System.out.println(updateEmployee+"修改666");
    
        return "redirect:/selectEmployee";
    }
    
    @GetMapping("/removeEmployee")//删除员工
    public String delEmp(Model model,String ids){
        
        String[] eids = ids.split(",");
        ArrayList<Integer> list=new ArrayList<>();
        for (String eid : eids) {
            list.add(Integer.parseInt(eid));
        }
        int delEmployee = empService.delEmployee(list);
        
        System.out.println(delEmployee+"个已经删除");
        return "redirect:/selectEmployee";
    }
}

 

比如以上的Controller层在Bean实现类中通过一些Annotation来标注Bean类:

·@Component:标注一个普通的Spring Bean类(可以指定Bean名称,未指定时默认为小写字母开头的类名)

·@Controller:标注一个控制器类

·@Service:标注一个业务逻辑类

·@Repository:标注一个DAO类

通过在成员变量或者方法入参处标注@Autowired按类型匹配注入,也可以使用@Qualifier按名称配置注入。通过在方法上标注@PostConstrut和PreDestroy注解指定的初始化方法和销毁方法(可以定义任意多个)。通过@Scope(“prototype”)指定Bean的作用范围。通过在类定义处标注@Lazy(true)指定Bean的延迟加载。

当Bean的实现类是当前项目开发的,可以直接在Java类中使用基于注解的配置,配置比较简单。

eg代碼Impl:

package com.qf.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qf.dao.IEmployeeDao;
import com.qf.pojo.Employee;
import com.qf.service.IEmployeeService;
@Service
public class EmployeesServiceImpl implements IEmployeeService{
    @Autowired
    private IEmployeeDao employeeDao;
    @Override
    public List<Employee> getAllEmployees() {
...................

 

posted @ 2017-12-09 17:36  你的安静  阅读(189)  评论(0编辑  收藏  举报