权威MyBatis框架详解,一文带你看尽MyBatis!

注意:所有内容均来自B站《狂神说Java系列课程》一个宝藏男孩,强烈推荐!
视频地址:https://www.bilibili.com/video/BV1NE411Q7Nx
笔者也是基于视频做的笔记,方便日后复习与查看。有不懂的地方可观看视频讲解。也希望大家多多支持狂神!

MyBatis

1、MyBatis简介

1.1 什么是MyBatis?

  • MyBatis 是一款优秀的持久层框架

  • 它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。ps:摘自官网!

  • MyBatis 本是 apache 的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了 google code,并且改名为MyBatis 。

  • 2013年11月迁移到Github
    如何获得MyBatis?

maven仓库:

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.3</version>
</dependency>

GitHub地址:https://github.com/mybatis/mybatis-3

官方地址:https://mybatis.org/mybatis-3/zh/index.html

1.2 持久化

数据持久化

  • 持久化就是将程序的数据在持久状态和瞬时状态转化的过程
  • 内存:断电即失
  • 数据库(jdbc),io文件持久化。
  • 在生活中理解持久化:冷藏、罐头等

为什么需要持久化?

  • 有一些对象,不能让他丢掉。
  • 内存太贵了

1.3 持久层

Dao层,Service层,Controller层…

  • 完成持久化工作的代码块
  • 层界限十分明显。

1.4 为什么需要使用MyBatis?

  • 方便,更容易学习
  • 帮助开发人员将数据存入到数据库中。
  • 传统的JDBC代码太复杂了。简化。框架。自动化的实现。

mybatis特点:

  • 简单易学:本身就很小且简单。没有任何第三方依赖,最简单安装只要两个jar文件+配置几个sql映射文件易于学习,易于使用,通过文档和源代码,可以比较完全的掌握它的设计思路和实现。
  • 灵活:mybatis不会对应用程序或者数据库的现有设计强加任何影响。 sql写在xml里,便于统一管理和优化。通过sql语句可以满足操作数据库的所有需求。
  • 解除sql与程序代码的耦合:通过提供DAO层,将业务逻辑和数据访问逻辑分离,使系统的设计更清晰,更易维护,更易单元测试。sql和代码的分离,提高了可维护性。
  • 提供映射标签,支持对象与数据库的orm字段关系映射。
  • 提供对象关系映射标签,支持对象关系组建维护。
  • 提供xml标签,支持编写动态sql。

最重要的一点:使用的人多。

2、第一个MyBatis程序

2.1、搭建环境

1、数据库准备

# MyBatis练习
CREATE DATABASE `mybatis`;
USE `mybatis`;

# 建表
CREATE TABLE `user`(
    id int(20) PRIMARY KEY NOT null,
    name varchar(30) DEFAULT NULL,
    password varbinary(30) DEFAULT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8;

# 插入数据
INSERT INTO USER(id,name,password) VALUES
(1,'Java','123'),
(2,'Python','123'),
(3,'C/C++','123');

SELECT * FROM user;

2、新建maven项目

3、导入依赖

要使用 MyBatis, 只需将 mybatis-x.x.x.jar 文件置于类路径(classpath)中即可。

如果使用 Maven 来构建项目,则需将下面的依赖代码置于 pom.xml 文件中:

<!--mysql-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.21</version>
</dependency>

<!--MyBatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.3</version>
</dependency>

<!--junit-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>provided</scope>
</dependency>

到此基础环境已搭建完毕!

2.2、编写MyBatis核心配置文件与工具类

1、MyBatis核心配置文件

包含了对 MyBatis 系统的核心设置,包括获取数据库连接实例的数据源(DataSource)以及决定事务作用域和控制方式的事务管理器(TransactionManager)

# db.properties
driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=FALSE&serverTimezone=UTC
username=root
password=101323

mybatis-config.xml

<?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>
    <properties resource="db.properties"/> <!--加载配置文件-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    
    <!--每一个Mapper.xml都需要在MaBatis核心配置文件中注册!-->
    <mappers>
        <mapper resource="com/howie/dao/UserMapper.xml"/>
    </mappers>
    
</configuration>

2、编写MyBatis工具类

每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为核心的。SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先配置的 Configuration 实例来构建出 SqlSessionFactory 实例。

这里的工具类并不标准,只是简单的实现加载配置文件和实例对象的获取。

标准MyBatis工具类的编写可参考:https://github.com/laizhenghua2/mybatis-template

package com.howie.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;

/**
 * @description: MyBatis工具类,SqlSessionFactory构建sqlSession
 * @author: laizhenghua
 * @date: 2020/11/9 22:14
 */
public class MyBatisUtil {
    
    
    private static SqlSessionFactory sqlSessionFactory = null;
    static{
    
    
        // 使用MyBatis第一步:获取 SqlSessionFactory 对象
        String resource = "mybatis-config.xml";
        try {
    
    
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
    /*
    既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
    SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。
    */
    public static SqlSession getSqlSession(){
    
    
        if(sqlSessionFactory != null){
    
    
            return sqlSessionFactory.openSession();
        }
        return null;
    }
}

2.3、编写实验代码

  • 实体类
package com.howie.pojo;

/**
 * @description: user实体类
 * @author: laizhenghua
 * @date: 2020/11/9 22:32
 */
public class User {
    
    
    private int id;
    private String name;
    private String password;

    public User() {
    
    
    }
...
  • Dao接口
/**
 * @description: UserDao的接口
 * @author: laizhenghua
 * @date: 2020/11/9 22:36
 */
public interface UserDao {
    
    
    List<User> getUserList();
}
  • 接口实现类由原来的UserDaoImpl转换为Mapper配置文件UserMapper.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">
<!--namespace: 绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.howie.dao.UserDao">
    <!--查询语句:id属性必须和接口抽象方法名对应-->
    <select id="getUserList" resultType="com.howie.pojo.User">
        select * from mybatis.user
    </select>
</mapper>

2.4、测试

注意点:

org.apache.ibatis.binding.BindingException: Type interface com.howie.dao.UserDao 
is not known to the MapperRegistry.

如果没有在核心配置文件中注册xxxMapper.xml,就报此种错误!

MapperRegistry是什么?

核心配置文件中注册Mappers

这里使用junit测试:

@Test
public void getUserListTest(){
    
    
    // 1、获得sqlSession对象
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    // 2、执行(方式1:getMapper)
    assert sqlSession != null;
    UserDao userDao = sqlSession.getMapper(UserDao.class);
    List<User> userList = userDao.getUserList();
    
    // 2、执行(方式2:使用sqlSession提供的方法)
    List<User> users = sqlSession.selectList("com.howie.dao.UserDao.getUserList");
    // 遍历
    for(User user : userList){
    
    
        System.out.println(user);
    }
    // 3、关闭sqlSession
    sqlSession.close();
}

输出结果:

在这里插入图片描述
你们可以能会遇到的问题:

1.配置文件没有注册
2.绑定接口错误。
3.方法名不对
4.返回类型不对
5.Maven导出资源问题

3、CRUD的实现

3.1、namespace

namespace中的包名要和Dao/Mapper接口的包名一致!

3.2、select标签

选择,查询语句。

属性详解:

id # 就是对应的namespace中的方法名
resultType # SQL语句执行的返回值,如果返回多个自动封装为 List
parameterType # 参数类型

1、编写接口

// 根据id查询用户
User getUserById(int id);

2、编写对应Mapper中SQL语句

<!--根据id查询用户-->
<select id="getUserById" parameterType="int" resultType="com.howie.pojo.User">
     select * from mybatis.user where id = #{id}
</select>

3、测试

@Test
    public void getUserByIdTest(){
    
    
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        assert sqlSession != null;
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        
        User user = userDao.getUserById(1);
        System.out.println(user);
        sqlSession.close();
    }

3.3、insert标签

注意:增删该需要手动提交事务!

1、编写接口
2、编写对应Mapper中SQL语句

<!--添加新用户-->
<insert id="addUser" parameterType="com.howie.pojo.User">
    insert into mybatis.user(id,name,password) value(#{id},#{name},#{password})
</insert>

3、测试
4、代码在下方集中演示

3.4、update标签

注意点:需要手动提交事务

1、编写接口
2、编写对应Mapper中SQL语句

<!--修改用户信息-->
<update id="updateUser" parameterType="com.howie.pojo.User">
    update mybatis.user set name = #{name},password = #{password} where id = #{id}
</update>

3、测试
4、代码在下方集中演示

3.5、delete标签

注意点:需要手动提交事务

<!--删除一个用户-->
<delete id="deleteUserById" parameterType="int">
    delete from mybatis.user where id = #{id}
</delete>

代码集中演示:增删改查!

1、userDao接口

/**
 * @description:
 * @author: laizhenghua
 * @date: 2020/11/9 22:36
 */
public interface UserDao {
    
    
    // 查询所有用户
    List<User> getUserList();
    // 根据id查询用户
    User getUserById(int id);
    // 添加一个用户
    int addUser(User user);
    // 修改用户信息
    int updateUser(User user);
    // 删除一个用户
    int deleteUserById(int id);
}

2、userMapper.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">
<!--namespace: 绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.howie.dao.UserDao">
    <!--查询语句:id属性必须和接口抽象方法名对应-->
    <select id="getUserList" resultType="com.howie.pojo.User">
        select * from mybatis.user
    </select>
    <!--根据id查询用户-->
    <select id="getUserById" parameterType="int" resultType="com.howie.pojo.User">
        select * from mybatis.user where id = #{id}
    </select>
    <!--添加新用户-->
    <insert id="addUser" parameterType="com.howie.pojo.User">
        insert into mybatis.user(id,name,password) value(#{id},#{name},#{password})
    </insert>
    <!--修改用户信息-->
    <update id="updateUser" parameterType="com.howie.pojo.User">
        update mybatis.user set name = #{name},password = #{password} where id = #{id}
    </update>
    <!--删除一个用户-->
    <delete id="deleteUserById" parameterType="int">
        delete from mybatis.user where id = #{id}
    </delete>
</mapper>

3、测试类

@Test
public void getUserByIdTest(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    assert sqlSession != null;
    UserDao userDao = sqlSession.getMapper(UserDao.class);
    User user = userDao.getUserById(1);
    System.out.println(user);
    sqlSession.close();
}
@Test
public void addUserTest(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    assert sqlSession != null;
    UserDao userDao = sqlSession.getMapper(UserDao.class);

    User user = new User();
    user.setId(4);
    user.setName("Linux");
    user.setPassword("123");
    // 注意:MyBatis增删改需要提交事务
    int count = userDao.addUser(user);
    System.out.println(count); // count就是数据库影响的行数
    sqlSession.commit(); // 提交事务,才能持久化到数据库
    sqlSession.close();
}
@Test
public void updateUserTest(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    assert sqlSession != null;
    UserDao userDao = sqlSession.getMapper(UserDao.class);

    User user = new User();
    user.setId(1);
    user.setName("Java");
    user.setPassword("1234656");
    // 注意:MyBatis增删改需要提交事务
    int count = userDao.addUser(user);
    System.out.println(count);
    sqlSession.commit(); // 提交事务,才能持久化到数据库
    sqlSession.close();
}
@Test
public void deleteUserByIdTest(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    assert sqlSession != null;
    UserDao userDao = sqlSession.getMapper(UserDao.class);

    sqlSession.commit();
    int count = userDao.deleteUserById(4);
    System.out.println(count);
}

3.6、map作为传递参数的扩展

假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应当考虑使用Map!Map在某些场合使用起来非常灵活,方便,例如我们假设用户表字段非常多,我们只想修改其中一个字段,此时传递参数就可以选用map,通过map传递给MyBatis修改的字段值和id即可。

1、Dao接口

// 使用map作为参数添加用户
int addUserByMap(Map<String,Object> map);

2、Mapper.xml

<!--
	使用map作为参数,添加用户.
	这里的占位参数和map里的键一一对应!
	#{userId} -> map.get("userId")
	...
-->
<insert id="addUserByMap" parameterType="Map">
     insert into mybatis.user(id,name,password) value(#{userId},#{userName},#{userPassword})
</insert>

3、测试

@Test
public void addUserByMapTest(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    assert sqlSession != null;
    UserDao dao = sqlSession.getMapper(UserDao.class);

    Map<String,Object> map = new HashMap<>();
    map.put("userId","5");
    map.put("userName","JavaScript");
    map.put("userPassword","123");
    
    int count = dao.addUserByMap(map);
    sqlSession.commit(); // 提交事务
    System.out.println(count);
    sqlSession.close();
}

map传递参数,直接在SQL中取出key即可![parameterType=“Map”]
对象传递参数,直接在SQL中取对象的属性即可![parameterType=“类名”]
只有一个基本数据类型的参数情况下,可以直接在SQL中取到![parameterType=“基本数据类型”]

3.7、MyBatis的模糊查询

模糊查询怎么写?

1、Java代码执行的时候,传递通配符 %%

// 模糊查询
List<User> getUserLike(String name);
<!--name模糊查询-->
<select id="getUserLike" parameterType="String" resultType="com.howie.pojo.User">
    select * from mybatis.user where name like #{name}
</select>
List<User> userList = userDao.getUserLike("%Ja%"); // 传递通配符
System.out.println(userList);

2、在SQL中使用空格拼接通配符!MySQL中空格相当于+号(拼接)

<!--name模糊查询-->
<select id="getUserLike" parameterType="String" resultType="com.howie.pojo.User">
    select * from mybatis.user where name like '%' #{name} '%'
</select>
List<User> userList = dao.getUserLike("Ja");
System.out.println(userList);

4、MyBatis配置文件解析

4.1、核心配置文件

  • mybatis-config.xml
  • MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息。 配置文档的顶层结构如下
properties(属性)
settings(设置)
typeAliases(类型别名)
typeHandlers(类型处理器)
objectFactory(对象工厂)
plugins(插件)
environments(环境配置)
environment(环境变量)
transactionManager(事务管理器)
dataSource(数据源)
databaseIdProvider(数据库厂商标识)
mappers(映射器)

4.2、环境配置(environments)

详细配置可查看官方文档:https://mybatis.org/mybatis-3/zh/configuration.html#environments

MyBatis可以配置成适应多种环境

不过要记住:尽管可以配置多个环境,但每个SqISessionFactory实例只能选择一种环境。

学会配置多套运行环境!

<environments default="development">
	<environment id="development">
	...
<!--测试环境-->
<environments defaut="test">
	<environment id="test">
	...

Mybatis默认的事务管理器就是 JDBC,连接池:POOLED

4.3、属性(properties)

我们可以通过properties属性来实现引用配置文件。

这些属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过properties元素的子元素来传递。[db.properties]

1、编写一个配置文件

driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=FALSE&serverTimezone=UTC
# jdbc:mysql://localhost:3306/crm?useSSL=FALSE&serverTimezone=UTC&allowPublicKeyRetrieval=true
username=root
password=101323

2、在核心配置文件中引入

<!--核心配置文件-->
<configuration>
    <!--引入外部配置文件-->
    <properties resource="db.properties"/>
    ...

4.4、类型别名(typeAliases)

  • 类型别名是为Java类型设置一个短的名字。
  • 存在的意义仅在于用来减少类完全限定名的冗余。
  • 类型别名有两种方式
<!--核心配置文件-->
<configuration>
    <!--引入外部配置文件-->
    <properties resource="db.properties"/>
    <!--可以给实体类起别名-->
    <typeAliases>
        <!--
            方式1:为指定的类分别起别名,别名的命名由我们来决定
            <typeAlias type="com.howie.pojo.Province" alias="pd"/>
            type:要为那个domain/pojo起别名,填写包.类名称
            alias:别名的名字
        -->
        <!--
            方式2:使用package标签批量起别名
            别名是MyBatis默认为我们取好的,命名不是由我们自己决定,别名为类(类名的字母不区分大小写)
            name:指定一个包结构,表示在该包下,所有的domain/pojo自动起好了别名
            
            例如:<package name="com.howie.pojo"/>
        -->
        <typeAlias type="com.howie.pojo.User" alias="User"/>
    </typeAliases>
	...

在实体类比较少的时候,建议使用第一种
在实体类比较多的时候,建议使用第二种

若有注解,我们还可以使用注解,别名为其注解值。见下面的例子:

@Alias("author")
public class Author {
    
    
    ...
}

对于常见 Java 类型内建的类型别名问题,如int Integer Map等。可在官网上查看对应的映射!
https://mybatis.org/mybatis-3/zh/configuration.html#typeAliases

4.5、设置(settings)

这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。

详细配置信息可查看官网:https://mybatis.org/mybatis-3/zh/configuration.html#settings

常用配置

在这里插入图片描述

4.6、映射器(mappers)

官网地址:https://mybatis.org/mybatis-3/zh/configuration.html#mappers

既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要来定义 SQL 映射语句了。 但首先,我们需要告诉 MyBatis 到哪里去找到这些语句。 在自动查找资源方面,Java 并没有提供一个很好的解决方案,所以最好的办法是直接告诉 MyBatis 到哪里去找映射文件。

MapperRegistry:注册绑定我们的Mapper文件!

方式一[推荐使用]:

<!--每一个Mapper.xml都需要在MaBatis核心配置文件中注册!-->
<mappers>
    <mapper resource="com/howie/dao/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配置文件必须在同一个包下!

4.7、生命周期和作用域

在这里插入图片描述
生命周期,和作用域,是至关重要的,因为错误的使用会导致非常严重的并发问题

SqlSessionFactoryBuilder:

  • 一旦创建了SqlSessionFactory,就不再需要它了
  • 局部变量

SqlSessionFactory:

  • 说白了就是可以想象为:数据库连接池!
  • SqlSessionFactory 一旦被创建就应该在应用的运行期间一直存在,没有任何理由丢弃它或重新创建另一个实例。
  • SqlSessionFactory 的最佳作用域是应用作用域。
  • 最简单的就是使用单例模式或者静态单例模式

SqlSession:

  • 想象为连接到数据库连接处的一个请求!
  • 每个线程都应该有它自己的 SqlSession 实例。SqlSession 的实例不是线程安全的,因此是不能被共享的,所以它的最佳的作用域是请求或方法作用域。
  • 用完之后需要马上关闭,否则资源被占用。
    在这里插入图片描述
    这里的每个Mapper,就代表一个具体的业务!

5、Mapper映射文件

5.1、属性名与字段名不一致问题

在实际开发过程中,我们可能会遇到实体类的属性名与数据库的字段名不一致的情况!如果我们还是以原来的方式去执行SQL语句时,会发现属性名与字段名不一致的实体类属性匹配失败,显示null。

比如:我们把实体类的属性password(与数据库字段一致)修改为pwd。

public class User {
    
    
    private int id;
    private String name;
    private String pwd; // 此时与数据字段名不一致了
    ...
}

执行SQL语句查看结果!

在这里插入图片描述
SQL语句

select * from mybatis.user where id = #{id}

select id,name,password from mybatis.user where id = #{id}

解决方法:

为查询字段起别名(与实体类属性名一致)

<select id="getUserById" parameterType="int" resultType="User">
    select id,name,password as pwd from mybatis.user where id = #{id}
</select>

5.2、结果集映射(resultMap)

上面我们讲到为查询字段起别名可以解决属性名与字段名不一致问题!但是这种方式在某些场景有弊端,比如说复杂的查询语句起别名可能并不能解决问题,还有一点起别名并不是MyBatis提供的解决方式,针对此种问题MyBatis专门为我们提供了一种解决方案!就是使用 resultMap 标签,我们称之为结果集映射

结果集映射详细信息官方地址:https://mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps

结果集映射,说白了就是数据库中的字段映射到实体类的属性!

<!--结果集映射-->
<resultMap id="UserMap" type="User">
    <!--
        column:数据库字段
        property:实体类属性
    -->
    <result column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="password" property="pwd"/><!--上面两句可以不写-->
</resultMap>
<!--根据id查询用户-->
<select id="getUserById" parameterType="int" resultMap="UserMap">
    select * from mybatis.user where id = #{id}
</select>

总结:

  • resultMap 元素是 MyBatis 中最重要最强大的元素。
  • ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。
  • ResultMap最优秀的地方在于,虽然你已经对它相当了解了,但是根本就不需要显式地用到他们。

6、日志

6.1、日志工厂

如果一个数据库操作,出现了异常,我们需要排错。日志就是最好的助手!

曾经:sout打印输出,debug等

现在:我们要使用日志工厂!

设置和指定MyBatis所用的日志:

在这里插入图片描述
在Mybatis中具体使用那个一日志实现,在设置中设定!

SLF4J 
LOG4J # 重点
LOG4J2 
JDK_LOGGING 
COMMONS_LOGGING 
STDOUT_LOGGING  # 重点,标准的日志输出,使用简单,核心配置文件配置即可
NO_LOGGING

在mybatis核心配置文件中,配置我们的日志!

 <!--设置日志-->
 <settings>
     <setting name="logImpl" value="STDOUT_LOGGING"/>
 </settings>

执行sql时输出整个过程!
日志输出

6.2、LOG4J

1、什么是Log4j?

  • Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件。
  • 我们也可以控制每一条日志的输出格式。
  • 通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。
  • 最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。

2、如何使用log4j?

1、先导入Log4j依赖

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

2、在CLASSPATH下建立log4j.properties,编写配置文件。

# log4j.properties 经典模板
#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file

#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n

#文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/howie.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{
    
    yy-MM-dd}][%c%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的使用。直接运行测试代码即可在后台看到日志信息!
日志输出的信息
5、log4j的其他简单使用

1、要在使用log4j的类中,导入包(import org.apache.log4j.Logger;)

2、日志对象,参数为当前类的class。

/**
 * @description: 测试类
 * @author: laizhenghua
 * @date: 2020/11/11 15:15
 */
public class UserTest {
    
    
    private static Logger logger = Logger.getLogger(UserTest.class);
    ...

3、日志级别

@Test
public void log4jTest(){
    
    
    // 不同的日志级别
    logger.info("info:进入了UserTest!");
    logger.debug("debug:进入了UserTest!");
    logger.error("error:进入了UserTest!");
}

7、分页

思考:为什么要使用分页?

  • 减少数据的处理量

MyBatis里实现分页的方式有3种,下面介绍3种实现方式。

7.1、使用limit实现分页

使用limit分页

# 语法
SELECT * FROM user LIMIT startIndex,pageSize;

使用Mybatis实现分页,核心SQL

1.接口

// 分页
List<User> getUserByLimit(Map<String,Integer> map);

2.Mapper.xml

<!--分页-->
<select id="getUserByLimit" parameterType="Map" resultType="User">
    select * from mybatis.user limit #{startIndex},#{pageSize}
</select>

3.测试

@Test
public void getUserListByLimit(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    assert sqlSession != null;
    UserDao userDao = sqlSession.getMapper(UserDao.class);

    Map<String,Integer> map = new HashMap<>();
    map.put("startIndex",1);
    map.put("pageSize",2);
    List<User> users = userDao.getUserByLimit(map);

    for(User user : users){
    
    
        System.out.println(user);
    }
}

7.2、RowBounds类分页

不在使用SQL实现分页

1、接口

// 分页2
List<User> getUserListByRowBounds();

2、Mapper.xml

<!--分页2-->
<select id="getUserListByRowBounds" resultType="User">
    select * from mybatis.user
</select>

3、测试

@Test
public void getUserListByRowBoundsTest(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    // RowBounds实现
    RowBounds rowBounds = new RowBounds(1,2);
    // 通过Java代码层面实现分页
    
    assert sqlSession != null;
    List<User> userList = sqlSession.selectList("com.howie.dao.UserDao.getUserListByRowBounds",null,rowBounds);
    for(User user : userList){
    
    
        System.out.println(user);
    }
    sqlSession.close();
}

7.3、分页插件

插件
官方文档地址:https://pagehelper.github.io/docs/howtouse/

了解即可,万一以后公司的架构师,说要使用,你需要知道它是什么东西!

8、使用注解开发

8.1、回顾面向接口编程

  • 大家之前都学过面向对象编程,也学习过接口,但在真正的开发中,很多时候我们会选择面向接口编程。
  • 根本原因∶解耦,可拓展,提高复用,分层开发中,上层不用管具体的实现,大家都遵守共同的标准,使得开发变得容易,规范性更好。
  • 在一个面向对象的系统中,系统的各种功能是由许许多多的不同对象协作完成的。在这种情况下,各个对象内部是如何实现自己的,对系统设计人员来讲就不那么重要了。
  • 而各个对象之间的协作关系则成为系统设计的关键。小到不同类之间的通信,大到各模块之间的交互,在系统设计之初都是要着重考虑的,这也是系统设计的主要工作内容。面向接口编程就是指按照这种思想来编程。

关于接口的理解

  • 接口从更深层次的理解,应是定义(规范,约束)与实现(名实分离的原则)的分离。
  • 接口的本身反映了系统设计人员对系统的抽象理解。
  • 接口应有两类:
    1、第一类是对一个个体的抽象,它可对应为一个抽象体(abstract class);
    2、第二类是对一个个体某一方面的抽象,即形成一个抽象面(interface) ;
  • 一个体有可能有多个抽象面。抽象体与抽象面是有区别的。

三个面向区别

  • 面向对象是指,我们考虑问题时,以对象为单位,考虑它的属性及方法。
  • 面向过程是指,我们考虑问题时,以一个具体的流程(事务过程)为单位,考虑它的实现。
  • 接口设计与非接口设计是针对复用技术而言的,与面向对象(过程)不是一个问题.更多的体现就是对系统整体的架构。

8.2、使用注解开发

1、注解在接口上实现

@Select("select * from mybatis.user")
List<User> getUserList();

2、需要在核心配置文件中绑定接口

<!--绑定接口-->
<mappers>
    <mapper class="com.howie.dao.UserDao"/>
</mappers>

3、测试

@Test
public void getUserListTest(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    assert sqlSession != null;
    UserDao dao = sqlSession.getMapper(UserDao.class);
    // 底层主要应用反射,推导出执行方法的返回值类型
    List<User> userList = dao.getUserList();

    for(User user : userList){
    
    
        System.out.println(user);
    }
    sqlSession.close();
}

关于注解的说明:

  • 使用注解来映射简单语句会使代码显得更加简洁,但对于稍微复杂一点的语句,Java 注解不仅力不从心,还会让你本就复杂的 SQL 语句更加混乱不堪。 因此,如果你需要做一些很复杂的操作,最好用 XML 来映射语句。
  • 本质:反射机制
  • 底层:动态代理

8.3、MyBatis详细执行流程

Mybatis详细执行流程

8.4、注解实现CRUD

我们知道MyBatis中对于 "增删改"需要手动提交事务!在获取sqlSession对象中我们也可以设置自动提交事务!

源码分析:

public class DefaultSqlSessionFactory implements SqlSessionFactory {
    
    
	...
	// 获取sqlSession对象的方法
	@Override
	public SqlSession openSession() {
    
    
		// 下面方法里的参数 false 就代表默认不提交事务 
		return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
  }
	// 获取sqlSession对象的重载方法,参数 autoCommit=true 时就可自动提交事务
	@Override
	public SqlSession openSession(boolean autoCommit) {
    
    
    	return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, autoCommit);
  }
	...

1、编写接口,增加注解

// 方法存在多个参数时,每个参数前面都要加上 @Param 注解
@Select("select * from mybatis.user where id = #{id}")
User getUserById(@Param("id") Integer id);

@Insert("insert into mybatis.user(id,name,password) value(#{id},#{name},#{password})")
int addUser(User user);

@Update("update mybatis.user set name = #{name},password = #{password} where id = #{id}")
int updateUser(User user);

@Delete("delete from mybatis.user where id = #{uid}")
int deleteUser(@Param("uid") Integer id);

2、测试

注意点:必须要将接口注册绑定到核心配置文件中!

public interface UserDao {
    
    
	@Test
	public void annotationTest(){
    
    
	    SqlSession sqlSession = MyBatisUtil.getSqlSession();
	    assert sqlSession != null;
	    UserDao dao = sqlSession.getMapper(UserDao.class);
	    
	    User user = dao.getUserById(1);
	    System.out.println(user);
	    sqlSession.close();
	}
// 这里只测试了一个,后面可自行测试
...

关于@Param()注解

  • 基本类型的参数或者String类型,需要加上
  • 引用类型不需要加
  • 如果只有一个基本类型的话,可以忽略,但是建议大家都加上!
  • 我们在SQL中引用的就是我们这里的@Param()中设定的属性名!

8.5、#{} 与 ${}

使用在SQL语句中的符号

# 1、#{}:表示占位符,可以有效防止SQL注入。使用#{}设置参数。无序考虑参数的类型 PreparedStatement

# 2、${}:表示拼接符,无法防止SQL注入,使用${}设置参数必须考虑参数的类型 Statement

# 3、传递简单类型参数

  # a.如果获取简单类型参数,#{}中可以使用value或其他名称

  # b.如果获取简单类型参数,${}中只能使用valeu。例如:select * from tbl_student where id='${value}'

# 4、在没有特殊要求的情况下,通常使用#{}占位符

# 5、有些情况必须使用${},比如需要动态拼接表名,
select * from ${tablename} 
# 比如:动态拼接排序字段:
select * from tablename order by ${username} desc

9、Lombok

1、Lombok项目是一个Java库,它会自动插入编辑器和构建工具中,Lombok提供了一组有用的注释,用来消除Java类中的大量样板代码(如 get/set/toString等)。

2、使用步骤:

  • 在IDEA中安装lombok插件

  • 从maven导入lombok依赖

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>
  • 在实体类中加注解即可
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    
    
    private Integer id;
    private String name;
    private String password;
}

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

10、MyBatis对【多对一】的处理

我们以学生和老师场景举例,他们之间存在以下关系:

  • 多个学生,对应一个老师
  • 对于学生这边而言,关联…多个学生,关联一个老师【多对一】
  • 对于老师而言,集合,一个老师,有很多学生【一对多】

10.1、搭建测试环境

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');

1、导入lombok

2、新建实体类Teacher,Student

/**
 * @description: 学生表实体类
 * @author: laizhenghua
 * @date: 2020/11/12 12:39
 */
@Data
public class Student {
    
    
    private Integer id;
    private String name;
    private Teacher teacher; // 关联老师,注意这里是老师类不是tid
}

3、建立Mapper接口

4、建立Mapper.XML文件

5、在核心配置文件中绑定注册我们的Mapper接口或者文件!【方式很刻】

6、测试查询是否能够成功!

10.2、按照查询嵌套处理(子查询)

1、接口

// 查询所有的学生信息,以及对应的老师信息
List<Student> getStudentList();
/*
	思考:在Studnet实体类中,关联老师时,我们把此属性设置成Teacher类型
	如何查询才能完全显示学生信息呢?
	思路:
		1、先在学生表查询出所有信息
		2、在根据查找出来的tid,在老师表查询老师信息
	实际上就是子查询!代码实现如下!
*/

2、Mapper映射文件

<select id="getStudentList" resultMap="studentTeacher">
    select * from student
</select>
<resultMap id="studentTeacher" type="Student">
    <!--
        复杂的属性需要单独处理
        association 关联/对象
        collection 集合
    -->
    <result column="id" property="id"/>
    <result column="name" property="name"/>
    <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
    select * from teacher where id = #{id}
</select>

3、测试,输出

在这里插入图片描述
我们可以看到,关联的老师属性,可以正常显示!

10.3、按照结果嵌套处理(连表查询)

依旧是上面的需求!

// 查询所有的学生信息,以及对应的老师信息
List<Student> getStudentList();

除了子查询解决方式外,我们还有一种解决方式!就是连表查询。

<select id="getStudents" resultMap="studentMap">
    select s.id as sid,s.name as sname,t.name as tname
    from student s join teacher t on s.tid = t.id
</select>
<resultMap id="studentMap" type="Student">
    <result column="sid" property="id"/>
    <result column="sname" property="name"/>
    <association property="teacher" javaType="Teacher">
        <result column="tname" property="name"/>
    </association>
</resultMap>

11、MyBatis对【一对多】的处理

比如一个老师拥有多个学生!

对于老师而言,就是一对多的关系!

11.1、测试环境搭建

和上面一样,我们只需要修改实体类即可!

Student

/**
 * @description: Student实体类
 * @author: laizhenghua
 * @date: 2020/11/12 17:15
 */
@Data
@NoArgsConstructor
public class Student {
    
    
    private Integer id;
    private String name;
    private Integer tid; // 注意这里改回了和数据库一样的字段
}

Teacher

/**
 * @description: Teacher实体类
 * @author: laizhenghua
 * @date: 2020/11/12 17:16
 */
@Data
@NoArgsConstructor
public class Teacher {
    
    
    private Integer id;
    private String name;
    // 一个老师拥有多个学生,所以表示如下
    private List<Student> studentList;
}

11.2、按照结果嵌套处理(连表查询)

1、接口

public interface TeacherDao {
    
    
    // 获取指定老师下的所有学生及老师的信息
    Teacher getTeacher(@Param("tid") Integer id);

2、Mapper映射文件

<!--按结果嵌套查询-->
<select id="getTeacher" parameterType="int" resultMap="teacherStudent">
    select t.id as tid,t.name as tname,s.id as sid,s.name as sname
    from teacher t join student s on t.id = s.tid where t.id = #{tid}
</select>
<!--
    复杂的属性需要单独处理
    association 关联/对象
    collection 集合
    javaType 指定属性类型,集合中泛型信息,使用ofType获取
-->
<resultMap id="teacherStudent" type="Teacher">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <collection property="studentList" ofType="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
    </collection>
</resultMap>

3、测试

@Test
public void teacherTest(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    assert sqlSession != null;
    TeacherDao studentDao = sqlSession.getMapper(TeacherDao.class);
    Teacher teacher = studentDao.getTeacher(1);
    /*
        Teacher(id=1, name=秦老师, studentList=[Student(id=1, name=小明, tid=null), Student(id=2, name=小红, tid=null)...
    */
    System.out.println(teacher);
}

11.3、按照查询嵌套处理(子查询)

Mapper映射文件

<!--子查询方式-->
<select id="getTeacher2" parameterType="int" resultMap="TeacherMap">
    select * from teacher where id = #{tid}
</select>
<resultMap id="TeacherMap" type="Teacher">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <collection property="studentList" ofType="Student" javaType="ArrayList" select="getStudentById" column="id"/>
</resultMap>
<select id="getStudentById" resultType="Student">
    select * from student where tid = #{tid}
</select>

小结:

1、集合 -> collection -> 一对多

2、关联 -> association -> 多对一

3、javaType 用来指定实体类中属性的类型

4、ofType 指定泛型信息

注意点:

  • 保证SQL的可读性,尽量保证通俗易懂
  • 注意一对多和多对一中,属性名和字段的问题!
  • 如果问题不好排查错误,可以使用日志,建议使用Log4j

12、动态SQL

12.1、什么是动态SQL

官方地址:https://mybatis.org/mybatis-3/zh/dynamic-sql.html

1)在实际开发过程中,往往会遇到各种各样的需求,我们不可能为每一个需求都创建SQL语句,肯定是要将SQL语句写成动态的形式。

2)动态SQL语句的核心思想就是,有那个查询条件,就动态的在 where 关键字后面挂在那个查询条件

什么是动态SQL:动态SQL就是指根据不同的条件生成不同的SQL语句。

官方解释:

/*
动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,
你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添
加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底
摆脱这种痛苦
*/

如果你之前用过 JSTL 或任何基于类 XML 语言的文本处理器,你对动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中,需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类,现在要学习的元素种类比原来的一半还要少。

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

搭建测试环境SQL

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

创建一个基础工程

1、导包

2、编写配置文件

3、编写实体类

/**
 * @description: Blog实体类
 * @author: laizhenghua
 * @date: 2020/11/13 8:48
 */
@Data
public class Blog {
    
    
    private Integer id;
    private String title;
    private String author;
    private Date createTime; // 创建时间
    private String views; // 浏览量
}

4、编写实体类对应Mapper接口和Mapper.XML文件

12.2、if

使用动态 SQL 最常见情景是根据条件包含 where 子句的一部分。比如

<select id="queryBlog" parameterType="Map" resultType="Blog">
    select * from blog where 1=1
    <if test="title != null">
        and title = #{title}
    </if>
    <if test="author != null">
        and author = #{author}
    </if>
</select>

<!--加上where标签进行改进-->
<!--where标签-->
<!--
     当where标签在使用的时候,必须要搭配where标签对中的if标签来使用
     通过if标签的判断,如果有查询条件,则展现where关键字,如果没有查询条件则不展现where关键字
     where标签会自动的屏蔽掉第一个连接符 and/or
-->
<select id="queryBlog" parameterType="Map" resultType="Blog">
	select * from blog
	<where>
		<if test="title != null">
            title = #{title}
        </if>
        <if test="author != null">
            and author like #{author}
        </if>
	</where>

测试

@Test
public void queryTest(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    assert sqlSession != null;
    BlogDao blogDao = sqlSession.getMapper(BlogDao.class);
    Map<String,Object> map = new HashMap<>();
    map.put("title","Java");
    map.put("author","狂神"); // select * from blog where 1=1 and title = ? and author = ?
    List<Blog> blogs = blogDao.queryBlog(map);
    for(Blog blog : blogs){
    
    
        System.out.println(blog);
    }
    sqlSession.close();
}

12.3、choose (when, otherwise)

有时候,我们不想使用所有的条件,而只是想从多个条件中选择一个使用。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句

<select id="queryBlogChoose" parameterType="Map" resultType="Blog">
    select * from mybatis.blog
    <where>
        <choose>
            <when test="title != null">
                title = #{title}
            </when>
            <when test="author != null">
                and author = #{author}
            </when>
            <otherwise>
                and views = #{views}
            </otherwise>
        </choose>
    </where>
</select>

12.4、trim (where, set)

trim:自定义 trim 元素,可定制 where、set等 元素的功能。具体使用可在网上查询!

前面where标签我们已经使用过了,现在我们来看看set标签!

1、需求,也就是编写接口

// 修改blog
int updateBlog(Map<String,String> map);

2、编写xml映射文件

<update id="updateBlog" parameterType="Blog">
    update mybatis.blog
    <set>
        <if test="title != null">
            title = #{title},
        </if>
        <if test="author != null">
            author = #{author}
        </if>
    </set>
    where id = #{id}
</update>

3、测试

@Test
public void updateBlogTest(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    assert sqlSession != null;
    BlogDao blogDao = sqlSession.getMapper(BlogDao.class);
    Map<String,String> map = new HashMap<>();
    
    // 这里的map可以自己改变键值进行测试,体会动态sql
    map.put("id","df6b9f19d69f41d2bb217c45cbae1e0d");
    map.put("title","JavaScript");
    blogDao.updateBlog(map);
    sqlSession.close();
}

4、输出结果

在这里插入图片描述
5、小结:所谓的动态SQL,本质还是SQL语句,只是我们可以在SQL层面,去执行一个逻辑代码。

if where when choose set 等

12.5、foreach

动态 SQL 的另一个常见使用场景是对集合进行遍历(尤其是在构建 IN 条件语句的时候

<select id="select2" resultType="Province">
    select * from province where id in
    <!--
        foreach标签:用来遍历传递来的数组参数
        collection:标识传递参数的类型。array -> 数组  list -> 集合
        item:每一次遍历出来的元素,在使用该元素的时候,需要套用在#{}中
        open:拼接循环的开始符号
        close:拼接循环的结束符号
        separator:元素与元素之间的分隔符
    -->
    <!--例如:select * from province where id in(1,2,3,4); 可以写成如下代码-->
    <foreach collection="array" item="id" open="(" close=")" separator=",">
        #{id}
    </foreach>
</select>

foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。

你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象作为集合参数传递给 foreach。当使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。

案例实现:查询前3条博客信息

1、编写接口

// 把id封装成一个list集合
List<Blog> getBlogListByIds(List<String> idList);

2、编写xml映射文件

<select id="getBlogListByIds" parameterType="List" resultType="Blog">
    select * from mybatis.blog where id in
    <foreach collection="list" item="id" open="(" close=")" separator=",">
        #{id}
    </foreach>
</select>

3、测试

@Test
public void foreachTest(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    assert sqlSession != null;
    BlogDao blogDao = sqlSession.getMapper(BlogDao.class);

    // 保存前三个id
    List<String> ids = new ArrayList<>();
    ids.add("8b376d0ff8f741d4aaedf525b6a56f0b");
    ids.add("df6b9f19d69f41d2bb217c45cbae1e0d");
    ids.add("c33427a49f21433e85dfaa092e8b9bdb");

    List<Blog> blogList = blogDao.getBlogListByIds(ids);
    for(Blog blog : blogList){
    
    
        System.out.println(blog);
    }
    sqlSession.close();
}

4、查看输出结果

在这里插入图片描述

12.6、SQL片段

有的时候,我们可能会将一些功能的部分抽取出来,方便复用!

<!--
   使用SQL标签制作SQL片段
   SQL片段的作用是用来代替SQL语句中的代码
   如果你的mapper映射文件中的SQL语句某些代码出现了大量的重复,我们可以使用SQL片段来代替他们
   id属性:SQL片段的唯一标识,将来找到SQL片段使用id来进行定位

   将来的实际项目开发中,使用SQL片段用来代替重复率高,且复杂的子查询
-->
<sql id="sql1">
    select * from province
</sql>
<select id="select3" parameterType="int" resultType="Province">
    <include refid="sql1"/> where id = #{id};
</select>

注意事项:

  • 最好基于单表来定义SQL片段!
  • 不要存在where标签

13、缓存

13.1、缓存简介

1、什么是缓存(Cache )?

  • 存在内存中的临时数据。
  • 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题。

2、为什么使用缓存?

  • 减少和数据库的交互次数,减少系统开销,提高系统效率。

3、什么样的数据能使用缓存?

  • 经常查询并且不经常改变的数据。

13.2、Mybatis缓存

MyBatis包含一个非常强大的查询缓存特性,它可以非常方便地定制和配置缓存。缓存可以极大的提升查询效率。

MyBatis系统中默认定义了两级缓存:一级缓存二级缓存

  • 默认情况下,只有一级缓存开启。(SqlSession级别的缓存,也称为本地缓存)。二级缓存需要手动开启和配置,他是基于namespace级别的缓存。
  • 为了提高扩展性,MyBatis定义了缓存接口Cache。我们可以通过实现Cache接口来自定义二级缓存
  • Cache接口里的抽象方法

在这里插入图片描述

13.3、一级缓存(默认开启)

1、—级缓存也叫本地缓存 (SqlSession级别的缓存) :

  • 与数据库同一次会话期间查询到的数据会放在本地缓存中。
  • 以后如果需要获取相同的数据,直接从缓存中拿,没必须再去查询数据库;
@Test
public void getBlogById(){
    
    
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    assert sqlSession != null;
    BlogDao blogDao = sqlSession.getMapper(BlogDao.class);
    
    Blog blog1 = blogDao.getBlogById("8b376d0ff8f741d4aaedf525b6a56f0b");
    // 这里blog2不在从从数据里读取的,而是从缓存中读取,SQL也只执行了一次
    Blog blog2 = blogDao.getBlogById("8b376d0ff8f741d4aaedf525b6a56f0b");
    
    System.out.println("debug ==> " + (blog1 == blog2));
    
    sqlSession.close();
}

查看输出结果和日志!

在这里插入图片描述
2、缓存失效情况:

1、查询不同的东西

2、增删改操作,可能会改变原来的数据,所以必定会刷新缓存!

3、查询不同的Mapper.xml

4、手动清理缓存!

在这里插入图片描述
小结:一级缓存默认是开启的,只在一次SqlSession中有效,也就是拿到连接到关闭连接这个区间段!

13.4、二级缓存(手动开启)

官方地址:https://mybatis.org/mybatis-3/zh/sqlmap-xml.html#cache

1、二级缓存也叫全局缓存,一级缓存作用域太低了,所以诞生了二级缓存·基于namespace级别的缓存,一个名称空间,对应一个二级缓存;

2、工作机制

  • 一个会话查询一条数据,这个数据就会被放在当前会话的一级缓存中;
  • 如果当前会话关闭了,这个会话对应的一级缓存就没了;但是我们想要的是,会话关闭了,一级缓存中的数据被保存到二级缓存中;
  • 新的会话查询信息,就可以从二级缓存中获取内容;
  • 不同的mapper查出的数据会放在自己对应的缓存(map)中;

3、步骤

1.开启全局缓存(核心配置文件中)

<!--显示的开启全局缓存-->
<setting name="cacheEnabled" value="true"/>

2.在要使用二级缓存的Mapper映射文件中开启

<cache
  eviction="FIFO"
  flushInterval="60000"
  size="512"
  readOnly="true"/>
<!--这里自定义了参数,也可以不定义参数-->

3.编写测试方法

注意:如果二级缓存没有设置只读 (readOnly=“true”) 的属性,实体类必须设置成可序列化,否则会报错!

@Test
public void test(){
    
    
    SqlSession sqlSession1 = MyBatisUtil.getSqlSession();
    SqlSession sqlSession2 = MyBatisUtil.getSqlSession();

    assert sqlSession1 != null;
    BlogDao blogDao1 = sqlSession1.getMapper(BlogDao.class);
    Blog blog1 = blogDao1.getBlogById("8b376d0ff8f741d4aaedf525b6a56f0b");
    sqlSession1.close(); // 关闭会话


    assert sqlSession2 != null;
    BlogDao blogDao2 = sqlSession2.getMapper(BlogDao.class);
    Blog blog2 = blogDao2.getBlogById("8b376d0ff8f741d4aaedf525b6a56f0b");

    System.out.println("debug ==> " + (blog1 == blog2));
    sqlSession2.close();
}

4.查看输出结果和日志

在这里插入图片描述
小结:

  • 只要开启了二级缓存,在同一个Mapper下就有效
  • 所有的数据都会先放在一级缓存中;
  • 只有当会话提交,或者关闭的时候,才会提交到二级缓冲中!

13.5、缓存原理

在这里插入图片描述

13.6、自定义缓存-ehcahe

除了上述自定义缓存的方式,你也可以通过实现你自己的缓存,或为其他第三方缓存方案创建适配器,来完全覆盖缓存行为。

Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。

在我们的程序中使用:

1、导包

<!--ehcache-->
<!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
<dependency>
  <groupId>org.mybatis.caches</groupId>
  <artifactId>mybatis-ehcache</artifactId>
  <version>1.1.0</version>
</dependency>

2、编写配置文件

<?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>

3、在需要的Mapper映射文件中注册!

<!--开启第三方缓存,代替MyBatis默认的-->
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>

4、我们使用第三方缓存包,可以增加一些额外的功能!在特定场合比MyBatis默认的更加实用!但是此种自定义缓存了解即可,对于缓存,后面我们会使用Redis!Redis专为缓存而生,而且功能更加强大,性能更加优秀!

end

Thank you for watching!

end

猜你喜欢

转载自blog.csdn.net/m0_46357847/article/details/109592355