MyBatis-09-缓存

Mybatis的最后一部分啦,学完这个就可以搞Spring了,接下来我会慢慢更新Spring5,最近在准备面试,更新较慢,谅解一哈~~

目录结构:

MyBatis-09-缓存_第1张图片

pom.xml



    
        mybatis-study
        org.kuang
        1.0-SNAPSHOT
    
    4.0.0

    mybatis-09

    
        
        
            org.projectlombok
            lombok
            1.18.10
        
        
        
            org.mybatis.caches
            mybatis-ehcache
            1.1.0
        
    

User

package com.kuang.pojo;

import lombok.Data;

import java.io.Serializable;

@Data
public class User implements Serializable {
    private int id;
    private String name;
    private String pwd;
}

UserMapper

package com.kuang.dao;

import com.kuang.pojo.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserMapper {

    //根据id查询用户
    User queryUserByID(@Param("id") int id);
}

UserMapper.xml




    
    


    

MybatisUtils

package com.kuang.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;
    static {
        try {
            //获取使用Mybatis第一步SqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

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

MyCache

package com.kuang.utils;

import org.apache.ibatis.cache.Cache;

public class MyCache implements Cache {
    public String getId() {
        return null;
    }

    public void putObject(Object o, Object o1) {

    }

    public Object getObject(Object o) {
        return null;
    }

    public Object removeObject(Object o) {
        return null;
    }

    public void clear() {

    }

    public int getSize() {
        return 0;
    }
}

db.properties

driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis?useSSL=true&usUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&serverTimezone=UTC
username = root
password = lgz0921

ehcache



    
    

    

    
    
    

mybatis-config.xml






    
    

    
        
        
        
        
    

    
        
    

    
        
            
            
                
                
                
                
            
        
    
    
        
    

MyTest

import com.kuang.dao.UserMapper;
import com.kuang.pojo.User;
import com.kuang.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class MyTest {
    @Test
    public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        SqlSession sqlSession2 = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);

        User user = mapper.queryUserByID(1);
        System.out.println(user);
        sqlSession.close();

        User user1 = mapper2.queryUserByID(1);
        System.out.println(user1);

        System.out.println(user==user1);
        sqlSession2.close();
    }
}

 

你可能感兴趣的:(MyBatis,mybatis)