前言

Github:https://github.com/HealerJean

博客:http://blog.healerjean.com

1、开始Demo

1.1、hlj-02-vertical_shard.sql

drop database if exists ds_0;
create database ds_0 character set 'utf8' collate 'utf8_general_ci';
use ds_0;

drop table if exists user;
create table `user`
(
  `id`        bigint(20) unsigned not null,
  city        varchar(20)         not null default '',
  name        varchar(20)         not null default '',
  status      int(10)             not null default '0' comment '状态',
  create_time datetime            not null default current_timestamp comment '创建时间',
  update_time datetime            not null default current_timestamp on update current_timestamp comment '修改时间',
  primary key (`id`)
) engine = innodb
  default charset = utf8;


create table `demo_entity`
(
  `id`          bigint(20) unsigned not null comment '主键',
  `name`        varchar(64)         not null,
  `phone`       varchar(20)                  default '' comment '手机号',
  `email`       varchar(64)                  default '' comment '邮箱',
  `age`         int(10)                      default null,
  `status`      varchar(8)          not null comment '状态',
  `create_user` bigint(16) unsigned          default null comment '创建人',
  `create_name` varchar(64)                  default '' comment '创建人名称',
  `create_time` datetime            not null default current_timestamp comment '创建时间',
  `update_user` bigint(16) unsigned          default null comment '更新人',
  `update_name` varchar(64)                  default '' comment '更新人名称',
  `update_time` datetime            not null default current_timestamp on update current_timestamp comment '更新时间',
  primary key (`id`)
) engine = innodb
  default charset = utf8;


drop database if exists ds_1;
create database `ds_1` character set 'utf8' collate 'utf8_general_ci';
use ds_1;

drop table if exists company;
create table `company`
(
  `id`                 bigint(20) unsigned not null comment '主键',
  name                 varchar(20)         not null default '' comment '企业名称',
  company_name_english varchar(128)        not null default '' comment '企业英文名称',
  status               int(10)             not null default '0' comment '状态',
  create_time          datetime            not null default current_timestamp comment '创建时间',
  update_time          datetime            not null default current_timestamp on update current_timestamp comment '修改时间',

  primary key (`id`)
) engine = innodb
  default charset = utf8;

1.1.1、数据库图文

1585555871944

1.2、依赖


<!--shardingsphere-->
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>4.0.0-RC1</version>
</dependency>

1.3、配置文件:application.properties

server.port=8888


# 配置 mybatis的一些配置,也可以在 application.properties 中配置,如果配置了就不需要了mybatis.xml
#mybatis-plus.config-location=classpath:mybatis.xml
#Maven 多模块项目的扫描路径需以 classpath*: 开头 (即加载多个 jar 包下的 XML 文件)
mybatis-plus.mapper-locations=classpath*:mapper/*.xml
mybatis-plus.type-aliases-package=com.healerjean.proj.pojo
##主键类型  0:"数据库ID自增,非常大", 1:"用户输入ID(如果用户不输入,则默认是0)",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
mybatis-plus.id-type: 0
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
mybatis-plus.field-strategy: 2
#数据库大写下划线转换
mybatis-plus.capital-mode: true
mybatis-plus.refresh-mapper: true



# 垂直拆分(不同的表在不同的库中)
spring.shardingsphere.datasource.names=ds0,ds1
# #当遇到同样名字的时候,是否允许覆盖注册
spring.main.allow-bean-definition-overriding=true
# 显示SQL
spring.shardingsphere.props.sql.show=true

# 不会指定默认数据源。查询的时候会随机指定一个,如果表不存在会报错,插入的时候,会报错,提示没有数据源(如下)
### Error updating database.  Cause: java.lang.IllegalStateException: Missing the data source name: 'null'
### The error may exist in com/healerjean/proj/dao/mapper/DemoEntityMapper.java (best guess)
### The error may involve com.healerjean.proj.dao.mapper.DemoEntityMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO demo_entity  ( id, name, phone,   status )  VALUES  ( ?, ?, ?,   ? )
### Cause: java.lang.IllegalStateException: Missing the data source name: 'null'
# 数据源,
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.url=jdbc:mysql://localhost:3306/ds_0?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=123456
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.url=jdbc:mysql://localhost:3306/ds_1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=123456

# 绑定company表所在库
spring.shardingsphere.sharding.tables.company.actual-data-nodes=ds1.company
# 绑定user表所在库
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user
# 使用mybatis-plus的 id策略,不用下面的
# 设置自增ID
#spring.shardingsphere.sharding.tables.user.key-generator.column=id
# 设置自增ID算法
#spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE

1.4、具体测试方法和类

1.4.1、实体类

1.4.1.1、User.java

@Data
@Accessors(chain = true)
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    /** 主键  */
    private Long id;
    private String name;
    private String city;
    private String status;
    private Date createTime;
    private Date updateTime;
}


1.4.1.2、Company.java

@Data
public class Company {

	private Long id;
	private String name;
	private String companyNameEnglish;
	private String status;
	private Date createTime;
	private Date updateTime;
}

1.4.1.3、DemoEntity.java

@Data
@Accessors(chain = true)
public class DemoEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    private Long id;
    /** 姓名 */
    private String name;
    /** 手机号  */
    private String phone;
    /**  邮箱 */
    private String email;
    /** 年龄  */
    private Integer age;
    /**  10可用,99删除  */
    private String status;
    /** 创建人 */
    private Long createUser;
    /** 创建人名称  */
    private String createName;
    /**  创建时间 */
    private java.util.Date createTime;
    /**  更新人 */
    private Long updateUser;
    /** 更新人名称 */
    private String updateName;
    /**  更新时间 */
    private java.util.Date updateTime;

}

1.4.2、DTO数据

1.4.2.1、UserDTO.java

@Data
@Accessors(chain = true)
@ApiModel(value = "demo实体类")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserDTO {


    @ApiModelProperty(value = "主键", hidden = true)
    @JsonSerialize(using = JsonLongSerializer.class )
    private Long id;

    @ApiModelProperty(value = "姓名")
    @NotBlank(message = "姓名不能为空", groups = ValidateGroup.HealerJean.class)
    private String name;

    @ApiModelProperty(value = "城市")
    private String city;

    @ApiModelProperty(value = "状态", hidden = true)
    private String status;


    @ApiModelProperty(value = "创建时间", hidden = true)
    @JsonFormat(pattern = DateUtils.YYYY_MM_dd_HH_mm_ss, timezone = "GMT+8")
    private Date createTime;

    @ApiModelProperty(value = "修改时间", hidden = true)
    @JsonFormat(pattern = DateUtils.YYYY_MM_dd_HH_mm_ss, timezone = "GMT+8")
    private Date updateTime;

}


1.4.2.2、CompanyDTO.java


@Data
public class CompanyDTO {

	@JsonSerialize(using = JsonLongSerializer.class )
	private Long id;
	private String name;
	private String companyNameEnglish;
	private String status;


	@ApiModelProperty(value = "创建时间", hidden = true)
	@JsonFormat(pattern = DateUtils.YYYY_MM_dd_HH_mm_ss, timezone = "GMT+8")
	private Date createTime;

	@ApiModelProperty(value = "修改时间", hidden = true)
	@JsonFormat(pattern = DateUtils.YYYY_MM_dd_HH_mm_ss, timezone = "GMT+8")
	private Date updateTime;
}

1.4.2.3、DemoDTO.java

@Data
@Accessors(chain = true)
@ApiModel(value = "demo实体类")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class DemoDTO extends PageQuery {

    @JsonSerialize(using = JsonLongSerializer.class )
    private Long id;

    @ApiModelProperty(value = "姓名")
    @NotBlank(message = "姓名不能为空", groups = ValidateGroup.HealerJean.class)
    private String name;

    @ApiModelProperty(value = "年龄")
    private Integer age;

    @ApiModelProperty(value = "手机号")
    private String phone;

    @ApiModelProperty(value = "邮箱")
    private String email;

    @ApiModelProperty(value = "是否删除,10可用,99删除 ", hidden = true)
    private String status;

    @ApiModelProperty(value = "创建人", hidden = true)
    private Long createUser;

    @ApiModelProperty(value = "创建人名字", hidden = true)
    private String createName;

    @ApiModelProperty(value = "创建时间", hidden = true)
    private java.util.Date createTime;

    @ApiModelProperty(value = "更新人", hidden = true)
    private Long updateUser;

    @ApiModelProperty(value = "更新人名称", hidden = true)
    private String updateName;

    @ApiModelProperty(hidden = true)
   private java.util.Date updateTime;

}

1.4.3、Mapper

1.4.3.1、UserMapper.java

public interface UserMapper extends BaseMapper<User> {


}

1.4.3.1、CompanyMapper.java

public interface CompanyMapper  extends BaseMapper<Company> {


}

1.4.3.1、DemoEntityMapper.java

public interface DemoEntityMapper extends BaseMapper<DemoEntity> {

}

1.4.4、Service

1.4.4.1、 UserService.java

public interface UserService {


    UserDTO insert(UserDTO userDTO);

    UserDTO findById(Long id);

    List<UserDTO> list();

}

1.4.4.2、 CompanyService.java

public interface CompanyService {


    CompanyDTO insert(CompanyDTO companyDTO);

    CompanyDTO findById(Long id);

    List<CompanyDTO> list();
}

1.4.4.3、 DemoEntityService.java

public interface DemoEntityService {


    DemoDTO insert(DemoDTO demoEntity);

    DemoDTO findById(Long id);

    List<DemoDTO> list();


    /**
     * 测试多个数据库事务
     */
    void dbTransactional(UserDTO userDTO, CompanyDTO companyDTO);

}

1.4.5、ServiceImpl.java

1.4.5.1、UserServiceImpl.java

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;


    @Override
    public UserDTO insert(UserDTO userDTO) {
        User user = BeanUtils.dtoToUserDTO(userDTO);
        user.setStatus(StatusEnum.生效.code);
        userMapper.insert(user);
        userDTO.setId(user.getId());
        return userDTO;
    }

    @Override
    public UserDTO findById(Long id) {
        User user = userMapper.selectById(id);
        return user == null ? null : BeanUtils.userToDTO(user);
    }

    @Override
    public List<UserDTO> list() {
        List<User> users = userMapper.selectList(null);
        List<UserDTO> list = null;
        if (!EmptyUtil.isEmpty(users)) {
            list = users.stream().map(BeanUtils::userToDTO).collect(Collectors.toList());
        }
        return list;
    }

}

1.4.5.2、CompanyServiceImpl.java

@Service
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;

    @Override
    public CompanyDTO insert(CompanyDTO companyDTO) {
        Company company = BeanUtils.dtoToCompany(companyDTO);
        company.setStatus(StatusEnum.生效.code);
        companyMapper.insert(company);
        companyDTO.setId(company.getId());
        return companyDTO;
    }

    @Override
    public CompanyDTO findById(Long id) {
        Company company = companyMapper.selectById(id);
        return company == null ? null : BeanUtils.companyToDTO(company);
    }

    @Override
    public List<CompanyDTO> list() {
        List<Company> companys = companyMapper.selectList(null);
        List<CompanyDTO> list = null;
        if (!EmptyUtil.isEmpty(companys)) {
            list = companys.stream().map(BeanUtils::companyToDTO).collect(Collectors.toList());
        }
        return list;
    }
}

1.4.5.3、DemoEntityServiceImpl.java

@Service
@Slf4j
public class DemoEntityServiceImpl implements DemoEntityService {


    @Resource
    private DemoEntityMapper demoEntityMapper;

    @Resource
    private CompanyService companyService;
    @Resource
    private UserService userService;

    @Override
    public DemoDTO insert(DemoDTO demoDTO) {
        DemoEntity demoEntity = BeanUtils.dtoToDemo(demoDTO);
        demoEntity.setStatus(StatusEnum.生效.code);
        demoEntityMapper.insert(demoEntity);
        demoDTO.setId(demoEntity.getId());
        return demoDTO;
    }


    @Override
    public DemoDTO findById(Long id) {
        DemoEntity demoEntity = demoEntityMapper.selectById(id);
        return demoEntity == null ? null : BeanUtils.demoToDTO(demoEntity);
    }

    @Override
    public List<DemoDTO> list() {
        List<DemoDTO> collect = null;
        List<DemoEntity> list = demoEntityMapper.selectList(null);
        if (!EmptyUtil.isEmpty(list)) {
            collect = list.stream().map(BeanUtils::demoToDTO).collect(Collectors.toList());
        }
        return collect;
    }


    /**
     * 分库分表也是有事务的,如果跑出了异常,则都不能成功
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void dbTransactional(UserDTO userDTO, CompanyDTO companyDTO) {
        userService.insert(userDTO);
        companyService.insert(companyDTO);
        int i = 1/0;
    }

}

1.4.6、Controller

1.4.6.1、UserController.java

@ApiResponses(value = {
        @ApiResponse(code = 200, message = "访问正常"),
        @ApiResponse(code = 301, message = "逻辑错误"),
        @ApiResponse(code = 500, message = "系统错误"),
        @ApiResponse(code = 401, message = "未认证"),
        @ApiResponse(code = 403, message = "禁止访问"),
        @ApiResponse(code = 404, message = "url错误")
})
@Api(description = "demo控制器")
@Controller
@RequestMapping("hlj/demo")
@Slf4j
public class UserController {



    @Autowired
    private UserService userService;

    @ApiOperation(value = "insert",
            notes = "insert",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = UserDTO.class)
    @PostMapping(value = "insert", produces = "application/json; charset=utf-8")
    @ResponseBody
    public ResponseBean insert(UserDTO userDTO) {
        log.info("样例--------mybaits-plus添加demo实体------数据信息{}", userDTO);
        String validate = ValidateUtils.validate(userDTO, ValidateGroup.HealerJean.class);
        if (!validate.equals(CommonConstants.COMMON_SUCCESS)) {
            throw new BusinessException(ResponseEnum.参数错误, validate);
        }
        return ResponseBean.buildSuccess(userService.insert(userDTO));
    }


    @ApiOperation(notes = "findById",
            value = "findById",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = UserDTO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "demo主键", required = true, paramType = "path", dataType = "long"),
    })
    @GetMapping("findById/{id}")
    @ResponseBody
    public ResponseBean findById(@PathVariable Long id) {
        log.info("样例--------findById------数据:id:{}", id);
        return ResponseBean.buildSuccess(userService.findById(id));
    }

    @ApiOperation(notes = "list",
            value = "list",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = UserDTO.class)
    @GetMapping("list")
    @ResponseBody
    public ResponseBean list() {
        log.info("样例--------list------");
        return ResponseBean.buildSuccess(userService.list());
    }


}

1.4.6.2、CompanyController.java

@ApiResponses(value = {
        @ApiResponse(code = 200, message = "访问正常"),
        @ApiResponse(code = 301, message = "逻辑错误"),
        @ApiResponse(code = 500, message = "系统错误"),
        @ApiResponse(code = 401, message = "未认证"),
        @ApiResponse(code = 403, message = "禁止访问"),
        @ApiResponse(code = 404, message = "url错误")
})
@Api(description = "demo控制器")
@Controller
@RequestMapping("hlj/company")
@Slf4j
public class CompanyController {


    @Autowired
    private CompanyService companyService;

    @ApiOperation(value = "insert",
            notes = "insert",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = UserDTO.class)
    @PostMapping(value = "insert", produces = "application/json; charset=utf-8")
    @ResponseBody
    public ResponseBean insert(CompanyDTO companyDTO) {
        log.info("user--------insert------请求参数:{}", companyDTO);
        return ResponseBean.buildSuccess(companyService.insert(companyDTO));
    }


    @ApiOperation(notes = "findById",
            value = "findById",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = UserDTO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "demo主键", required = true, paramType = "path", dataType = "long"),
    })
    @GetMapping("findById/{id}")
    @ResponseBody
    public ResponseBean findById(@PathVariable Long id) {
        log.info("company--------findById------id:{}", id);
        return ResponseBean.buildSuccess(companyService.findById(id));
    }



    @ApiOperation(notes = "list",
            value = "list",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = UserDTO.class)
    @GetMapping("list")
    @ResponseBody
    public ResponseBean list() {
        log.info("company--------list------");
        return ResponseBean.buildSuccess(companyService.list());
    }

}

1.4.6.3、DemoController.java

@ApiResponses(value = {
        @ApiResponse(code = 200, message = "访问正常"),
        @ApiResponse(code = 301, message = "逻辑错误"),
        @ApiResponse(code = 500, message = "系统错误"),
        @ApiResponse(code = 401, message = "未认证"),
        @ApiResponse(code = 403, message = "禁止访问"),
        @ApiResponse(code = 404, message = "url错误")
})
@Api(description = "demo控制器")
@Controller
@RequestMapping("hlj/demo")
@Slf4j
public class DemoController {

    @Autowired
    private DemoEntityService demoEntityService;


    @ApiOperation(value = "insert",
            notes = "insert",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = UserDTO.class)
    @PostMapping(value = "insert", produces = "application/json; charset=utf-8")
    @ResponseBody
    public ResponseBean insert(DemoDTO demoDTO) {
        log.info("demo--------insert------请求参数:{}", demoDTO);
        return ResponseBean.buildSuccess(demoEntityService.insert(demoDTO));
    }


    @ApiOperation(notes = "findById",
            value = "findById",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = UserDTO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "demo主键", required = true, paramType = "path", dataType = "long"),
    })
    @GetMapping("findById/{id}")
    @ResponseBody
    public ResponseBean findById(@PathVariable Long id) {
        log.info("demo--------findById------id:{}", id);
        return ResponseBean.buildSuccess(demoEntityService.findById(id));
    }



    @ApiOperation(notes = "list",
            value = "list",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = UserDTO.class)
    @GetMapping("list")
    @ResponseBody
    public ResponseBean list() {
        log.info("demo--------list------");
        return ResponseBean.buildSuccess(demoEntityService.list());
    }

    @ApiOperation(notes = "dbTransactional",
            value = "dbTransactional",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = UserDTO.class)
    @PostMapping("dbTransactional")
    @ResponseBody
    public ResponseBean dbTransactional(UserDTO userDTO, CompanyDTO companyDTO) {
        log.info("demo--------dbTransactional------");
        demoEntityService.dbTransactional(userDTO, companyDTO);
        return ResponseBean.buildSuccess("执行成功");
    }


}

1.5、开始测试

1.5.1、UserController测试

1.5.1、插入数据: /hlj/user/insert

1.5.1.1、测试数据
name city
HealerJean 北京
XiaoMing 山西
1.5.1.2、查询数据库ds_0user表的数据
select * from ds_0.user

<!DOCTYPE html>

idcitynamestatuscreate_timeupdate_time
1244541509103194113北京HealerJean102020-03-30 16:26:332020-03-30 16:26:33
1244541907067146241山西XiaoMing102020-03-30 16:28:072020-03-30 16:28:07

1.5.2、CompanyController测试

1.5.1、插入数据: /hlj/company/insert

1.5.1.1、测试数据
name companyNameEnglish
字节跳动 zijie
小米 xiaomi
1.5.1.2、查询数据库ds_1company表的数据
select * from ds_1.company;

<!DOCTYPE html>

idnamecompany_name_englishstatuscreate_timeupdate_time
1244543048496648194字节跳动zijie102020-03-30 16:32:392020-03-30 16:32:39
1244543217464184833小米小米102020-03-30 16:33:202020-03-30 16:33:20

1.5.3、归纳总结

通过上面的两个插入就可以看出来,纵向分库实现了

问题

1、如果不指定默认数据源,有其他表存在的情况会使用哪个数据源呢?

1.1、DemoController测试

1.1.1、插入数据:/hlj/demo/insert

1.1.2、查询数据:/hlj/demo/findById

1.2、归纳总结

**查询的时候:会随机指定一个,如果对应数据库表不存在,会报错(如下), **

Caused by: java.sql.SQLSyntaxErrorException: Table 'ds_1.demo_entity' doesn't exist

插入的时候,会报错,提示没有数据源(如下)

### Error updating database.  Cause: java.lang.IllegalStateException: Missing the data source name: 'null'
### The error may exist in com/healerjean/proj/dao/mapper/DemoEntityMapper.java (best guess)
### The error may involve com.healerjean.proj.dao.mapper.DemoEntityMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO demo_entity  ( id, name, phone,   status )  VALUES  ( ?, ?, ?,   ? )
### Cause: java.lang.IllegalStateException: Missing the data source name: 'null'

1.2、多个数据库事务一致性

1.2.1、DemoController测试

1.2.1.1、DemoEntityService

/**
* 测试多个数据库事务
*/
void dbTransactional(UserDTO userDTO, CompanyDTO companyDTO);

1.2.1.2、DemoEntityServiceImpl


/**
* 分库分表也是有事务的,如果跑出了异常,则都不能成功
*/
@Transactional(rollbackFor = Exception.class)
@Override
public void dbTransactional(UserDTO userDTO, CompanyDTO companyDTO) {
    userService.insert(userDTO);
    companyService.insert(companyDTO);
    int i = 1/0;
}

1.2.1.3、DemoController

   @ApiOperation(notes = "dbTransactional",
            value = "dbTransactional",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = UserDTO.class)
    @PostMapping("dbTransactional")
    @ResponseBody
    public ResponseBean dbTransactional(UserDTO userDTO, CompanyDTO companyDTO) {
        log.info("demo--------dbTransactional------");
        demoEntityService.dbTransactional(userDTO, companyDTO);
        return ResponseBean.buildSuccess("执行成功");
    }

1.2.1.4、测试并总结

可以看到抛出了异常,两个数据库分表对应的表user、company中也有新添加的数据,说明事务是有一致性的

2020-03-30 16:49:00 ERROR -[                                ]- ====系统错误=== java.lang.ArithmeticException: / by zero
	at com.healerjean.proj.service.impl.DemoEntityServiceImpl.dbTransactional(DemoEntityServiceImpl.java:76) ~[classes/:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_201]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_201]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_201]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_201]
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.1.7.RELEASE.jar:5.1.7.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.1.7.RELEASE.jar:5.1.7.RELEASE]

ContactAuthor