美文网首页
Spring Boot整合Mybatis

Spring Boot整合Mybatis

作者: 次饭饭_2628 | 来源:发表于2019-04-17 19:41 被阅读0次

    创建项目

    进行多功能模块的建立项目
    建立一个spring-boot-mybatis

    image.png image.png
    • 不需要进行勾选


      image.png
    • finish即可


      image.png
    • 按照如图创建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<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);
    
        @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<Course> 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<Course> 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<Course> selectAll();
        Course getOne(long courseId);
        void delete(long courseId);
        void insert(Course course);
        void update(Course course);
        List<Course> selectDoing(short finished);
        List<Course> 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<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 void insert(Course course) {
            courseMapper.insert(course);
        }
        @Override
        public void update(Course course) {
            courseMapper.update(course);
        }
        @Override
        public List<Course> selectDoing(short finished) {
            return courseMapper.selectDoing(finished);
        }
        @Override
        public List<Course> 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<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 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<Course> selectDoing(@PathVariable("id") short id) {
            return courseService.selectDoing(id);
        }
    
        @RequestMapping(value = "/finish/{id}", method = RequestMethod.GET)
        public List<Course> selectFined(@PathVariable("id") short id) {
            return courseService.selectFined(id);
        }
    }
    
    

    在postman的运行结果显示200即成功·~

    相关文章

      网友评论

          本文标题:Spring Boot整合Mybatis

          本文链接:https://www.haomeiwen.com/subject/pjammqtx.html