package com.shardingjdbc.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shardingjdbc.entity.Book;
import com.shardingjdbc.mapper.BookMapper;
import com.shardingjdbc.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* 序号 数据条数 方式 耗时
* 1 10000 saveBatch?&rewriteBatchedStatements=true 11785ms
* 2 10000 saveBatch 10798ms
* 3 10000 saveBatchTran :事务控制 11800ms
*/
@SpringBootTest
public class BookServiceImplTest {
@Autowired
private BookService bookService;
@Resource
private BookMapper bookMapper;
/**
* 批量新增 :super.save() 方式
*/
@Test
public void saveBatchBySave() {
long start = System.currentTimeMillis();
for (int i = 0; i < 1; i++) {
Book book = Book.builder().name("name" + i).count(i).build();
bookService.save(book);
}
System.err.println(System.currentTimeMillis() - start);
}
/**
* 批量新增 :super.saveBatch() 方式
*/
@Test
public void saveBatch() {
List<Book> books = new ArrayList<>();
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
Book book = Book.builder().id((long) i).name("name" + i).count(i).build();
books.add(book);
}
bookService.saveBatch(books);
System.err.println(System.currentTimeMillis() - start);
}
/**
* 批量新增 :事务 方式
*/
@Test
public void saveBatchByTran() {
List<Book> books = new ArrayList<>();
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
Book book = Book.builder().id((long) i).name("name" + i).count(i).build();
books.add(book);
}
bookService.saveBatchTran(books);
System.err.println(System.currentTimeMillis() - start);
}
/**
* 使用了分页插件
* 查询 :mybatis-plus 的 QueryWrapper 方式
*/
@Test
public void listPage() {
IPage<Book> listPage = bookService.getBookListPage(5, 10);
for (Book record : listPage.getRecords()) {
System.err.println(record);
}
}
/**
* 使用了分页插件
* 查询:mybatis-plus 的 QueryWrapper 方式
* 指定 id 和 count,则只会查询某张表,但是分页仍然从0查到最大
* SELECT id,name,count FROM book_1 WHERE id = ? AND count = ? ORDER BY id ASC LIMIT ?,? ::: [[1, 1, 0, 50]]
*/
@Test
public void listPageByIdCountWrapper() {
IPage<Book> listPage = bookService.getBookListPage(5, 10, 1, 1);
for (Book record : listPage.getRecords()) {
System.err.println(record);
}
}
/**
* 未使用分页插件
* 查询:写sql方式 :select * from book limit ${start},${size}
* 若使用sql查询,则shardingjdbc会自动转为子表查询
* 1、因未指定id和count ,则查询所有表
*/
@Test
public void selectPageMy() {
List<Book> books = bookMapper.selectPageMy(1, 2);
for (Book book : books) {
System.err.println(book);
}
}
/**
* 未使用分页插件
* 查询:写sql方式 :select * from book where id = 1 limit ${start},${size}
* 若使用sql查询,则shardingjdbc会自动转为子表查询
* 1、因指定了 id ,则仅查询某些库
*/
@Test
public void selectById() {
List<Book> books = bookMapper.selectById(1, 2);
for (Book book : books) {
System.err.println(book);
}
}
/**
* 未使用分页插件
* 查询:写sql方式 :select * from book where count = 1 limit ${start},${size}
* 若使用sql查询,则shardingjdbc会自动转为子表查询
* 1、因指定了 count ,则仅查询所有库的某些表
*/
@Test
public void selectByCount() {
List<Book> books = bookMapper.selectByCount(1, 2);
for (Book book : books) {
System.err.println(book);
}
}
/**
* 未使用分页插件
* 查询:写sql方式 :select * from book where id = 1 and count = 1 limit ${start},${size}
* 若使用sql查询,则shardingjdbc会自动转为子表查询
* 1、因指定了 id 和 count ,则仅查询某张表
*/
@Test
public void selectByIdAndCount() {
List<Book> books = bookMapper.selectByIdAndCount(1, 2);
for (Book book : books) {
System.err.println(book);
}
}
/**
* 查询所有:mybatis-plus 的 QueryWrapper 方式
*/
@Test
public void listAll() {
List<Book> bookList = bookService.getBookList();
for (Book book : bookList) {
System.err.println(book);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Sharding-JDBC使用案例-分库分表
共25个文件
java:14个
xml:8个
iml:1个
需积分: 10 1 下载量 130 浏览量
2023-03-23
14:16:46
上传
评论 1
收藏 30KB ZIP 举报
温馨提示
Sharding-JDBC使用案例-分库分表 分库分表
资源推荐
资源详情
资源评论
收起资源包目录
sharding-分库分表.zip (25个子文件)
sharding
pom.xml 3KB
src
test
java
com
shardingjdbc
service
impl
BroadConfigServiceImplTest.java 1KB
BookServiceImplTest.java 5KB
main
resources
sql
book.sql 2KB
application.properties 3KB
java
com
shardingjdbc
mapper
BookMapper.java 995B
BroadConfigMapper.java 544B
controller
BookController.java 1KB
ShardingjdbcApplication.java 346B
service
BookService.java 809B
BroadConfigService.java 492B
impl
BookServiceImpl.java 3KB
BroadConfigServiceImpl.java 1KB
entity
Book.java 684B
BroadConfig.java 701B
config
SwaggerConfig.java 1KB
MybatisPlusConfig.java 543B
sharding.iml 81B
.idea
uiDesigner.xml 9KB
qaplug_profiles.xml 41KB
workspace.xml 33KB
misc.xml 1KB
inspectionProfiles
Project_Default.xml 1KB
compiler.xml 777B
encodings.xml 262B
共 25 条
- 1
资源评论
小安灬
- 粉丝: 47
- 资源: 27
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功