目录
建一个springboot-mybatisplus-01工程:
1.需要配置web目录(或者叫webapp),放在src/main/web里面
2.安装JSP依赖(放在dependencies里面,千万不能放在dependencyManagement里面的dependencies)
3.配置maven打包web目录(写在pom.xml中的build标签中,把pligins折叠起来写在后面)
在启动类中使用@MapperScan("mapper包的全路径")注解
MyBatis-Plus框架:
持久层:
操作数据存储的层,与什么数据库无关,与什么技术无关
ORM:
O(Object) R(Relationship) M(Mapping)对象关系映射
MyBatis框架就是一款持久层的ORM框架
MyBatis与JDBC的关系:
MyBatis的底层就是JDBC,基于反射技术在运行时调用JDBC
Mybatis-Plus与SpringBoot的关系:
Mybatis-Plus框架能够与SpringBoot无缝衔接
学习一门框架
- 搭建环境
- 安装哪些依赖 (三方库)
- 需要哪些配置
- 框架提供了哪些API
- 如何应用
- 研究它的底层
Mybatis-Plus苞米豆baomidou官方文档
建一个springboot-mybatisplus-01工程:
1.新建一个工程
2.整合JSP目录:
1.需要配置web目录(或者叫webapp),放在src/main/web里面
2.安装JSP依赖(放在dependencies里面,千万不能放在dependencyManagement里面的dependencies)
<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>
3.配置maven打包web目录(写在pom.xml中的build标签中,把pligins折叠起来写在后面)
<resources>
<resource>
<directory>src/main/web</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
4.Mybatis-Plus基础配置参数
spring.application.name=springboot-mybatisplus-01 server.port=8082 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.name=defaultDataSource spring.datasource.url=jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl spring.mvc.view.prefix=/jsp/ spring.mvc.view.suffix=.jsp
代码写在工的哪个包里:
dao包:用原生的JDBC写,包名就叫dao
mapper包:用mybatis-plus框架写,包名建议叫mapper
创建持久层的包
在mapper子包中定义接口:
在XML文件中编写SQL语句:
XML文件写在resource/mapper目录中
<?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>
在启动类中使用@MapperScan("mapper包的全路径")注解
如何拿到框架创建的Mapper接口的实现类?
拿不到(因为没有物理文件,它存在于JVM内存中)
如何拿到框架创建的Mapper接口的实现类的对象?
框架说:实现类你拿不到的,对象我帮你创建好放在内存中,你直接拿对象
用注解拿对象:
操作User表的持久层接口:
安装插件(可以实现接口/xml文件快速访问):
在idea的插件市场中搜索并安装MybatisX插件
检查接口中的抽象方法在绑定xml文件中是否有对应标签绑定
File-Setting-Plugins-MarketPlace
增:
开启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
动态SQL语句还是在xml文件中写比较方便
3.不绑定xml文件,继承官方的BaseMapper接口
@TableName(映射表名)
@Tabled(value="",type)
@TableFiled(映射字段名)
配置打包文件:
<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>