|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2014 [email protected] |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.github.abel533.test.country; |
| 26 | + |
| 27 | +import com.github.abel533.mapper.CachedCountryMapper; |
| 28 | +import com.github.abel533.mapper.MybatisHelper; |
| 29 | +import com.github.abel533.model.Country; |
| 30 | +import org.apache.ibatis.session.SqlSession; |
| 31 | +import org.junit.Assert; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +/** |
| 35 | + * 通过实体类属性进行查询 |
| 36 | + * |
| 37 | + * @author liuzh |
| 38 | + */ |
| 39 | +public class TestCache { |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testCache() { |
| 43 | + SqlSession sqlSession = MybatisHelper.getSqlSession(); |
| 44 | + try { |
| 45 | + CachedCountryMapper mapper = sqlSession.getMapper(CachedCountryMapper.class); |
| 46 | + Country country = new Country(); |
| 47 | + country.setCountrycode("CN"); |
| 48 | + //第一次查询,会被缓存 |
| 49 | + country = mapper.selectOne(country); |
| 50 | + Assert.assertEquals(true, country.getId() == 35); |
| 51 | + Assert.assertEquals("China", country.getCountryname()); |
| 52 | + //只有close才会真正缓存,而不是用一级缓存 |
| 53 | + sqlSession.close(); |
| 54 | + |
| 55 | + //====================================================================== |
| 56 | + sqlSession = MybatisHelper.getSqlSession(); |
| 57 | + mapper = sqlSession.getMapper(CachedCountryMapper.class); |
| 58 | + country = new Country(); |
| 59 | + country.setCountrycode("CN"); |
| 60 | + //第二次查询,会使用第一次的缓存 |
| 61 | + country = mapper.selectOne(country); |
| 62 | + |
| 63 | + Assert.assertEquals(true, country.getId() == 35); |
| 64 | + Assert.assertEquals("China", country.getCountryname()); |
| 65 | + //只有close才会真正缓存,而不是用一级缓存 |
| 66 | + sqlSession.close(); |
| 67 | + |
| 68 | + //====================================================================== |
| 69 | + sqlSession = MybatisHelper.getSqlSession(); |
| 70 | + mapper = sqlSession.getMapper(CachedCountryMapper.class); |
| 71 | + |
| 72 | + country = new Country(); |
| 73 | + country.setCountryname("天朝"); |
| 74 | + country.setId(35); |
| 75 | + //更新操作会清空缓存 |
| 76 | + int result = mapper.updateByPrimaryKeySelective(country); |
| 77 | + Assert.assertEquals(1, result); |
| 78 | + sqlSession.commit(); |
| 79 | + //只有close才会真正缓存,而不是用一级缓存 |
| 80 | + sqlSession.close(); |
| 81 | + |
| 82 | + //====================================================================== |
| 83 | + sqlSession = MybatisHelper.getSqlSession(); |
| 84 | + mapper = sqlSession.getMapper(CachedCountryMapper.class); |
| 85 | + country = new Country(); |
| 86 | + country.setCountrycode("CN"); |
| 87 | + //第三次查询,会重新查询,不使用缓存 |
| 88 | + country = mapper.selectOne(country); |
| 89 | + |
| 90 | + Assert.assertEquals(true, country.getId() == 35); |
| 91 | + Assert.assertEquals("天朝", country.getCountryname()); |
| 92 | + |
| 93 | + country = new Country(); |
| 94 | + country.setCountryname("China"); |
| 95 | + country.setId(35); |
| 96 | + //还原 |
| 97 | + result = mapper.updateByPrimaryKeySelective(country); |
| 98 | + sqlSession.commit(); |
| 99 | + Assert.assertEquals(1, result); |
| 100 | + } finally { |
| 101 | + sqlSession.close(); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments