持久层
操作数据存储的层
与什么数据库无关
与什么技术无关
ORM
O(Object)R (Relationship) M(Mapping)对象关系映射
MyBatis框架就是一款持久层的ORM框架
MyBatis 与 JDBC 的关系
MyBatis(由苞米豆公司开源)
MyBatis 底层是JDBC,基于反射技术在运行时调用JDBC,实现数据库编程
mybatis-Plus与SpringBoot的关系
Mybatis-Plus框架能够与SpringBoot框架无缝整合
学习一门框架的步骤
1.搭建环境
(1)安装那些依赖(三方库)
(2)需要哪些配置
(3)框架提供了哪些API,怎么用
2.如何应用
3.研究它底层
配置环境:
整合JSP
1.需要配置web目录(或者叫webapp),放在src/main/web里面
2.安装jsp依赖(放在dependencies里面,千万不能放在dependencyManagement里面的dependencies)
再装一个依赖,就不要构造getter和setter方法,也不要写toString(), equals(), hashCode()
但是得要写一个注解 @Date
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
3.配置maven打包web目录(写在pom.xml中的build标签中,把plugins折叠起来写在后面)
<resources>
<resource>
<directory>src/main/web</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
4.配置jsp文件的前缀和后缀(写在resources/application.properties配置文件中)
代码写在工程里的哪个包里面
dao包:用原生的JDBC写,包名就叫Dao包
mapper包:用MyBatis-plus框架写,包名建议叫mapper
创建持久层的子包
在mapper子包中定义接口
在xml文件中编写SQL语句
xml文件写在resourse/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插件。在setting中
可以实现接口和xml文件的快速访问
检查接口中的抽象方法在绑定xml文件中是否有对应标签绑定
在启动类中使用@MapperScan注解
如何拿到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.不绑定XML文件,继承官方的BaseMapper接口
框架帮你写SQL语句,框架怎么知道你的表名,字段名的?
框架说:用注解把表名,字段名给我描述清楚
注解的sql语句
public interface BaseMapper<T> extends Mapper<T> {
1.int insert(T entity);
null的字段不会出现在SQL语句,空字符串会出现在SQL中
2.int deleteById(Serializable id);
根据id物理删除一条数据
3. int deleteByMap(@Param("cm") Map<String, Object> columnMap);
根据任意条件删除一条数据
传HashMap给deleteByMap方法
map.put("mobile","139032494");
底层条件是where mobile="13245657"
4. int delete(@Param("ew") Wrapper<T> queryWrapper);
5.int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
根据id批量删除(传一个ArrayList给deleteBatchIds方法)
例如:List<Integer>idlist=new ArrayList
idlist.add(10);
idlist.add(11);
idlist.add(12);
6.int updateById(@Param("et") T entity);
根据id更新一条数据,为null的字段不会出现在SQL中,空字符串会出现在SQL
7. int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
8. T selectById(Serializable id);
根据ID查询一条完整的数据
9. List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
根据ID批量查询多条数据,传一个ArrayList
10. List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
根据HashMap查询,把HashMap遍历出来作为条件
11.T selectOne(@Param("ew") Wrapper<T> queryWrapper);
Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
List<T>返回值类型, selectList方法名,Wrapper<T>参数类型,queryWrapper参数名(指针)
List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
<E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);
E(泛型) 返回值类型 <E extends IPage<T>>类型限定符(E必须是继承IPage<T>)E是指针Page指针的类型,page参数名
<E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
}
Wapper 子句构造器父类
1.QueryWapper<T> 查询where条件子句的构造器
2.UpdateWapper<T> 作为set子句的构造器
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;
}
框架整合
框架整合与优化
我自己文件中的框架整合