counchbase springmvc maven (nosql)
程序员文章站
2022-07-14 08:42:46
...
package com.cmcc.couchbase; import java.net.URI; import java.util.ArrayList; import com.couchbase.client.CouchbaseConnectionFactory; public class CouchbaseConnector { private String serverAddress; private String name; private String pwd; public CouchbaseConnectionFactory connect() { CouchbaseConnectionFactory cf = null; try { String[] serverNames = serverAddress.split(","); ArrayList<URI> serverList = new ArrayList<URI>(); for (String serverName : serverNames) { URI base = null; base = URI.create(String.format("http://%s/pools",serverName)); serverList.add(base); } cf = new CouchbaseConnectionFactory(serverList, name, pwd); return cf; } catch (Exception e) { e.printStackTrace(); return null; } } public void close(){ } public String getServerAddress() { return serverAddress; } public void setServerAddress(String serverAddress) { this.serverAddress = serverAddress; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
package com.cmcc.couchbase; import java.io.IOException; import com.couchbase.client.CouchbaseClient; import com.couchbase.client.protocol.views.View; public class CouchbaseTemplate extends CouchbaseClient implements ICouchbaseTemplate { public CouchbaseTemplate(CouchbaseConnector couchbaseConnector) throws IOException { super(couchbaseConnector.connect()); } public void addRecord(String key, Object value) { this.add(key, 0, value); } public Object getRecord(String key) { return this.get(key); } }
package com.cmcc.couchbase; import com.couchbase.client.CouchbaseClientIF; public interface ICouchbaseTemplate extends CouchbaseClientIF { public void addRecord(String key,Object value); public Object getRecord(String key); public Object get(String key); }
applicationContext.xml
<bean id="couchbaseConnector" class="com.cmcc.couchbase.CouchbaseConnector" destroy-method="close"> <property name="serverAddress"> <value>${couchBase.server}</value> </property> <property name="name"> <value>${couchBase.name}</value> </property> <property name="pwd"> <value>${couchBase.pwd}</value> </property> </bean> <bean id="couchbaseTemplate" class="com.cmcc.couchbase.CouchbaseTemplateMock"> <constructor-arg> <ref bean="couchbaseConnector" /> </constructor-arg> </bean>
pom.xml
<dependency> <groupId>com.couchbase.client</groupId> <artifactId>couchbase-client</artifactId> <version>1.2.1</version> <type>jar</type> </dependency>
使用
@Service @Transactional public class AppInfoService { @Resource ICouchbaseTemplate couchbaseTemplate; public AppInfo getAppInfo(String appid) { Gson gson = new Gson(); String json = (String) couchbaseTemplate.get("AppInfo-AppId:" + appid); AppInfo appInfo = null; if(json != null) appInfo = gson.fromJson(json, AppInfo.class); return appInfo; } }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!
推荐阅读
-
基于maven配置最简SpringMVC
-
JAVA WEB快速入门之从编写一个基于SpringMVC框架的网站了解Maven、SpringMVC、SpringJDBC
-
idea+spring4+springmvc+mybatis+maven实现简单增删改查CRUD
-
荐 使用IDEA实现SSM整合(Maven+Spring+Mybatis+SpringMvc)
-
javaweb各种框架组合案例(三):maven+spring+springMVC+hibernate
-
IntelliJ IDEA下Maven SpringMVC+Mybatis入门搭建例子
-
解决:IDEA springmvc maven 项目搭建完后没有生成 webcontent 目录
-
springmvc+mybatis+redis的session共享+maven管理
-
counchbase springmvc maven (nosql)
-
SpringMvc基础(二):快速搭建一个Maven项目的SpringMvc