从b站学习springcloud,现在进行总结,该总结除去了视频中出现的小错误,对有些易错的地方进行了提醒
b站链接:https://www.bilibili.com/video/av55993157
资料链接:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
提取码: 21ru
上一节链接:https://blog.csdn.net/qq_40893824/article/details/106853969
下一节链接:https://blog.csdn.net/qq_40893824/article/details/106867958
下面的内容总结:
1 新建工程 spring-mybatis:
直到 Finish
2 在 pom 文件中加入代码:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
![](https://img-blog.csdnimg.cn/20200619195857963.png)
3.1 创建数据表
use mbtest;
create table student(
id int primary key auto_increment,/* auto_increment:主键自动增长 */
name varchar(11),
score double,
birthday date
);
![](https://img-blog.csdnimg.cn/2020061921013454.png)
3.2 加入数据
4 创文件夹 com.southwind.entity,创实体类 Student,加入代码:
package com.southwind.entity;
import lombok.Data;
import java.util.Date;
@Data
public class Student {
private Long id;
private String name;
private double score;
private Date birthday;
}
![](https://img-blog.csdnimg.cn/2020061921141612.png)
5 接口:在 southwind 下创包 repository,repository 内新建接口 StudentRepository,加入代码:
package com.southwind.repository;
import com.southwind.entity.Student;
import java.util.List;
public interface StudentRepository {
public void save(Student student);
public void update(Student student);
public void deleteById(Long id);
public List<Student> findAll();
public Student findById(Long id);
}
![](https://img-blog.csdnimg.cn/20200619211816626.png)
6 在 resources 下创包 mapping,它放置 接口StudentRepository 对应的Mapper.xml,新建 StudentRepository.xml
里面的代码 变成:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.southwind.repository.StudentRepository">
<insert id="save" parameterType="Student" >
/* parameterType:输入参数 */
insert into student(name, score, birthday) value(#{name}, #{score}, #{birthday})
</insert>
<update id="update" parameterType="Student">
update student set name = #{name}, score = #{score}, birthday = #{birthday} where id = #{id}
</update>
<delete id="deleteById" parameterType="java.lang.Long">
delete from student where id = #{id}
</delete>
<select id="findAll" resultType="Student">/* resultType:返回值类型 */
select * from student
</select>
<select id="findById" parameterType="java.lang.Long" resultType="Student">
/* parameterType:输入参数 */
select * from student where id = #{id}
</select>
</mapper>
7 在 southwind 中创包 controller,期内创建实体类 StudentHandler,加入代码:
package com.southwind.controller;
import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@PostMapping("/save")
public void save(@RequestBody Student student){
studentRepository.save(student);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
studentRepository.update(student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id")long id){
studentRepository.deleteById(id);
}
@GetMapping("/findAll")
public List<Student> findAll(){
return studentRepository.findAll();
}
@ResponseBody
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id")long id){
return studentRepository.findById(id);
}
}
![](https://img-blog.csdnimg.cn/2020062008273255.png)
8 在 resources 中,新建 application.yml,加入代码:
spring:
datasource:
url: jdbc:mysql://localhost:3306/mbtest?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:/mapping/*.xml
type-aliases-package: com.southwind.entity
type-aliases-package 对应 mapping 文件中 parameterType、resultType的对应类名
它们不用再类名前写对应的路径了
type-aliases-package 实现这个功能
9 在 southwind 中创建 Application,加入代码:
package com.southwind;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.southwind.repository")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
10 检查
10.1 进入 http://localhost:8080/findAll
进入 postman
10.2 增加数据 post → http://localhost:8080/save
增加成功!
10.3 修改数据 put → http://localhost:8080/update
修改成功!
10.4 删除数据 delete → http://localhost:8080/deleteById/4
删除成功!
10.5 查数据 http://localhost:8080/findById/3
上一节链接:https://blog.csdn.net/qq_40893824/article/details/106853969
下一节链接:https://blog.csdn.net/qq_40893824/article/details/106867958