3. SpringBoot和Mybatis的集成练习
2019-03-24 本文已影响0人
叶小慈呀
关于课程表的增删改查操作
1.项目结构图
012.详细代码
- pom配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>spring-boot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-mybatis</name>
<description>SpringBoot Mybatis project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 连接数据库application.propertis
这里用的数据库是MySQL
## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/db_spring?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=111aaa..
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#指定实体类映射的包
mybatis.type-aliases-package=com.springboot.mybatis.entity
- Course类
package com.springboot.mybatis.entity;
public class Course {
private Long courseId;
private String courseName;
private Long userId;
private String courseClass;
private String cover;
private String courseCode;
private Short finished;
- 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(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 ")
List<Course> selectAll();
@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 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"),
})
@Delete("DELETE FROM t_course WHERE course_id = #{courseId} ")
void delete(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"),
})
@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类
package com.springboot.mybatis.service;
import com.springboot.mybatis.entity.Course;
import com.springboot.mybatis.entity.CourseVO;
import java.util.List;
public interface CourseService {
List<Course> selectAll();
Course getOne(long courseId);
void delete(long courseId);
void insert(Course course);
void update(Course course);
}
- CourServiceImpl类
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 void insert(Course course) {
courseMapper.insert(course);
}
@Override
public void update(Course course) {
courseMapper.update(course);
}
}
- 测试类
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;
import static org.junit.Assert.*;
@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(3L);
}
@Test
public void insert() {
Course course = new Course();
course.setCourseName("微信小程序开发");
course.setCourseClass("软件1721");
course.setUserId(6L);
course.setCover("1.jpg");
course.setCourseCode(RandomUtil.getRandomCode());
course.setFinished((short) 0);
courseService.insert(course);
}
@Test
public void update() {
Course course = courseService.getOne(2L);
course.setCover("9.jpg");
course.setFinished((short) 1);
courseService.update(course);
}
}
- 测试类运行成功后在controller运行
- CourseController类
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 = "/courses/{id}", method = RequestMethod.GET)
public Course getOne(@PathVariable("id") long id) {
return courseService.getOne(id);
}
@RequestMapping(value = "/course", method = RequestMethod.POST)
public void addCourse(@RequestBody Course course) {
course.setCourseCode(RandomUtil.getRandomCode());
courseService.insert(course);
}
/**
* 根据id删除班课
*
* @param id
*/
@RequestMapping(value = "/course/{id}", method = RequestMethod.DELETE)
public void deleteCourse(@PathVariable("id") long id) {
courseService.delete(id);
}
/**
* 更新班课
*
* @param course
*/
@RequestMapping(value = "/course", method = RequestMethod.PUT)
public void updateCourse(@RequestBody Course course) {
courseService.update(course);
}
}
关于参数
- 路径参数
@RequestMapping(value = "/courses/{id}",method = RequestMethod.GET)
public Course getOne(@PathVariable("id") long id){
return courseService.getOne(id);
}
- json对象传参
@RequestMapping(value = "/course",method = RequestMethod.POST)
public void addCourse(@RequestBody Course course){
CourseService.insert(course);
}
02