首先建立表
实体类(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;
}
@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<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"),
@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 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);
}
.
CourseService
public interface CourseService {
List<Course> seleceAll();
Course getOne(long courseId);
void delete(long courseId);
void insert(Course course);
void update(Course course);
}
CourseServiceImpl
@Service
public class CourseServiceImpl implements CourseService{
@Resource
private CourseMapper courseMapper;
@Override
public List<Course> 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 <Course> 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 = "/api")
public class CourseController {
@Resource
private CourseService courseService;
@RequestMapping(value = "/courses",method = RequestMethod.GET)
public List<Course> selectAll(){
return courseService.seleceAll();
}
@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 void addCourse(@RequestBody Course course){
course.setCourseCode( RandomUtil.getRandomCode() );
courseService.insert(course);
}
@RequestMapping(value = "/coursec",method = RequestMethod.PUT)
public void update(@RequestBody Course course){
courseService.update(course);
}
}