• 目录和依赖
    • 相关配置
    • 增删改查操作
    • 测试类:
    • 一些注意点

目录和依赖

  <dependencies><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.2</version></dependency><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>2.0.0-alpha1</version><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies><!--在build中配置resources,来防止我们资源导出失败的问题--><build><resources><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>true</filtering></resource><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>true</filtering></resource></resources></build>

数据表:

相关配置

1.mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!--引入外部配置文件--><properties resource="db.properties"/><settings><setting name="mapUnderscoreToCamelCase" value="true"/><setting name="cacheEnabled" value="true"/><setting name="logImpl" value="STDOUT_LOGGING"/></settings><typeAliases><typeAlias type="com.hou.pojo.User" alias="User"></typeAlias></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><!--绑定接口--><mappers><mapper class="com.hou.mapper.UserMapper"></mapper></mappers>
</configuration>

2.log4j.properties

log4j.rootLogger=debug, stdout, Rlog4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%nlog4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.loglog4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=5log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

3.ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"updateCheck="false"><!--diskStore:为缓存路径,ehcache分为内存和磁盘两级,此属性定义磁盘的缓存位置。参数解释如下:user.home – 用户主目录user.dir  – 用户当前工作目录java.io.tmpdir – 默认临时文件路径--><diskStore path="java.io.tmpdir/Tmp_EhCache"/><!--defaultCache:默认缓存策略,当ehcache找不到定义的缓存时,则使用这个缓存策略。只能定义一个。--><!--name:缓存名称。maxElementsInMemory:缓存最大数目maxElementsOnDisk:硬盘最大缓存个数。eternal:对象是否永久有效,一但设置了,timeout将不起作用。overflowToDisk:是否保存到磁盘,当系统当机时timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。clearOnFlush:内存数量最大时是否清除。memoryStoreEvictionPolicy:可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。FIFO,first in first out,这个是大家最熟的,先进先出。LFU, Less Frequently Used,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。LRU,Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。--><defaultCacheeternal="false"maxElementsInMemory="10000"overflowToDisk="false"diskPersistent="false"timeToIdleSeconds="1800"timeToLiveSeconds="259200"memoryStoreEvictionPolicy="LRU"/><!--    在mybatis中使用二级缓存--><cachename="cloud_user"eternal="false"maxElementsInMemory="5000"overflowToDisk="false"diskPersistent="false"timeToIdleSeconds="1800"timeToLiveSeconds="1800"memoryStoreEvictionPolicy="LRU"/></ehcache>

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3308/data1?userSSL=true&userUnicode=true&characterEncoding=UTF-8
username=heziyi
password=123456

mybatis配置:

//sqlSessionFactory --> sqlSession
public class MybatisUtils {private static SqlSessionFactory sqlSessionFactory;static {try {//使用mybatis第一步:获取sqlSessionFactory对象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();}}public static SqlSession getSqlSession(){return sqlSessionFactory.openSession(true);}}

增删改查操作

pojo:

@Data
public class User implements Serializable {private int id;private String name;private String pwd;public User(int id, String name, String pwd) {this.id = id;this.name = name;this.pwd = pwd;}
}

mapper和接口:

public interface UserMapper {User queryUserByid(@Param("id") int id);}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.hou.mapper.UserMapper"><cache type="org.mybatis.caches.ehcache.EhcacheCache"/><select id="queryUserByid" resultType="User">select * from data1.user where id = #{id}</select><update id="updateUser" parameterType="User">update data1.user set name=#{name}, pwd=#{pwd} where id=#{id}</update>
</mapper>

测试类:


public class MyTest {@Testpublic void addBlog(){SqlSession sqlSession = MybatisUtils.getSqlSession();SqlSession sqlSession1 = MybatisUtils.getSqlSession();UserMapper userMapper = sqlSession.getMapper(UserMapper.class);User user = userMapper.queryUserByid(1);System.out.println(user);sqlSession.close();UserMapper userMapper1 = sqlSession1.getMapper(UserMapper.class);User user1 = userMapper1.queryUserByid(1);System.out.println(user1);//User(id=1, name=你好, pwd=345678)System.out.println(user==user1);//trueUser u3 = userMapper1.queryUserByid(2);System.out.println(u3);sqlSession1.close();}}

运行结果:

一些注意点



【过程记录】springboot中使用EhcacheCache+mybatis相关推荐

  1. 在springboot中使用PageHelper(mybatis的分页插件) 以及自定义分页

    PageHelper是国内非常优秀的一款开源的mybatis分页插件,它支持基本主流与常用的数据库, 例如mysql. oracle.mariaDB. DB2. SQLite.Hsqldb等. 本项目 ...

  2. springboot中整合mybatis及简单使用

    springboot中整合mybatis及简单使用 1.引入依赖 2.在applicaiton.yaml中配置数据源以及mybatis 3.创建sql测试表 4.编写mapper接口和mapper.x ...

  3. SpringBoot中post请求报405错误排坑

    记一次排坑过程. SpringBoot中前端向后端发起post请求,页面提示405错误,方法不被支持. 然后尝试了一下get请求,是完全没问题的. 一开始以为是控制器配置错了,但仔细排查,控制器用的是 ...

  4. SpringBoot中关于Mybatis使用的三个问题

    SpringBoot中关于Mybatis使用的三个问题 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8495453.html 原本是要讲讲PostgreSQL ...

  5. 记录springBoot项目使用jar包中的mapper报错问题

    记录springBoot项目使用jar包中的mapper报错问题 背景 双数据源的实现 启动时报找不到对应的service 启动报mapper找不到 启动报no sqlsessionfactory 启 ...

  6. springboot 中 mybatis configuration 配置失效问题

    springboot 中 mybatis configuration 配置失效问题 环境 场景 springboot角度分析 SqlSessionFactory 设置Configuration Myb ...

  7. springboot中的mybatis是如果使用pagehelper的

    springboot中的mybatis是如果使用pagehelper的 springboot中使用其他组件都是基于自动配置的AutoConfiguration配置累的,pagehelper插件也是一样 ...

  8. springboot 项目输出 sql 到控制台、 SpringBoot 中 Mybatis 打印 sql

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. SpringBoot中Mybatis打印sql 如果使用的是 application.propert ...

  9. springboot中使用generator自动生成mybatis的接口、实体类和映射文件-springboot学习笔记

    在使用mybatis的时候,我们需要自己建立实体类.接口和对应的映射文件.一个字母写错就可能导致运行不起来,很是麻烦.使用generator可以帮助我们自动生成上述的文件. 首先,新建一个spring ...

最新文章

  1. “雪花”项目:Microsoft探索在.NET中实现手工内存管理
  2. python 并行计算 并行方法总结 concurrent.futures pp pathos multiprocessing multiprocess模块 总结对比
  3. 什么是 XML Web Service
  4. eclpse中不小心关掉一些窗口怎么办?
  5. 前端导出文件,后端返回文件流过大直接干崩溃
  6. 盘点程序猿经常说的那些行话,你了解吗?
  7. 「工具」IndexDB 版备忘录
  8. 计算机多媒体设计徽章,酷毙了:Hackaday将会议徽章设计成一台可编程的电脑
  9. 山石网科发布山石云·景产品 安全运维管理进入SaaS模式
  10. 【Linux】VMware连接CRT
  11. Win7/Win8/Win8.1下配置apache+php运行缓慢问题解决方法
  12. java读取配置文件properties_让我来告诉你Spring框架是怎么样通过properties来获得对象的?...
  13. C#递归拷贝文件夹下文件以及文件夹
  14. jQuery练手之滑动出现效果
  15. codevs 2928 你缺什么
  16. 第一次作业+105032014140
  17. A星算法优化(二)权重系数
  18. Ubuntu安装ssh服务详细过程
  19. android手机分辨率
  20. 布局改变时的过场动画

热门文章

  1. mysql超级_Mysql 超级快速入门
  2. ios弧形进度条_ios 圆形进度条
  3. 函数的傅立叶展开掐死我吧_《傅立叶的梦》第一章——天才少年Fejér的平均和一致收敛问题...
  4. 五、开始学习Excel函数,效率快速提高
  5. leetcode 刷题140 141
  6. pytorh 激活函数,循环神经网络层,损失函数和优化器
  7. 从4篇最新论文详解NLP新范式——Continuous Prompt
  8. 深度强化学习落地指南:弥合DRL算法原理和落地实践之间的断层 | 文末送书
  9. OAG – WhoIsWho 同名消歧竞赛发布 | 10万元奖金双赛道
  10. 仅剩3天 | 带学《百面机器学习》葫芦书,算法+leetcode一应俱全