Spring,Mybatis 整合Memcache
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。
安装Memcached:
我在本机安装测用的(windows7 64位) 官网好像没有windows的 附带下载地址http://www.runoob.com/Memcached/window-install-memcached.html 上面也有详细的安装教程。照着来就行、
安装成功开始用了。整合开始:
Memcached配置文件
#设置服务器地址
memcached.server=127.0.0.1:11211
#过期时间
memcached.expiration=3600
#容错
memcached.failOver=true
#设置初始连接数
memcached.initConn=20
#设置最小连接数
memcached.minConn=10
#设置最大连接数
memcached.maxConn=50
#设置连接池维护线程的睡眠时间
memcached.maintSleep=3000
#设置是否使用Nagle算法(Socket的参数),如果是true在写数据时不缓冲,立即发送出去
memcached.nagle=false
#设置socket的读取等待超时时间
memcached.socketTO=3000
#设置连接心跳监测开关
memcached.aliveCheck=true
Spring 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 自动扫描 -->
<context:component-scan base-package="com.lin.smmem" /> <!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- 数据库配置文件 -->
<value>classpath*:/application.properties</value>
</list>
</property>
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/lin/smmem/mapping/*.xml"></property>
<property name="typeAliasesPackage" value="com.lin.smmem.pojo"/>
<!-- Mybatis设置属性-->
<property name="configLocation" value="classpath:mybatisConfiguration.xml"/>
</bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lin.smmem.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <tx:annotation-driven /> </beans>
mybatis设置属性配置文件 mybatisConfiguration.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL MAP Config 3.1//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="callSettersOnNulls" value="true"/>
</settings>
</configuration>
OK.接下来写个简单的映射文件:UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.lin.smmem.dao.IUserDao" >
<!-- 重点主要这里。 -->
<cache type="org.mybatis.caches.memcached.MemcachedCache" />
<cache type="org.mybatis.caches.memcached.LoggingMemcachedCache" /> <resultMap id="BaseResultMap" type="User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
</resultMap> <sql id="Base_Column_List">
id, user_name, password, age
</sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" useCache="true">
select
<include refid="Base_Column_List" />
from user.user_t
where id = #{id,jdbcType=INTEGER}
</select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user.user_t
where id = #{id,jdbcType=INTEGER}
</delete> <!-- 插入 -->
<insert id="insert" parameterType="com.lin.smmem.pojo.User" >
insert into user.user_t
(
id,
user_name,
password,
age
)
values
(
#{id,jdbcType=INTEGER},
#{userName,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER}
)
</insert> <update id="updateByPrimaryKey" parameterType="com.lin.smmem.pojo.User" >
update user.user_t
set user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update> </mapper>
官方放出了mybatis和memcached的整合包。我们直接用就行。超级简单。在pom.xml加入
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-memcached</artifactId>
<version>1.0.0</version>
</dependency>
只要在mapper中引入<cache type="org.mybatis.caches.memcached.MemcachedCache" />就可以用了。
最后自己写DAO Service 就可以测试了。
Spring,Mybatis 整合Memcache的更多相关文章
- 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)
前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...
- Spring + mybatis整合方案总结 结合实例应用
Spring + mybatis整合实例应用 项目结构图 (Spring3.0.2 +mybatis3.0.4) 方案一: 通过配置文件整合Spring和mybatis 应用数据库 -- --数据库 ...
- SpringMVC+Spring+Mybatis整合
SpringMVC+Spring+Mybatis整合 导包 配置jdbc.properties.log4j.properties jdbc.driver=com.mysql.jdbc.Driver j ...
- SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。
SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...
- Spring+Mybatis整合时 Failed to read candidate component class,Caused by:IllegalArgumentException
Spring+Mybatis整合时Caused by: java.lang.IllegalArgumentException错误 org.springframework.beans.factory.B ...
- Springmvc+Spring+Mybatis整合开发(架构搭建)
Springmvc+Spring+Mybatis整合开发(架构搭建) 0.项目结构 Springmvc:web层 Spring:对象的容器 Mybatis:数据库持久化操作 1.导入所有需要的jar包 ...
- struts2 spring mybatis 整合(test)
这几天搭了个spring+struts2+mybatis的架子,练练手,顺便熟悉熟悉struts2. 环境:myEclipse10+tomcat7+jdk1.6(1.8的jre报错,所以换成了1.6) ...
- 2.springMVC+spring+Mybatis整合
前面已经说了,springMVC+spring+Mybatis的整合思路了,现在就照着这个思路来做一下: 在开始之前先来看一下工程的目录结构: config这个目录底下放的都是配置文件: mybati ...
- springMVC + Spring + MyBatis 整合
整理下SSM(基于注解)的整合 1. web.xml 配置文件 <?xml version="1.0" encoding="UTF-8"?> < ...
随机推荐
- Eclipse使用Maven创建web3.0项目
安装Maven插件 这一步不细说了,自己下载的Eclipse-JAVA EE 版已自带 Maven插件 开始创建 文本1New一个 Maven Web App项目:File-->New--> ...
- spring 定时任务@Scheduled
1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...
- 理解 neutron(15):Neutron linux-bridge-agent 创建 linux bridge 的简要过程
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- 升级Ubuntu 16.04 LTS后 DSL拨号上网(ppp)连接自动断开解决办法
原本在Ubuntu 15.10用拨号上网没有问题,但升级了16.04 LTS后发现原来的DSL连接不上了.主要表现为: 1.在NetworkManager里面选择DSL Connection能够尝试拨 ...
- hdu1452 Happy 2004(规律+因子和+积性函数)
Happy 2004 题意:s为2004^x的因子和,求s%29. (题于文末) 知识点: 素因子分解:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en 因子 ...
- BZOJ 1984: 月下“毛景树” [树链剖分 边权]
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1728 Solved: 531[Submit][Status][Discu ...
- [AR]高通Vuforia Getting Started
Vuforia Getting Started 简介 Vuforia创建增强现实应用程序是一个软件平台.开发人员可以轻松地将先进的计算机视觉功能添加到任何应用程序中,允许它识别图像和对象,或在现实 ...
- kettle中含有参数传递的定时任务
(1)新建一个作业(新建->作业),并在控制面板右键: (2)设置一个命令参数: (3)把作业的参数传递给转换: (4)在转换中右键设置转换属性: (5)接收作业中设置的传递参数: (6)参数的 ...
- Ettercap几种好玩的姿势
0x00 简单的arp欺骗 命令 ettercap -i 网卡 -Tq -M arp:remote /受害者ip/ /网关ip/ 以下是我欺骗192.168.1.102主机并模拟http-get请 ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...