[代码完善](master): 完善sharding-jdbc功能及测试

完善了sharding-jdbc的CRUD并测试
配置了XA事务,但是还未测试
配置了动态数据源,但是还未测试
master
土豆兄弟 3 years ago
parent b5935c489f
commit 24f37e5382

@ -18,4 +18,5 @@
查看配置文件 查看配置文件
### 2. ### 2. 事务支持
https://segmentfault.com/a/1190000023379017

@ -15,6 +15,7 @@
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target> <maven.compiler.target>8</maven.compiler.target>
<spring.boot.version>2.3.2.RELEASE</spring.boot.version> <spring.boot.version>2.3.2.RELEASE</spring.boot.version>
<apache.shardingsphere.version>4.1.1</apache.shardingsphere.version>
</properties> </properties>
<packaging>pom</packaging> <packaging>pom</packaging>
@ -31,14 +32,28 @@
<dependency> <dependency>
<groupId>org.apache.shardingsphere</groupId> <groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version> <version>${apache.shardingsphere.version}</version>
</dependency> </dependency>
<!-- for spring namespace --> <!-- for spring namespace -->
<dependency> <dependency>
<groupId>org.apache.shardingsphere</groupId> <groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId> <artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>4.1.1</version> <version>${apache.shardingsphere.version}</version>
</dependency>
<!-- 使用XA事务时需要引入此模块 -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-transaction-xa-core</artifactId>
<version>${apache.shardingsphere.version}</version>
</dependency>
<!-- 使用BASE事务时需要引入此模块 -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-transaction-base-seata-at</artifactId>
<version>${apache.shardingsphere.version}</version>
</dependency> </dependency>
@ -87,6 +102,7 @@
<artifactId>druid</artifactId> <artifactId>druid</artifactId>
<version>1.2.4</version> <version>1.2.4</version>
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
</project> </project>

@ -34,6 +34,11 @@
<artifactId>sharding-jdbc-spring-namespace</artifactId> <artifactId>sharding-jdbc-spring-namespace</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-transaction-xa-core</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>

@ -0,0 +1,31 @@
/*
ps:
Source Server : mysql
Source Server Type : MySQL
Source Server Version : 80026
Source Host : localhost:3306
Source Schema : shard-1
Target Server Type : MySQL
Target Server Version : 80026
File Encoding : 65001
Date: 24/10/2021 01:42:50
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_order_0
-- ----------------------------
DROP TABLE IF EXISTS `t_order`;
CREATE TABLE `t_order_0` (
`id` bigint NOT NULL AUTO_INCREMENT,
`user_id` bigint NOT NULL,
`order_name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
SET FOREIGN_KEY_CHECKS = 1;

@ -2,6 +2,10 @@ package com.baiye;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/** /**
* @author q * @author q
@ -12,4 +16,19 @@ public class ShardingMultipleDataSourceApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(ShardingMultipleDataSourceApplication.class, args); SpringApplication.run(ShardingMultipleDataSourceApplication.class, args);
} }
/**
* open-in-view=false使 no session
*
* OpenEntityManagerInViewFiltersessionwebsession
*/
@Bean
public FilterRegistrationBean<OpenEntityManagerInViewFilter> openEntityManagerInViewFilterFilterRegistrationBean(){
FilterRegistrationBean<OpenEntityManagerInViewFilter> filterRegistrationBean = new FilterRegistrationBean<>();
OpenEntityManagerInViewFilter filter = new OpenEntityManagerInViewFilter();
filterRegistrationBean.setFilter(filter);
filterRegistrationBean.setOrder(5);
return filterRegistrationBean;
}
} }

@ -0,0 +1,29 @@
package com.baiye.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
/**
* shardingJDBC -
*/
@Configuration
@EnableTransactionManagement
public class TransactionConfiguration {
@Bean(name = "transactionManager")
public PlatformTransactionManager txManager(final DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public JdbcTemplate jdbcTemplate(final DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}

@ -10,7 +10,7 @@ import org.springframework.transaction.annotation.Transactional;
/** /**
* @author q * @author q
*/ */
public interface OrderRepository extends JpaRepository<Order,Integer> { public interface OrderRepository extends JpaRepository<Order,Long> {
@Query(value = "insert into order (id,ordername) values (?1,?2)", nativeQuery = true) @Query(value = "insert into order (id,ordername) values (?1,?2)", nativeQuery = true)
@Modifying @Modifying

@ -7,14 +7,16 @@ import java.io.Serializable;
@Entity @Entity
@Data @Data
@Table(name = "order") @Table(name = "t_order")
public class Order implements Serializable { public class Order implements Serializable {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id; private Long id;
private String userid; @Column(name = "user_id")
private Long userId;
private String ordername; @Column(name = "order_name")
private String orderName;
} }

@ -3,11 +3,14 @@ package com.baiye.service;
import com.baiye.dao.OrderRepository; import com.baiye.dao.OrderRepository;
import com.baiye.domain.Order; import com.baiye.domain.Order;
import com.baiye.domain.User; import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
import java.util.Optional;
/** /**
* @author q * @author q
@ -21,35 +24,45 @@ public class ShardService {
/** /**
* *
*/ */
public Order insertOne(Order order){ public Order insertOne(Order order) {
return orderRepository.save(order); return orderRepository.save(order);
} }
/** /**
* *
*/ */
public List<Order> batchInsert(List<Order> orders){ public List<Order> batchInsert(List<Order> orders) {
return orderRepository.saveAll(orders); return orderRepository.saveAll(orders);
} }
public Order queryOne(Long id) {
Optional<Order> optional = orderRepository.findById(id);
return optional.orElse(null);
}
/** /**
* *
*/ */
public List<Order> queryByCondition(){ public List<Order> queryByCondition(List<Long> ids) {
return orderRepository.findAll(); return orderRepository.findAllById(ids);
} }
/** /**
* *
* <p>
* TransactionType.LOCAL, TransactionType.XA(), TransactionType.BASE
*/ */
public Order updateByCondition(Order order){ @Transactional(value = "transactionManager")
@ShardingTransactionType(TransactionType.XA)
public Order updateByCondition(Order order) {
return orderRepository.save(order); return orderRepository.save(order);
} }
/** /**
* *
*/ */
public void deleterByCondition(Order order){ public void deleterByCondition(Order order) {
orderRepository.delete(order); orderRepository.delete(order);
} }

@ -1,22 +1,23 @@
spring.shardingsphere.datasource.names=ds0,ds1 spring.shardingsphere.datasource.names=ds0,ds1
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/shard-3 spring.shardingsphere.datasource.ds0.url=jdbc:mysql://127.0.0.1:3306/shard-1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&useTimezone=true
spring.shardingsphere.datasource.ds0.username=root spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=root spring.shardingsphere.datasource.ds0.password=root
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/shard-4 spring.shardingsphere.datasource.ds1.url=jdbc:mysql://127.0.0.1:3306/shard-2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&useTimezone=true
spring.shardingsphere.datasource.ds1.username=root spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=root spring.shardingsphere.datasource.ds1.password=root
spring.shardingsphere.sharding.tables.order.actual-data-nodes=ds$->{0..1}.order$->{0..1}
spring.shardingsphere.sharding.tables.order.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.order.table-strategy.inline.algorithm-expression=id$->{id % 2}
spring.shardingsphere.sharding.tables.order.key-generator.column=userid
spring.shardingsphere.sharding.tables.order.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=id spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{id % 2} spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{id % 2}
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order_$->{0..1}
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{user_id % 2}
spring.shardingsphere.sharding.tables.t_order.key-generator.column=id
spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE

@ -9,4 +9,6 @@ spring:
properties: properties:
hibernate: hibernate:
show_sql: true show_sql: true
database: mysql
main:
allow-bean-definition-overriding: true

@ -4,13 +4,25 @@ package com.baiye.service;
import com.baiye.ShardingMultipleDataSourceApplication; import com.baiye.ShardingMultipleDataSourceApplication;
import com.baiye.dao.OrderRepository; import com.baiye.dao.OrderRepository;
import com.baiye.domain.Order; import com.baiye.domain.Order;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
/**
* :
*
* :
* - 使JPA
* - 使 JdbcTemplate
*/
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = ShardingMultipleDataSourceApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(classes = ShardingMultipleDataSourceApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ShardServiceTest { public class ShardServiceTest {
@ -28,30 +40,90 @@ public class ShardServiceTest {
@Test @Test
public void insertOne() { public void insertOne() {
Order order = new Order(); Order order = new Order();
order.setOrdername("111"); order.setOrderName("111");
order.setUserId(1L);
Order order1 = shardService.insertOne(order); Order order1 = shardService.insertOne(order);
System.out.println("order1 = " + order1); System.out.println("order1 = " + order1);
} }
@Test @Test
public void batchInsert() { public void batchInsert() {
for (int i = 1; i < 1000; i++) { List<Order> orders = new ArrayList<>();
for (long i = 1; i < 1000; i++) {
Order order = new Order(); Order order = new Order();
// order.setUserid(i); order.setUserId(i);
order.setOrdername("testOrder" + i); order.setOrderName("testOrder" + i);
orders.add(order);
}
List<Order> orders1 = shardService.batchInsert(orders);
System.out.println("orders1.size() = " + orders1.size());
}
@Test
public void queryOne() {
Order order = shardService.queryOne(658828065589166080L);
System.out.println("order = " + order);
} }
@Test
public void queryByIds() {
List<Long> ids = new ArrayList<>();
ids.add(658828067556294656L); // 分表1
ids.add(658828070718799872L);
ids.add(658828067615014913L);
ids.add(658828066444804097L);
List<Order> orders = shardService.queryByCondition(ids);
System.out.println("orders = " + orders);
} }
@Test @Test
public void queryByCondition() { public void updateByJDBC() {
jdbcTemplate.execute("UPDATE t_order SET order_name = ? WHERE id = ?",
(PreparedStatementCallback<Object>) preparedStatement -> {
preparedStatement.setObject(1, "test");
preparedStatement.setObject(2, 1L);
return preparedStatement.executeUpdate();
});
// Order order = new Order();
// order.setUserId(1L);
// order.setOrderName("test");
// Order order1 = shardService.updateByCondition(order);
// System.out.println("order1 = " + order1);
Order order2 = shardService.queryOne(658828065589166080L);
System.out.println("order2 = " + order2);
} }
/**
* fixme ,使
*/
@Test @Test
public void updateByCondition() { @Ignore
public void updateByJPA() {
Order order = new Order();
order.setUserId(2L);
order.setOrderName("test");
Order order1 = shardService.updateByCondition(order);
System.out.println("order1 = " + order1);
Order order2 = shardService.queryOne(658828066071511041L);
System.out.println("order2 = " + order2);
} }
@Test @Test
public void deleterByCondition() { public void deleterByCondition() {
jdbcTemplate.execute("DELETE FROM t_order WHERE id = ?",
(PreparedStatementCallback<Object>) preparedStatement -> {
preparedStatement.setObject(1, 658828067426271232L);
return preparedStatement.executeUpdate();
});
Order order2 = shardService.queryOne(658828067426271232L);
System.out.println("order2 = " + order2);
} }
} }
Loading…
Cancel
Save