首页 > 编程语言 > 详细

SpringBBoot整合MyBatis

时间:2019-12-14 13:43:35      阅读:95      评论:0      收藏:0      [点我收藏+]

一、目录展示

  技术分享图片

二、导入依赖

  技术分享图片

三、配置文件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)查询数据

    技术分享图片

SpringBBoot整合MyBatis

原文:https://www.cnblogs.com/Zzzzn/p/12038529.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!