🚨 Test: 增加测试类

This commit is contained in:
fanxb 2019-03-26 17:21:27 +08:00
parent 9c54ae9896
commit f78085343d
15 changed files with 272 additions and 141 deletions

View File

@ -29,6 +29,12 @@
<version>2.0.0</version>
</dependency>
<!--看不到getter/setter因为这个,请注意-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--这里有个大坑版本过高使用xa事务会报错未指定数据库参见:https://github.com/apache/incubator-shardingsphere/issues/1842-->
<dependency>
<groupId>mysql</groupId>
@ -40,18 +46,11 @@
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!--druid versoin-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.14</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>

View File

@ -42,6 +42,6 @@ public interface OrderDao {
* @author fanxb
* @date 2019/3/25 14:24
*/
List<Order> getOrderByUserId(int id);
List<Order> getOrderByUserId(long id);
}

View File

@ -31,4 +31,15 @@ public interface OrderItemDao {
*/
List<OrderItem> getByOrderId(int id);
/**
* Description: 根据订单金额连表查询
*
* @author fanxb
* @date 2019/3/26 16:55
* @param price
* @return java.util.List<com.fanxb.sjdemo.entity.OrderItem>
*/
List<OrderItem> getOrderItemByPrice(int price);
}

View File

@ -0,0 +1,37 @@
package com.fanxb.sjdemo.dao;
import com.fanxb.sjdemo.entity.Dictionary;
import com.fanxb.sjdemo.entity.OtherTable;
import java.util.List;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/3/22 16:25
*/
public interface OtherTableDao {
/**
* Description:
*
* @author fanxb
* @date 2019/3/26 10:16
* @param dictionary dictionary
* @return long
*/
long addOne(OtherTable table);
/**
* Description:
*
* @author fanxb
* @date 2019/3/26 16:31
* @return java.util.List<com.fanxb.sjdemo.entity.OtherTable>
*/
List<OtherTable> getAll();
}

View File

@ -6,7 +6,7 @@ import lombok.Data;
@Data
public class OrderItem {
// private long userId;
private long userId;
private long orderItemId;
private long orderId;
private String name;

View File

@ -0,0 +1,16 @@
package com.fanxb.sjdemo.entity;
import lombok.Data;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/3/26 16:27
*/
@Data
public class OtherTable {
private Long id;
private String name;
}

View File

@ -11,7 +11,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.logging.ConsoleHandler;
/**
* 类功能简述
@ -41,7 +40,7 @@ public class UserService {
* 测试跨库事务
*/
@ShardingTransactionType(TransactionType.XA)
@Transactional
@Transactional(rollbackFor = Exception.class)
public void testTransactional() {
User user1 = new User(123, "988", 12);
logger.info("user1已经插入");

View File

@ -1,7 +1,8 @@
sharding:
jdbc:
datasource:
names: ds0,ds1
# 配置数据源
names: ds0,ds1,ds2
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
@ -14,28 +15,30 @@ sharding:
url: jdbc:mysql://10.82.27.177:3306/ds1
username: root
password: 123456
# ds2:
# type: com.alibaba.druid.pool.DruidDataSource
# driver-class-name: com.mysql.jdbc.Driver
# url: jdbc:mysql://10.82.27.177:3306/ds2
# username: root
# password: 123456
ds2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://10.82.27.177:3306/ds2
username: root
password: 123456
config:
sharding:
# 默认数据源,可以将不分库分表的数据表放在这里(此处的表需与已经分库分表的表完全没有关联,不会产生联表查询操作,因为跨库连表查询是没办法实现的),3.1.0版本dql查询存在bug不使用默认库
# default-data-source-name: ds2
# 默认分库策略
# 默认数据源,可以将不分库分表的数据表放在这里(此处的表需与已经分库分表的表完全没有关联,不会产生联表查询操作,因为跨库连表查询是没办法实现的)
# 3.1.0版本中dql查询存在bug不使用默认库.会在下个版本中修复
default-data-source-name: ds2
# 默认分库策略,根据userId对2取余确定库
default-database-strategy:
inline:
sharding-column: userId
algorithm-expression: ds$->{userId % 2}
# 配置表策略
tables:
# 公共表(比如字典表,角色表,权限表等),不分库或者分表,数据将发送到所有库中,方便联表查询
# 公共表(比如字典表,角色表,权限表等),不分库分表,数据将发送到所有库中,方便联表查询
dictionary:
# 配置主键以便sharding-jdbc生成主键
key-generator-column-name: dictionaryId
actual-data-nodes: ds$->{0..1}.dictionary
# user 前面已经根据uId分库因此user表不进行分表
# user 已经根据userId分库因此user表不进行分表
user:
key-generator-column-name: userId
actual-data-nodes: ds$->{0..1}.user
@ -44,7 +47,9 @@ sharding:
actual-data-nodes: ds$->{0..1}.order$->{0..1}
table-strategy:
inline:
# 设置分片键,相同分片键的连表查询不会出现笛卡儿积
sharding-column: orderId
# 设置分表规则,根据订单id对2取余分两个表
algorithm-expression: order$->{orderId%2}
order_item:
key-generator-column-name: orderItemId
@ -52,6 +57,8 @@ sharding:
table-strategy:
inline:
sharding-column: orderId
# 设置分表规则,根据订单id对2取余分两个表
algorithm-expression: order_item$->{orderId%2}
# 打印sql解析过程
props:
sql.show: true

View File

@ -3,11 +3,15 @@
<mapper namespace="com.fanxb.sjdemo.dao.OrderItemDao">
<insert id="addOne" parameterType="orderItem" useGeneratedKeys="true" keyProperty="orderItemId">
insert into order_item(orderId,name,price) values(#{orderId},#{name},#{price})
insert into order_item(userId,orderId,name,price) values(#{userId},#{orderId},#{name},#{price})
</insert>
<select id="getByOrderId" resultType="orderItem">
select * from order_item where orderId=#{id}
</select>
<select id="getOrderItemByPrice" resultType="orderItem">
select a.* from order_item a inner join order b on a.orderId = b.orderId where b.totalPrice=#{price}
</select>
</mapper>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fanxb.sjdemo.dao.OtherTableDao">
<insert id="addOne" useGeneratedKeys="true" keyProperty="id" parameterType="otherTable">
insert into other_table(name) values(#{name})
</insert>
<select id="getAll" resultType="otherTable">
select * from other_table
</select>
</mapper>

View File

@ -0,0 +1,161 @@
package com.fanxb.sjdemo;
import com.fanxb.sjdemo.dao.*;
import com.fanxb.sjdemo.entity.*;
import com.fanxb.sjdemo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.internal.matchers.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class MainTest {
@Autowired
private OrderDao orderDao;
@Autowired
private UserDao userDao;
@Autowired
private OrderItemDao orderItemDao;
@Autowired
private DictionaryDao dictionaryDao;
@Autowired
private OtherTableDao otherTableDao;
@Autowired
private UserService userService;
//固定用户id
private long userId = 8009821;
//固定订单金额
private int orderPrice = 300;
/**
* Description: 分库分表插入数据测试
*
* @return void
* @author fanxb
* @date 2019/3/26 16:35
*/
@Test
public void insert1() throws Exception {
//插入用户
User user = new User();
user.setUserId(userId);
user.setName("test");
user.setAge(12);
this.userDao.addOne(user);
log.info("插入用户id{}", user.getUserId());
//插入订单
Order order = new Order();
order.setUserId(user.getUserId());
order.setTotalPrice(orderPrice);
order.setCreateTime(new Date());
this.orderDao.addOne(order);
log.info("插入订单id:{}", order.getOrderId());
//插入订单详细项
OrderItem item = new OrderItem();
item.setOrderId(order.getOrderId());
item.setName("apple");
item.setPrice(10);
//要根据用户id判断属于哪个库
item.setUserId(user.getUserId());
this.orderItemDao.addOne(item);
log.info("插入订单项id{}", item.getOrderItemId());
}
/**
* Description:查询用户订单,能根据用户id确定表不会出现笛卡儿积
*
* @return void
* @author fanxb
* @date 2019/3/26 16:52
*/
@Test
public void selectOrder() {
List<Order> orders = this.orderDao.getOrderByUserId(userId);
log.info("查询用户订单结果为:{}", Arrays.toString(orders.toArray()));
}
/**
* Description:根据订单金额连表查询订单项,会产生笛卡儿积查询没有关键字确定库或者表
*
* @return void
* @author fanxb
* @date 2019/3/26 16:54
*/
@Test
public void selectOrderItem() {
List<OrderItem> orderItems = this.orderItemDao.getOrderItemByPrice(orderPrice);
log.info("查询到结果为:{}", Arrays.toString(orderItems.toArray()));
}
/**
* Description: 公共表插入测试
*
* @return void
* @author fanxb
* @date 2019/3/26 16:59
*/
@Test
public void insertDictionary() {
Dictionary dictionary = new Dictionary();
dictionary.setName("key");
dictionary.setValue("value");
this.dictionaryDao.addOne(dictionary);
log.info("字典表插入id为{}", dictionary.getDictionaryId());
}
/**
* Description: 其他表插入测试
*
* @author fanxb
* @date 2019/3/26 17:00
*/
@Test
public void insertOtherTable() {
OtherTable table = new OtherTable();
table.setName("test");
this.otherTableDao.addOne(table);
log.info("其它表插入id为{}", table.getId());
}
/**
* Description:其它表查询测试会报错
*
* @return void
* @author fanxb
* @date 2019/3/26 17:02
*/
@Test
public void selectOtherTable() {
this.otherTableDao.getAll();
}
/**
* Description: 事务测试
*
* @author fanxb
* @date 2019/3/26 17:03
*/
@Test
public void transTest() {
this.userService.testTransactional();
}
}

View File

@ -1,46 +0,0 @@
package com.fanxb.sjdemo;
import com.fanxb.sjdemo.dao.OrderDao;
import com.fanxb.sjdemo.entity.Order;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.internal.matchers.Or;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.*;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;
import java.util.logging.ConsoleHandler;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SjdemoApplicationTests {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Resource
private DataSource dataSource;
@Autowired
private OrderDao orderDao;
@Test
public void contextLoads() throws Exception {
try (
Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement();
) {
statement.execute("insert into user(name,age) value('2012-12-12 12:12:12',1212)");
statement.execute("select * from user");
}
}
}

View File

@ -1,35 +0,0 @@
package com.fanxb.sjdemo.serviceTest;
import com.fanxb.sjdemo.entity.Order;
import com.fanxb.sjdemo.service.OrderService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/3/22 15:47
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderServiceTest {
@Autowired
private OrderService orderService;
@Test
public void addOrderTest() {
Order order = new Order();
order.setUserId(12);
order.setCreateTime(new Date());
order.setTotalPrice(12);
orderService.insertOne(order);
}
}

View File

@ -1,36 +0,0 @@
package com.fanxb.sjdemo.serviceTest;
import com.fanxb.sjdemo.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/3/22 15:47
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
private Logger logger = LoggerFactory.getLogger(UserServiceTest.class);
@Autowired
private UserService userService;
@Test
public void addUserTest(){
}
@Test
public void getOneTest(){
logger.info(userService.getOne(1).toString());
}
}