Spring|Spring Boot整合Mybatis进行CRUD
【Spring|Spring Boot整合Mybatis进行CRUD】进行多功能模块的建立项目
建立一个spring-boot-mybatis
文章图片
56.png entity的course类
package com.springboot.mybatis.entity;CourseMapper类
import lombok.Data;
@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;
}
package com.springboot.mybatis.mapper;CourseService类
import com.springboot.mybatis.entity.Course;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface CourseMapper {
@Select("SELECT * FROM t_course ")
@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")
})
ListselectAll();
@Select("SELECT * FROM t_course WHERE course_id = #{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")
})
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);
}
package com.springboot.mybatis.service;RandomUtil类
import com.springboot.mybatis.entity.Course;
import java.util.List;
public interface CourseService {
ListselectAll();
Course getOne(Long courseId);
void delete(long courseId);
Course insert(Course course);
void update(Course course);
}
package com.springboot.mybatis.util;SpringBootMybatisApplication类
import java.util.Random;
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();
}
}
package com.springboot.mybatis;CourseServiceImplTest类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.springboot.mybatis.mapper")
public class SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApplication.class, args);
}
}
package com.springboot.mybatis.service;CourseController类
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;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CourseServiceImplTest {
@Resource
private CourseService courseService;
@Test
public void selectAll() {
ListcourseList = 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(3L);
}
@Test
public void insert(){
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(){
Course course=courseService.getOne(7L);
course.setCover("99.jpg");
course.setFinished((short)1);
courseService.update(course);
}
}
package com.springboot.mybatis.controller;在postman的运行结果
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 = "https://www.it610.com/api")
public class CourseController {
@Resource
private CourseService courseService;
@RequestMapping(value = "https://www.it610.com/courses", method = RequestMethod.GET)
public ListselectAll() { 改*/
return courseService.selectAll();
}
@RequestMapping(value = "https://www.it610.com/course/{id}", method = RequestMethod.GET)
public Course getOne(@PathVariable("id") long id) {
return courseService.getOne(id);
}
@RequestMapping(value = "https://www.it610.com/course/{id}",method = RequestMethod.DELETE)
public void deleteCourse(@PathVariable("id") long id){
courseService.delete(id);
}
@RequestMapping(value = "https://www.it610.com/course",method = RequestMethod.POST)
public Course addCourse(@RequestBody Course course){
course.setCourseCode(RandomUtil.getRandomCode());
return courseService.insert(course);
}
/
@RequestMapping(value = "https://www.it610.com/course", method = RequestMethod.PUT)
public void updateCourse(@RequestBody Course course) {
courseService.update(course);
}
}
文章图片
1.png
文章图片
2.png
文章图片
3.png
文章图片
4.png
文章图片
5.png
推荐阅读
- Activiti(一)SpringBoot2集成Activiti6
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- 2018-07-09|2018-07-09 Spring 的DBCP,c3p0
- spring|spring boot项目启动websocket
- Spring|Spring Boot 整合 Activiti6.0.0
- Spring集成|Spring集成 Mina
- springboot使用redis缓存
- Spring|Spring 框架之 AOP 原理剖析已经出炉!!!预定的童鞋可以识别下发二维码去看了
- Spring|Spring Boot之ImportSelector