提交 e61fe08d 编写于 作者: E Erwin

CRUD

上级 8fecb8f8
# CRUD
- 增加(Create)
- 检索(Retrieve)
- 更新(Update)
- 删除(Delete)
对于MyBatis-Plus而言,增删改查,有三种方式实现。
1、使用MyBatis-Plus提供的服务接口。
2、使用MyBatis的Mapper接口。
3、使用Mybatis-Plus提供的AR特性,Model可进行CRUD操作。
AR即为ActiveRecord,是一种领域模型模式,一个模型类对应一个表。通过实体类对象直接进行表的CRUD操作。
\ No newline at end of file
# MyBatis-Plus条件查询
## Wrapper
### QueryWrapper
查询条件构造器
### LambdaQueryWrapper
Lambda的使用
### LambdaQueryChainWrapper
链式查询
\ No newline at end of file
#spring
spring:
datasource:
......@@ -8,6 +9,9 @@ spring:
password: 123456
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
url: jdbc:p6spy:mysql://192.168.16.128:3306/mybatis-plus-example
# url: mpw:pBZqJb+r/StFGyRopKJyhZN8dvstxaXxubewECBMWM3e7XK+719AUXlC1y1Cu14i1MwqEdiJbBa+sEwFzyWEgA==
# username: mpw:hjR022j+cjKFE+35FLTQgg==
# password: mpw:kzLGIBmi4hT+dPmqnXLAIw==
mybatis-plus:
configuration:
......
package com.fengwenyi.mybatisplusexample;
import com.fengwenyi.mybatisplusexample.entity.CategoryEntity;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
/**
*
* model test
*
* <p>
* CRUD
* </p>
*
* @author Erwin Feng
* @since 2021-03-30
*/
@Component
public class CategoryModelTests extends MybatisPlusExampleApplicationTests {
private CategoryEntity model;
// 创建数据
private void create() {
model = new CategoryEntity().setName("category test data");
model.insert();
}
// 清除数据
private void clear() {
model.deleteById();
}
@Test
public void testAdd() {
model = new CategoryEntity().setName("category test data");
boolean testResult = model.insert();
Assert.isTrue(testResult, "data add test failure");
clear();
}
@Test
public void testDelete() {
create();
boolean testResult = model.deleteById();
Assert.isTrue(testResult, "data delete test failure");
}
@Test
public void testUpdate() {
create();
model.setName("category update");
boolean testResult = model.updateById();
Assert.isTrue(testResult, "data update test failure");
}
}
package com.fengwenyi.mybatisplusexample;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fengwenyi.mybatisplusexample.entity.CategoryEntity;
import com.fengwenyi.mybatisplusexample.repository.MPCategoryRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.util.List;
/**
*
* MPCategoryRepository test
*
* <p>
* CRUD
* </p>
*
* @author Erwin Feng
* @since 2021-03-08
*/
@Component
@Slf4j
public class CategoryRepositoryTests extends MybatisPlusExampleApplicationTests {
@Autowired
private MPCategoryRepository mpCategoryRepository;
private CategoryEntity entity;
// 创建数据
private void create() {
entity = new CategoryEntity().setName("category test data");
mpCategoryRepository.save(entity);
}
// 清除数据
private void clear() {
mpCategoryRepository.removeById(entity);
}
@Test
public void testAdd() {
CategoryEntity categoryEntity = new CategoryEntity().setName("testAddCategory");
boolean categorySaveResult = mpCategoryRepository.save(categoryEntity);
Assert.isTrue(categorySaveResult, "商品类别保存失败");
boolean categoryDeleteResult = mpCategoryRepository.removeById(categoryEntity.getId());
Assert.isTrue(categoryDeleteResult, "商品类别删除失败");
boolean insert = categoryEntity.insert();
boolean b = categoryEntity.deleteById();
entity = new CategoryEntity().setName("category test data");
boolean testResult = mpCategoryRepository.save(entity);
Assert.isTrue(testResult, "data add test failure");
clear();
}
@Test
public void testDelete() {
create();
boolean testResult = mpCategoryRepository.removeById(entity.getId());
Assert.isTrue(testResult, "data delete test failure");
}
@Test
public void testDeleteByWrapper() {
create();
LambdaQueryWrapper<CategoryEntity> queryWrapper = new LambdaQueryWrapper<CategoryEntity>()
.eq(CategoryEntity::getName, entity.getName())
;
boolean testResult = mpCategoryRepository.remove(queryWrapper);
Assert.isTrue(testResult, "data delete by wrapper test failure");
}
@Test
public void testUpdate() {
create();
entity.setName("category update");
boolean testResult = mpCategoryRepository.updateById(entity);
Assert.isTrue(testResult, "data update test failure");
clear();
}
@Test
public void testQuery() {
create();
// 根据主键ID查询
mpCategoryRepository.getById(entity.getId());
// 查询所有
List<CategoryEntity> list = mpCategoryRepository.list();
// 添加查询
LambdaQueryWrapper<CategoryEntity> queryWrapper = new LambdaQueryWrapper<CategoryEntity>()
.eq(CategoryEntity::getName, entity.getName())
;
mpCategoryRepository.list(queryWrapper);
// 分页查询
int currentPage = 1;
int pageSize = 10;
Page<CategoryEntity> categoryPage = mpCategoryRepository.page(new Page<>(currentPage, pageSize), queryWrapper);
long current = categoryPage.getCurrent();
long size = categoryPage.getSize();
long pages = categoryPage.getPages();
List<CategoryEntity> categoryList = categoryPage.getRecords();
long total = categoryPage.getTotal();
clear();
}
}
package com.fengwenyi.mybatisplusexample;
import com.baomidou.mybatisplus.core.toolkit.AES;
import com.fengwenyi.javalib.util.PrintUtils;
import org.junit.jupiter.api.Test;
/**
* @author Erwin Feng
* @since 2021-03-31
*/
public class MPWTests {
@Test
public void test() {
// 生成 16 位随机 AES 密钥
String randomKey = AES.generateRandomKey();
PrintUtils.info(randomKey);
String prefix = "mpw:";
// 随机密钥加密
String url = "jdbc:p6spy:mysql://192.168.16.128:3306/mybatis-plus-example";
String result =prefix + AES.encrypt(url, randomKey);
PrintUtils.info(result);
String username = "root";
result =prefix + AES.encrypt(username, randomKey);
PrintUtils.info(result);
String password = "123456";
result =prefix + AES.encrypt(password, randomKey);
PrintUtils.info(result);
/*
2021-03-31 00:57:49.494 INFO com.fengwenyi.mybatisplusexample.MPWTests#test : ba8a2cea8df4929f
2021-03-31 00:57:49.871 INFO com.fengwenyi.mybatisplusexample.MPWTests#test : mpw:pBZqJb+r/StFGyRopKJyhZN8dvstxaXxubewECBMWM3e7XK+719AUXlC1y1Cu14i1MwqEdiJbBa+sEwFzyWEgA==
2021-03-31 00:57:49.871 INFO com.fengwenyi.mybatisplusexample.MPWTests#test : mpw:hjR022j+cjKFE+35FLTQgg==
2021-03-31 00:57:49.871 INFO com.fengwenyi.mybatisplusexample.MPWTests#test : mpw:kzLGIBmi4hT+dPmqnXLAIw==
*/
}
}
package com.fengwenyi.mybatisplusexample;
import com.fengwenyi.mybatisplusexample.repository.MPCategoryRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册