# Sharding-jdbc简介
Sharding-jdbc是开源的数据库操作中间件;定位为轻量级Java框架,在Java的JDBC层提供的额外服务。它使用客户端直连数据库,以jar包形式提供服务,无需额外部署和依赖,可理解为增强版的JDBC驱动,完全兼容JDBC和各种ORM框架。
官方文档地址:https://shardingsphere.apache.org/document/current/cn/overview/
本文demo实现了分库分表功能。如有错误,欢迎各位在评论中指出。不胜感激!
# 项目结构
首先创建一个一般的Spring boot项目,项目采用三层架构,结构图如下:
添加图片注释,不超过 140 字(可选)
POM.xml文件如下:
实体类以书本为例
package com.macky.springbootshardingjdbc.entity; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.extension.activerecord.Model; import groovy.transform.EqualsAndHashCode; import lombok.Data; import lombok.experimental.Accessors; /** * @author Macky * @Title class Book * @Description: 书籍是实体类 * @date 2019/7/13 15:23 */ @Data @EqualsAndHashCode(callSuper = true) @Accessors(chain = true) @TableName("book") public class Book extends Model
开放保存和查询两个接口,代码如下:
package com.macky.springbootshardingjdbc.controller; import com.macky.springbootshardingjdbc.entity.Book; import com.macky.springbootshardingjdbc.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * @author Macky * @Title class BookController * @Description: TODO * @date 2019/7/12 20:53 */ @RestController public class BookController { @Autowired BookService bookService; @RequestMapping(value = "/book", method = RequestMethod.GET) public List
BookServiceImpl.java
package com.macky.springbootshardingjdbc.service.impl; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.macky.springbootshardingjdbc.entity.Book; import com.macky.springbootshardingjdbc.mapper.BookMapper; import com.macky.springbootshardingjdbc.service.BookService; import org.springframework.stereotype.Service; import java.util.List; /** * @author Macky * @Title class BookServiceImpl * @Description: TODO * @date 2019/7/12 20:47 */ @Service public class BookServiceImpl extends ServiceImpl
BookMapper.java
package com.macky.springbootshardingjdbc.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.macky.springbootshardingjdbc.entity.Book; /** * @author Macky * @Title class BookMapper * @Description: TODO * @date 2019/7/12 20:46 */ public interface BookMapper extends BaseMapper
创建数据库表,DDL语句如下
创建数据库表数据 CREATE DATABASE IF NOT EXISTS `db0`; USE `db0`; DROP TABLE IF EXISTS `book_0`; CREATE TABLE `book_0` ( `id` INT ( 11 ) NOT NULL, `name` VARCHAR ( 255 ) DEFAULT NULL, `count` INT ( 11 ) DEFAULT NULL, PRIMARY KEY ( `id` ) ) ENGINE = INNODB DEFAULT CHARSET = utf8mb4; DROP TABLE IF EXISTS `book_1`; CREATE TABLE `book_1` ( `id` INT ( 11 ) NOT NULL, `name` VARCHAR ( 255 ) DEFAULT NULL, `count` INT ( 11 ) DEFAULT NULL, PRIMARY KEY ( `id` ) ) ENGINE = INNODB DEFAULT CHARSET = utf8mb4; CREATE DATABASE IF NOT EXISTS `db1`; USE `db1`; DROP TABLE IF EXISTS `book_0`; CREATE TABLE `book_0` ( `id` INT ( 11 ) NOT NULL, `name` VARCHAR ( 255 ) DEFAULT NULL, `count` INT ( 11 ) DEFAULT NULL, PRIMARY KEY ( `id` ) ) ENGINE = INNODB DEFAULT CHARSET = utf8mb4; DROP TABLE IF EXISTS `book_1`; CREATE TABLE `book_1` ( `id` INT ( 11 ) NOT NULL, `name` VARCHAR ( 255 ) DEFAULT NULL, `count` INT ( 11 ) DEFAULT NULL, PRIMARY KEY ( `id` ) ) ENGINE = INNODB DEFAULT CHARSET = utf8mb4; CREATE DATABASE IF NOT EXISTS `db2`; USE `db2`; DROP TABLE IF EXISTS `book_0`; CREATE TABLE `book_0` ( `id` INT ( 11 ) NOT NULL, `name` VARCHAR ( 255 ) DEFAULT NULL, `count` INT ( 11 ) DEFAULT NULL, PRIMARY KEY ( `id` ) ) ENGINE = INNODB DEFAULT CHARSET = utf8mb4; DROP TABLE IF EXISTS `book_1`; CREATE TABLE `book_1` ( `id` INT ( 11 ) NOT NULL, `name` VARCHAR ( 255 ) DEFAULT NULL, `count` INT ( 11 ) DEFAULT NULL, PRIMARY KEY ( `id` ) ) ENGINE = INNODB DEFAULT CHARSET = utf8mb4;
配置分库分表策略application.properties:
> 推荐一个艿艿写的 3000+ Star 的 SpringCloud Alibaba 电商开源项目的仓库:
接口测试使用postman
示例:
GET请求------>http://localhost:8080/book
POST请求:------->http://localhost:8080/book?id=1&name=java编程思想&count=8
demo的github地址:
https://github.com/Macky-He/spring-boot--shardingsphere-examples
如各位觉得有帮助的话,还请给个star鼓励鼓励博主,谢谢!
# 总结
分库分表实现按照官方文档做一个demo是第一步,如需深入还需要研究源码,研究架构,研究思想;此文仅作为入门demo搭建指南,如需深入理解,还请移步至官方文档。
资源获取:
大家 点赞、收藏、关注、评论啦 、 查看 微信公众号获取联系方式
精彩专栏推荐订阅:在 下方专栏
每天学四小时:Java+Spring+JVM+分布式高并发,架构师指日可待