mybatis 整合 mybatis-redis

 

1.pom文件


		
			org.mybatis
			mybatis
			3.4.1
		

		
		
			mysql
			mysql-connector-java
			6.0.6
		
		
		
			org.mybatis.caches
			mybatis-redis
			1.0.0-beta2
		

		
		
			log4j
			log4j
			1.2.17
		

		
		
			com.alibaba
			fastjson
			1.2.46
		

	

2.模型

package com.testmybatis.model;

import java.io.Serializable;

public class Test implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int id;
	private String name;

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String toString(){
		return "id:"+id+" name:"+name;
	}

}

3.dao

package com.testmybatis.dao;

import java.util.List;

import com.testmybatis.model.Test;


public interface TestMapper {
	public List test();
}

4.配置文件

TestMapper.xml





	

	

 mybatis-config.xml





	
		
			
			
				
				
				
				
			
		
	
	
	
		
	
	

/src/main/resources/redis.properties

redis.host=localhost
redis.port=6379
redis.connectionTimeout=5000
redis.soTimeout=5000
redis.password=
redis.database=0
redis.clientName=

 

5.测试

package com.testmybatis;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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 org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.testmybatis.dao.TestMapper;
import com.testmybatis.model.Test;

public class testlanjie {
	
	private static Logger log=Logger.getLogger(testlanjie.class);
	
	public static void main(String args[]){
		
		try {
			String resource = "com/testmybatis/mybatis-config.xml";
			InputStream inputStream = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			SqlSession session = sqlSessionFactory.openSession();
			try {
				TestMapper mapper=session.getMapper(TestMapper.class);
				List tests=mapper.test();
				session.commit();
				log.info(JSON.toJSONString(tests));
				
			} finally {
				session.close();
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}

第一次结果

2018-09-05 17:15:38 DEBUG [org.apache.ibatis.logging.LogFactory] Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
2018-09-05 17:15:39 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] PooledDataSource forcefully closed/removed all connections.
2018-09-05 17:15:39 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] PooledDataSource forcefully closed/removed all connections.
2018-09-05 17:15:39 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] PooledDataSource forcefully closed/removed all connections.
2018-09-05 17:15:39 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] PooledDataSource forcefully closed/removed all connections.
2018-09-05 17:15:39 DEBUG [com.testmybatis.dao.TestMapper] Cache Hit Ratio [com.testmybatis.dao.TestMapper]: 0.0
2018-09-05 17:15:39 DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] Opening JDBC Connection
2018-09-05 17:15:39 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] Created connection 680576081.
2018-09-05 17:15:39 DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2890c451]
2018-09-05 17:15:39 DEBUG [com.testmybatis.dao.TestMapper.test] ==>  Preparing: select * from test 
2018-09-05 17:15:39 DEBUG [com.testmybatis.dao.TestMapper.test] ==> Parameters: 
2018-09-05 17:15:39 DEBUG [com.testmybatis.dao.TestMapper.test] <==      Total: 4
2018-09-05 17:15:39 INFO [com.testmybatis.testlanjie] [{"id":1,"name":"adb"},{"id":2,"name":"dafdsa"},{"id":3,"name":"dafa"},{"id":4,"name":"fffff"}]
2018-09-05 17:15:39 DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2890c451]
2018-09-05 17:15:39 DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2890c451]
2018-09-05 17:15:39 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] Returned connection 680576081 to pool.

第二次

2018-09-05 17:16:08 DEBUG [org.apache.ibatis.logging.LogFactory] Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
2018-09-05 17:16:08 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] PooledDataSource forcefully closed/removed all connections.
2018-09-05 17:16:08 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] PooledDataSource forcefully closed/removed all connections.
2018-09-05 17:16:08 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] PooledDataSource forcefully closed/removed all connections.
2018-09-05 17:16:08 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] PooledDataSource forcefully closed/removed all connections.
2018-09-05 17:16:08 DEBUG [com.testmybatis.dao.TestMapper] Cache Hit Ratio [com.testmybatis.dao.TestMapper]: 1.0
2018-09-05 17:16:09 INFO [com.testmybatis.testlanjie] [{"id":1,"name":"adb"},{"id":2,"name":"dafdsa"},{"id":3,"name":"dafa"},{"id":4,"name":"fffff"}]

在redis中查看

127.0.0.1:6379> keys *
1) "com.testmybatis.dao.TestMapper"
127.0.0.1:6379> dump com.testmybatis.dao.TestMapper
"\x04\x01@f-1239684800:2518816703:com.testmybatis.dao.TestM47:select * from test:development\xc3@\xb4@\xc6\x1f\xac\xeda.util.ArrayListx\x81\xd2\x1d\x99\x14\xc7a\x9d\x03\x00\x01I0\x00\x04w\x04@\x05 4\x17\x1acom.testmybatis.model.T \x11 !\x02I\x00\x02idL\x00\x04namet\x00\x12L@i\x0c/lang/String;`Uq\x00~\x00\x02 :\t\x02t\x00\x06dafdsa\xe0\x00\x12\x03\x03t\x10\x04\x04t\x00\x05f \x00\x01fx\x06\x00\xea[W\xfal\xcblQ"

 

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