🎈环境:
- JDK1.8
- Mysql8
- Maven
- IDEA
回顾:
- JDBC
- Mysql
- Java
- Junit
SSM框架:配置文件最好的方式:看官网文档
🎉简介
🚲什么是Mybatis
- MyBatis 是一款优秀的持久层框架 (DAO)
- 它支持自定义 SQL、存储过程以及高级映射。
- MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
- MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
🚌 如何获得Mybatis
-
maven仓库:
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> -
mybatis/mybatis-3: MyBatis SQL mapper framework for Java (github.com)
🔆 持久化
数据持久化:
- 持久化就是将程序的数据在持久状态和瞬时状态转化的过程
- 内存:断电即失
- 数据库(JDBC),IO文件持久化。
为什么要持久化?
- 有一些对象不能丢掉
- 内存太贵
⚓ 持久层
Dao层,Service层,Controller层……
- 完成持久化工作的代码块
- 层界限十分明显。
🎸为什么需要Mybatis
- 方便
- 传统JDBC代码较为复杂。
- 帮助程序员帮数据存入到数据库中
- Mybatis 非必须。更容易上手罢了。
- 优点:
- 简单易学
- 灵活
- sql和代码的分离,提高了可维护性。
- 提供映射标签,支持对象与数据库的orm字段关系映射
- 提供对象关系映射标签,支持对象关系组建维护
- 提供xml标签,支持编写动态sql。
TheMostImportant: 用的人多!!
Mybatis详细执行流程

🚀第一个Mybatis程序
{% note red 'fas fa-fan' modern %} 思路:搭建环境–>导入Mybatis–> 编写代码–> 测试!{% endnote %}
🚑搭建环境:
-
搭建数据库
-
新建Maven项目–> 导入依赖 Mybatis mysql junit
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 父工程--> <groupId>org.mecca</groupId> <artifactId>Mybatis-study</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <!-- 导入依赖--> <dependencies> <!-- mysql驱动--> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> <!-- mybatis--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <!-- Junit--> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> </project>
🚛 创建模块
-
编写mybatis的核心配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--核心配置文件--> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> </configuration> -
编写mybatis的工具类
package org.mecca.utils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; //SqlSessionFactory --> SqlSession public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; { try { //使用Mybatis第一步 获取 SqlSessionFactory 对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
🛴 编写代码
-
实体类
package org.mecca.pojo; public class User { private int id; private String name; private String pwd; public User(){ } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } } -
Dao接口
public interface UserDao { List<User> getUserList(); } -
接口实现类转换为一个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"> <!--绑定一个对应的DAO接口--> <mapper namespace="org.mecca.dao.UserDao"> <!-- 查询语句--> <!-- id 对应方法名--> <select id="getUserList" resultType="org.mecca.pojo.User"> select * from mybatis.user </select> </mapper>
🚖 测试
🚧CRUD
🔆namespace
namespace 中的包名要和 Dao/mapper 中的接口的包名一致!
<mapper namespace="org.mecca.dao.UserMapper">
🐱🏍select
查询语句
- id就是对应namespace中的方法名:
- resultType:SQL 语句执行的返回值 !
- parameterType 参数类型
<select id="getUserById" resultType="org.mecca.pojo.User" parameterType="int">
select * from mybatis.user where id = #{id}
</select>
🚌insert
<insert id="addUser" parameterType="org.mecca.pojo.User">
insert into mybatis.user (id, name, pwd) VALUES (#{id},#{name},#{pwd})
</insert>
🚠增删改需要提交事务
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.addUser(new User(5, "哈哈哈", "123456"));
if (res>0){
System.out.println("Succress!!");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
update
delete
以上步骤全部都是一样的:
{% note blue 'fas fa-bullhorn' modern %} 增删改需要提交事务 {% endnote %}
-
编写接口
public interface UserMapper { //查询全部用户 List<User> getUserList(); //根据ID查询用户 User getUserById(int id); //Insert一个用户 // insert into tables () values () int addUser(User user); //修改用户 int updateUser(User user); //删除用户 int deleteUser(int id); } -
编写对应mapper中的sql语句
<?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="org.mecca.dao.UserMapper"> <select id="getUserList" resultType="org.mecca.pojo.User"> select * from mybatis.user </select> <select id="getUserById" resultType="org.mecca.pojo.User" parameterType="int"> select * from mybatis.user where id = #{id} </select> <insert id="addUser" parameterType="org.mecca.pojo.User"> insert into mybatis.user (id, name, pwd) VALUES (#{id},#{name},#{pwd}) </insert> <update id="updateUser" parameterType="org.mecca.pojo.User"> update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id} </update> <delete id="deleteUser" parameterType="org.mecca.pojo.User" > delete from mybatis.user where id=#{id} </delete> </mapper> -
测试
package org.mecca.dao; import org.apache.ibatis.session.SqlSession; import org.mecca.pojo.User; import org.mecca.utils.MybatisUtils; import org.junit.Test; import java.util.List; public class UserDaoTest { @Test public void test(){ //获取SqlSession对象 SqlSession sqlSession = MybatisUtils.getSqlSession(); //方式一:getmapper UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUserList(); for (User user : userList) { System.out.println(user); } sqlSession.close(); } @Test public void getUserById(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserById(1); System.out.println(user); sqlSession.close(); } //增删改需要提交事务 @Test public void addUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int res = mapper.addUser(new User(5, "哈哈哈", "123456")); if (res>0){ System.out.println("Succress!!"); } //提交事务 sqlSession.commit(); sqlSession.close(); } @Test public void updateUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.updateUser(new User(4,"嘻嘻嘻","123123")); sqlSession.commit(); sqlSession.close(); } @Test public void deleteUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.deleteUser(4); sqlSession.commit(); sqlSession.close(); } }
🚖万能MAP
{% note red 'fas fa-fan' modern %}
假设,我们的实体类,或者数据库中的表,字段或参数过多,我们应当考虑使用Map!
{% endnote %}
插入用户
<insert id="addUser2" parameterType="map">
insert into mybatis.user (id, name, pwd) VALUES (#{userId},#{userName},#{pwd})
</insert>
int addUser2(Map<String,Object> map);
@Test
public void addUser2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Object> map = new HashMap<>();
map.put("userId",5);
map.put("userName","HelloMap");
map.put("pwd","112233");
mapper.addUser2(map);
sqlSession.commit();
sqlSession.close();
}
🎸Tips:
- Map传递参数,直接再Sql中取出Key即可
- 对象传递参数,直接在sql中取对象的属性即可
- 只有一个基本类型参数的情况下,可以直接在sql中取
🚌模糊查询
- Java代码执行的时候,传递通配符
% % - 在sql拼接中使用通配符
% % - 使用# 防止sql注入
{% note red 'fas fa-fan' modern %}
注意SQL注入!!!!
{% endnote %}
🚛错误分析:
- Mapper中中文报错
- 在XML配置文件中把最上面的UTF-8改为UTF8(不完全对)
- Mapper中 :
namespace绑定使用的是.而不是/ mybatis-config使用的<mapper resource="UserMapper.xml"/>中路径中使用/而不是.- idea报错 从下往上读
- 程序配置文件必须符合规范
- 空指针异常原因: 没有注册到资源
- Maven资源没有导出问题—-在
pom.xml中的build标签 添加约束 因为maven约定大于配置
🛴配置解析
核心配置文件
mybatis-config.xml- MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息。 配置文档的顶层结构如下:
- properties(属性)
- settings(设置)
- typeAliases(类型别名)
- typeHandlers(类型处理器)
- objectFactory(对象工厂)
- plugins(插件)
- environments(环境配置)
- environment(环境变量)
- transactionManager(事务管理器)
- dataSource(数据源)
- environment(环境变量)
- databaseIdProvider(数据库厂商标识)
- mappers(映射器)
环境配置:
MyBatis 可以配置成适应多种环境,不过要记住:尽管可以配置多个环境,但每个 SqlSessionFactory 实例只能选择一种环境。
学会使用配置多套运行环境
Mybatis默认的事务管理器是JDBC,连接池:POOLED
属性(properties)
我们可以使用属性(properties) 来实现引用配置文件
这些属性可以在外部进行配置,并可以进行动态替换。你既可以在典型的 Java 属性文件中配置这些属性,也可以在 properties 元素的子元素中设置 [db.properties]
-
编写一个配置文件
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true username=root password=root -
在核心配置文件中引入 必须放在最前
<properties resource="db.properties"> -
可以直接引入外部文件
-
可以在其中增加一些属性配置
-
如果连个文件有同一个字段,优先使用外部文件
typeAliases 类型别名
- 类型别名可为 Java 类型设置一个缩写名字。
- 意在降低冗余的全限定类名书写
类型别名可为 Java 类型设置一个缩写名字
<typeAlias type="org.mecca.pojo.User" alias="User" />
也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean
<package name="org.mecca.pojo"/>
{%note red 'fas fa-fan' %} 实体类少的时候使用第一种方式,如果实体类十分多,建议使用第二种 {%endnote%}
设置(settings)
| 设置名 | 描述 | 有效值 | 默认值 |
|---|---|---|---|
| cacheEnabled | 全局性地开启或关闭所有映射器配置文件中已配置的任何缓存。 | true | false | true |
| lazyLoadingEnabled | 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。 特定关联关系中可通过设置 fetchType 属性来覆盖该项的开关状态。 | true | false | false |
| defaultResultSetType | 指定语句默认的滚动策略。(新增于 3.5.2) | FORWARD_ONLY | SCROLL_SENSITIVE | SCROLL_INSENSITIVE | DEFAULT(等同于未设置) | 未设置 (null) |
其他设置
映射器(mappers)
MapperRegistry: 注册绑定Mapper文件
-
方式一:
<mappers> <mapper resource="UserMapper.xml"/> </mappers> -
方式二:
<!-- 使用映射器接口实现类的完全限定类名 --> <mappers> <mapper class="org.mybatis.builder.AuthorMapper"/> <mapper class="org.mybatis.builder.BlogMapper"/> <mapper class="org.mybatis.builder.PostMapper"/> </mappers>注意:
- 接口和他的Mapper配置文件必须同名!
- 接口和他的Mapper配置文件必须在同一个包下!
-
方式三:
<mappers> <package name="org.mybatis.builder"/> </mappers>注意:
- 接口和他的Mapper配置文件必须同名!
- 接口和他的Mapper配置文件必须在同一个包下!
作用域(Scope)和生命周期
{% note red 'fas fa-fan' modern %}不同作用域和生命周期类别是至关重要的,因为错误的使用会导致非常严重的并发问题。{% endnote %}
SqlSessionFactoryBuilder
- 一旦创建了
SqlSessionFactory,就不再需要它了 - 局部变量
SqlSessionFactory
- 说白了就是可以想象为: 数据库连接池
- 一旦被创建就应该在应用的运行期间一直存在,没有任何理由丢弃它或重新创建另一个实例。
- 多次重建
SqlSessionFactory被视为一种代码“坏习惯” - 因此
SqlSessionFactory的最佳作用域是应用作用域。 有很多方法可以做到,最简单的就是使用单例模式或者静态单例模式。
SqlSession
- 连接到连接池的一个请求
- 请求结束需要关闭
close SqlSession的实例不是线程安全的,因此是不能被共享的,所以它的最佳的作用域是请求或方法作用域- 用完关闭 否则资源被占用
- 每一个Mapper相当于就代表一个具体的业务
🚖ResultMap
解决属性名和字段名不一致的问题
数据库字段名

Java:
private int id;
private String name;
private String password;
Test:
@Test
public void getUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
结果:

解决:
-
方法一:起别名
<select id="getUserById" resultType="user" parameterType="int"> select id,name,pwd as password from mybatis.user where id = #{id} </select> -
方法二:
ResultMap
结果集映射
id name pwd
id name password
{% note red 'fas fa-fan' modern %} column:数据库中的字段名 property: 实体类中的属性名{% endnote %}
<!--结果集映射
column:数据库中的字段名
property: 实体类中的属性名
-->
<resultMap id="UserMap" type="user">
<result column="pwd" property="password"/>
</resultMap>
<select id="getUserById" resultMap="UserMap" parameterType="int">
select * from mybatis.user where id = #{id}
</select>

resultMap元素是 MyBatis 中最重要最强大的元素。- 实际上,在为一些比如连接的复杂语句编写映射代码的时候,一份
resultMap能够代替实现同等功能的数千行代码。ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。 ResultMap的优秀之处——你完全可以不用显式地配置它们
{% note blue 'fas fa-fan' modern %}如果这个世界总是这么简单就好了。{% endnote %}
🚑日志
日志工厂
如果一个数据库操作出现了异常,我们需要排查错误,日志就是最好的助手!!
曾经:sout.debug
现在:日志工厂
| logImpl | 指定 MyBatis 所用日志的具体实现,未指定时将自动查找。 | SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING |
|---|
- SLF4J
- LOG4J [*]
- LOG4J2
- JDK_LOGGING
- COMMONS_LOGGING
- STDOUT_LOGGING [*]
- NO_LOGGING
在Mybatis中具体使用哪一个日志实现,在设置中设定
STDOUT_LOGGINGSTDOUT_LOGGING 标准日志输出
在mybatis配置文件中配置我们的日志!
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

LOG4J
Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。 ——–百度百科
什么是Log4j?
- Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件
- 我们也可以控制每一条日志的输出格式
- 通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程
- 这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码
1.导包
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.log4j.properties
{% note red 'fas fa-fan' modern %}
log4j:ERROR Category option " 1 " not a decimal integer.
log4j.properties 的配置文件中:
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
把{1}中的空格去掉。
{% endnote %}
{% note red 'fas fa-fan' modern %}
log4j:WARN custom level class not found
properties文件的#注释只能出现在行首,不能出现在后面,如果这么配置就会导致log4j找到正确的日志错误级别。
{% endnote %}
### set log levels ###
log4j.rootLogger = DEBUG,console,file
### 输出到控制台 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
### 输出到日志文件 ###
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File = logs/log.log
log4j.appender.file.Append = true
## 输出DEBUG级别以上的日志
log4j.appender.file.Threshold = DEBUG
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 日志输出级别 ###
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
3.配置log4j为日志的实现
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
4.log4j的使用

简单使用
1.在要使用log4j的类中导入包
import org.apache.log4j.Logger;
2.日志对象,加载参数为当前类的class
static Logger logger= Logger.getLogger(UserDaoTest.class);
3.日志级别
Logger.info("info: 进入了testlog4j");
Logger.debug("debug:进入了testlog4j");
Logger.error("error:进入了testlog4j");
🚠分页
思考:为什么要分页?
- 减少数据的处理量
使用Limit分页:
select * from user limit 0,2;
#每页显示两个从第0个开始查
select * from user limit 2;
#从0到n
使用Mybatis 实现分页,核心SQL:
1.接口
List<User> getUserByLimit(Map<String,Integer> map);
2.Mapper.xml
<select id="getUserByLimit" parameterType="map" resultMap="UserMap">
select * from mybatis.user limit #{startIndex},#{pageSize}
</select>
3.测试类
public void getUserByLimit(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Integer> map = new HashMap<>();
map.put("startIndex",2);
map.put("pageSize",4);
List<User> userList=mapper.getUserByLimit(map);
for (User user : userList) {
System.out.println(user);
}
}
🎸注解开发
面向接口编程
{% note red 'fas fa-fan' modern %} 解耦 {% endnote %}
理解:
- 定义与实现的分离
- 接口可以反映设计人员对系统的抽象理解
- 接口应有两类:
- 对一个个体的抽象,可对应为一个抽象体 (
abstract class) - 对一个个体某一方面的抽象,即形成一个抽象面(
interface)
- 对一个个体的抽象,可对应为一个抽象体 (
使用注解开发
接口
@Select("select * from mybatis.user ")
List<User> getUsers();
test类
public void getUsers(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.getUsers();
for (User user : users) {
System.out.println(user);
}
sqlSession.close();
}
本质:反射机制实现
底层:动态代理
CRUD
在工具类创建的时候可以实现自动提交事务
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
🐱👤Lombok
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation(注解) your class has a fully featured builder, Automate your logging variables, and much more.
Project Lombok 是一个 Java 库,可自动插入您的编辑器并构建工具,为您的 Java 增添趣味。
永远不要再编写另一个 getter 或 equals 方法,通过一个注释,您的类就有一个功能齐全的构建器,自动化您的日志变量等等。
- java library
- plugin
- build tools
- with one annotation(注解) your class
使用步骤:
1.IDEA中安装 Lombok插件
2.在项目中导入Lombok的JAR包
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
3.在实体类上加注解即可
@Getter and @Setter
@FieldNameConstants
@ToString
@EqualsAndHashCode
@AllArgsConstructor, @RequiredArgsConstructor and @NoArgsConstructor
@Log, @Log4j, @Log4j2, @Slf4j, @XSlf4j, @CommonsLog, @JBossLog, @Flogger, @CustomLog
@Data
@Builder
@SuperBuilder
@Singular
@Delegate
@Value
@Accessors
@Wither
@With
@SneakyThrows
@val
@var
experimental @var
@UtilityClass
Lombok config system
Code inspections
Refactoring actions (lombok and delombok)
说明:
@Date: 无参构造,get,set,tostring,hashcode,equals
@AllArgsConstructor: 有参构造(无参被抵消)
@NoArgsConstructor: 无参构造
@ToString
@EqualsAndHashCode
@Getter : 放在类上 生成所有属性get 放在字段上只生成一个属性的get
🐱🚀多对一处理
例:
- 多个学生有同一个老师
- 对于学生而言,关联....多个学生关联一个老师 [多对一]
- 对于老师而言, 集合 .... 一个老师有很多学生 [一对多]
SQL:
CREATE TABLE `teacher` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO teacher(`id`, `name`) VALUES (1, '秦老师');
CREATE TABLE `student` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`tid` INT(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fktid` (`tid`),
CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (1, '小明', 1);
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (2, '小红', 1);
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (3, '小张', 1);
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (4, '小李', 1);
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (5, '小王', 1);
测试环境搭建:
- 导入Lombok
- 新建实体类Teacher,Student
- 建立Mapper接口
- 建立Mapper.xml文件
- 在核心配置文件中绑定注册我们的Mapper接口或者文件 [方式很多]
- 测试查询是否成功
按照查询嵌套处理
<!--
1.查询所有学生信息
2.根据学生信息查询对应的老师
-->
<select id="getStudent" resultMap="StudentTeacher" >
select * from student;
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!--复杂的属性单独处理-->
<!--对象-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id=#{id};
</select>
按照结果嵌套处理
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname ,t.name tname
from student s ,teacher t
where s.tid=t.id;
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher" >
<result property="name" column="tname"/>
</association>
</resultMap>
回顾Mysql多对一查询
- 子查询
- 连表查询
🐱👓一对多处理
比如:一个老师拥有多个学生,对于老师而言,就是一对多的关系
环境搭建:
实体类修改
public class Teacher {
private int id;
private String name;
//老师包含多个学生
private List<Student> students;
}
public class Student {
private int id;
private String name;
private int tid;
}
mapper
<select id="getTeacher" resultMap="TeacherStudent" parameterType="int">
select s.id sid,s.name sname,t.name tname,t.id tid
from student s ,teacher t
where s.tid=t.id and t.id=#{tid};
</select>
<resultMap id="TeacherStudent" type="org.mecca.pojo.Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="org.mecca.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
🐱💻动态SQL
什么是动态SQL: 动态SQL就是指根据不同的条件生成不同的SQL语句;
使用动态 SQL 并非一件易事,但借助可用于任何 SQL 映射语句中的强大的动态 SQL 语言,MyBatis 显著地提升了这一特性的易用性。
如果你之前用过 JSTL 或任何基于类 XML 语言的文本处理器,你对动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中,需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类,现在要学习的元素种类比原来的一半还要少。
- if
- choose (when, otherwise)
- trim (where, set)
- foreach
环境搭建
CREATE TABLE `blog`(
`id` VARCHAR(50) NOT NULL COMMENT '博客id',
`title` VARCHAR(100) NOT NULL COMMENT '博客标题',
`author` VARCHAR(30) NOT NULL COMMENT '博客作者',
`create_time` DATETIME NOT NULL COMMENT '创建时间',
`views` INT(30) NOT NULL COMMENT '浏览量'
)ENGINE=INNODB DEFAULT CHARSET=utf8;

创建一个基础工程
-
导包
-
编写配置文件
-
编写实体类
@Data public class Blog { private String id; private String title; private String author; private Date create_time; private int views; } -
编写实体类对应的Mapper接口和Mapper.xml文件
IF
@Test
public void queryBlogIF(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map=new HashMap();
//map.put("title","Java");
map.put("author","狂神说");
List<Blog> blogs=mapper.queryBlogIF(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
<select id="queryBlogIF" parameterType="map" resultType="org.mecca.pojo.Blog">
select * from mybatis.blog
<where>
<if test="title!=null">
AND title=#{title}
</if>
<if test="author!=null">
AND author=#{author}
</if>
</where>
</select>
choose、when、otherwise
<select id="queryBlogChoose" parameterType="map" resultType="org.mecca.pojo.Blog">
select * from mybatis.blog
<where>
<choose>
<when test="title!=null">
AND title=#{title}
</when>
<when test="author!=null">
AND author=#{author}
</when>
<otherwise>
AND views=#{views};
</otherwise>
</choose>
</where>
</select>
choose中会执行第一条符合条件的语句 只有一条!!!
trim、where、set
<update id="updateBlog" parameterType="map">
update mybatis.blog
<set>
<if test="title!=null">
title=#{title},
</if>
<if test="author!=null">
author=#{author},
</if>
</set>
WHERE id=#{id}
</update>
set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号
where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。
SQL片段
有些时候,我们可能会将一些功能的部分抽取出来,复用!!
- 使用SQL标签抽取公共部分
<sql id="if-tittle-author">
<if test="title!=null">
AND title=#{title}
</if>
<if test="author!=null">
AND author=#{author}
</if>
</sql>
- 在需要的地方使用include 标签引用
<select id="queryBlogIF" parameterType="map" resultType="org.mecca.pojo.Blog">
select * from mybatis.blog
<where>
<include refid="if-tittle-author"></include>
</where>
</select>
注意:
- 最好基于单表定义SQL片段
- 不要存在Where标签
Foreeach
select * from user where 1=1 and (id=1 or id =2 or id=3)
<select id="queryBlogForeach" parameterType="map" resultType="org.mecca.pojo.Blog">
select * from mybatis.blog
<where>
<foreach collection="ids" item="id" open="(" separator="or" close=")">
id=#{id}
</foreach>
</where>
</select>
@Test
public void queryBlogForeach(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map = new HashMap();
ArrayList<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
map.put("ids",ids);
List<Blog> blogs = mapper.queryBlogForeach(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
动态SQL就是在拼接SQL语句,我们只要保证SQL的正确性,按照SQL的格式去排列组合就可以了
建议:
- 先在写出完整的Sql 再对应地去修改成为我们的动态SQL实现通用即可
🏎️缓存
1.简介
查询: 连接数据库,耗费资源!
一次查询的结果,给他暂存在一个可以直接取到的地方! --> 内存 : 缓存
我们再次查询相同数据的时候直接走缓存就不用走数据库了!
1.1 什么是缓存?
- Cache
- 存在内存中的临时数据
- 提高效率,提高查询效率,解决高并发系统的性能问题!
1.2为什么使用缓存?
- 减少和数据库的交互次数,减少系统开销,提高系统效率.
1.3什么样的数据能使用缓存?
- 经常查询并且不经常改变的数据.
2.Mybatis缓存
- MyBatis 内置了一个强大的事务性查询缓存机制,它可以非常方便地配置和定制。
- 默认情况下,只启用了本地的会话缓存,它仅仅对一个会话中的数据进行缓存。
- 要启用二级缓存,只需要在你的 SQL 映射文件中添加一行:
<cache/>
3.一级缓存
- 一级缓存也叫本地缓存 :SqlSession
- 与数据库同一次会话期间查询到的数据会放在本地缓存中.
- 以后如果需要获取相同的数据,直接从缓存中拿,没必要再去查询数据库.
缓存失效的情况:
-
查询不同的东西
-
增删改操作,可能会改变原理啊的数据,所以必定会刷新缓存
-
查询不同的Mapper.xml
-
手动清除
一级缓存默认是开启的,也关不掉,只在一次SqlSession中有效,也就是在拿到连接和关闭连接之间有效
一级缓存相当于一个map,用完就丢掉.
4.二级缓存
- 二级缓存也叫全局缓存,一级缓存作用域太低了,所以诞生了二级缓存.
- 基于namespace 级别的缓存,一个命名空间,对应一个二级缓存;
- 工作机制
- 一个会话查询一条数据,这个数据就会被放在当前会话的一级缓存中;
- 如果当前会话关闭,这个会话对应的一级缓存就没了;但我们想要的是,绘画关闭,一级缓存中华的数据被保存到二级缓存中;
- 新的会话查询信息,就可以从二级缓存中获取内容;
- 不同的mapper 查处的数据会放在自己的缓存**(map)**中
步骤:
-
开启全局缓存
<!--开启全局缓存--> <setting name="cacheEnabled" value="true"/> -
在要使用二级缓存的mapper.xml中开启
<!--在当前mapper.xml中使用二级缓存--> <cache/>也可以自定义参数
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/> -
测试
- 问题: 我们需要将实体类序列化! 否则就会报错!!
-
小结:
- 只要开启了二级缓存,在同一个Mapper下都有效!
- 所有的数据都会先放在一级缓存中
- 只有当会话提交或者关闭的时候才会提交到二级缓存中
运行步骤:
- 查看二级缓存中有没有数据
- 查看一级缓存中有没有数据
- 进入数据库查询
自定义缓存-ehcache
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认CacheProvider。Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。
Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现。它支持注解方式使用缓存,非常方便。
要在程序中使用ehcache ,先要导包
<!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.2.1</version>
</dependency>
配置中应用
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
ehcache的配置
<?xml version="1.0" encoding="UTF-8" ?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="./tmpdir/Tmp_EhCache"/>
<defaultCache
eternal="false"
maxElementsInMemory="10000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="259200"
memoryStoreEvictionPolicy="LRU"/>
<cache
name="cloud_user"
eternal="false"
maxElementsInMemory="5000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
实际应用最广的是 Redis