小白学习Spring+SpringMVC+Mybatis整合 SSM框架整合(三)

本文介绍使用MyBatis实现学生信息系统的增删改查功能,包括实体类、Dao层、Service层及Controller层的设计与实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、编写实体类

在这里插入图片描述
在这里插入图片描述
Student.java

package com.hjs.domain;

public class Student {
    int id;
    String name;
    String 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 String getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

二、编写Dao层

1.新建dao中的StudentDao接口
在这里插入图片描述

package com.hjs.dao;

import com.hjs.domain.Student;

import java.util.List;

public interface StudentDao {
    int insertStudent(Student student);
    List<Student> selectStudents();
}

2.新建mapper文件
在这里插入图片描述
要区分大小写 两个文件名字一致

<?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">
<!--namespace为接口的全限定名称
    id为方法名称
    resultType为返回值的类型
-->
<mapper namespace="com.hjs.dao.StudentDao">
    <select id="selectStudents" resultType="com.hjs.domain.Student">

  </select>
</mapper>

由于mybatis配置文件中我们设置了实体包的别名所以说我们可以直接把返回值的类型改为别名

<?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">
<!--namespace为接口的全限定名称
    id为方法名称
    resultType为返回值的类型
-->
<mapper namespace="com.hjs.dao.StudentDao">
    <select id="selectStudents" resultType="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>

三、编写Service层

1.新建service层接口
在这里插入图片描述
com.hjs.service.StudentService.java

package com.hjs.service;

import com.hjs.domain.Student;

import java.util.List;

public interface StudentService {
    int insertStudent(Student student);
    List<Student> findStudents();
}

com.hjs.service.impl.StudentServiceImpl.java

package com.hjs.service.impl;

import com.hjs.domain.Student;
import com.hjs.service.StudentService;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
    @Override
    public int insertStudent(Student student) {
        return 0;
    }

    @Override
    public List<Student> findStudents() {
        return null;
    }
}

**2.在com.hjs.service.impl.StudentServiceImpl.java中声明
Dao对象
**
package com.hjs.service.impl;

import com.hjs.dao.StudentDao;
import com.hjs.domain.Student;
import com.hjs.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
    //引用类型的自动注入@Autowired,@Resource
    @Resource
    private StudentDao studentDao;
    @Override
    public int insertStudent(Student student) {
        int nums = studentDao.insertStudent(student);
        return nums;
    }

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

在这里插入图片描述
spring创建的对象自动注入

四、编写Controller层

1.新建controller文件
在这里插入图片描述
2.编写controller文件

package com.hjs.controller;

import com.hjs.domain.Student;
import com.hjs.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
@RequestMapping(value = "/student")
public class StudentController{
    @Resource
    private StudentService service;
    //注册学生
    @RequestMapping(value = "/addStudent.do" , method = RequestMethod.POST)
    public ModelAndView addStudent(Student student){
        ModelAndView mv = new ModelAndView();
        String tips = "注册失败";
        //调用service处理student
        int nums = service.insertStudent(student);
        System.out.println(nums+"===========");
        if (nums>0){
            //注册成功
            tips ="学生【"+student.getName()+"】注册成功";
        }
        //添加数据
        mv.addObject("tips",tips);
        //指定结果页面
        mv.setViewName("result");
        System.out.println("访问成功");
        return mv;
    }
}

在这里插入图片描述
访问顺序
index.jsp---->addStudent.jsp---->student/addStudent.do(service的dao的方法)---->result.jsp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值