SpringBBoot整合MyBatis

只谈情不闲聊 提交于 2019-12-14 11:49:50

一、目录展示

  

二、导入依赖

  

三、配置文件application.yml

  

四、Student实体类

  

package com.zn.entity;
public class Student {
    private Integer stu_id;
    private String stu_name;

    @Override
    public String toString() {
        return "Student{" +
                "stu_id=" + stu_id +
                ", stu_name='" + stu_name + '\'' +
                '}';
    }

    public Student() {
    }

    public Student(String stu_name) {
        this.stu_name = stu_name;
    }

    public Student(Integer stu_id, String stu_name) {
        this.stu_id = stu_id;
        this.stu_name = stu_name;
    }


    public Integer getStu_id() {
        return stu_id;
    }

    public void setStu_id(Integer stu_id) {
        this.stu_id = stu_id;
    }

    public String getStu_name() {
        return stu_name;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }
}

五、StudentDao层

package com.zn.dao;

import com.zn.entity.Student;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface StudentDao {

    //添加数据
    int insertStudent(Student student);

    //修改数据
    int updateStudent(Student student);

    //删除数据
    int deleteStudent(Integer id);

    //查询数据
    List<Student> findAll();
}

六、resources下的mapper中的StudentDao.xml

<?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="com.zn.dao.StudentDao" >
    <!--添加数据-->
    <insert id="insertStudent">
        insert  into  studentinfo(stu_name) values(#{stu_name})
    </insert>

    <!--修改数据-->
    <update id="updateStudent">
        update studentinfo set stu_name=#{stu_name} where stu_id=#{stu_id}
    </update>

    <!--删除数据-->
    <delete id="deleteStudent">
        delete from studentinfo where stu_id=#{stu_id}
    </delete>

    <!--查询数据-->
    <select id="findAll" resultType="com.zn.entity.Student">
        select * from studentinfo
    </select>
</mapper>

七、StudentService层

package com.zn.service;

import com.zn.dao.StudentDao;
import com.zn.entity.Student;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentService {

    @Resource
    StudentDao studentDao;

    //增加数据
    public int insertStudent(Student student) {
        return studentDao.insertStudent(student);
    }

    //修改数据
    public int updateStudent(Student student) {
        return studentDao.updateStudent(student);
    }

    //删除数据
    public int deleteStudent(Integer id) {
        return studentDao.deleteStudent(id);
    }

    //查询数据
    public List<Student> findAll(){
        return studentDao.findAll();
    }
}

八、StudentController层

package com.zn.controller;

import com.zn.entity.Student;
import com.zn.service.StudentService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class StudentController {

    @Resource
    StudentService studentService;

    //添加数据
    @RequestMapping("/insertStudent")
    public int insertStudent(){
        return studentService.insertStudent(new Student("刘姐"));
    }

    //修改数据
    @RequestMapping("/updateStudent")
    public int updateStudent(){
        return studentService.updateStudent(new Student(5,"小傻子"));
    }

    //删除数据
    @RequestMapping("/deleteStudent")
    public int deleteStudent(){
        return studentService.deleteStudent(4);
    }

    //查询数据
    @RequestMapping("/findAll")
    public List<Student> findAll(){
        return studentService.findAll();
    }
}

九、测试类

  

十、效果展示

  省略增删改效果

  (1)查询数据

    

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!