MyBatis-Plus框架
操作数据存储的层
与什么数据库无关
与什么技术无关
O(Object)R(Relationship)M(Mapping)对象关系映射
MyBatis框架就是一款持久层的ORM框架
MyBatis的底层是JDBC,基于反射技术在运行时调用JDBC,实现数据库编程
MyBatis-Plus苞米豆 baomidou官方文档
MyBatis-Plus框架能够与Spring Boot框架无缝整合
1.搭建环境
(1)安装哪些依赖(三方库)
(2) 需要哪些配置
(3)框架提供了哪些API,怎么用
2.如何应用
3.研究它的底层怎么实现
注意的是 区别于昨天 数据库给个版本
把cj删掉
dao包:用原生的JDBC写,包名就叫dao
mapper:用MyBatis-Plus写,包名建议mapper,模块子包,小于等于启动类,持久层
在mapper子包中定义接口
在xml文件中编写SQL语句
xml文件写在resources/mapper目录中
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
在idea的插件市场搜索安装MyBatisX插件
检查接口中的抽象方法在绑定的xml文件中是否有对应的标签绑定
在启动类上使用@MapperScan("mapper包的全路径")注解
如何拿到Mapper接口的实现类?
拿不到(因为没有物理文件,存在于JVM内存中)
如何拿到框架创建的Mapper接口的实现类的对象?
框架说:实现类你拿不到,对象我帮你创建好放在内存中,你直接拿对象
pubilc class UserMapperImpl implements UserMapper{
@Override
pubilc int insert(User user) throws Exception{
根据接口绑定关系寻址xml文件
根据方法的绑定关系寻址绑定的SQL语句
获取连接
预编译SQL
填充参数
执行SQL
}
}
UserMapper userMapper=new UserMapperImpl();
开启SQL日志打印功能,方便查看SQL语句
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
1.在绑定的xml文件中写
2.不需要xml文件,使用注解写
@insert
@update
@delete
@select
动态SQL语句还是在xml文件中写比较方便
3.不绑定xml文件,继承官方的BaseMapper接口
src/main/web
META-INF/resources
**/*.*
src/main/resources
**/*.xml
**/*.properties
public interface BaseMapper
extends Mapper { //未写的字段默认为空
int insert(T entity);//根据id删除一条数据 Serializable是接口,序列化
int deleteById(Serializable id);
//根据任意条件删除
//HashMap
map=new HashMap(); //map.put(“mobile”,“13823232324”);
int deleteByMap(@Param("cm") Map
columnMap); //先跳过
int delete(@Param("ew") Wrapper
queryWrapper); //根据ID批量删除 , 传ArrayList给deleteBatchIds方法
//List
idList =new ArrauList(); //idList.add(10);
//idList.add(11);
//idList.add(12);
//底层条件 where id in(10,11,12)
int deleteBatchIds(@Param("coll") Collection extends Serializable> idList);
//只有null才不会出现在SQL里面
int updateById(@Param("et") T entity);
//先略过
int update(@Param("et") T entity, @Param("ew") Wrapper
updateWrapper); //查询一条完整的数据 返回值类型T 根据所给类型自动转换
T selectById(Serializable id);
//根据id批量查询,传ArrayList给deleteBatchIds方法
//List
idList =new ArrauList(); //idList.add(10);
//idList.add(11);
//idList.add(12);
//底层条件 where id in(10,11,12)
List
selectBatchIds(@Param("coll") Collection extends Serializable> idList); //根据HashMap查询,遍历HashMap作为条件
List
selectByMap(@Param("cm") Map columnMap); //
T selectOne(@Param("ew") Wrapper
queryWrapper); //
Integer selectCount(@Param("ew") Wrapper
queryWrapper); //
List
selectList(@Param("ew") Wrapper queryWrapper); //
List
//
List
//
> E selectPage(E page, @Param("ew") Wrapper queryWrapper); //
>> E selectMapsPage(E page, @Param("ew") Wrapper queryWrapper);
}
1.QueryWrapper
2.UpdateWrapper
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;
}