Spring整合SequoiaDB SQL

SequoiaDB發表於2016-11-02

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位,下載地址為:http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html#jdk-7u80-oth-JPR 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整合的整個過程。 建立專案工程如下圖:

enter image description here 圖3-1-1

說明: 1、Spring整合SequoiaDB的時候建議選擇DBCP連線池管理資料庫連線 2、SequoiaDB以及SequoiaDB SQL安裝可參考http://www.sequoiadb.com/cn/index.php?a=index&m=Files上面的安裝和聯結器章節

3.2、集合空間和集合建立及資料準備 1、以sdbadmin使用者登入,進入到/opt/sequoiadb/bin目錄中,進入到SDB Shell控制檯建立集合空間和集合 建立集合空間指令碼如下:

var db = new Sdb();
db.dropCS('CS');

建立集合指令碼如下:

db.CS.createCL('account_tx');

執行結果如下圖:

enter image description here 圖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

執行結果如下圖:

enter image description here 圖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' ) ;

執行結果如下圖:

enter image description here 圖3-2-3

4、在PostgreSQL驗證測試資料 驗證測試資料指令碼如下:

select * from account_tx limit 5;

執行結果如下圖:

enter image description here 圖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="http://java.sun.com/xml/ns/javaee" xmlns:web=http://java.sun.com/xml/ns/javaee 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.Driver
jdbc.url=jdbc:postgresql://192.168.1.48:5432/cs
jdbc.username=sdbadmin
jdbc.password=

3、資料操作以及事務控制配置資訊在applicationContext-dao.xml和applicationContext-transaction.xml中。 applicationContext-dao.xml具體資訊如下:

 <beans xmlns=http://www.springframework.org/schema/beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc=http://www.springframework.org/schema/mvc xmlns:context=http://www.springframework.org/schema/context 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.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://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=http://www.springframework.org/schema/beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc=http://www.springframework.org/schema/mvc xmlns:context=http://www.springframework.org/schema/context 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.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://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=http://www.springframework.org/schema/beans xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd  http://www.springframework.org/schema/mvc  http://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=http://www.springframework.org/schema/beans xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd  http://www.springframework.org/schema/mvc  http://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 configuration
PUBLIC "-//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" "h

ttp://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檔案中的原始碼。具體查詢功能結果展示如下圖:

enter image description here 圖4-2-1

5、總結 從上述整合過程可以看出,Spring整合SequoiaDB SQL套件和Spring整合其他資料庫如PostgreSQL資料庫流程一致。整合過程中主要是根據持久層框架的選擇如MyBatis、Hibernate來確定Spring的配置資訊,並且在持久層框架確定後需將SequoiaDB的Java驅動jar包新增到專案中。

SequoiaDB巨杉資料庫2.6最新版下載

SequoiaDB巨杉資料庫技術部落格

SequoiaDB巨杉資料庫社群

相關文章