Spring Boot整合Mybatis進(jìn)行CRUD

依賴

<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);
    }


}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市份殿,隨后出現(xiàn)的幾起案子实愚,更是在濱河造成了極大的恐慌,老刑警劉巖醋闭,帶你破解...
    沈念sama閱讀 211,948評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡璧南,警方通過查閱死者的電腦和手機赤屋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,371評論 3 385
  • 文/潘曉璐 我一進(jìn)店門立镶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人类早,你說我怎么就攤上這事媚媒。” “怎么了涩僻?”我有些...
    開封第一講書人閱讀 157,490評論 0 348
  • 文/不壞的土叔 我叫張陵缭召,是天一觀的道長。 經(jīng)常有香客問我逆日,道長嵌巷,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,521評論 1 284
  • 正文 為了忘掉前任室抽,我火速辦了婚禮搪哪,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘坪圾。我一直安慰自己晓折,他們只是感情好惑朦,可當(dāng)我...
    茶點故事閱讀 65,627評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著漓概,像睡著了一般漾月。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上胃珍,一...
    開封第一講書人閱讀 49,842評論 1 290
  • 那天梁肿,我揣著相機與錄音,去河邊找鬼堂鲜。 笑死栈雳,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的缔莲。 我是一名探鬼主播哥纫,決...
    沈念sama閱讀 38,997評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼痴奏!你這毒婦竟也來了蛀骇?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,741評論 0 268
  • 序言:老撾萬榮一對情侶失蹤读拆,失蹤者是張志新(化名)和其女友劉穎擅憔,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體檐晕,經(jīng)...
    沈念sama閱讀 44,203評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡暑诸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,534評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了辟灰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片个榕。...
    茶點故事閱讀 38,673評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖芥喇,靈堂內(nèi)的尸體忽然破棺而出西采,到底是詐尸還是另有隱情,我是刑警寧澤继控,帶...
    沈念sama閱讀 34,339評論 4 330
  • 正文 年R本政府宣布械馆,位于F島的核電站,受9級特大地震影響武通,放射性物質(zhì)發(fā)生泄漏霹崎。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,955評論 3 313
  • 文/蒙蒙 一冶忱、第九天 我趴在偏房一處隱蔽的房頂上張望尾菇。 院中可真熱鬧,春花似錦、人聲如沸错沽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,770評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽千埃。三九已至,卻和暖如春忆植,著一層夾襖步出監(jiān)牢的瞬間放可,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,000評論 1 266
  • 我被黑心中介騙來泰國打工朝刊, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留耀里,地道東北人。 一個月前我還...
    沈念sama閱讀 46,394評論 2 360
  • 正文 我出身青樓拾氓,卻偏偏與公主長得像冯挎,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子咙鞍,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,562評論 2 349

推薦閱讀更多精彩內(nèi)容