美文网首页SpringBootJava
Springboot整合Mybatis进行CRUD

Springboot整合Mybatis进行CRUD

作者: 寂川_b645 | 来源:发表于2019-03-18 18:43 被阅读58次

    1.Spring-boot-Mybatis模块结构

    image.png

    2.内部各类代码(增删改查功能的实现)

    1.Controller
    UserController.java

    package com.springboot.mybatis.controller;

    import com.springboot.mybatis.entity.Course;
    import com.springboot.mybatis.service.CourseService;
    import com.springboot.mybatis.util.RandomUtil;
    import org.springframework.web.bind.annotation.*;

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

    @RestController
    @RequestMapping(value = "/api")
    public class CourseController {
    @Resource
    private CourseService courseService;

    /**
     * 查询所有班课
     *
     * @return
     */
    @RequestMapping(value = "/courses", method = RequestMethod.GET)
    public List<Course> selectAll() {
        return courseService.selectAll();
    }
    
    /**
     * 根据id查询班课
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/courses/{id}", method = RequestMethod.GET)
    public Course getOne(@PathVariable("id") long id) {
        return courseService.getOne(id);
    }
    
    /**
     * 根据id删除班课
     *
     * @param id
     */
    @RequestMapping(value = "/courses/{id}", method = RequestMethod.DELETE)
    public void deleteCourse(@PathVariable("id") long id) {
        courseService.delete(id);
    }
    
    /**
     * 新增班课
     *
     * @param course
     */
    @RequestMapping(value = "/courses", method = RequestMethod.POST)
    public void addCourse(@RequestBody Course course) {
        course.setCourseCode(RandomUtil.getRandomCode());
        courseService.insert(course);
    }
    
    /**
     * 更新班课
     *
     * @param course
     */
    @RequestMapping(value = "/course", method = RequestMethod.PUT)
    public void updateCourse(@RequestBody Course course) {
        courseService.update(course);
    }}
    

    2.entity
    Course.java

    package com.springboot.mybatis.entity;

    import lombok.Data;

    @Data
    public class Course {
    private Long courseId;
    private String courseName;
    private Long userId;
    private String courseClass;
    private String cover;
    private String courseCode;
    private Short finished;

    public Long getCourseId() {
        return courseId;
    }
    
    public void setCourseId(Long courseId) {
        this.courseId = courseId;
    }
    
    public String getCourseName() {
        return courseName;
    }
    
    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }
    
    public Long getUserId() {
        return userId;
    }
    
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    
    public String getCourseClass() {
        return courseClass;
    }
    
    public void setCourseClass(String courseClass) {
        this.courseClass = courseClass;
    }
    
    public String getCover() {
        return cover;
    }
    
    public void setCover(String cover) {
        this.cover = cover;
    }
    
    public String getCourseCode() {
        return courseCode;
    }
    
    public void setCourseCode(String courseCode) {
        this.courseCode = courseCode;
    }
    
    public Short getFinished() {
        return finished;
    }
    
    public void setFinished(Short finished) {
        this.finished = finished;
    }
    

    }

    3.Mapper
    CourseMapper.java

    package com.springboot.mybatis.mapper;

    import com.springboot.mybatis.entity.Course;
    import org.apache.ibatis.annotations.*;

    import java.util.List;

    public interface CourseMapper {
    @Results({
    @Result(property = "courseId", column = "course_id"),
    @Result(property = "courseName", column = "course_name"),
    @Result(property = "userId", column = "user_id"),
    @Result(property = "courseClass", column = "course_class"),
    @Result(property = "cover", column = "cover"),
    @Result(property = "courseCode", column = "course_code"),
    @Result(property = "finished", column = "finished")
    })
    @Select("SELECT * FROM t_course ")
    List<Course> selectAll();

    @Results({
            @Result(property = "courseId", column = "course_id"),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "courseClass", column = "course_class"),
            @Result(property = "cover", column = "cover"),
            @Result(property = "courseCode", column = "course_code"),
            @Result(property = "finished", column = "finished")
    })
    @Select("SELECT * FROM t_course WHERE course_id = #{courseId} ")
    Course getOne(Long courseId);
    
    @Delete("DELETE FROM t_course WHERE course_id =#{courseId} ")
    void delete(Long courseId);
    
    @Insert("INSERT INTO t_course(course_name,user_id,course_class,cover,course_code,finished)" +
            " VALUES(#{courseName}, #{userId}, #{courseClass},#{cover},#{courseCode},#{finished}) ")
    void insert(Course course);
    
    @Update("UPDATE t_course SET cover=#{cover},finished=#{finished} WHERE course_id =#{courseId}")
    void update(Course course);
    

    }

    • 注: image.png
      此处SQL语句中 image.png 只是为了转行所进行的操作,如不需转行,将 “ + ” 去掉并添加空格即可。

    4.service
    CourseService.java

    package com.springboot.mybatis.service;

    import com.springboot.mybatis.entity.Course;

    import java.util.List;

    public interface CourseService {
    List<Course> selectAll();

    Course getOne(long courseId);
    
    void delete(long courseId);
    
    void insert(Course course);
    
    void update(Course course);
    

    }

    5.service.Impl
    CourseServiceImpl.java

    package com.springboot.mybatis.service.Impl;

    import com.springboot.mybatis.entity.Course;
    import com.springboot.mybatis.mapper.CourseMapper;
    import com.springboot.mybatis.service.CourseService;
    import org.springframework.stereotype.Service;

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

    @Service
    public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseMapper courseMapper;

    @Override
    public List<Course> selectAll() {
        return courseMapper.selectAll();
    }
    
    @Override
    public Course getOne(long courseId) {
        return courseMapper.getOne(courseId);
    }
    
    @Override
    public void delete(long courseId) {
        courseMapper.delete(courseId);
    }
    
    @Override
    public void insert(Course course) {
        courseMapper.insert(course);
    }
    
    @Override
    public void update(Course course) {
        courseMapper.update(course);
    }
    

    }

    6.util工具类
    随机数工具类RandomUtil

    package com.springboot.mybatis.util;

    import java.util.Random;

    /**随机数工具类 */
    public class RandomUtil {
    public static String getRandomCode() {
    StringBuffer stringBuffer = new StringBuffer();
    Random random = new Random();
    for (int i = 0; i < 6; i++) {
    //生成[0,bound)的随机整数
    int num = random.nextInt(10);
    //将随机数加入可变长字符串
    stringBuffer.append(num);
    }
    return stringBuffer.toString();
    }
    }

    7.junit测试类
    CourseServiceImplTest

    package com.springboot.mybatis.service.Impl;

    import com.springboot.mybatis.entity.Course;
    import com.springboot.mybatis.service.CourseService;
    import com.springboot.mybatis.util.RandomUtil;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;

    import javax.annotation.Resource;

    import java.util.List;

    import static org.junit.Assert.*;

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class CourseServiceImplTest {
    @Resource
    private CourseService courseService;

    @Test
    public void selectAll() {
        List<Course> courseList = courseService.selectAll();
        courseList.forEach(course -> System.out.println(course));
    }
    
    @Test
    public void getOne() {
        Course course = courseService.getOne(1L);
        System.out.println(course);
    }
    
    @Test
    public void delete() {
        courseService.delete(1L);
    }
    
    @Test
    public void insert() {
        Course course = new Course();
        course.setCourseName("微信小程序开发22");
        course.setCourseClass("软件1721");
        course.setUserId(6L);
        course.setCover("1.jpg");
        course.setCourseCode(RandomUtil.getRandomCode());
        course.setFinished((short) 0);
        courseService.insert(course);
    }
    
    @Test
    public void update() {
        Course course = courseService.getOne(4L);
        course.setCover("999.jpg");
        course.setFinished((short) 1);
        courseService.update(course);
    }
    

    }

    3.数据库中数据表的配置

    image.png

    相关文章

      网友评论

        本文标题:Springboot整合Mybatis进行CRUD

        本文链接:https://www.haomeiwen.com/subject/osbbmqtx.html