MENU

SpringBoot整合Mybatis增删改查

• February 13, 2020 • Mybatis,Spring Boot

1.使用IDEA新建Spring Boot项目

勾选Lombok,Web,Mybatis,Mysql依赖.(这一步也可以先不勾选,到项目创建完成后前往pom.xml中添加依赖.参考步骤二

2.没有勾选的话就在pom.xml中加入依赖.

    <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.1.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

3.创建数据库和数据表

CREATE TABLE student(
id INT PRIMARY KEY auto_increment,
name VARCHAR(10),
sex int,
birthday date
);

INSERT INTO student(name,sex,birthday) values
 ("小明",1,"2020-1-1 00:00:00"),
 ("小红",0,"2020-2-8 00:00:00"),
 ("小白",0,"2020-3-4 00:00:00");

4.entity层

在entity中,写实体类

@Data
public class Student {
    private int id;
    private String name;
    private int sex;
    private Date birthday;
}

5.mapper层

在mapper层中写StudentMapper接口

public interface StudentMapper {
    /**
     * 增加一个新学生
     * @param student
     */
    void save(Student student);

    /**
     * 通过id删除一个学生
     * @param id
     */
    void delete(int id);

    /**
     * 根据id修改学生
     * @param student
     */
    void update(Student student);

    /**
     * 查询所有学生
     * @return 学生
     */
    List<Student> findAll();

    /**
     * 通过id查询学生
     * @param id
     * @return 学生
     */
    Student findById(int id);

}

6.Mapper.xml

在resources/mapping中创建StudentMapper接口的Mapper.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.example.mapper.StudentMapper">
    <select id="findAll" resultType="student">
        select * from student
    </select>
    <select id="findById" resultType="student" >
        select * from student where id = #{id}
    </select>
    <insert id="save" parameterType="student">
        insert into student(name,sex,birthday) values (#{name},#{sex},#{birthday})
    </insert>
    <update id="update" parameterType="student">
        update student set name=#{name},sex=#{sex},birthday=#{birthday} where id = #{id}
    </update>
    <delete id="delete">
        delete from student where id = #{id}
    </delete>

</mapper>

7.controller层

在controller层新建StudentHandler类

注意: 这里注入时bean会提示不能注入,可以在StudentMapper中添加@Mapper注解,或者在启动类中添加扫描包

eg: @MapperScan("com.example.mapper")

@RestController
public class StudentHandler {

    @Autowired
    private StudentMapper studentMapper;

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

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

    @PutMapping("/save")
    public void save(@RequestBody Student student){
        studentMapper.save(student);
    }

    @PutMapping("/update")
    public void update(@RequestBody Student student){
        studentMapper.update(student);
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") int id){
        studentMapper.delete(id);
    }
}

8.application.yml

在resources下新建application.yml

填写数据库信息

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
    username: root
    password: 123456

mybatis:
  type-aliases-package: com.example.entity
  mapper-locations: classpath:/mapping/*.xml

9.在启动类中添加扫描mapper

@MapperScan("com.example.mapper")  //mapper包的路径

10.运行并测试

这里我使用的是ApiPost, 百度就可以下载得到。

Last Modified: February 19, 2020
Archives QR Code Tip
QR Code for this page
Tipping QR Code