package com.elong.pb.newdda.client.datasource;

import com.elong.pb.newdda.client.router.rule.ShardingRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.UUID;

/**
 * 支持分片的数据源 相关测试
 * User: zhangyong
 * Date: 2016/3/29
 * Time: 20:11
 * To change this template use File | Settings | File Templates.
 */
@ContextConfiguration(locations = {"/spring/spring-config-sharding-datasource2.xml"})
public class ShardingDataSourceAndTableUnitTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private ShardingDataSource shardingDataSource;

    @Autowired
    private ShardingRule shardingRule;

    @Test
    public void testInsertStatement() throws SQLException {
        Connection shardingConnection = shardingDataSource.getConnection();
        Statement statement = null;
        String uuid = "'" + UUID.randomUUID().toString() + "'";
        String sql = "INSERT INTO `t_coupon`(id , member_id ,coupon_value, create_time ,update_time) VALUES (" + uuid + ", 302919, 12.2, now(), now())";
        try {
            statement = null;
            statement = shardingConnection.createStatement();
            int updateRow = statement.executeUpdate(sql);
            System.out.println("updateRow=" + updateRow);
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (shardingConnection != null) {
                shardingConnection.close();
            }
        }
    }

}

@@ -368,24 +368,51 @@
 Connection;%0A
+import java.sql.ResultSet;%0A
 import java.
@@ -1771,10 +1771,930 @@
 %0A    %7D%0A%0A
+%0A    @Test%0A    public void testSelectStatement() throws SQLException %7B%0A        Connection shardingConnection = shardingDataSource.getConnection();%0A        Statement statement = null;%0A        ResultSet rs = null;%0A        String sql = %22select * from t_coupon where  member_id  = 302919%22;%0A        try %7B%0A            statement = null;%0A            rs = null;%0A            statement = shardingConnection.createStatement();%0A            rs = statement.executeQuery(sql);%0A            while (rs.next()) %7B%0A                System.out.println(%22%E7%BB%93%E6%9E%9C%E6%98%AF:%22 + rs.getString(%22id%22) + %22 coupon:%22 + rs.getBigDecimal(%22coupon_value%22));%0A            %7D%0A        %7D finally %7B%0A            if (statement != null) %7B%0A                statement.close();%0A            %7D%0A            if (rs != null) %7B%0A                rs.close();%0A            %7D%0A            if (shardingConnection != null) %7B%0A                shardingConnection.close();%0A            %7D%0A        %7D%0A    %7D%0A%0A%0A
 %7D%0A

添加 coupon 相关的内容