0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

项目:08-SpringBoot3集成Mybatis

Last updated at Posted at 2025-01-09

一:SpringBoot配置Mybatis

⭐application.yml
设置 MyBatis 的映射文件路径、日志输出方式以及如何处理数据库字段和 Java 实体属性之间的命名规则。

# 配置mybatis实体和xml映射
mybatis:
  # 映射XML
  mapper-locations: classpath:mapper/*.xml
  configuration:
    # 配置日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true

image.png
⭐java表示属性一般使用驼峰的形式:deptId
数据库里面表示字段一般使用下划线的形式:dept_id

二:基本的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.EmployeeMapper">
</mapper>

image.png

1.xml是跟mapper接口一一对应的

image.png

2.告诉springboot如何扫描mapper包

image.png

3.查询员工表所有信息

image.png
image.png

4.根据id查询员工信息

image.png
image.png

5.分页查询

⭐①引入分页查询插件

<!-- 分页插件PageHelper -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.4.6</version>
    <exclusions>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </exclusion>
    </exclusions>
</dependency>

⭐②代码
image.png
image.png

6.接口传参方式

⭐①RequestParam注解:
image.png
⭐②PathVariable注解:
image.png
⭐③对象参数:通过对象可以接受多个url参数
image.png
image.png

三:使用Mybatis实现数据库的增删改查

1.get:查询操作

2.post:新增操作

通过RequestBody注解,可以把前端传来的json字符串映射成java的对象或者数组
⭐Mybatis里面写sql使用下划线,涉及到绑定java对象值就写驼峰
image.png
image.png

3.put:修改操作

image.png
image.png

4.delete:删除操作

image.png
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?