Mybatis-9.28(进阶)

Mybatis-9.28(进阶)

环境:

  • JDK1.8
  • Mysql5.7
  • maven 3.6.3
  • IDEA

进场须知(掌握):

  • JDBC
  • MySql
  • Java基础
  • Maven
  • Junit
  • JavaWeb基础

SSM框架:配置文件的。Spring SpringMVC Mybatis

1、Brief Introduction

1.1、What Is Mybatis

  • MyBatis 是一款优秀的持久层框架
  • 它支持自定义 SQL、存储过程以及高级映射。
  • MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
  • MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
  • MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。
  • 2013年11月迁移到Github

截止到2021.1.22 新版本是MyBatis 3.5.6 ,其发布时间是2020年10月6日。

如何获得Mybatis?

  • maven仓库

     <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
     </dependency>
    
  • github:https://github.com/mybatis/mybatis-3

  • 中文介绍:https://mybatis.org/mybatis-3/zh

1.2、持久化

数据持久层

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

为什么需要持久化?

钱不能丢,面子不可失

1.3、持久层

Dao层、Service层、Controller层…

  • 完成持久化的代码
  • 操作数据库负责业务的逻辑
1.4、Why Studay Mybatis?
  • 程序员可以较快将数据存入数据库中
  • 方便
  • 自动化,简介
  • jdbc就是低层,技术无贵贱之分
  • sql写在xml中
  • sql和代码完全分离

不断学习,不断提升

少说话,多办事

2、第一个Mybatis程序

准备工作:搭建环境–>导入Mybatis–>编写代码–>调试

2.1、环境搭建

新建一个普通的maven项目

删掉src目录

创建model

在父工程中配置依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.2</version>
</dependency>

在resource下创建一个名为 : 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>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

创建一个mybatis 工具类

import java.io.IOException;
import java.io.InputStream;
public class MybatisUtil {
    static{
        String resource = "org/mybatis/example/mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
}
package com.chen.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;

public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory=null;
    static{
        String resource = "org/mybatis/example/mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    //既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。例如:
    public static SqlSession getSqlSession(){
       return sqlSessionFactory.openSession();
    }
}

编写实体类

编写Dao层代码

public interface UserDao {
    public List<user> getUserList();
}

编写Dao层的实现类 但是这个是配置文件

<?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">
<!--implements UserDao的接口-->
<mapper namespace="com.chen.dao.UserDao">
<!-- id是方法名字   resultType返回的是该结果集的类型-->
    <select id="UserDao" resultType="com.chen.pojo.user">
        select * from Blog where id = #{id}
    </select>
</mapper>

此时我项目的目录为:

测试类

package com.chen.dao;
import com.chen.pojo.user;
import com.chen.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
    @Test
    public void test(){
// 获取SqlSession对象
        SqlSession sqlSession = MybatisUtil.getSqlSession();
//        面向接口接口编程使用使接口的对象指向实现类的对象  获取sql指令
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<user> userList = mapper.getUserList();
        for (user user : userList) {
            System.out.println(user.getName());
        }
       sqlSession.close();
    }
}

每一个Mapper.XML都需要在mybatis核心配置文件中注册

<mappers>
    <mapper resource="com/chen/dao/UserMapping.xml"></mapper>
</mappers>

然而惊喜却不断出现,如下:

解决办法: 约定大约配置,导出的时候maven可能会过滤一些文件,其中就包括xml文件,解决代码 在pom中添加如下代码

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>

        </resource>
    </resources>
</build>

测试文件

    public void test(){
// 获取SqlSession对象
        SqlSession sqlSession = MybatisUtil.getSqlSession();
//        面向接口接口编程使用使接口的对象指向实现类的对象  获取sql指令
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<user> userList = mapper.getUserList();
        for (user user : userList) {
            System.out.println(user.getName());
        }
        sqlSession.close();
    }

3、CRUD

根据id获取某一个用户

user getUserById(int id);

xml中的配置

  <select id="getUserById" resultType="com.chen.pojo.user" parameterType="int">
        select * from user where id=#{id};
    </select>

测试

    public void selectById(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
//        面向接口接口编程使用使接口的对象指向实现类的对象  获取sql指令
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        user user = mapper.getUserById(2);
        System.out.println(user);
        sqlSession.close();
    }

修改

 int update(user us);

xml配置

<update id="update" parameterType="com.chen.pojo.user">
        update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id};
    </update>

测试

    public void updateTest(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
//        面向接口接口编程使用使接口的对象指向实现类的对象  获取sql指令
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        int number = mapper.update(new user(1,"张三","34567"));
        if(number>0){
            System.out.println("更新成功!");
        }

        sqlSession.close();
    }

注意传递的参数还可以是map类型

  • 使用map类型可以灵活写参数,并且用new新建对象的时候,new()里面的参数必须和构造函中的参数一一对应,
  • 当表中的属性数据量多的时候,你就很难辨认出那个参数是相对应的了
    user getUserById2(Map<String,Object> map);

xml配置文件

   <select id="getUserById2" resultType="com.chen.pojo.user" parameterType="map">
        select * from user where id=#{mapid};
    </select>
    public void selsectMaptest(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        Map<String,Object> map=new HashMap();
        map.put("mapid",2);
        user user2 = mapper.getUserById2(map);
        System.out.println(user2);
    }

模糊查询

1.在java代码中直接加上“%%”

4、配置解析

1、核心配置文件
2、环境配置(environments)

尽管可以配置多个环境,但每个 SqlSessionFactory 实例只能选择一种环境。

学会配置使用多套运行环境!

Mybatis默认的事务管理就是JDBC

3、属性(properties)

这些属性可以在外部进行配置,并可以进行动态替换。你既可以在典型的 Java 属性文件中配置这些属性,也可以在 properties 元素的子元素中设置。例如:

编写一个配置文件 db.properties

driver:com.mysql.jdbc.Driver
url:jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username:root
password:root

在mybatis-config.xml配置文件中配置

<properties resource="db.properties">
</properties>
4、起别名

在mybatis-config.xml配置文件中配置

第一种方式

<typeAliases >
        <typeAlias alias="User" type="com.chen.pojo.user"/>
    </typeAliases>

第二种方式 —以包创建,包以下的类默认使用小写作为新名

<typeAliases>
  <package name="com.chen.pojo"/>
</typeAliases>

在实体类比较少的时候,可以使用第一种方式

在实体类较多的时候选第二种,第二种想要自定义别名的话,就在class的实体类上添加注解@Alias("");即可

5、设置

6、映射器(mappers)

注册mapper文件

第一种方式

<!-- 使用相对于类路径的资源引用 -->
<mappers>
  <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
</mappers>

第二种方式

<!-- 使用映射器接口实现类的完全限定类名 -->
<mappers>
  <mapper class="org.mybatis.builder.AuthorMapper"/>
</mappers>

第三种方式

<!-- 将包内的映射器接口实现全部注册为映射器 -->
<mappers>
  <package name="org.mybatis.builder"/>
</mappers>

注意:第二种方式和第三种方式均需要将xml实现接口的配置文件和接口类同名,且必须在同一个包下。

7、生命周期,作用域

作用域和生命周期类别是至关重要的,因为错误的使用会导致非常严重的并发问题。

SqlSessionFactoryBuilder

局部变量,创建完就会释放

SqlSessionFactory
  • 数据库连接池

  • 应用的运行期间一直存在

SqlSession
  • 每个线程都应该有它自己的 SqlSession 实例
  • 他们正在使用连接池
  • 用完需要关闭
  • 知足常乐

5、解决属性名和字段名不一致的问题

数据库中的字段

实体类中的字段

public class user {
    private int id;
    private String name;
    private String password;
}

测试出现问题

password=null

解决办法

①起别名 pwd as password

resultMap

 <resultMap id="UserMap" type="user">
        <result column="pwd" property="password"></result>
    </resultMap>
    <select id="getUserList" resultType="com.chen.pojo.user" resultMap="UserMap">
        select * from user
    </select>

6.日志

6.1、日志工厂

日志是排错的最好用的工具

曾经:sout、debug

  • SLF4J
  • Apache Commons Logging
  • Log4j 2
  • Log4j
  • JDK logging
  • COMMONS_LOGGING
  • STDOUT_LOGGING 标准的日志实现

在mybatis中配置核心日志文件

    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
6.2、Log4j

什么是Log4j?

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

1、先导入log4j依赖

<!-- 加入log4j支持 -->

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.17</version>

</dependency>

2、配置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/kuang.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
6.3、Log4J配置
<settings>
        <setting name="logImpl" value="Log4j"/>
    </settings>
6.4、Log4J使用

日志级别

info()

debug()

error()

7分页

使用Mybatis实现分页,核心是SQL

1.接口

//     实现分页
     List<user> limitUser(Map<String,Object> map);

2.mapping

   <select id="limitUser" parameterType="map" resultType="com.chen.pojo.user">
        select * from user limit #{startIndex},#{pageSize}
    </select>

3.test

    public void testLimitMap(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        Map<String, Object> map = new HashMap<>();
        map.put("startIndex",0);
        map.put("pageSize",2);
        List<user> users = mapper.limitUser(map);
        for (user user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }

8.使用注解开发

8.1面向接口编程

解耦

越抽象越稳定

本质:使用反射机制,反射获取注解

注解在接口上实现

8.2、CRUD

我们可以在工具类创建实现自动提交事务!

public static SqlSession getSqlSession(){
       return sqlSessionFactory.openSession(true);
    }

编写接口增加注解

要是方法的参数是基本类型,有多个基本类型,在每个参数之前加上@param()

9、Lombok

可以省去实体类的编写

使用方式:

1.File—>Setting—>Plugins 下载lombok插件

2.导入依赖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>

所有的注解

@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

最常用的

@Data :无参构造,get,set,toString,hashcode,equals

@AllArgsConstructor   有参数构造方法
@NoArgsConstructor		无参构造

10、多对一

多个学生对应一个老师,在学生表中建立一个外键与老师的主键相关联即可实现

数据库环境

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=utf8INSERT 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

3.建立Mapper接口

4.建立Mapper.xml文件

5.在核心配置文件中绑定我们注册的Mapper接口或者文件!

6.测试查询是否成功

都是使用association ----------对象

多对一的student 中的老师设置成 Teacher 类型

第一种方式

①按照查询嵌套处理------子查询

<select id="getAllStudent" resultMap="StudentTeacher">
    select * from student
</select>

<resultMap id="StudentTeacher" type="com.chen.pojo.Student">
    <association property="teacher" column="tid" javaType="com.chen.pojo.Teacher" select="getAllTeacher"></association>
</resultMap>
<select id="getAllTeacher" resultType="com.chen.pojo.Teacher">
    select * from teacher where id=#{id}
</select>

②按照结果嵌套处理

    <select id="getAllStudent2" resultMap="StudentTeacher2">
        select a.id sid,a.name sname,b.name tname from student a,teacher b where a.tid=b.id;
    </select>
    <resultMap id="StudentTeacher2" type="Student">
        <result property="id" column="sid"></result>
        <result property="name" column="sname"></result>
        <association property="teacher" javaType="Teacher" >
            <result property="name" column="tname"></result>
        </association>
    </resultMap>

11、一对多

一个老师对应多个学生

pojo类

package com.chen.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Teacher {
    private int id;
    private String name;
    private List<Student> student;
}

dao类

Teacher getTeacher(@Param("tid") int i);

xml配置文件

<select id="getTeacher" resultMap="teacher" parameterType="_int">
    select a.id sid,a.name sname,b.id tid,b.name tname from student a,teacher b
    where a.tid=b.id and b.id=#{tid}
</select>
    <resultMap id="teacher" type="Teacher">
        <result column="tid" property="id"></result>
        <result property="name" column="tname"></result>
<!--        集合使用collection-->
        <collection property="student" ofType="Student">
            <result property="id" column="sid"></result>
            <result property="name" column="sname"></result>
            <result property="tid" column="tid"></result>
        </collection>
    </resultMap>

12、动态SQL

什么是动态SQL:动态SQl就是根据不同的条件从而来进行查询

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

if的应用-------------------根据不同的条件进行查询

可以根据if来动态传入参数,从而执行不同情况的SQL

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

when

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>

trim (where, set)

加一个where 标签可以很智能的增删and 什么都不传,where会被抹去

<select id="selectBlog" parameterType="map" resultType="Blog">
    select * from blog 
 <where>  
    <if test="title != null">
        title = #{title}
    </if>
     <if test="title != null">
        and title = #{title}
    </if>
     </where> 
</select>

set

<update id="updateAuthorIfNecessary">
  update Author
    <set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio}</if>
    </set>
  where id=#{id}
</update>

代码片段

可以把公共的SQL部分抽离出来,抽离出来的变量为

    <sql id="if-title">
        <if test="title != null">
            and title = #{title}
        </if>
    </sql>

引用的时候使用include

    <select id="selectBlog" parameterType="map" resultType="Blog">
        select * from blog where 1=1
       <include refid="if-title"></include>
    </select>

ForEach

需求类似: (id=1 or id =2 or id=3)

动态SQl就是在拼接SQl语句,保准SQl正确性加以逻辑即OK

13、缓存

什么是缓存?

  • 存放在内存中的临时数据
  • 将用户经常访问的数据放在缓存中,从缓存中查询,解决了高并发系统的性能问题

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

  • 经常查询且不经常改变的数据。
2.Mybatis缓存
  • Mybatis包含一个非常强大的查询缓存特性,它可以非常方便的定制和配置缓存,缓存可以极大地提升查询效率。
  • Mybatis默认定义了两极缓存:一级缓存和二级缓存
    • 默认只有一级缓存开启,(SqlSession级别的缓存,本地缓存)
    • 二级缓存需要手动开启,基于namespace级别
    • 可以自定义实现Cache缓存
3.一级缓存

在sqlsession关闭之前有效

失效的情况:

  • 增删改会刷新缓存
  • 查询不同的东西
  • 查询不同的Mapper.xml
  • 手动清除
4.二级缓存
  • 二级缓存称为全局缓存
  • 工作机制
    • 一级缓存失效后会传给二级缓存
    • 新的会话查询信息,就可以从二级缓存中获取内容
    • 不同的Mapper查询的数据会放在自己对应的缓存(map)中

步骤:

1.开启全局缓存

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

2.在要使用二级缓存中的mapper开启

  <cache/> 
<!-- 或者加一些配置-->

3.测试

​ 1.问题:我们需要将实体类序列化!

​ 解决办法:

​ 或者:实体类实现 Serializable

5.缓存原理

14、自定义缓存(Ehcache)

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

导入依赖

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

在mapper中使用自定义缓存

<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>

ehcache.xml文件

<ehcache>
 
     <!--
         磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
         path:指定在硬盘上存储对象的路径
      -->
     <diskStore path="java.io.tmpdir" />
 
     <!--
         defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
         maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
         eternal:代表对象是否永不过期
         timeToIdleSeconds:最大的发呆时间
         timeToLiveSeconds:最大的存活时间
         overflowToDisk:是否允许对象被写入到磁盘
      -->
     <defaultCache maxElementsInMemory="10000" eternal="false"
         timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
 
     <!--
         cache:为指定名称的对象进行缓存的特殊配置
         name:指定对象的完整名
      -->
     <cache name="com.zbaccp.entity.Person" maxElementsInMemory="10000" eternal="false"
         timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />
 
 </ehcache>

This is my Git ➡️ https://88888888.com

关于我

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值