6 Spring Boot 整合 MyBatis

在这里插入图片描述

  • 创建Maven工程,pom.xml 添加依赖
<!-- 基础父包 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <version>2.7.5</version>
    <artifactId>spring-boot-starter-parent</artifactId>
</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>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>
  • 创建数据库

在这里插入图片描述

  • 创建实体类
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;
}
  • 创建 StudentRepository.java
package com.southwind.repository;

import com.southwind.entity.Student;

import java.util.List;

public interface StudentRepository {
    public List<Student> findAll();
    public Student findById(Long id);
    public void save(Student student);
    public void update(Student student);
    public void deleteById(Long id);
}
  • 在 resources/mapping 路径下创建 StudentRepository 接口对应的 StudentMapper.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">
    <select id="findAll" resultType="Student">
        select * from student
    </select>

    <select id="findById" parameterType="java.lang.Long" resultType="Student">
        select * from student where id = #{id}
    </select>

    <insert id="save" parameterType="Student">
        insert into Student(name,score,birthday) values(#{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>
</mapper>
  • 创建 StudentHandler,注入 StudentRepository
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;

    @GetMapping("/findAll")
    public List<Student> findAll(){
        return studentRepository.findAll();
    }

    @GetMapping("/findById/{id}")
    public Student findById(@PathVariable("id") Long id){
        return studentRepository.findById(id);
    }

    @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);
    }
}
  • application.yml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/javaee2?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 111111
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  mapper-locations: classpath:/mapping/*.xml
  type-aliases-package: com.southwind.entity
  • 创建启动类 application.java
package com.southwind;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
// 因为 repository 现由 mybatis 管理,所以需要扫描进来
@MapperScan("com.southwind.repository")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值