当前位置 : 主页 > 编程语言 > c++ >

mybaties的二级缓存

来源:互联网 收集:自由互联 发布时间:2021-07-03
UserInfoMapper u.*,r.id as "rinfo.id",r.name as "rinfo.name" select * from userinfo; select * from userinfo where id=#{id} select * from userinfo where name=#{name} and pwd=#{pwd} select seq2.nextval from dual insert into userinfo values#{i
UserInfoMapper
 

 
	
 
  
 
  	
  
  	
      
  	
  
  		
   
    
    
    
    
    
    
     
     
    
  
  		
		
  
		
  
    u.*,r.id as "rinfo.id",r.name as "rinfo.name" 
   
		
		 	
  	
  
  	
  
  	
  
  	
  
  	
  	
  
  	
  	
  
  	
  	
  
  	
  
  	
  	
  
  	
  
  	
   
    
     select seq2.nextval from dual 
    insert into userinfo values#{id},#{name},#{pwd},#{roleid} 
  
  	
  	
  
  	
  	
  
    update userinfo
    
    
      name=#{name}, 
     
    
      pwd=#{pwd}, 
     
    
      roleid=#{roleid} 
     
    where id=0 
  
  
 
test
package pidan.test;

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

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 pidan.dao.UserInfoDao;

public class Test {
	static SqlSessionFactory sqlSessionFactory;
	public static void main(String[] args) throws Exception {
		SqlSession session1=getSqlSession();
		
		//二级缓存
		UserInfoDao dao1=session1.getMapper(UserInfoDao.class);
		System.out.println(dao1.getUserById(1005));
		
		Thread.sleep(15000);
		session1.close();
		
		SqlSession session2=getSqlSession();
		UserInfoDao dao2=session2.getMapper(UserInfoDao.class);
		System.out.println(dao2.getUserById(1005));
	}
	
	static{
		
		try {
			String resource="mybatis.xml";
			InputStream inputStream=Resources.getResourceAsStream(resource);
			sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);	
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	//SqlSession不能使用单例,sqlSessionFactory可以使用单例
	public static SqlSession getSqlSession() {
		return sqlSessionFactory.openSession();
	}
}
网友评论