MyBatis-Plus框架
持久层
操作数据存储的层
与什么数据库无关
与什么技术无关
ORM
O(Object)R(Relationship)M(Mapping)对象关系映射
MyBatis框架就是一款持久层的ORM框架
MyBatis与JDBC的关系
MyBatis的底层是JDBC,基于反射技术在运行时调用JDBC,实现数据库编程
MyBatis-Plus苞米豆baomidou官方文档
MyBatis-Plus与SpringBoot关系
MyBatis-plus框架能够与SpringBoot框架无缝整合
学习一门框架
1.搭建环境
(1)安装哪些依赖(三方库)
(2)需要哪些配置
(3)框架提供了哪些API ,怎么用
2.如何使用
3.研究它的底层
MyBatis-Plus基础配置参数
数据库用5.1.47
把cj去了
代码写在工程的哪个包里面
dao包:用原生的JDBC写,包名就叫dao
mapper包:用MyBatis-plus框架写,包名建议叫mapper
创建持久层的子包
在mapper子包中定义接口
在xml文件中编写SQL语句
xml文件写在resources/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>
</mapper>
设置xml文件
在Idea的插件市场中搜索并安装MyBatisX插件
实现接口与xml绑定后快速访问
检查接口中的抽象方法在绑定xml文件中是否有对应标签绑定
在启动类中使用@MapperScan("mapper包的全路径")注解
如何拿到Mapper接口的实现类?
拿不到(因为没有物理文件,它存在于JVM内存中)
如何拿到框架创建的Mapper接口的实现类对象?
框架说:实现类你拿不到的,对象我帮你创建好放在内存中,你直接拿对象
开启SQL日志打印功能(方便在测试的时候排查错误)
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
MyBatis写SQL语句的几种方式
1.在绑定XML文件中写
<insert></insert>
<update></update>
<delete></delete>
<select></select>
2.不需要XML文件使用注解写
@Insert
@Update
@Delete
@Select
3.使用依赖自带的方法写
Wrapper条件构造器
1.QueryWrapper查询条件子句的构造器
2.UpdateWrapper修改set子句的构造器
Wrapper子句构造器父类
子类
1.QueryWrapper<T>
2.UpdateWrapper<T>
安装lombok依赖,可以删除getter和setter方法,加@Data
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.12</version>
</dependency>
IPage分页器接口
1.Page类
不同数据库的分页的SQL语句不一样,你需要告诉框架你用的是什么数据库
MySQL:limit?,?
Oracle:基于rownum做子查询
在启动类中添加下面的方法,即可告诉MyBatis,我们使用的是MySQL数据库
@Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL); mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor); return mybatisPlusInterceptor;