Spring Boot整合Mybatis

创建项目

进行多功能模块的建立项目
建立一个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即成功·~

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,634评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,951评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,427评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,770评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,835评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,799评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,768评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,544评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,979评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,271评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,427评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,121评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,756评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,375评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,579评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,410评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,315评论 2 352

推荐阅读更多精彩内容