依赖
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
mysql
mysql-connector-java
5.1.39
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
org.projectlombok
lombok
1.18.6
true
实体类(entity)
Course
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;
@RequestMapping(value = "/courses",method = RequestMethod.GET)
public List selectAll(){
return courseService.selectAll();
}
@RequestMapping(value = "/course/{id}",method = RequestMethod.GET)
public Course getOne(@PathVariable("id") long id){
return courseService.getOne(id);
}
@RequestMapping(value = "/course/{id}",method = RequestMethod.DELETE)
public void delete(@PathVariable("id") long id){
courseService.delete(id);
}
@RequestMapping(value = "/course",method = RequestMethod.POST)
public Course addCourse(@RequestBody Course course){
course.setCourseCode(RandomUtil.getRandomCode());
return courseService.insert(course);
}
}
mapper
CourseMapper
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(column = "course_id", property = "courseId"),
@Result(column = "course_name", property = "courseName"),
@Result(column = "user_id", property = "userId"),
@Result(column = "course_class", property = "courseClass"),
@Result(column = "cover", property = "cover"),
@Result(column = "course_code", property = "courseCode"),
@Result(column = "finished", property = "finished"),
})
@Select("select * from t_course")
List selectAll();
@Results({
@Result(column = "course_id", property = "courseId"),
@Result(column = "course_name", property = "courseName"),
@Result(column = "user_id", property = "userId"),
@Result(column = "course_class", property = "courseClass"),
@Result(column = "cover", property = "cover"),
@Result(column = "course_code", property = "courseCode"),
@Result(column = "finished", property = "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 course_name= #{courseName},cover= #{cover},finished=#{finished}")
void update(Course course);
}
Service
CourseService
package com.springboot.mybatis.service;
import com.springboot.mybatis.entity.Course;
import java.util.List;
public interface CourseService {
List selectAll();
Course getOne(Long courseId);
void delete(long courseId);
Course insert(Course course);
void update(Course course);
}
impl
CourseServiceImpl
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 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 Course insert(Course course) {
courseMapper.insert(course);
return course;
}
@Override
public void update(Course course){
courseMapper.update(course);
}
}
controller
CourseController
package com.springboot.mybatis.controller;
import com.springboot.mybatis.entity.User;
import com.springboot.mybatis.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping(value = "/API")
public class UserController {
@Resource
private UserService userService;
@RequestMapping(value = "/users",method = RequestMethod.GET)
public List selectAll(){
return userService.selectAll();
}
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
public User getOne(@PathVariable("id") long id){
return userService.getOne(id);
}
@RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
public void delete(@PathVariable("id") long id){
userService.delete(id);
}
@RequestMapping(value = "/user",method = RequestMethod.POST)
public void addUser(@RequestBody User user){
userService.insert(user);
}
@RequestMapping(value = "/user",method = RequestMethod.PUT)
public void updateUser(@RequestBody User user){
userService.update(user);
}
}
测试类
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CourseServiceImplTest {
@Resource
private CourseService courseService;
@Test
public void selectAll() {
List 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(5L);
}
@Test
public void insert(){
Course course=new Course();
course.setCourseName("微信开发小程序");
course.setCourseClass("软件1721");
course.setUserId(1L);
course.setCover("1.jpg");
course.setCourseCode(RandomUtil.getRandomCode());
course.setFinished((short) 0);
courseService.insert(course);
}
@Test
public void update(){
Course course=courseService.getOne(5L);
course.setCourseName("js");
course.setCover("123.jpg");
course.setFinished((short)0);
courseService.update(course);
}
}