Mongo service


1. Mongo service introduction

What is the mongo service? The mongo service is a service that provides MongoDB operations in tangyuan, contains a series of SQL statements and XML tags. Mongo service are divided into basic service and combination service. The basic service consists of a complete SQL statement and a tag, such as:

<selectSet id="getUserList" dsKey="mongods"><![CDATA[
    SELECT * from user_info where age >= 18 and age <= 28 order by age limit 10
 ]]></selectSet>

Combination service is composed of a series of basic services and tags, such as:

 <mongo-service id="opUser" dsKey="mongods">
    <insert>
       INSERT INTO user_info(id, name, age, amount) VALUES(2, 'tangyuan-mongo', 18, 1000);
    </insert>   
    <update><![CDATA[
       UPDATE user_info SET name = 'xson.org', age = age + 2 where id = 1
     ]]></update>
    <selectSet resultKey="{users}"><![CDATA[
       SELECT * from user_info where age >= 18 and age <= 28 order by age limit 10
     ]]></selectSet>
     <return>
       <property value="{users}"/>
     </return>
 </mongo-service>

All Mongo services need to be defined in the Mongo service plugin, such as the previous test-mongo.xml file. For detailed specifications, refer to the http://xson.org/schema/tangyuan/mongo/service.xsd file.

The Mongo service plugin contains two types of tags, one is the service tag, which means that the tag represents a Mongo service, which can be accessed and invoked directly, including the following tags:

Tag Description
selectSet Query a result set, such as: select * from user
If used within mongo-service, it will become an auxiliary tag, can not be called separately, and the use of some attributes will change.
selectOne Query a single record, such as: select * from user where id = 1
If used within mongo-service, it will become an auxiliary tag, can not be called separately, and the use of some attributes will change.
selectVar Query a field, such as: select name from user where id = 1
If used within mongo-service, it will become an auxiliary tag, can not be called separately, and the use of some attributes will change.
update Update operation, such as: update user set name ……
If used within mongo-service, it will become an auxiliary tag, can not be called separately, and the use of some attributes will change.
delete Delete operation, such as: DELETE FROM user where …
If used within mongo-service, it will become an auxiliary tag, can not be called separately, and the use of some attributes will change.
insert Insert operation, such as: INSERT into user ….
If used within mongo-service, it will become an auxiliary tag, can not be called separately, and the use of some attributes will change.
mongo-service combination service tag that contain some basic service tags and secondary tags, but can not contain itself.
sql Define some common SQL statements for other mongo service references. This tag can not be invoked separately and can not be included in other tags.

The other is a auxiliary tag, through these auxiliary tags, can achieve some complex service functions. Including the following tags:

Tag Description
if Conditional control tag, such as: <if test="{x} &lt; 0">
elseif Conditional control tag, need to be used in conjunction with if tags,such as: <elseif test="{x} &lt; 1">
else Conditional control tag, need to be used in conjunction with elseif tags, such as: <else>
foreach foreach tag,such as: <foreach collection="{ids}" index="{i}">
return The result returns the tag, can only be used in mongo-service. such as: <return value="{set}" />
exception exception tag, when the conditional failure will throw an exception, interrupt the follow-up operation, can only be used in mongo-service. such as:
<exception test="{x} &gt; 1" code="1" message="error message" />
setvar Variable set the tag, you can in XML to a variable assignment, can only be used in mongo-service. such as:
<setvar key="{x}" value="1" />
log The log output tag, can be used for the output of the log in XML, can only be used in mongo-service. such as:
<log level="info" message="Log information" />
call service invoke tag, which can call other tangyuan services in XML, including the mongo service, can only be used in mongo-service.
include The SQL reference tag, can reference the SQL statement defined in the sql tag before.

2. Mongo basic service tag

2.1 selectSet tag

Example

<selectSet id="getUserList" dsKey="ds">
    select * from user
</selectSet>

Description

In the example above, we defined a basic service with the ID as getUserList. The service uses the data source ds, executes a query, and the returned result defaults to the List<XCO> type.

selectSet tag attribute description

Attribute name Use and description Required Value
id Service ID, need to be unique. This is meaningless when used as a auxiliary tag, can be omitted. Y String
dsKey The data source ID used. If the user does not specify a data source, the default data source is used. If the system does not set the default data source, an exception is thrown. N String
resultKey It is used only as auxiliary tag, follow-up instructions. N String
resultType Return type, reference data mapping section. N String
resultMap Data mapping, reference data mapping section. N String
cacheUse Cache use, refer to the tangyuan-cache component. N String

2.2 selectOne tag

Example

<selectOne id="getUser" dsKey="ds">
    select * from user where id = #{id}
</selectOne>

Description

In the example above, we defined a basic service with the ID as getUser. The service uses the data source ds, executes a query, and the returned result defaults to the XCO type.

selectOne tag attribute description

Attribute name Use and description Required Value
id Service ID, need to be unique. This is meaningless when used as a auxiliary tag, can be omitted. Y String
dsKey The data source ID used. If the user does not specify a data source, the default data source is used. If the system does not set the default data source, an exception is thrown. N String
resultKey It is used only as auxiliary tag, follow-up instructions. N String
resultType Return type, reference data mapping section. N String
resultMap Data mapping, reference data mapping section. N String
cacheUse Cache use, refer to the tangyuan-cache component. N String

2.3 selectVar tag

Example

<selectVar id="getName" dsKey="ds">
    select name from user where id = #{id}
</selectVar>

Description

In the example above, we defined a basic service with the ID as getName. The service uses the data source ds, executes a query, and return the name field, the data type depends on the field. In the example above, the name field in Mongo is a String, so the corresponding Java type is String.

selectVar tag attribute description

Attribute name Use and description Required Value
id Service ID, need to be unique. This is meaningless when used as a auxiliary tag, can be omitted. Y String
dsKey The data source ID used. If the user does not specify a data source, the default data source is used. If the system does not set the default data source, an exception is thrown. N String
resultKey It is used only as auxiliary tag, follow-up instructions. N String
cacheUse Cache use, refer to the tangyuan-cache component. N String

2.4 update tag

Example

<update id="updateName" dsKey="ds">
    update user set name = 'xsonorg' where id = #{id}
</update>

Description

In the example above, we defined a basic service with the ID as updateName. The service uses the data source ds, executes an update operation, and the return value is the number of rows affected, Integer type.

update tag attribute description

Attribute name Use and description Required Value
id Service ID, need to be unique. This is meaningless when used as a auxiliary tag, can be omitted. Y String
dsKey The data source ID used. If the user does not specify a data source, the default data source is used. If the system does not set the default data source, an exception is thrown. N String
rowCount It is used only as auxiliary tag, follow-up instructions. N String
cacheClear Cache clear, refer to the tangyuan-cache component. N String

2.5 delete tag

Example

<delete id="deleteUser" dsKey="ds">
    delete from user where where id = #{id}
</delete>

Description

In the example above, we defined a basic service with the ID as deleteUser. The service uses the data source ds, executes an delete operation, and the return value is the number of rows affected, Integer type.

delete tag attribute description

Attribute name Use and description Required Value
id Service ID, need to be unique. This is meaningless when used as a auxiliary tag, can be omitted. Y String
dsKey The data source ID used. If the user does not specify a data source, the default data source is used. If the system does not set the default data source, an exception is thrown. N String
rowCount It is used only as auxiliary tag, follow-up instructions. N String
cacheClear Cache clear, refer to the tangyuan-cache component. N String

2.6 insert tag

Example

<insert id="insertUser" dsKey="ds">
    insert into user(name, age) values('xsonorg', 26);
</insert>

Description

In the example above, we defined a basic service with the ID as insertUser. The service uses the data source ds, executes an insert operation, and the return value is the _id value of the document, the String type.

insert tag attribute description

Attribute name Use and description Required Value
id Service ID, need to be unique. This is meaningless when used as a auxiliary tag, can be omitted. Y String
dsKey The data source ID used. If the user does not specify a data source, the default data source is used. If the system does not set the default data source, an exception is thrown. N String
resultKey It is used only as auxiliary tag, follow-up instructions. N String
cacheClear Cache clear, refer to the tangyuan-cache component. N String

3. Use of auxiliary tags

3.1 if/elseif/else tag

Example 1

<selectSet id="getUserList" dsKey="ds">
    select * from user where 1 = 1
    <if test="{user_name} != null">
        and user_name = #{user_name}
    </if>
    <if test="{start_time}!=null AND {start_time} !='' "><![CDATA[
       and #{start_time} > create_time
    ]]></if>
    <if test="{end_time}!=null AND {end_time} != '' "><![CDATA[
       and #{end_time} < create_time
    ]]></if>
    ORDER BY id DESC 
    limit #{start}, #{pageSize}         
</selectSet>

Description

Example 1 is a classic conditional control scenario, we can use the if tag’s test attribute to determine whether the user entered a query condition field, so that spliced into a complete SQL statement.

Example 2

<mongo-service id="updateUser" dsKey="ds">
    <update rowCount="{nCount}">
       update order set state = 20 where id = #{id} AND state = 10
    </update>
    <if test="{nCount} == 1">
       <insert>
         insert into log(context) values('Order status update is successful');
       </insert>
    </if>
</mongo-service>

Description

In Example 2, if the update operation is successful (depending on the number of rows affected), subsequent insertions are performed.

Example 3

<mongo-service id="updateUser" dsKey="ds">
    <selectVar resultKey="{type}">
       select type from user where id = #{id}
    </selectVar>
    <if test="{type} == 1">
       <insert>
         insert into log(context) values('Order status update is successful');
       </insert>
    </if>
    <elseif test="{type} == 2">
       <update>
         update order set state = 20 where id = #{id} AND state = 10
       </update>
    </elseif>
    <else>
       <delete>
         delete from user where where id = #{id}
       </delete>
    </else>
</mongo-service>

Description

Example 3 Use the if, elseif, and else tags to enter different processing flows depending on the type value.

Test expression description

The format of the expression

Simple expression: test="A Operator B"

Complex expression: test="A Operator B [AND|OR] C Operator D"

Where A/B/C/D is a comparison object and can be a variable or a constant. The following operators are supported.

SN Operation symbol Meaning Writing in XML
1 == Equal ==
2 != Not equal !=
3 > Greater than &gt;
4 >= greater than or equal &gt;=
5 < Less than &lt;
6 <= less than or equal &lt;=
7 AND AND
8 OR OR

Note:

1.Where 1-6 is used between the object and the logical comparison between objects, 7-8 is used between the expression groups.

2.In the use of complex expressions, only select a relationship [AND|OR], can not be mixed use.

3.There are four types of object data that participate in expression operations:

1. NULL
2. Value wrapping type (Byte, Short, Integer, Long, Float, Double)
3. String type
4. Object type

Which compare the type of object need to pay attention to: NULL can be compared with all types. String and String using equals comparison. The value wrapping type is compared between its values. The comparison between objects is to compare their memory addresses.

4.The variable uses the {xxx} way.

5.When using the 3,4,5,6 operator, you need to use XML entity characters, as given in the table above.

3.2 foreach tag

Example 1

<mongo-service id="insert2" dsKey="ds">
    <foreach collection="{urlList}" index="{i}">
       <insert>
         INSERT INTO resources(
          sn_id, type, title, url, create_time
         ) VALUES (
          #{sn}, 2, #{urlList[i].title}, #{urlList[i].url}, #{create_time|now()}
         )
       </insert>
    </foreach>
</mongo-service>

Execute the log

INSERT INTO resources(
    sn_id, type, title, url, create_time
) VALUES (
    '123x', 2, 'Picture 1', 'http://p1.sinaimg.cn/xxx', '2016-10-27 21:24:13'
)

INSERT INTO resources(
    sn_id, type, title, url, create_time
) VALUES (
    '123x', 2, 'Picture 2', 'http://p1.sinaimg.cn/xxx', '2016-10-27 21:24:13'
)

Example 2

<mongo-service id="insert3" dsKey="ds">
    <selectSet>
       select * from resources where res_id in
       <foreach collection="{ids}" index="{i}" open="(" close=")" separator=",">
        #{ids[i]}
       </foreach>         
    </selectSet>
</mongo-service>

Execute the log

select * from resources where res_id in

( 2 , 3 , 4 )

Description

The above two examples represent different application scenarios for foreach tags; Example 1 represents the generation of multiple insert statements, multiple internal services; Example 2 represents the generation of partial fields to form a complete SQL statement.

foreach tag attribute description

Attribute name Use and description Required Value
collection Need to traverse the collection name, can be an array, List, Set Y String
index The index variable of the collection is used when traversing the collection. N String
open Start the string, such as the values part of the insert, starting with “(”. N String
close End the string, such as the values part of the insert, ending with “)”. N String
separator Separator , such as the values part of the insert, the separator is “,”. N String

3.3 sql tag

The purpose of the sql tag is to define some common SQL statements, and the include tag is a SQL statement that references the SQL tag, so the SQL tag and the include tag are used in conjunction.

Example

<sql id="getUserListWhere">
    <if test="{user_name} != null">
        and user_name = #{user_name}
    </if>
    <if test="{start_time}!=null AND {start_time} !='' "><![CDATA[
       and #{start_time} > create_time
    ]]></if>
    <if test="{end_time}!=null AND {end_time} != '' "><![CDATA[
       and #{end_time} < create_time
    ]]></if>   
</sql>

<mongo-service id="getUserList" dsKey="ds">
    <selectVar resultKey="{total}">
       SELECT count(1) from user where 1 = 1 
       <include ref="getUserListWhere"/>
    </selectVar>
    <selectSet>
       select * from user where 1 = 1
       <include ref="getUserListWhere"/>
       ORDER BY id DESC 
       limit #{start}, #{pageSize}     
    </selectSet>
    <return>
       <property value="{total}"/>
       <property value="{projects}"/>
    </return>   
</mongo-service>

Description

In the above example, one is the SQL statement getUserListWhere defined by the sql tag, the other is the getUserList service defined by the mongo-service tag. In the mongo-service internal, through the include tag reference getUserListWhere defined SQL statement.

sql tag attribute description

Attribute name Use and description Required Value
id ID, need to be unique. The include tag is used when it is referenced. Y String

3.4 include tag

References the contents of the previously defined SQL tags, equivalent to the definition in the current service tag.

Example: Refer to the sql tag example

include tag attribute description

Attribute name Use and description Required Value
ref Reference to the SQL tag ID; ** Note:** need to increase the namespace. Y String

3.5 Special mark in text content

Example 1

<selectSet>
    select * from ${table} where id = #{id}
</selectSet>

Description

Two special text mark appear in Example 1 above, one is #{id}, which has been multiple times in the previous example, meaning that the user places a placeholder variable here. When the service call, according to the user’s incoming parameters, to replace the text here. General #{xxx} use of the scene is: conditional judgment in the where statement, Set assignment in the update statement, The values part of the insert statement. The other is ${table}, the function is roughly the same, are the placeholder and text replacement, the difference is that ${xxx} will only be a simple replacement. Generally used as a dynamic selection of field columns, table name replacement.

Example 2

<insert>
    insert into user(
       name, state, create_time
    ) values(
       #{name}, #{state|0}, #{create_time|now()}
    );
</insert>

Description

In the above example 2 there is a special text mark |, his use is when the placeholder variable is not assigned, the use of | after the default value to replace. The following table shows the default values that are supported by the system:

Example Description
#{xxx|’’} The default value is an empty string "“ | | #{xxx|‘abc’} | The default value is a string ”abc"
#{xxx|0}
#{xxx|1.3}
The default value is int, according to its value range to determine int or long, here is the int.
The default value is float, according on its value range to determine the float or double, here is the float.
#{xxx|now()} The default value is Java current time, corresponding to java.util.Date.
#{xxx|date()} The default value is Java current time, corresponding to java.sql.Date.
#{xxx|time()} The default value is Java current time, corresponding to java.sql.Time.
#{xxx|null} The default value is NULL

4. Combination service tag

4.1 mongo-service tag

mongo-service tag attribute description

Attribute name Use and description Required Value
id Service ID, need to be unique. Y String
dsKey The data source ID used. There are several cases to explain here:
1.Set the data source (A) here, internal service does not set the data source, internal service using data source (A).
2.Set the data source (A) here, internal service sets the data source (B), internal service uses the data source (B).
3.The data source is not set here, internal service sets the data source (B), internal service uses the data source (B).
4.In the case of sharding, here and internal services on the data source settings, the following chapters will be described in detail.
N String
resultType Return Type: Default XCO N xco/map
cacheUse Cache use, refer to the tangyuan-cache component. N String
cacheClear Cache clear, refer to the tangyuan-cache component. N String

Description

The composite service can contain one or more of the basic service tags in selectSet, selectOne, selectVar, update, delete, insert, but can not contain its own mongo-service tags.

4.2 selectSet is used as a auxiliary tag

Example 1

<mongo-service id="myService" dsKey="ds">
    <selectSet resultKey="{users}">
       select * from user
    </selectSet>
    <return>
       <property value="{users}"/>
    </return>
</mongo-service>

Description

In the above example, we define a Mongo combination service myService, which contains a selectSet basic service. When the program executes the selectSet service, it will put the result List<XCO> with users as key, into the context parameters, and finally through the return tag to return. The concrete return result is a XCO object that contains a List<XCO> element, and the settings for the return tag and the returned result are described in other chapters, and we will not elaborate here. The following is the result of the return XML format.

Return the result

<?xml version="1.0" encoding="UTF-8"?>
<X>
    <XL K="users">
       <X>
         <L K="user_id" V="1"/>
         <S K="user_name" V="Harry"/>
         <B K="user_age" V="18"/>
         <A K="create_time" V="2016-10-20 21:30:58"/>
       </X>
       <X>
         <L K="user_id" V="2"/>
         <S K="user_name" V="Kate"/>
         <B K="user_age" V="18"/>
         <A K="create_time" V="2016-10-20 21:31:58"/>
       </X>
       ......
    </XL>
</X>

In the example above selectSet as an internal service, or auxiliary tag, the use of its attributes has undergone some changes:

selectSet tag attribute and change description:

Attribute name Use and description Required Value
id meaningless N String
dsKey The data source ID used. There are several cases (non-sharding):
1. If the user does not set this property, use the mongo-service tag’s data source;
2. If the user has set this attribute, use the data source set by the user.
On the sharding application scenarios, this setting we will be in a specific chapter to illustrate.
N String
resultKey Defines the key of the return result of the current query operation in the context parameter. N String
resultType meaningless N String
resultMap meaningless N String
cacheUse Cache use, refer to the tangyuan-cache component. N String

See here we may be skeptical, and in example 1, why not directly use the selectSet tag? Because before has been given to everyone has introduced the mongo-service defined service is a combination of services, which can contain internal selectSet, selectOne,selectVar, update, delete, insert These basic services, in the back of the tutorial we can feel the powerful combination of services.

4.3 selectOne is used as a auxiliary tag

Example 2

<mongo-service id="myService2" dsKey="ds">
    <selectOne resultKey="{user}">
       select * from user where user_id = #{user_id}
    </selectOne>
    <return value="{user}"/>
</mongo-service>

Description

In Example 2 we defined a Mongo combination service myService2, which contains a selectOne basic service. After execution, the result is a XCO object, the XML format is as follows:

Return the result

<X>
    <L K="user_id" V="1"/>
    <S K="user_name" V="xson.org"/>
    <B K="user_age" V="18"/>
    <A K="create_time" V="2016-10-20 21:30:58"/>
</X>

selectOne tag attribute and change description:

Attribute name Use and description Required Value
id meaningless N String
dsKey Reference selectSet.dsKey N String
resultKey Defines the key of the return result of the current query operation in the context parameter. N String
resultType meaningless N String
resultMap meaningless N String
cacheUse Cache use, refer to the tangyuan-cache component. N String

4.4 selectVar is used as a auxiliary tag

Example 3

<mongo-service id="myService3" dsKey="ds">
    <selectVar resultKey="{userName}">
       select user_name from user where user_id = #{user_id}
    </selectVar>
    <return>
       <property name="{userName}" value="{userName}"/>
    </return>
</mongo-service>

Description

In Example 3 we define a Mongo combination service myService3, which contains a selectVar basic service. After execution, the result is a XCO object that contains a userName entry. The XML format is as follows:

Return the result

<?xml version="1.0" encoding="UTF-8"?>
<X>
    <S K="userName" V="xson.org"/>
</X>

selectVar tag attribute and change description:

Attribute name Use and description Required Value
id meaningless N String
dsKey Reference selectSet.dsKey N String
resultKey Defines the key of the return result of the current query operation in the context parameter. N String
cacheUse Cache use, refer to the tangyuan-cache component. N String

4.5 update is used as a auxiliary tag

Example 4

<mongo-service id="myService4" dsKey="ds">
    <update rowCount="{nCount}">
       update user set user_name = 'xson.org' where user_id = #{user_id}
    </update>
    <exception test="{nCount} != 1" code="-1" message="User update failed"/>
</mongo-service>

Description

In Example 4, we define a Mongo combination service myService4, which contains an internal update basic service. When the program executes update, place affect the number of rows into the context parameter, take nCount as the key. And then through the exception tag to determine the validity of nCount, if nCount does not meet the conditions, the service will throw a service exception.

Return result: none

update tag attribute and change description:

Attribute name Use and description Required Value
id meaningless N String
dsKey Reference selectSet.dsKey N String
rowCount Defines the key of the return result(affect the number of rows) of the current update operation in the context parameter. N String
cacheClear Cache clear, refer to the tangyuan-cache component. N String

4.6 delete is used as a auxiliary tag

Example 5

<mongo-service id="myService5" dsKey="ds">
    <delete rowCount="{nCount}">
       delete from user where user_id = #{delete_user_id}
    </delete>
    <exception test="{nCount} != 1" code="-1" message="Delete user failed"/>
</mongo-service>

Description

In Example 5, we define a Mongo combination service myService5, which contains an internal delete basic service. When the program executes delete, place affect the number of rows into the context parameter, take nCount as the key. And then through the exception tag to determine the validity of nCount, if nCount does not meet the conditions, the service will throw a service exception.

Return result: none

delete tag attribute and change description:

Attribute name Use and description Required Value
id meaningless N String
dsKey Reference selectSet.dsKey N String
rowCount Defines the key of the return result(affect the number of rows) of the current delete operation in the context parameter. N String
cacheClear Cache clear, refer to the tangyuan-cache component. N String

4.7 insert is used as a auxiliary tag

Example 6

<mongo-service id="myService6" dsKey="ds">
    <insert resultKey="{_id}">
       insert into user(user_name, user_age, create_time) values('xson.org', 26, #{create_time|now()});
    </insert>
    <return>
       <property value="{_id}"/>
    </return>
</mongo-service>

Description

In Example 6, we define a Mongo combination service myService6, which contains an internal insert basic service. When the program executes insert, place the document’s _id into the context parameter, take _id as the key. And then return it through the return tag, the XML format is as follows.

Return result

<?xml version="1.0" encoding="UTF-8"?>
<X>
    <L K="_id" V="598bcb1d2932a10fd86d2daa"/>
</X>

insert tag attribute and change description:

Attribute name Use and description Required Value
id meaningless N String
dsKey Reference selectSet.dsKey N String
resultKey Defines the key of the return result(The document’s _id) of the current insert operation in the context parameter. N String
cacheClear Cache clear, refer to the tangyuan-cache component. N String

Before we see examples are some simple combination of services, the following we look at a complex example of a combination of services.

4.8 Use of combination service

Example 7

<mongo-service id="myService7" dsKey="ds">
    <selectSet resultKey="{users}">
         select * from user
    </selectSet>

    <selectOne resultKey="{user}">
       select * from user where user_id = #{user_id}
    </selectOne>

    <selectVar resultKey="{userName}">
       select user_name from user where user_id = #{user_id}
    </selectVar>

    <update rowCount="{nCount}">
       update user set user_name = 'xson.org' where user_id = #{user_id}
    </update>
    <exception test="{nCount} != 1" code="-1" message="User update failed"/>

    <delete rowCount="{nCount}">
       delete from user where user_id = #{delete_user_id}
    </delete>
    <exception test="{nCount} != 1" code="-1" message="User delete failed"/>   

    <insert>
       insert into user(user_name, user_age, create_time) values('david', 26, #{create_time|now()});
    </insert>

    <return>
       <property value="{users}"/>
       <property value="{user}"/>
       <property value="{userName}"/>
       <property value="{user_id}"/>
    </return>       

</mongo-service>

Return result

<?xml version="1.0" encoding="UTF-8"?>
    <X>
       <XL K="users">
         <X>
          <L K="user_id" V="1"/>
          <S K="user_name" V="Harry"/>
          <B K="user_age" V="18"/>
          <A K="create_time" V="2016-10-20 21:30:58"/>
         </X>
         <X>
          <L K="user_id" V="2"/>
          <S K="user_name" V="Kate"/>
          <B K="user_age" V="18"/>
          <A K="create_time" V="2016-10-20 21:31:58"/>
         </X>
         ...
       </XL>
       <X K="user">
         <L K="user_id" V="1"/>
         <S K="user_name" V="Harry"/>
         <B K="user_age" V="18"/>
         <A K="create_time" V="2016-10-20 21:30:58"/>
       </X>
       <S K="userName" V="Harry"/>
       <L K="user_id" V="11"/>
    </X>

Description

Example 7 is actually the previous example of the integration of 1 to 6, in a service to complete the six operations, and return the results according to need, as SQL stored procedures, Java functions in general, this is the advantages of Mongo combination services. Through some combination of basic services, and some auxiliary tags, to achieve complex business logic. So that even developers do not understand Mongo can complete most of the service development work.

4.9 return tag

The return tag can be used to define the return content in the combination service. The use of the return tag generally has the following two situations:

Example 8

<mongo-service id="myService" dsKey="ds">
    <selectSet resultKey="{users}">
       select * from user
    </selectSet>
    <return>
       <property value="{users}"/>
    </return>
</mongo-service>

Description

The return tag in Example 8 defines the return object (the default is XCO type), which contains a property named users, of type List<XCO>.

Example 9

<mongo-service id="myService" dsKey="ds">
    <selectVar resultKey="{userName}">
       select user_name from user where user_id = #{user_id}
    </selectVar>
    <return value="{userName}" />
</mongo-service>

Description

In Example 9, the return tag returns the value represented by userName directly, where the type of userName is String.

The above two examples illustrate the use of the return tag, one is to return a package object, and then through the property tag to define its internal specific attributes and attribute values. The other is to directly determine the return value by the value attribute of the return tag. The return type in this case is determined by the variable in the value attribute.

Note that these two methods can not be mixed, of course, if a service does not need to return type, do not need to use the return tag, but if you use the return tag, there can only be one.

return tag attribute description:

Attribute name Use and description Required Value
value The name of the variable that needs to be returned directly, such as: value=“{user}” N String

property tag attribute and change description:

Attribute name Use and description Required Value
name Returns the name of the property in the object, which can be omitted. The default is the variable name in value. N String
value The name of the variable representing the attribute value, such as: value=“{user}” Y String

4.10 log tag

The log tag is used for the detection and log output during service execution.

Example 10

<mongo-service>
    <log message="Update User: Start" level="info"/>
    <update rowCount="{nCount}">...</update>
    <log message="Update User: End"/>
    <log message="Update class: start"/>
    <update rowCount="{nCount}">...</update>
    <log message="Update Class: End"/>
</mongo-service>

Description

The above example uses four log tags, respectively, before and after the update operation. When the test time, you can easily from the log to observe the implementation of the service

log tag attribute description:

Attribute name Use and description Required Value
message Log content, where variables can be used. such as: <log message="USER_ID: {user_id}, CLASS_ID: {class_id}"/> Y String
level Log level, the default info, reference log4j log level. N error/warn/info/debug

4.11 setvar tag

Use: variable assignment tag, in XML to a variable assignment.

Example 11

<mongo-service>
    <setvar key="{x}" value="0"/>
    <if test="{type} == 1">
       <selectOne resultKey="{x}">....</selectOne>
    </if>
    <return>
       <property value="{x}"/>
    </return>
</mongo-service>

Description

In the above example, the variable x is assigned a value of 0, and then the condition is evaluated. If type == 1, the selectOne operation is executed, the result is reassigned to the variable x, and finally return x.

setvar tag attribute description:

Attribute name Use and description Required Value
key Variable name, such as:key=“{x}” Y String
value Variable value, if the user does not specify type, the default will be based on user input variable value automatically analyze its type; For example:
value=“0”,Type is int
value=“1.3”,Type is float
value=“true”,Type is boolean
value=“‘xxx’”,Type is String
value=“yyyy-MM-dd HH:mm:ss”,Type is dateTime
value=“yyyy-MM-dd”,Type is date
value=“HH:mm:ss”,Type is time
Y
type The data type of the variable N int
long
float
double
short
boolean
byte
char
dateTime
date
time

4.13 call tag

Service call tag, call other services internally within the combination service, can be a basic service, it can be another combination of services.

Example 13A

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

<mongo-service id="myService" dsKey="ds">
    <update rowCount="{nCount}">
       update user set user_name = 'xson.org' where user_id = #{user_id}
    </update>
    <call service="demo2/getUserById" resultKey="{user}"/>
    <return>
       <property value="{user}"/>
    </return>
</mongo-service>    

Description

In the above example, the getUserById service is called inside myService, just like the call between JAVA functions. When the call has completed the call, it will return the result, with user as the key, and puts it into the parameter context.

Careful friends may be in doubt, getUserById service is the need for a user_id parameters, how to get its value? In fact, in the above example myService implied to pass their own context and parameters to getUserById, myService parameter is the existence of user_id, so getUserById service can successfully take the value.

In the following way, we can specify the parameters passed to the getUserById service:

Example 13B

<call service="demo2/getUserById" resultKey="{user}">
    <property value="{user_id}"/>
</call>

In this way, when calling the getUserById service, only to pass it a user_id a parameter.

Example 13C

<call service="demo2/getUserById" resultKey="{user}" mode="EXTEND" exResultKey="{ex}"/>

Description

In the example 13C, the call tag uses the mode attribute and the exResultKey attribute. The mode attribute indicates the calling mode. TheexResultKey attribute indicates that the exception information is encapsulated as a new object after the call has occurred (Default to XCO object), with ex as the key, and puts it into the context for later use.

** Note: ** Avoid loops or recursive calls.

call tag attribute description:

Attribute name Use and description Required Value
service The called service name. The format is: namespace + “/” + service ID Y String
resultKey Returns the key of the result N String
mode Call mode, default EXTEND.
EXTEND:The called party will inherit the caller’s context.
ALONE:The callee has a separate context.
ASYNC:Asynchronous call, the called party has a separate context.
N EXTEND/ALONE/ASYNC
exResultKey After the exception occurs in the call, the exception information in the context of the key. N String

property tag attribute description:

Attribute name Use and description Required Value
name Parameter name, default is the variable name of value N String
value Parameter value Y String

4.14 exception tag

Exception tag. When the test conditions are met, the service exception ServiceException will be thrown.

Example 14

<mongo-service id="myService4" dsKey="ds">
    <update rowCount="{nCount}">......</update>
    <exception test="{nCount} != 1" code="-1" message="User update failed"/>
    ......
</mongo-service>

Description

There have been many examples of the use of the exception tag, and here we highlight the effects of the exception in the Mongo service:

  1. If there is a transaction in the SQL service, throwing an exception will cause the transaction to roll back, Mongo service does not exist in the transaction concept, when the exception is thrown, the subsequent operation will be interrupted.
  2. If the current call contains SQL services, will also lead to SQL transaction rollback.

exception tag attribute description:

Attribute name Use and description Required Value
test Logical expression, refer to the test attribute of the if tag. Y String
code The error code that is carried when the service exception is thrown. N int
message The error message that is carried when the service exception is thrown. N String