第一篇 快速入门
通过以下4步流程,我们可以快速的搭建出一个基本的tangyuan应用。
1.创建一个标准的Maven项目,并加入以下依赖:
<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>
2.在src/main/resources下面增加配置文件tangyuan-configuration.xml,并加入如下配置:
<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&characterEncoding=utf8" />
<property name="driver" value="com.mysql.jdbc.Driver" />
</dataSource>
<!-- 定义事务定义 -->
<transaction id="tx_01" behavior="required"/>
<!-- 定义事务的使用范围 -->
<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>
<!-- 加载服务插件 -->
<plugin resource="tangyuan-sqlservices.xml" />
</configuration>
3.在src/main/resources下面新建SQL服务文件tangyuan-sqlservices.xml,并增加如下SQL服务:
<?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 rowCount="{nCount}" incrementKey="{user_id}">
INSERT into user(user_name, user_age, create_time) VALUES(#{user_name}, #{user_age}, #{create_time|now()})
</insert>
<!-- 打印一条日志 -->
<log message="插入一条用户数据: {user_name}, nCount: {nCount}, user_id: {user_id}"/>
<!-- 查询用户列表 -->
<selectSet resultKey="{users}">
SELECT * from user
</selectSet>
<!-- 返回 -->
<return>
<property value="{users}"/>
</return>
</sql-service>
</sqlservices>
4.单元测试:
@Before
public void init() {
try {
// 框架初始化
String xmlResource = "tangyuan-configuration.xml";
TangYuanContainer.getInstance().start(xmlResource);
System.out.println("-------------------------------------------------------------------------------");
} catch (Throwable e) {
e.printStackTrace();
}
}
@Test
public void test1() {
// 创建一个参数对象
XCO request = new XCO();
request.setLongValue("user_id", 1);
// 执行服务
Object result = ServiceActuator.executeAlone("user.getUserById", request);
// 对结果进行简单的输出
System.out.println("result: \n" + result);
}
@Test
public void test2() {
XCO request = new XCO();
request.setStringValue("user_name", "张三");
request.setIntegerValue("user_age", 18);
Object result = ServiceActuator.executeAlone("user.insertAndGet", request);
System.out.println("result: \n" + result);
}
附:SQL脚本
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 '姓名',
user_age TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '年龄',
create_time DATETIME NOT NULL COMMENT '创建时间',
PRIMARY KEY (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';