创建项目
进行多功能模块的建立项目
建立一个spring-boot-mybatis
-
不需要进行勾选
-
finish即可
- 按照如图创建package
entity的course类:
public class Course {
private Long courseId;
private String courseName;
private Long userId;
private String courseClass;
private String cover;
private String courseCode;
private Short finished;
private String username;
private String avatar;
}
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")
})
@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);
@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 finished=#{finished} ")
List selectDoing(short finished);
@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 finished=#{finished}")
List selectFined(short finished);
@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 selectAll();
Course getOne(long courseId);
void delete(long courseId);
void insert(Course course);
void update(Course course);
List selectDoing(short finished);
List selectFined(short finished);
}
RandomUtil类
/*
* 随机数工具类
* */
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();
}
}
SpringBootMybatisApplication类
@SpringBootApplication
@MapperScan("com.springboot.mybatis.mapper")
@EnableSwagger2Doc
public class SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApplication.class, args);
}
}
CourseServiceImplTest类
@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 void insert(Course course) {
courseMapper.insert(course);
}
@Override
public void update(Course course) {
courseMapper.update(course);
}
@Override
public List selectDoing(short finished) {
return courseMapper.selectDoing(finished);
}
@Override
public List selectFined(short finished) {
return courseMapper.selectFined(finished);
}
}
CourseController类
@RestController
@RequestMapping(value = "/api")
@CrossOrigin("http://localhost:80")
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 deleteCourse(@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 = "/course", method = RequestMethod.PUT)
public void updateCourse(@RequestBody Course course) {
courseService.update(course);
}
@RequestMapping(value = "/doing/{id}", method = RequestMethod.GET)
public List selectDoing(@PathVariable("id") short id) {
return courseService.selectDoing(id);
}
@RequestMapping(value = "/finish/{id}", method = RequestMethod.GET)
public List selectFined(@PathVariable("id") short id) {
return courseService.selectFined(id);
}
}
在postman的运行结果显示200即成功·~