Spring Boot 整合 MyBatis

举报
周棋洛 发表于 2022/05/25 23:24:04 2022/05/25
【摘要】 文件目录 1. 依赖导入 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.or...

文件目录
在这里插入图片描述

1. 依赖导入

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <artifactId>springboot_day3</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

2. 创建数据表

CREATE TABLE USER(
  id INT PRIMARY KEY AUTO_INCREMENT,
  NAME VARCHAR (30) DEFAULT '该用户什么都没写',
  phone VARCHAR (20) UNIQUE,
  pwd INT NOT NULL
) ;

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3. 根据表结构创建实体类

@Data
public class User {
    private Long id;
    private String name;
    private String phone;
    private String pwd;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4. 创建 UserDAO 接口

package com.liu.dao;

import com.liu.entity.User;

import java.util.List;

public interface UserDAO {
    List<User> findAll();
    User findById(Long id);
    void save(User user);
    void update(User user);
    void deleteById(Long id);
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

5. 在 resources/mapping 下新建 UserDAO接口对应的 UserDAO.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.liu.dao.UserDAO">

    <select resultType="com.liu.entity.User" id="findAll">
        select * from user;
    </select>

    <select id="findById" parameterType="java.lang.Long" resultType="com.liu.entity.User">
        select *
        from user where id = #{id};
    </select>
    <insert id="save" parameterType="com.liu.entity.User">
        insert into user (name, phone, pwd)
        values (#{name},#{phone},#{pwd});
    </insert>

    <update id="update" parameterType="java.lang.Long">
        update user
        set name = #{name},phone=#{phone},pwd=#{pwd}
        where id = #{id};
    </update>
    <delete id="deleteById" parameterType="java.lang.Long">
        delete
        from user
        where id = #{id};
    </delete>
</mapper>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

6. 控制层

@RestController
@RequestMapping("user")
public class UserController {
    
    @Autowired
    private UserDAO userDAO;

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

    @GetMapping("/findById/{id}")
    public User findById(@PathVariable("id") long id) {
        return userDAO.findById(id);
    }

    /**
     * @param user
     * @RequestBody 这个注解把 json 格式的请求封装成 java 对象
     * @ResponseBody 这个注解把 java 对象转换成 json 格式返回给 client
     */
    @PostMapping("/save")
    public void save(@RequestBody User user) {
        userDAO.save(user);
    }

    @PostMapping("/update")
    public void update(@RequestBody User user) {
        userDAO.update(user);
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") long id) {
        userDAO.deleteById(id);
    }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

7. application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db08?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 8485
    driver-class-name: com.mysql.cj.jdbc.Driver

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

8. 创建启动类

注意位置,要覆盖项目所有包

package com.liu;

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

@SpringBootApplication
@MapperScan("com.liu.dao")
public class SpringbootDay3Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDay3Application.class, args);
    }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

启动项目,使用 postman 进行接口测试
在这里插入图片描述

文章来源: blog.csdn.net,作者:周棋洛ყ ᥱ ᥉,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/m0_53321320/article/details/124055413

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。