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(","); ArrayListserverList = new ArrayList (); 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
${couchBase.server} ${couchBase.name} ${couchBase.pwd}
pom.xml
com.couchbase.client couchbase-client 1.2.1 jar
使用
@Service@Transactionalpublic 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捐助),没钱捧个人场,谢谢各位。