第三章 SSM整合案例2-写后端程序(重要).md

zhanglei 2022年05月30日 351次浏览

第三章 SSM整合案例2-写后端程序(重要)

实体类:Student

package domain;

public class Student {
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Controller层 :StudentController.java

package controller;

import domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.StudentService;

import java.util.List;

@Controller
@RequestMapping("/student")

public class StudentController {
    @Autowired
    private StudentService service;
    //注册学生
    @RequestMapping("/addStudent.do")
    public ModelAndView addStudent(Student student){
        ModelAndView mv=new ModelAndView();
        String tips="注册失败";
        //调用service处理student
        int nums=service.addStudent(student);
        if(nums > 0){
            //注册成功
            tips="学生["+student.getName()+"]注册成功";
        }
        //添加数据
        mv.addObject("tips",tips);
        //指定结果页面
        mv.setViewName("result");
        return mv;
    }

    //处理查询,响应ajax
    @RequestMapping("/queryStudent.do")
    @ResponseBody
    public List<Student> queryStudent(){
        //参数检查
        List<Student> students = service.findStudents();
        return students;
    }
}

Service层:StudentService接口以及StudentServiceImpl实现类

package service;

import domain.Student;

import java.util.List;

public interface StudentService {
    int addStudent(Student student);
    List<Student> findStudents();
}
package service.imp;

import dao.StudentDao;
import domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Service;
import service.StudentService;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;
    @Override
    public int addStudent(Student student) {
        int nums=studentDao.insertStudent(student);
        return nums;
    }

    @Override
    public List<Student> findStudents() {
        return studentDao.selectStudents();
    }
}

Dao层:StudentDao接口以及StudentDao.xml(mapper)

package dao;

import domain.Student;

import java.util.List;

public interface StudentDao {
    int insertStudent(Student student);
    List<Student> selectStudents();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--绑定接口-->
<mapper namespace="dao.StudentDao">

    <!--
        mapper是根标签
        mapper里面可以写<insert>,<update>,<select>等标签
            标签的id属性:正常是需要重写的方法名
            标签的resultType属性:将数据赋值给哪个类型的Java对象
        标签内部都有同名的sql语句
    -->
    <select id="selectStudents" resultType="domain.Student">
        select id,name,age from student order by id desc
    </select>
    
    <insert id="insertStudent">
        insert into student(name,age)values(#{name},#{age})
    </insert>
</mapper>