✨ Feat: sharding-jdbc分库分表demo完成
This commit is contained in:
parent
2c481c9982
commit
eb639e5dbd
0
spring-boot/sjdemo/logs/xa_tx.lck
Normal file
0
spring-boot/sjdemo/logs/xa_tx.lck
Normal file
@ -29,10 +29,11 @@
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<!--这里有个大坑,版本过高使用xa事务会报错未指定数据库,参见:https://github.com/apache/incubator-shardingsphere/issues/1842-->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
<version>5.1.24</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.shardingsphere</groupId>
|
||||
@ -50,6 +51,26 @@
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.56</version>
|
||||
</dependency>
|
||||
|
||||
<!--xa分布式事务-->
|
||||
<dependency>
|
||||
<groupId>io.shardingsphere</groupId>
|
||||
<artifactId>sharding-transaction-2pc-xa</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.shardingsphere</groupId>
|
||||
<artifactId>sharding-transaction-spring-boot-starter</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
@ -0,0 +1,31 @@
|
||||
package com.fanxb.sjdemo.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.fanxb.sjdemo.entity.Dictionary;
|
||||
import com.fanxb.sjdemo.service.DictionaryService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/26 10:17
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("dictionary")
|
||||
public class DictionaryController {
|
||||
|
||||
@Autowired
|
||||
private DictionaryService dictionaryService;
|
||||
|
||||
@PutMapping("")
|
||||
public long addOne(@RequestBody JSONObject object) {
|
||||
Dictionary dictionary = object.toJavaObject(Dictionary.class);
|
||||
return this.dictionaryService.addOne(dictionary);
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.fanxb.sjdemo.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.fanxb.sjdemo.entity.Order;
|
||||
import com.fanxb.sjdemo.service.OrderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/25 14:25
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/order")
|
||||
public class OrderController {
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
@PutMapping("")
|
||||
public long addOne(@RequestBody JSONObject obj){
|
||||
Order order = obj.toJavaObject(Order.class);
|
||||
return this.orderService.insertOne(order);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.fanxb.sjdemo.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.fanxb.sjdemo.entity.OrderItem;
|
||||
import com.fanxb.sjdemo.service.OrderItemService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/25 14:25
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/orderItem")
|
||||
public class OrderItemController {
|
||||
|
||||
@Autowired
|
||||
private OrderItemService orderItemService;
|
||||
|
||||
@PutMapping("")
|
||||
public long addOne(@RequestBody JSONObject obj) {
|
||||
OrderItem item = obj.toJavaObject(OrderItem.class);
|
||||
return this.orderItemService.addOne(item);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.fanxb.sjdemo.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.fanxb.sjdemo.entity.User;
|
||||
import com.fanxb.sjdemo.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/25 14:25
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
|
||||
@PutMapping("")
|
||||
public long addUser(@RequestBody JSONObject obj){
|
||||
User user = obj.toJavaObject(User.class);
|
||||
return userService.addOne(user);
|
||||
}
|
||||
|
||||
@GetMapping("test")
|
||||
public void testTransactional(){
|
||||
this.userService.testTransactional();
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package com.fanxb.sjdemo.dao;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/22 15:21
|
||||
*/
|
||||
public interface BaseOperate <T>{
|
||||
long addOne(T t);
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.fanxb.sjdemo.dao;
|
||||
|
||||
import com.fanxb.sjdemo.entity.Dictionary;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/22 16:25
|
||||
*/
|
||||
public interface DictionaryDao {
|
||||
|
||||
/**
|
||||
* Description:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/26 10:16
|
||||
* @param dictionary dictionary
|
||||
* @return long
|
||||
*/
|
||||
long addOne(Dictionary dictionary);
|
||||
|
||||
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
package com.fanxb.sjdemo.dao;
|
||||
|
||||
import com.fanxb.sjdemo.entity.Order;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
@ -10,6 +13,35 @@ import com.fanxb.sjdemo.entity.Order;
|
||||
* @date 2019/3/22 16:25
|
||||
*/
|
||||
public interface OrderDao {
|
||||
/**
|
||||
* Description:
|
||||
*
|
||||
* @param order order
|
||||
* @return long
|
||||
* @author fanxb
|
||||
* @date 2019/3/25 14:23
|
||||
*/
|
||||
long addOne(Order order);
|
||||
|
||||
/**
|
||||
* Description:
|
||||
*
|
||||
* @param orderId orderId
|
||||
* @param userId userId
|
||||
* @return com.fanxb.sjdemo.entity.Order
|
||||
* @author fanxb
|
||||
* @date 2019/3/25 14:23
|
||||
*/
|
||||
Order selectOne(@Param("orderId") long orderId, @Param("userId") int userId);
|
||||
|
||||
/**
|
||||
* Description:
|
||||
*
|
||||
* @param id id
|
||||
* @return java.util.List<com.fanxb.sjdemo.entity.Order>
|
||||
* @author fanxb
|
||||
* @date 2019/3/25 14:24
|
||||
*/
|
||||
List<Order> getOrderByUserId(int id);
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
package com.fanxb.sjdemo.dao;
|
||||
|
||||
import com.fanxb.sjdemo.entity.OrderItem;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/22 16:25
|
||||
*/
|
||||
public interface OrderItemDao {
|
||||
/**
|
||||
* Description:
|
||||
*
|
||||
* @param order order
|
||||
* @author fanxb
|
||||
* @date 2019/3/25 14:12
|
||||
*/
|
||||
void addOne(OrderItem orderItem);
|
||||
|
||||
/**
|
||||
* Description:
|
||||
*
|
||||
* @param id id
|
||||
* @return java.util.List<com.fanxb.sjdemo.entity.OrderItem>
|
||||
* @author fanxb
|
||||
* @date 2019/3/25 14:12
|
||||
*/
|
||||
List<OrderItem> getByOrderId(int id);
|
||||
|
||||
}
|
@ -1,9 +1,6 @@
|
||||
package com.fanxb.sjdemo.dao;
|
||||
|
||||
import com.fanxb.sjdemo.entity.User;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
@ -13,7 +10,22 @@ import java.util.List;
|
||||
* @date 2019/3/22 15:29
|
||||
*/
|
||||
public interface UserDao {
|
||||
long addOne(User user);
|
||||
/**
|
||||
* Description:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/25 14:22
|
||||
* @param user user
|
||||
*/
|
||||
void addOne(User user);
|
||||
|
||||
User getOneById(int id);
|
||||
/**
|
||||
* Description:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/25 14:22
|
||||
* @param id id
|
||||
* @return com.fanxb.sjdemo.entity.User
|
||||
*/
|
||||
User getOneById(long id);
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
package com.fanxb.sjdemo.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/25 17:25
|
||||
*/
|
||||
@Data
|
||||
public class Dictionary {
|
||||
private Long dictionaryId;
|
||||
private String name;
|
||||
private String value;
|
||||
}
|
@ -8,7 +8,7 @@ import java.util.Date;
|
||||
public class Order {
|
||||
|
||||
private long orderId;
|
||||
private long uId;
|
||||
private long userId;
|
||||
private Date createTime;
|
||||
private long totalPrice;
|
||||
|
||||
|
@ -6,6 +6,7 @@ import lombok.Data;
|
||||
@Data
|
||||
public class OrderItem {
|
||||
|
||||
// private long userId;
|
||||
private long orderItemId;
|
||||
private long orderId;
|
||||
private String name;
|
||||
|
@ -1,33 +1,21 @@
|
||||
package com.fanxb.sjdemo.entity;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class User {
|
||||
|
||||
private long uId;
|
||||
private String name;
|
||||
private long age;
|
||||
private long userId;
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public long getuId() {
|
||||
return uId;
|
||||
public User() {
|
||||
}
|
||||
|
||||
public void setuId(long uId) {
|
||||
this.uId = uId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
public User(long userId, String name, int age) {
|
||||
this.userId = userId;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(long age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
package com.fanxb.sjdemo.service;
|
||||
|
||||
import com.fanxb.sjdemo.dao.DictionaryDao;
|
||||
import com.fanxb.sjdemo.entity.Dictionary;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/26 10:17
|
||||
*/
|
||||
@Service
|
||||
public class DictionaryService {
|
||||
|
||||
@Autowired
|
||||
private DictionaryDao dictionaryDao;
|
||||
|
||||
public long addOne(Dictionary dictionary) {
|
||||
this.dictionaryDao.addOne(dictionary);
|
||||
return dictionary.getDictionaryId();
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.fanxb.sjdemo.service;
|
||||
|
||||
import com.fanxb.sjdemo.dao.OrderItemDao;
|
||||
import com.fanxb.sjdemo.entity.OrderItem;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/25 14:53
|
||||
*/
|
||||
@Service
|
||||
public class OrderItemService {
|
||||
@Autowired
|
||||
private OrderItemDao orderItemDao;
|
||||
|
||||
public long addOne(OrderItem item){
|
||||
this.orderItemDao.addOne(item);
|
||||
return item.getOrderItemId();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -16,13 +16,12 @@ import org.springframework.stereotype.Service;
|
||||
*/
|
||||
@Service
|
||||
public class OrderService {
|
||||
private Logger logger = LoggerFactory.getLogger(OrderService.class);
|
||||
|
||||
@Autowired
|
||||
private OrderDao orderDao;
|
||||
|
||||
public void insertOne(Order order) {
|
||||
long id = orderDao.addOne(order);
|
||||
logger.info("订单插入id:{}", order.getOrderId());
|
||||
public long insertOne(Order order) {
|
||||
this.orderDao.addOne(order);
|
||||
return order.getOrderId();
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,16 @@ package com.fanxb.sjdemo.service;
|
||||
|
||||
import com.fanxb.sjdemo.dao.UserDao;
|
||||
import com.fanxb.sjdemo.entity.User;
|
||||
import io.shardingsphere.transaction.annotation.ShardingTransactionType;
|
||||
import io.shardingsphere.transaction.api.TransactionType;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.logging.ConsoleHandler;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
@ -15,21 +21,34 @@ import org.springframework.stereotype.Service;
|
||||
* @date 2019/3/22 15:36
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class UserService {
|
||||
Logger logger = LoggerFactory.getLogger(UserService.class);
|
||||
|
||||
@Autowired
|
||||
private UserDao dao;
|
||||
|
||||
public void addOne(String name, int age) {
|
||||
User user = new User();
|
||||
user.setName(name);
|
||||
user.setAge(age);
|
||||
long id= dao.addOne(user);
|
||||
logger.info("插入id:{}", user.getuId());
|
||||
public long addOne(User user) {
|
||||
this.dao.addOne(user);
|
||||
return user.getUserId();
|
||||
}
|
||||
|
||||
public User getOne(int id){
|
||||
public User getOne(long id) {
|
||||
return dao.getOneById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试跨库事务
|
||||
*/
|
||||
@ShardingTransactionType(TransactionType.XA)
|
||||
@Transactional
|
||||
public void testTransactional() {
|
||||
User user1 = new User(123, "988", 12);
|
||||
logger.info("user1已经插入");
|
||||
logger.info("user1已经插入");
|
||||
this.dao.addOne(user1);
|
||||
User user2 = new User(124, "988", 12);
|
||||
this.dao.addOne(user2);
|
||||
this.dao.addOne(user2);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
sharding:
|
||||
jdbc:
|
||||
datasource:
|
||||
names: ds0,ds1,ds2
|
||||
names: ds0,ds1
|
||||
ds0:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
@ -14,21 +14,31 @@ 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:
|
||||
# 默认数据源,可以将不分库分表的数据表放在这里
|
||||
default-data-source-name: ds2
|
||||
# 默认数据源,可以将不分库分表的数据表放在这里(此处的表需与已经分库分表的表完全没有关联,不会产生联表查询操作,因为跨库连表查询是没办法实现的),3.1.0版本dql查询存在bug,不使用默认库
|
||||
# default-data-source-name: ds2
|
||||
# 默认分库策略
|
||||
default-database-strategy:
|
||||
inline:
|
||||
sharding-column: uId
|
||||
algorithm-expression: ds$->{uId % 2}
|
||||
sharding-column: userId
|
||||
algorithm-expression: ds$->{userId % 2}
|
||||
# 配置表策略
|
||||
tables:
|
||||
# 公共表(比如字典表,角色表,权限表等),不分库或者分表,数据将发送到所有库中,方便联表查询
|
||||
dictionary:
|
||||
key-generator-column-name: dictionaryId
|
||||
actual-data-nodes: ds$->{0..1}.dictionary
|
||||
# user 前面已经根据uId分库,因此user表不进行分表
|
||||
user:
|
||||
key-generator-column-name: userId
|
||||
actual-data-nodes: ds$->{0..1}.user
|
||||
order:
|
||||
key-generator-column-name: orderId
|
||||
actual-data-nodes: ds$->{0..1}.order$->{0..1}
|
||||
@ -42,6 +52,6 @@ sharding:
|
||||
table-strategy:
|
||||
inline:
|
||||
sharding-column: orderId
|
||||
algorithm-expression: orderItemId$->{orderId%2}
|
||||
algorithm-expression: order_item$->{orderId%2}
|
||||
props:
|
||||
sql.show: true
|
@ -0,0 +1,9 @@
|
||||
<?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.DictionaryDao">
|
||||
|
||||
<insert id="addOne" useGeneratedKeys="true" keyProperty="dictionaryId" parameterType="dictionary">
|
||||
insert into dictionary(name,value) values(#{name},#{value})
|
||||
</insert>
|
||||
|
||||
</mapper>
|
@ -0,0 +1,13 @@
|
||||
<?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.OrderItemDao">
|
||||
|
||||
<insert id="addOne" parameterType="orderItem" useGeneratedKeys="true" keyProperty="orderItemId">
|
||||
insert into order_item(orderId,name,price) values(#{orderId},#{name},#{price})
|
||||
</insert>
|
||||
|
||||
<select id="getByOrderId" resultType="orderItem">
|
||||
select * from order_item where orderId=#{id}
|
||||
</select>
|
||||
|
||||
</mapper>
|
@ -2,8 +2,16 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.fanxb.sjdemo.dao.OrderDao">
|
||||
|
||||
<insert id="addOne" useGeneratedKeys="true" keyProperty="orderId">
|
||||
insert into order(uId,createTime,totalPrice) values(#{uId},#{createTime},#{totalPrice})
|
||||
<insert id="addOne" useGeneratedKeys="true" keyProperty="orderId" parameterType="user">
|
||||
insert into order(userId,createTime,totalPrice) values(#{userId},#{createTime},#{totalPrice})
|
||||
</insert>
|
||||
|
||||
<select id="selectOne" resultType="com.fanxb.sjdemo.entity.Order">
|
||||
select * from order where orderId=#{orderId} and userId=#{userId}
|
||||
</select>
|
||||
|
||||
<select id="getOrderByUserId" resultType="com.fanxb.sjdemo.entity.Order">
|
||||
select * from order a inner join user b on a.userId = b.userId where b.userId=#{id}
|
||||
</select>
|
||||
|
||||
</mapper>
|
@ -2,11 +2,19 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.fanxb.sjdemo.dao.UserDao">
|
||||
|
||||
<insert id="addOne" useGeneratedKeys="true" keyProperty="uId">
|
||||
insert into user(name,age) values(#{name},#{age})
|
||||
<insert id="addOne" useGeneratedKeys="true" keyProperty="userId">
|
||||
insert into user(name,age
|
||||
<if test="userId>0">
|
||||
,userId
|
||||
</if>
|
||||
) values(#{name},#{age}
|
||||
<if test="userId>0">
|
||||
,#{userId}
|
||||
</if>
|
||||
)
|
||||
</insert>
|
||||
|
||||
<select id="getOneById" resultType="User">
|
||||
|
||||
<select id="getOneById" resultType="user">
|
||||
select * from user where uId=#{id}
|
||||
</select>
|
||||
|
||||
|
@ -1,16 +1,24 @@
|
||||
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
|
||||
@ -21,15 +29,17 @@ public class SjdemoApplicationTests {
|
||||
@Resource
|
||||
private DataSource dataSource;
|
||||
|
||||
@Autowired
|
||||
private OrderDao orderDao;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
String sql = "SELECT i.* FROM order o JOIN order_item i ON o.orderId=i.orderId WHERE o.uId=? AND o.orderId=?";
|
||||
try (
|
||||
Connection conn = dataSource.getConnection();
|
||||
Statement statement = conn.createStatement();
|
||||
) {
|
||||
Boolean res = statement.execute("insert into user(name,age) value('2012-12-12 12:12:12',1212)");
|
||||
logger.info(res.toString());
|
||||
statement.execute("insert into user(name,age) value('2012-12-12 12:12:12',1212)");
|
||||
statement.execute("select * from user");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ public class OrderServiceTest {
|
||||
@Test
|
||||
public void addOrderTest() {
|
||||
Order order = new Order();
|
||||
order.setUId(12);
|
||||
order.setUserId(12);
|
||||
order.setCreateTime(new Date());
|
||||
order.setTotalPrice(12);
|
||||
orderService.insertOne(order);
|
||||
|
@ -27,7 +27,6 @@ public class UserServiceTest {
|
||||
|
||||
@Test
|
||||
public void addUserTest(){
|
||||
userService.addOne("xiaoming",12);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user