SpringBoot集成Sharding-JDBC+Mybatis-Plus实现分库分表⾸先创建⼀个⼀般的Spring boot项⽬,项⽬采⽤三层架构,结构图如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.macky</groupId>
<artifactId>spring-boot-shardingjdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-shardingjdbc</name>
<description>Demo project for spring-boot-shardingjdbc</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--Mybatis-Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<!--shardingsphere start-->
<!-- for spring boot -->
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>3.1.0</version>
</dependency>
<!-- for spring namespace -->
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId> <version>3.1.0</version>
</dependency>
<!--shardingsphere end-->
<!--lombok-->
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
</plugins>
</build>
</project>
实体类以书本为例
import batisplus.annotation.TableName; import sion.activerecord.Model; ansform.EqualsAndHashCode;
import lombok.Data;
perimental.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<Book> {
private int id;
private String name;
private int count;
}
开放保存和查询两个接⼝,代码如下:
import com.ity.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<Book> getItems(){
BookList();
}
@RequestMapping(value = "/book",method = RequestMethod.POST) public Boolean saveItem(Book book){
springboot架构图return bookService.save(book);
}
}
BookServiceImpl.java
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论