Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A Look at TangYuan: A New Java-Based Persistence Framework

DZone's Guide to

A Look at TangYuan: A New Java-Based Persistence Framework

TangYuan is a recently created Java-based persistence framework that specializes in using data from multiple caches and sources. See it in action with a sample app.

· Database Zone
Free Resource

Traditional relational databases weren’t designed for today’s customers. Learn about the world’s first NoSQL Engagement Database purpose-built for the new era of customer experience.

TangYuan is a Java-based persistence framework. (Editor's Note: The original page is in Chinese. An English version can be found here.) The provided persistence layer framework includes SQL Maps and Data Access Objects (DAO). TangYuan supports multiple data sources, so that when it comes to read and write separation, the using multiple databases becomes simple. 

It also provides native support for transactional propagation and isolation without relying on a third-party framework — while supporting JDBC transactions from multiple data sources. That native support extends to a variety of caches and also supports mixed use caches, user-defined configurations, and more.

Check out the GitHub page (English translation) for a full list of features. For now, let's dive in and build an app using TangYuan in four steps.

Step 1

Create a standard Maven project and add the following dependencies:

<dependency>
    <groupId>org.xson</groupId>
    <artifactId>tangyuan</artifactId>
    <version>1.0.0</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.28</version>
</dependency>

<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>


Step 2

In the src/main/resources, add the configuration file tangyuan-configuration.xml, then add the following configuration:

<configuration>
    <!-- Data source configuration -->
    <dataSource id="ds" type="DBCP" isDefault="true">
        <property name="username" value="root" />
        <property name="password" value="123456" />
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/tangyuan_db?Unicode=true&amp;characterEncoding=utf8" />
        <property name="driver" value="com.mysql.jdbc.Driver" />
    </dataSource>

    <!-- Transaction definition -->
    <transaction id="tx_01" behavior="required"/>

    <!-- Defines the scope of the transaction -->
    <setDefaultTransaction type="method">
        <property name="select*"    value="tx_01"/>
        <property name="get*"       value="tx_01"/>
        <property name="update*"    value="tx_01"/>
        <property name="insert*"    value="tx_01"/>
        <property name="delete*"    value="tx_01"/>
    </setDefaultTransaction>

    <!-- Load the service plug-in -->
    <plugin     resource="tangyuan-sqlservices.xml" />

</configuration>


Step 3

In the src/main/resources below, add the new SQL service file tangyuan-sqlservices.xml, and add the following SQL services:

<?xml version="1.0" encoding="UTF-8"?>
<sqlservices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://xson.org/tangyuan/sqlservices.xsd" ns="user">     

    <selectOne id="getUserById">
        SELECT * from user WHERE user_id = #{user_id}
    </selectOne>    

    <sql-service id="insertAndGet" txRef="tx_01" dsKey="ds">
        <!-- Insert a record -->
        <insert rowCount="{nCount}" incrementKey="{user_id}">
            INSERT into user(user_name, user_age, create_time) VALUES(#{user_name}, #{user_age}, #{create_time|now()})
        </insert>
        <!-- Print a log -->
        <log message="Insert a user data: {user_name}, nCount: {nCount}, user_id: {user_id}"/>
        <!-- Queries the list of users -->
        <selectSet resultKey="{users}">
            SELECT * from user
        </selectSet>
        <!-- return -->
        <return>
            <property value="{users}"/>
        </return>
    </sql-service>

</sqlservices>


Step 4

Let's create a quick unit test:

@Before
public void init() {
    try {
        // Frame initialization
        String xmlResource = "tangyuan-configuration.xml";
        TangYuanContainer.getInstance().start(xmlResource);
        System.out.println("-------------------------------------------------------------------------------");
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

@Test
public void test1() {
    // Creates a parameter object
    XCO request = new XCO();
    request.setLongValue("user_id", 1);
    // Execute the service
    Object result = ServiceActuator.executeAlone("user.getUserById", request);
    // The results of a simple output
    System.out.println("result: \n" + result);
}

@Test
public void test2() {
    XCO request = new XCO();
    request.setStringValue("user_name", "david");
    request.setIntegerValue("user_age", 18);
    Object result = ServiceActuator.executeAlone("user.insertAndGet", request);
    System.out.println("result: \n" + result);
}


Attached SQL script

DROP DATABASE IF EXISTS tangyuan_db;
CREATE DATABASE tangyuan_db CHARACTER SET utf8 COLLATE=utf8_bin;

use tangyuan_db;

DROP TABLE IF EXISTS user;
CREATE TABLE user (
    user_id              BIGINT UNSIGNED           NOT NULL AUTO_INCREMENT COMMENT 'ID',
    user_name            VARCHAR(50)               NOT NULL     DEFAULT '' COMMENT 'name',
    user_age             TINYINT UNSIGNED          NOT NULL     DEFAULT 1  COMMENT 'age',
    create_time          DATETIME                  NOT NULL                COMMENT 'create time',
    PRIMARY KEY (user_id)       
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='user table';


And there you have it! Anyone interested can visit the TangYuan GitHub page (English translation) for more information or to drop suggestions.

Learn how the world’s first NoSQL Engagement Database delivers unparalleled performance at any scale for customer experience innovation that never ends.

Topics:
database ,persistence framework ,tangyuan ,sql

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}