新增 sharding-jdbc 实例

This commit is contained in:
Administrator
2020-10-30 12:34:02 +08:00
parent ce35fe7aac
commit fb945b16d9
11 changed files with 230 additions and 71 deletions

View File

@ -1,6 +1,12 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.chinaunicom.ebtp</groupId>
<artifactId>mall-ebtp-cloud-parent</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.chinaunicom.mall.ebtp.cloud</groupId>
<artifactId>sharding-jdbc-example</artifactId>
@ -8,18 +14,28 @@
<packaging>jar</packaging>
<name>sharding-jdbc-example</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
<groupId>com.chinaunicom.ebtp</groupId>
<artifactId>mall-ebtp-cloud-jpa-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,13 +0,0 @@
package com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

View File

@ -0,0 +1,11 @@
package com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ShardingJdbcExampleApplication {
public static void main(String[] args) {
SpringApplication.run(ShardingJdbcExampleApplication.class, args);
}
}

View File

@ -0,0 +1,11 @@
package com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example.mapper;
import org.apache.ibatis.annotations.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example.model.Demo;
@Mapper
public interface DemoMapper extends BaseMapper<Demo> {
}

View File

@ -0,0 +1,11 @@
package com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example.mapper;
import org.apache.ibatis.annotations.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example.model.UndoLog;
@Mapper
public interface UndoLogMapper extends BaseMapper<UndoLog> {
}

View File

@ -0,0 +1,28 @@
package com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example.model;
import java.sql.Date;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class Demo {
private Long id;
private String col1;
private String col2;
/** 参数自动注入 */
@TableField(fill = FieldFill.INSERT)
private String createBy;
private Date createTime;
private String updateBy;
private Date updateTime;
private int delFlag;
}

View File

@ -0,0 +1,21 @@
package com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example.model;
import java.util.Date;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class UndoLog {
private Long id;
private Long branchId;
private String xid;
private String context;
private String rollbackInfo;
private Integer logStatus;
private Date logCreated;
private Date logModified;
}

View File

@ -0,0 +1,50 @@
spring:
shardingsphere:
datasource:
names: ds0, ds0slave1, ds0slave2, ds1
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://125.32.114.204:13306/ebtp-cloud?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: mall3-ebtp-dev
password: mall3-ebtp-dev
# 从库
ds0slave1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://125.32.114.204:13306/ebtp-cloud?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: mall3-ebtp-dev
password: mall3-ebtp-dev
ds0slave2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://125.32.114.204:13306/ebtp-cloud?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: mall3-ebtp-dev
password: mall3-ebtp-dev
ds1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://125.32.114.204:13306/ebtp_mall_evaluation?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: mall3-ebtp-dev
password: mall3-ebtp-dev
# 分库配置
sharding:
tables:
demo:
actual-data-nodes: ds0.demo
undo_log:
actual-data-nodes: ds1.undo_log
#读写分离
master-slave-rules:
ds0:
master-data-source-name: ds0
slave-data-source-names:
- ds0slave1
- ds0slave2

View File

@ -0,0 +1,10 @@
spring:
profiles:
active: shardingJdbc
application:
name: mall-ebtp-cloud-demo
logging:
level:
root: info

View File

@ -1,38 +0,0 @@
package com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

View File

@ -0,0 +1,52 @@
package com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example;
import java.util.Date;
import java.util.List;
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 com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example.mapper.DemoMapper;
import com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example.mapper.UndoLogMapper;
import com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example.model.Demo;
import com.chinaunicom.mall.ebtp.cloud.shardingjdbc.example.model.UndoLog;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShardingJdbcExampleApplication.class)
public class ShardingJdbcExampleTest {
private @Autowired DemoMapper demoMapper;
private @Autowired UndoLogMapper undoLogMapper;
@Test
public void testInsertDemo() {
Demo demo = new Demo().setCol1("123");
demoMapper.insert(demo);
}
@Test
public void testInsertUndolog() {
UndoLog undolog = new UndoLog();
undolog.setBranchId(100L);
undolog.setContext("23424");
undolog.setLogCreated(new Date());
undolog.setLogModified(new Date());
undolog.setLogStatus(1);
undolog.setRollbackInfo("234243");
undolog.setXid("2342");
undoLogMapper.insert(undolog);
}
@Test
public void testSelectFromDemo() {
List<Demo> list = demoMapper.selectList(new QueryWrapper<Demo>().select());
System.out.println(list.size());
}
}