04.Spring|04.Spring Boot整合Mybatis进行CRUD

首先建立表 04.Spring|04.Spring Boot整合Mybatis进行CRUD
文章图片
Snipaste_2019-03-18_17-12-54.png 实体类(Course)

@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; }

【04.Spring|04.Spring Boot整合Mybatis进行CRUD】@CourseMapper
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"), @Result(column = "username", property = "username"), @Result(column = "avatar", property = "avatar") }) @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"), @Result(column = "username", property = "username"), @Result(column = "avatar", property = "avatar") }) @Select("SELECT * FROM t_course WHERE course_id= #{courseId} ") Course getOne(Long courseId); @Delete( "DELETE FROMt_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})") voidinsert(Course course); @Update("UPDATE t_course SET cover=#{cover},finished=#{finished} WHERE course_id =#{courseId}") void update(Course course); } .

CourseService
public interface CourseService { List seleceAll(); Course getOne(long courseId); void delete(longcourseId); voidinsert(Course course); void update(Course course); }

CourseServiceImpl
@Service public class CourseServiceImpl implements CourseService{ @Resource privateCourseMapper courseMapper; @Override public List seleceAll() { 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); } }

进行测试
@RunWith(SpringRunner.class) @SpringBootTest public class CourseServiceTest { @Resource private CourseService courseService; @Test public void seleceAll() throws Exception { List courseList = courseService.seleceAll(); courseList.forEach( course -> System.out.println( course ) ); }@Test public void getOne() throws Exception { Course course = courseService.getOne( 1L ); System.out.println( course ); }@Test public void delete() throws Exception { courseService.delete( 6L ); }@Test public void insert() throws Exception { 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() throws Exception { Course course = courseService.getOne(4L); course.setCover("999.jpg"); course.setFinished((short) 1); courseService.update(course); }}

Controller
@RestController @RequestMapping(value = "https://www.it610.com/api") public class CourseController { @Resource private CourseService courseService; @RequestMapping(value = "https://www.it610.com/courses",method = RequestMethod.GET) public List selectAll(){ return courseService.seleceAll(); } @RequestMapping(value = "https://www.it610.com/course/{id}",method =RequestMethod.GET) public Course getOne(@PathVariable("id") long id){ returncourseService.getOne(id); } @RequestMapping(value = "https://www.it610.com/course/{id}",method = RequestMethod.DELETE) publicvoiddelete(@PathVariable("id") long id){ courseService.delete( id ); }@RequestMapping(value = "https://www.it610.com/course",method = RequestMethod.POST) public void addCourse(@RequestBody Course course){ course.setCourseCode( RandomUtil.getRandomCode() ); courseService.insert(course); } @RequestMapping(value = "https://www.it610.com/coursec",method = RequestMethod.PUT) publicvoid update(@RequestBody Course course){ courseService.update(course); }}

postman运行结果图 04.Spring|04.Spring Boot整合Mybatis进行CRUD
文章图片
delete.png 04.Spring|04.Spring Boot整合Mybatis进行CRUD
文章图片
Snipaste_2019-03-18_15-40-27.png 04.Spring|04.Spring Boot整合Mybatis进行CRUD
文章图片
Snipaste_2019-03-18_16-17-45.png 04.Spring|04.Spring Boot整合Mybatis进行CRUD
文章图片
Snipaste_2019-03-18_16-27-41.png 04.Spring|04.Spring Boot整合Mybatis进行CRUD
文章图片
users.png

    推荐阅读