新建SpringBoot项目

勾选 Lombok,Web,Mybatis,Mysql 依赖

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>

创建数据库和数据表

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

entity

在 entity 文件夹中,写实体类

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

mapper

在 mapper 层中写 StudentMapper 接口

@mapper
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);

}

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>

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

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

在启动类中添加扫描mapper

//这里你接口中都有mapper注解的话,可以不写,两者功能相同。
@MapperScan("com.example.mapper")  //mapper包的路径

运行并测试


Last modification:November 29, 2021
如果觉得我的文章对你有用,请随意赞赏