mybatis源码深度解析

一.mybatis测试代码
  1. maven配置文件

       
       
           junit
           junit
           4.12
           test
       

       
       
           org.slf4j
           slf4j-api
           1.7.10
       
       
           ch.qos.logback
           logback-classic
           1.1.2
       
       
           ch.qos.logback
           logback-core
           1.1.2
       

       
       
           mysql
           mysql-connector-java
           8.0.17
       

       
       
           org.mybatis
           mybatis
           3.5.3
       

   	
   		cn.hutool
   		hutool-all
   		4.6.7
   	

       
   
  1. User实体类和UserMapper接口
public class User implements Serializable{

   private static final long serialVersionUID = 1L;

   private Integer id;

   private String userName;

   private String realName;

   private Byte sex;

   private String mobile;

   private String email;

   public Integer getId() {
   	return id;
   }

   public void setId(Integer id) {
   	this.id = id;
   }

   public String getUserName() {
   	return userName;
   }

   public void setUserName(String userName) {
   	this.userName = userName;
   }

   public String getRealName() {
   	return realName;
   }

   public void setRealName(String realName) {
   	this.realName = realName;
   }

   public Byte getSex() {
   	return sex;
   }

   public void setSex(Byte sex) {
   	this.sex = sex;
   }

   public String getMobile() {
   	return mobile;
   }

   public void setMobile(String mobile) {
   	this.mobile = mobile;
   }

   public String getEmail() {
   	return email;
   }

   public void setEmail(String email) {
   	this.email = email;
   }

   @Override
   public String toString() {
   	return "User [id=" + id + ", userName=" + userName + ", realName=" + realName + ", sex=" + sex + ", mobile="
   			+ mobile + ", email=" + email + "]";
   }

}
public interface UserMapper {

   @Select("select * from user where id = #{id}")
   User findById(Integer id);
   
   User findById1(Integer id);
}
  1. mybatis-config.xml配置文件




   

   
   	
       
       
   


   
   
       
       
           
           
               
               
               
               
           
       

   

   
   
       
   

 
  1. 测试类
package com.mybatis.lx;

import java.io.IOException;
import java.io.InputStream;

import javax.sql.DataSource;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.Before;
import org.junit.Test;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class MybatisTest {

   
   private SqlSessionFactory sqlSessionFactory;

   /****
    * 构建SqlSessionFactory
    * @throws IOException
    */
   @Before
   public void init() throws IOException {
       String resource = "mybatis-config.xml";
       InputStream inputStream = Resources.getResourceAsStream(resource);
       // 1.构建SqlSessionFactory
       sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
       inputStream.close();
   }


   /***
    * 
    */
   @Test
   public void quickStart1() {
       // 2.获取sqlSession
       SqlSession sqlSession = sqlSessionFactory.openSession();
       // 3.ִ调用selectOne方法获取返回值
       User user = sqlSession.selectOne("com.mybatis.lx.UserMapper.findById", 1);
       // 4.关闭session
       sqlSession.close();
      System.out.println(user.toString());
   }

   /***
    * mybatis一级缓存,sqlsession级别的缓存,不同的sqlSession不会共享缓存,数据本地缓存
    */
   @Test
   public void quickStart2() {
       // 2.获取sqlSession
       SqlSession sqlSession = sqlSessionFactory.openSession();
       // 3.获取mapper
       UserMapper mapper = sqlSession.getMapper(UserMapper.class);
       // 4.调用mapper接口方法
       System.out.println("第一次调查询》》》");
       User user = mapper.findById(1);
       
       System.out.println("第二次查询》》》");
       User user2 = mapper.findById(1);
       
       //5.关闭session
       sqlSession.close();
       
       
       System.out.println(user.toString());
   }
   
   /**
    * 测试mybatis二级缓存,二级缓存时多个sqlSession共享同一个缓存,是全局缓存,可以以外部的redis等工具作为存储缓存的对象,需要自己实现一个Cache接口
    */
   @Test
   public void quickStart4() {
       // 2.获取sqlSession
       SqlSession sqlSession = sqlSessionFactory.openSession();
       // 3.获取mapper
       UserMapper mapper = sqlSession.getMapper(UserMapper.class);
       // 4.调用mapper接口方法
       System.out.println("第一次调查询》》》");
   

你可能感兴趣的:(mybatis,mybatis,源码,缓存)