Spring整合SequoiaDB SQL
1、背景
Spring在J2EE應用程序開發框架中佔據重要的作用,它實現了輕量級的IoC(控制反轉)和AOP(面向切面)容器框架,能夠對JavaBean的生命周期進行管理,可以單獨使用也可以和其他框架進行整合,如Struts、MyBatis、Hibernate等主流框架。
Spring的IoC技術促進了低耦合,能夠將一個對象依賴的其它對象通過被動的方式傳遞進來,而不是這個對象自己創建或者查找依賴對象。面向切面的特性,允許通過分離應用的業務邏輯與系統級服務(例如審計和事務管理)進行開發,業務邏輯主要完成業務的各種功能,系統級服務負責其它的關注點,例如日誌或事務支持。Spring包含並管理應用對象的配置和生命周期,做為一種容器可以根據業務特性創建一個單獨的實例或者每次需要時都生成一個新的實例。Spring可以將簡單的組件配置組合成為複雜的應用,因此能很好的與其他J2EE應用開發框架進行集成。
巨杉資料庫SequoiaDB支持海量分散式數據存儲,提供常見開發語言驅動程序便於開發人員直接操作資料庫中的數據。在大部分企業級應用程序開發過程中,大部分開發人員或者客戶傾向於使用標準的SQL語句做為數據操作的標準介面。巨杉資料庫利用巨杉SequoiaDB SQL套件支持標準的SQL語句對資料庫中的數據進行各種操作,本文主要講解巨杉SequoiaDB SQL套件與常見的J2EE開發框架進行集成以及開發過程中遇到問題的解決思路。
2、產品介紹
巨杉資料庫SequoiaDB是一款分散式非關係型文檔資料庫,可以被用來存取海量非關係型的數據,其底層主要基於分散式,高可用,高性能與動態數據類型設計,它兼顧了關係型資料庫中眾多的優秀設計:如索引、動態查詢和更新等,同時以文檔記錄為基礎更好地處理了動態靈活的數據類型。PostgreSQL支持標準SQL,巨杉SequoiaDB SQL套件通過擴展 PostgreSQL功能可以使用標準SQL 語句訪問 SequoiaDB 資料庫,完成對SequoiaDB 資料庫的各種操作。常見J2EE應用持久層開發框架,如Mybatis、Hibernate等支持PostgreSQL資料庫,而這些框架能很好的與Spring進行集成。因此巨杉SequoiaDB SQL套件與Spring集成和PostgreSQL與Spring集成流程相同。
3、環境搭建
3.1、軟體配置
操作系統:windows 7
JDK:1.7.0_80 64位,下載地址為:Java Archive Downloads
Myeclipse:12.0.0
SequoiaDB:1.12.5或以上版本
SequoiaDB SQL:9.3.4
依賴包:Spring 3.2.4、mybatis 3.3.0、mybatis-spring 1.1.1、mybatis-paginator 1.2.5、postgresql-9.3.jdbc4-20131010.203348-4
本項目主要實現從SequoiaDB中查詢數據並分頁來展示Spring整合SequoiaDB SQL整合的整個過程。
創建項目工程如下圖:
圖3-1-1
說明:
1、Spring整合SequoiaDB的時候建議選擇DBCP連接池管理資料庫連接
2、SequoiaDB以及SequoiaDB SQL安裝可參考文檔中心_SequoiaDB巨杉資料庫上面的安裝和連接器章節
3.2、集合空間和集合創建及數據準備
1、以sdbadmin用戶登錄,進入到/opt/sequoiadb/bin目錄中,進入到SDB Shell控制台創建集合空間和集合
創建集合空間腳本如下:
var db = new Sdb();db.dropCS(CS);
創建集合腳本如下:
db.CS.createCL(account_tx);
執行結果如下圖:
圖3-2-1
2、退出SDB Shell腳本,在linux控制台執行下面命令導入數據:
./sdbimprt --hosts=node03:11810,node02:11810 --type=csv --file=cs.csv -c CS -l account_tx --fields=account_id long,tx_id long,customer_name string,tx_time string,tx_value int,account_overage long,tx_type int,other_side_account long,tx_site string,summary string -j 3
執行結果如下圖:
圖3-2-2
3、利用PostgreSQL連接器創建資料庫和外部表
創建資料庫的腳本如下:
bin/createdb -p 5432 cs
創建外部表的腳本如下:
create foreign table account_tx(account_id bigint,tx_id bigint,customer_name text,tx_time text,tx_value integer,account_overage bigint,tx_type integer,other_side_account bigint,tx_site text,summary text) server sdb_server options ( collectionspace CS, collection account_tx ) ;
執行結果如下圖:
圖3-2-3
4、在PostgreSQL驗證測試數據
驗證測試數據腳本如下:
select * from account_tx limit 5;
執行結果如下圖:
圖3-2-4
4、代碼展示
4.1、框架搭建代碼展示
1、在web.xml文件初始化配置信息,如Spring listener和Dispatcher以及載入Spring相關配置文件。
具體配置信息如下:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns="Java EE: XML Schemas for Java EE Deployment Descriptors" xmlns:web=Java EE: XML Schemas for Java EE Deployment Descriptors xsi:schemaLocation=http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd id="WebApp_ID" version="2.5"> <display-name>csWeb</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping></web-app>
2、資料庫連接信息在jdbc.properties中配置,具體信息如下:
jdbc.driver=org.postgresql.Driverjdbc.url=jdbc:postgresql://192.168.1.48:5432/csjdbc.username=sdbadminjdbc.password=
3、數據操作以及事務控制配置信息在applicationContext-dao.xml和applicationContext-transaction.xml中。applicationContext-dao.xml具體信息如下:
<beans xmlns=Index of /schema/beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc=Index of /schema/mvc xmlns:context=Index of /schema/context xmlns:aop="Index of /schema/aop" xmlns:tx=Index of /schema/tx xsi:schemaLocation="Index of /schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdIndex of /schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdIndex of /schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdIndex of /schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsdIndex of /schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <context:property-placeholder location="classpath:jdbc.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="maxActive" value="30" /><property name="maxIdle" value="5" /></bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations"> <array> <value>classpath:mapper/*.xml</value> </array> </property> <property name="configLocation" value="classpath:mybatic-config.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com/sequoiadb/cs/mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean></beans>
applicationContext-transaction.xml具體信息如下:
<beans xmlns=Index of /schema/beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc=Index of /schema/mvc xmlns:context=Index of /schema/context xmlns:aop="Index of /schema/aop" xmlns:tx=Index of /schema/tx xsi:schemaLocation="Index of /schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdIndex of /schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdIndex of /schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdIndex of /schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsdIndex of /schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <aop:aspectj-autoproxy /> <aop:config> <aop:pointcut id="appService" expression="execution(* com.sequoiadb.cs.service..*Service*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="appService" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="select*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="get*" read-only="true" /> <tx:method name="*" /> </tx:attributes> </tx:advice> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean></beans>
5、spring MVC配置信息在springmvc.xml,具體信息如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=Index of /schema/beans xmlns:context="Index of /schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="Index of /schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Index of /schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdIndex of /schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdIndex of /schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:component-scan base-package="com.sequoiadb.cs.service" /> <context:component-scan base-package="com.sequoiadb.cs.controller" /> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="com.sequoiadb.cs.controller.converter.CustomDateConverter" /> </list> </property> </bean></beans>
6、數據操作採用的是MyBatis框架,MyBatis框架與Spring整合信息在mybatic-config.xml中,具體信息如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=Index of /schema/beans xmlns:context="Index of /schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="Index of /schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Index of /schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdIndex of /schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdIndex of /schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:component-scan base-package="com.sequoiadb.cs.service" /> <context:component-scan base-package="com.sequoiadb.cs.controller" /> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="com.sequoiadb.cs.controller.converter.CustomDateConverter" /> </list> </property> </bean></beans><?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> <typeAliases> <typeAlias type="com.sequoiadb.cs.entity.AccountTx" alias="AccountTx" /> </typeAliases> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="postgresql" /><property name="offsetAsPageNum" value="true" /> <property name="rowBoundsWithCount" value="true" /> <property name="pageSizeZero" value="true" /> <property name="reasonable" value="true" /> <property name="params" value="pageNum=start;pageSize=limit;" /> <property name="supportMethodsArguments" value="true" /> <property name="returnPageInfo" value="check" /> </plugin> </plugins></configuration>
4.2、業務實現代碼展示
Spring整合SequoiaDB SQL採用MVC設計模式,在Model層將外部表account_tx信息封裝到實體類,dao層採用MyBatis框架操作,Service層完成具體的業務邏輯,Controller層完成視圖的響應以及各種用戶動作業務的實現。
1、 Model層account_tx對應實體類在AccountTx類中,具體信息如下:
package com.sequoiadb.cs.entity;import java.io.Serializable;public class AccountTx implements Serializable { private String account_id; private String tx_id; private String customer_name; private String tx_time; private int tx_value; private long account_overage; private int tx_type; private String other_side_account; private String tx_site; private String summary; public String getAccount_id() { return account_id; } public void setAccount_id(String account_id) { this.account_id = account_id; } public String getTx_id() { return tx_id; } public void setTx_id(String tx_id) { this.tx_id = tx_id; } public String getCustomer_name() { return customer_name; } public void setCustomer_name(String customer_name) { this.customer_name = customer_name; } public String getTx_time() { return tx_time; } public void setTx_time(String tx_time) { this.tx_time = tx_time; } public int getTx_value() { return tx_value; } public void setTx_value(int tx_value) { this.tx_value = tx_value; } public long getAccount_overage() { return account_overage; } public void setAccount_overage(long account_overage) { this.account_overage = account_overage; } public int getTx_type() { return tx_type; } public void setTx_type(int tx_type) { this.tx_type = tx_type; } public String getOther_side_account() { return other_side_account; } public void setOther_side_account(String other_side_account) { this.other_side_account = other_side_account; } public String getTx_site() { return tx_site; } public void setTx_site(String tx_site) { this.tx_site = tx_site; } public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; }}
2、數據操作在AccountTxMapper類中,配置信息在AccountTxMapper.xml中
AccountTxMapper類具體信息如下:
package com.sequoiadb.cs.mapper;import java.util.List;import java.util.Map;import com.sequoiadb.cs.entity.AccountTx;public interface AccountTxMapper { public List<AccountTx> queryAll(Map map) throws Exception;// 查詢所有}
AccountTxMapper.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.sequoiadb.cs.mapper.AccountTxMapper"> <resultMap id="BaseResultMap" type="com.sequoiadb.cs.entity.AccountTx"> <id column="tx_id" property="tx_id" jdbcType="BIGINT" /> <result column="account_id" property="account_id" jdbcType="BIGINT" /> <result column="customer_name" property="customer_name" jdbcType="VARCHAR" /> <result column="tx_time" property="tx_time" jdbcType="VARCHAR" /> <result column="tx_value" property="tx_value" jdbcType="INTEGER" /> <result column="account_overage" property="account_overage" jdbcType="BIGINT" /> <result column="tx_type" property="tx_type" jdbcType="INTEGER" /> <result column="other_side_account" property="other_side_account" jdbcType="BIGINT" /> <result column="tx_site" property="tx_site" jdbcType="VARCHAR" /> <result column="summary" property="summary" jdbcType="VARCHAR" /> </resultMap> <select id="queryAll" resultType="com.sequoiadb.cs.entity.AccountTx" parameterType="java.util.Map"> select * from account_tx where 1=1 <if test="tx_id != null" > and tx_id = #{tx_id,jdbcType=BIGINT} </if> <if test="account_id != null" > and account_id = #{account_id,jdbcType=BIGINT} order by tx_time desc </if> </select></mapper>
3、Service完成具體的業務邏輯,AccountTxService類定義具體需完成的業務介面,AccountTxServiceImpl類繼承AccountTxService完成具體的業務操作。
AccountTxService介面具體信息如下:
package com.sequoiadb.cs.service;import java.util.List;import java.util.Map; import com.sequoiadb.cs.entity.AccountTx;public interface AccountTxService { public List<AccountTx> queryAll(Map map) throws Exception;// 查詢所有}
AccountTxServiceImpl具體實現如下:
package com.sequoiadb.cs.service.impl;import java.util.List;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.sequoiadb.cs.entity.AccountTx;import com.sequoiadb.cs.mapper.AccountTxMapper;import com.sequoiadb.cs.service.AccountTxService;@Service("accountTxService")public class AccountTxServiceImpl implements AccountTxService { @Autowired private AccountTxMapper accountTxMapper; @Override public List<AccountTx> queryAll(Map map) throws Exception { return accountTxMapper.queryAll(map); }}
4、Controller完成視圖的響應以及各種用戶動作業務的實現,具體實現在AccountTxController類中,具體信息如下:
package com.sequoiadb.cs.controller;import java.io.IOException;import java.io.PrintWriter;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import com.sequoiadb.cs.entity.AccountTx;import com.sequoiadb.cs.service.AccountTxService;import com.sequoiadb.cs.utils.JSONUtils;@Controller@RequestMapping("/accountTx")public class AccountTxController { @Autowired private AccountTxService accountTxService; @RequestMapping(value = "/query", method = { RequestMethod.POST}) @ResponseBody public String queryAll(HttpServletRequest request,HttpServletResponse response) throws Exception { String tx_id = request.getParameter("tx_id"); String account_id = request.getParameter("account_id"); String sEcho = null; int pageNum = 0; // 起始索引 int pageSize = 0; // 每頁顯示的行數 JSONArray jsonarray = JSONArray.fromObject(request.getParameter("aoData")); for (int i = 0; i < jsonarray.size(); i++) { JSONObject obj = (JSONObject) jsonarray.get(i); if (obj.get("name").equals("sEcho")) sEcho = obj.get("value").toString(); if (obj.get("name").equals("iDisplayStart")) pageNum = obj.getInt("value"); if (obj.get("name").equals("iDisplayLength")) pageSize = obj.getInt("value"); } PageHelper.startPage(pageNum, pageSize); Map map = new HashMap(); if(tx_id != null && tx_id.length() > 0){ map.put("tx_id", Long.parseLong(tx_id)); } if(account_id != null && account_id.length() > 0){ map.put("account_id", Long.parseLong(account_id)); } List<AccountTx> accountTxList = accountTxService.queryAll(map); PageInfo<AccountTx> pagehelper = new PageInfo<AccountTx>(accountTxList); int initEcho = Integer.parseInt(sEcho)+1; Map dataMap = new HashMap(); JSONObject jsonObject = new JSONObject(); jsonObject.put("iTotalRecords", pagehelper.getTotal()); jsonObject.put("sEcho",initEcho); jsonObject.put("iTotalDisplayRecords", pagehelper.getTotal()); jsonObject.put("aaData", pagehelper.getList()); JSONUtils.toJSONString(dataMap); return jsonObject.toString(); } private void sendJsonData(HttpServletResponse response, String data)throws IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out; out = response.getWriter(); out.println(data); out.flush(); out.close(); }}
5、本項目主要演示從SequoiaDB資料庫中,通過SequoiaDB SQL查詢數據並且分頁,由於篇幅較大具體前端頁面代碼不在此展示,可參考附件中index.jsp文件中的源碼。具體查詢功能結果展示如下圖:
圖4-2-1
5、總結
從上述整合過程可以看出,Spring整合SequoiaDB SQL套件和Spring整合其他資料庫如PostgreSQL資料庫流程一致。整合過程中主要是根據持久層框架的選擇如MyBatis、Hibernate來確定Spring的配置信息,並且在持久層框架確定後需將SequoiaDB的Java驅動jar包添加到項目中。
SequoiaDB巨杉資料庫 2.6 最新版下載SequoiaDB技術博客_SequoiaDB巨杉資料庫
SequoiaDB巨杉資料庫社區
推薦閱讀:
※怎麼閱讀Spring源碼?
※Spring Security(三) -- JWT驗證原理(上)
※Spring Boot 1.5.x新特性:動態修改日誌級別
※快速了解spring事務七種傳播方式、事務的4種隔離級別、臟讀、重複讀、幻讀;
※【spring指南系列】使用Redis進行消息傳遞