一: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
⭐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>
1.xml是跟mapper接口一一对应的
2.告诉springboot如何扫描mapper包
3.查询员工表所有信息
4.根据id查询员工信息
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>
6.接口传参方式
⭐①RequestParam注解:
⭐②PathVariable注解:
⭐③对象参数:通过对象可以接受多个url参数
三:使用Mybatis实现数据库的增删改查
1.get:查询操作
2.post:新增操作
通过RequestBody注解,可以把前端传来的json字符串映射成java的对象或者数组
⭐Mybatis里面写sql使用下划线,涉及到绑定java对象值就写驼峰