依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<optional>true</optional>
</dependency>
實體類(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<Course> 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<Course> 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<Course> 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<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 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<User> 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<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(5L);
}
@Test
public void insert(){
Course course=new Course();
course.setCourseName("微信開發(fā)小程序");
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);
}
}