1.com.dao(shopMapper)
public interface ShopMapper {
List<Shop> cache();
void delete();
}
<mapper namespace="com.dao.ShopMapper">
<!-- 开启二级缓存 -->
<cache/>
<select id="cache" resultType="shop">
select * from shop
</select>
<delete id="delete">
delete from shop where shopid=100
</delete>
</mapper>
2.com.pojo(cshop)
public class Shop implements Serializable {
private static final long serialVersionUID = 1L;
private String shopId;
private String shopName;
private String shopAddress;
private String contact;
public String getShopId() {
return shopId;
}
public void setShopId(String shopId) {
this.shopId = shopId;
}
public String getShopName() {
return shopName;
}
public void setShopName(String shopName) {
this.shopName = shopName;
}
public String getShopAddress() {
return shopAddress;
}
public void setShopAddress(String shopAddress) {
this.shopAddress = shopAddress;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
}
3.com.service(ShopService)
public class ShopService {
// 测试一级缓存,必须为同一个SqlSession对象
public List<Shop> cache1() {
SqlSession session = null;
try {
session = MyBatisUtils.getSqlSession();
ShopMapper mapper = session.getMapper(ShopMapper.class);
// 首次查询发出sql
List<Shop> list = mapper.cache();
session.commit(); // 执行commit或rollback操作将会清空一级缓存
// 若未执行commit或rollback操作将不会发出sql,从一级缓存中查找数据,否则发出
list = mapper.cache();
return list;
} finally {
MyBatisUtils.close(session);
}
}
// 测试二级缓存,必须为同一个Mapper对象
public List<Shop> cache2() {
SqlSession session = null;
try {
session = MyBatisUtils.getSqlSession();
ShopMapper mapper = session.getMapper(ShopMapper.class);
return mapper.cache();
} finally {
MyBatisUtils.close(session);
}
}
public void clearCache() {
SqlSession session = null;
try {
session = MyBatisUtils.getSqlSession();
ShopMapper mapper = session.getMapper(ShopMapper.class);
mapper.delete();
session.commit(); // 执行commit操作将会清空二级缓存
} finally {
MyBatisUtils.close(session);
}
}
}
4.com.test(TestCrud)
public class TestCrud {
private ShopService service = new ShopService();
@Test
public void testCache1() {
List<Shop> list = service.cache1();
for(Shop shop : list) {
String shopId = shop.getShopId();
String shopName = shop.getShopName();
String shopAddress = shop.getShopAddress();
String contact = shop.getContact();
System.out.println(shopId + "-" + shopName + "-" + shopAddress + "-" + contact);
}
}
@Test
public void testCache2() {
// 首次查询发出sql
List<Shop> list = service.cache2();
// 执行commit操作将会清空二级缓存
service.clearCache();
// 若未执行commit操作将不会发出sql,从二级缓存中查找数据,否则发出
list = service.cache2();
for(Shop shop : list) {
String shopId = shop.getShopId();
String shopName = shop.getShopName();
String shopAddress = shop.getShopAddress();
String contact = shop.getContact();
System.out.println(shopId + "-" + shopName + "-" + shopAddress + "-" + contact);
}
}
}
5.com.util(MyBatisUtil)
public final class MyBatisUtil {
private static SqlSessionFactory factory = buildSqlSessionFactory();
private MyBatisUtil() {}
private static final SqlSessionFactory buildSqlSessionFactory() {
String resource = "resources/mybatis-config.xml";
try {
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static final SqlSession getSqlSession() {
return factory.openSession();
}
public static final void close(SqlSession sqlSession) {
if(sqlSession != null) {
sqlSession.close();
}
}
}
6.resources(db.properties+mybatis-config.xml)
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=meitao
jdbc.password=123
___________________________________________
<configuration>
<!-- 外部属性文件 -->
<properties resource="resources/db.properties"/>
<settings>
<!-- 控制台显示sql语句(开发使用) -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- <mapper resource="resources/ShopMapper.xml"/> -->
<!-- 注册指定包中的所有映射器接口(推荐) -->
<package name="com.dao"/>
</mappers>
</configuration>