springboot学习7 —— 整合 MyBatis

从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>

3.1 创建数据表

use mbtest;
create table student(
	id int primary key auto_increment,/* auto_increment:主键自动增长 */
	name varchar(11),
    score double,
    birthday date
);

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

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_1403034144

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值