测试Fluent-Mybatis多对多关系

This commit is contained in:
Eee 2021-09-15 18:31:58 +08:00
parent 6a5146d7b0
commit f5212046ee
10 changed files with 469 additions and 97 deletions

View File

@ -0,0 +1,24 @@
package com.ruoyi.common.config;
import cn.org.atool.fluent.mybatis.spring.MapperFactory;
import com.ruoyi.common.core.domain.entity.test.relation.ProductCategoryRelationEntity;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Mapper配置类
* @author Geniou
* @create 2021/9/10 12:11
*/
@Configuration
public class MapperConfig {
@Bean
public MapperFactory mapperFactory() {
return new MapperFactory();
}
@Bean
public ProductCategoryRelationEntity entityRelation() { return new ProductCategoryRelationEntity(); }
}

View File

@ -0,0 +1,16 @@
package com.ruoyi.common.core.domain.entity.test.dao.impl;
import com.ruoyi.common.core.domain.entity.test.dao.base.ProductInventoryBaseDao;
import com.ruoyi.common.core.domain.entity.test.dao.intf.ProductInventoryDao;
import org.springframework.stereotype.Repository;
/**
* ProductInventoryDaoImpl: 数据操作接口实现
*
* 这只是一个减少手工创建的模板文件
* 可以任意添加方法和实现, 更改作者和重定义类名
* <p/>@author Powered By Fluent Mybatis
*/
@Repository
public class ProductInventoryDaoImpl extends ProductInventoryBaseDao implements ProductInventoryDao {
}

View File

@ -0,0 +1,14 @@
package com.ruoyi.common.core.domain.entity.test.dao.intf;
import cn.org.atool.fluent.mybatis.base.IBaseDao;
import com.ruoyi.common.core.domain.entity.test.entity.ProductInventory;
/**
* ProductInventoryDao: 数据操作接口
*
* 这只是一个减少手工创建的模板文件
* 可以任意添加方法和实现, 更改作者和重定义类名
* <p/>@author Powered By Fluent Mybatis
*/
public interface ProductInventoryDao extends IBaseDao<ProductInventory> {
}

View File

@ -50,9 +50,22 @@ public class Product extends RichEntity {
/**
* 创建时间
*/
@TableField("create_date_time")
@TableField(
value = "create_date_time",
insert = "now()"
)
private Date createDateTime;
/**
* 更新时间
*/
@TableField(
value = "update_date_time",
insert = "now()",
update = "now()"
)
private Date updateDateTime;
/**
* 运费
*/
@ -77,12 +90,6 @@ public class Product extends RichEntity {
@TableField("unit_price")
private BigDecimal unitPrice;
/**
* 更新时间
*/
@TableField("update_date_time")
private Date updateDateTime;
@Override
public Serializable findPk() {
return this.id;
@ -110,4 +117,12 @@ public class Product extends RichEntity {
public List<ProductCategory> findProductCategoryList() {
return super.invoke("findProductCategoryList", true);
}
/**
* 实现定义在{@link cn.org.atool.fluent.mybatis.base.IRefs}子类Refs上
*/
@RefMethod("productId = id")
public List<ProductInventory> findProductInventoryList() {
return super.invoke("findProductInventoryList", true);
}
}

View File

@ -7,7 +7,6 @@ import cn.org.atool.fluent.mybatis.annotation.TableId;
import cn.org.atool.fluent.mybatis.base.IEntity;
import cn.org.atool.fluent.mybatis.base.RichEntity;
import cn.org.atool.fluent.mybatis.functions.TableSupplier;
import java.io.Serializable;
import java.lang.Class;
import java.lang.Integer;
@ -17,7 +16,6 @@ import java.lang.String;
import java.lang.SuppressWarnings;
import java.util.Date;
import java.util.List;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@ -29,9 +27,17 @@ import lombok.experimental.Accessors;
*/
@SuppressWarnings({"unchecked"})
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@FluentMybatis(table = "product_category", schema = "ruoyi", suffix = "")
@Accessors(
chain = true
)
@EqualsAndHashCode(
callSuper = false
)
@FluentMybatis(
table = "product_category",
schema = "ruoyi",
suffix = ""
)
public class ProductCategory extends RichEntity {
private static final long serialVersionUID = 1L;
@ -41,6 +47,25 @@ public class ProductCategory extends RichEntity {
@TableId("id")
private Long id;
/**
* 创建时间
*/
@TableField(
value = "create_date_time",
insert = "now()"
)
private Date createDateTime;
/**
* 更新时间
*/
@TableField(
value = "update_date_time",
insert = "now()",
update = "now()"
)
private Date updateDateTime;
/**
* 分类名称
*/
@ -53,12 +78,6 @@ public class ProductCategory extends RichEntity {
@TableField("category_type")
private String categoryType;
/**
* 创建时间
*/
@TableField("create_date_time")
private Date createDateTime;
/**
* 层级
*/
@ -83,12 +102,6 @@ public class ProductCategory extends RichEntity {
@TableField("status_type")
private String statusType;
/**
* 更新时间
*/
@TableField("update_date_time")
private Date updateDateTime;
@Override
public Serializable findPk() {
return this.id;

View File

@ -5,13 +5,11 @@ import cn.org.atool.fluent.mybatis.annotation.TableField;
import cn.org.atool.fluent.mybatis.base.IEntity;
import cn.org.atool.fluent.mybatis.base.RichEntity;
import cn.org.atool.fluent.mybatis.functions.TableSupplier;
import java.lang.Class;
import java.lang.Long;
import java.lang.Override;
import java.lang.String;
import java.lang.SuppressWarnings;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@ -23,9 +21,17 @@ import lombok.experimental.Accessors;
*/
@SuppressWarnings({"unchecked"})
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@FluentMybatis(table = "product_category_relation", schema = "ruoyi", suffix = "")
@Accessors(
chain = true
)
@EqualsAndHashCode(
callSuper = false
)
@FluentMybatis(
table = "product_category_relation",
schema = "ruoyi",
suffix = ""
)
public class ProductCategoryRelation extends RichEntity {
private static final long serialVersionUID = 1L;

View File

@ -0,0 +1,105 @@
package com.ruoyi.common.core.domain.entity.test.entity;
import cn.org.atool.fluent.mybatis.annotation.FluentMybatis;
import cn.org.atool.fluent.mybatis.annotation.RefMethod;
import cn.org.atool.fluent.mybatis.annotation.TableField;
import cn.org.atool.fluent.mybatis.annotation.TableId;
import cn.org.atool.fluent.mybatis.annotation.Version;
import cn.org.atool.fluent.mybatis.base.IEntity;
import cn.org.atool.fluent.mybatis.base.RichEntity;
import cn.org.atool.fluent.mybatis.functions.TableSupplier;
import java.io.Serializable;
import java.lang.Class;
import java.lang.Integer;
import java.lang.Long;
import java.lang.Override;
import java.lang.String;
import java.lang.SuppressWarnings;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* ProductInventory: 数据映射实体定义
*
* @author Powered By Fluent Mybatis
*/
@SuppressWarnings({"unchecked"})
@Data
@Accessors(
chain = true
)
@EqualsAndHashCode(
callSuper = false
)
@FluentMybatis(
table = "product_inventory",
schema = "ruoyi",
suffix = ""
)
public class ProductInventory extends RichEntity {
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@TableId("id")
private Long id;
/**
* 库存数量
*/
@TableField("inventory_quantity")
private Integer inventoryQuantity;
/**
* 乐观锁
*/
@TableField(
value = "lock_version",
insert = "0",
update = "`lock_version` + 1"
)
@Version
private Long lockVersion;
/**
* 商品主键
*/
@TableField("product_id")
private Long productId;
/**
* 库存状态Enable-在售Disable-停售Delete-删除
*/
@TableField("status_type")
private String statusType;
@Override
public Serializable findPk() {
return this.id;
}
@Override
public final Class<? extends IEntity> entityClass() {
return ProductInventory.class;
}
@Override
public final ProductInventory changeTableBelongTo(TableSupplier supplier) {
return super.changeTableBelongTo(supplier);
}
@Override
public final ProductInventory changeTableBelongTo(String table) {
return super.changeTableBelongTo(table);
}
/**
* 实现定义在{@link cn.org.atool.fluent.mybatis.base.IRefs}子类Refs上
*/
@RefMethod("id = productId")
public Product findProduct() {
return super.invoke("findProduct", true);
}
}

View File

@ -0,0 +1,50 @@
package com.ruoyi.common.core.domain.entity.test.relation;
import cn.org.atool.fluent.mybatis.refs.IEntityRelation;
import com.ruoyi.common.core.domain.entity.test.entity.Product;
import com.ruoyi.common.core.domain.entity.test.entity.ProductCategory;
import com.ruoyi.common.core.domain.entity.test.wrapper.ProductCategoryQuery;
import com.ruoyi.common.core.domain.entity.test.wrapper.ProductCategoryRelationQuery;
import com.ruoyi.common.core.domain.entity.test.wrapper.ProductQuery;
import java.util.List;
/**
* 商品与分类关联关系实现
* @author Geniou
* @create 2021/9/13 19:08
*/
public class ProductCategoryRelationEntity implements IEntityRelation {
/**
* {@link Product#findProductCategoryList}
* @param entity
*/
@Override
public List<ProductCategory> findProductCategoryListOfProduct(Product entity) {
return ProductCategoryQuery.defaultQuery()
.where.id().in(
ProductCategoryRelationQuery.defaultQuery()
.select.productCategoryId().end()
.where.productId().eq(entity.getId())
.end()
).end()
.to().listEntity();
}
/**
* {@link ProductCategory#findProductList}
* @param entity
*/
@Override
public List<Product> findProductListOfProductCategory(ProductCategory entity) {
return ProductQuery.defaultQuery()
.where.id().in(
ProductCategoryRelationQuery.defaultQuery()
.select.productId().end()
.where.productCategoryId().eq(entity.getId())
.end()
).end()
.to().listEntity();
}
}

View File

@ -5,7 +5,6 @@ import cn.org.atool.generator.annotation.Relation;
import cn.org.atool.generator.annotation.RelationType;
import cn.org.atool.generator.annotation.Table;
import cn.org.atool.generator.annotation.Tables;
import cn.org.atool.generator.database.model.ConfigKey;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
@ -26,23 +25,19 @@ public class GeneratorTest {
FileGenerator.build(RelationMTM.class);
}
@Tables(
url = url, username = "root", password = "123456",
basePack = basePackage,
srcDir = "src/main/java/com/ruoyi/common/core/domain/entity/test",
tables = @Table(value = {"product_category", "product", "product_category_relation"})
)
static class Noting{}
@Tables(
url = url, username = "root", password = "123456",
basePack = basePackage,
srcDir = "src\\main\\java\\",
tables = {@Table(value = {"product", "product_category", "product_category_relation"})},
tables = {@Table(value = {"product", "product_category", "product_category_relation"}),
@Table(value = {"product_inventory"}, version = "lock_version")},
gmtCreated = "create_date_time", gmtModified = "update_date_time",
relations = {
@Relation(source = "product", target = "product_category", type = RelationType.TwoWay_N_N),
@Relation(source = "product_category", target = "product_category", type = RelationType.TwoWay_1_N,
where = "id = parent_id")
where = "id = parent_id"),
@Relation(source = "product", target = "product_inventory", type = RelationType.TwoWay_1_N,
where = "id = product_id")
},
entitySuffix = ""
)

View File

@ -1,22 +1,39 @@
package com.ruoyi.demo.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.org.atool.fluent.mybatis.base.model.FieldType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.test.entity.Product;
import com.ruoyi.common.core.domain.entity.test.entity.ProductCategory;
import com.ruoyi.common.core.domain.entity.test.entity.ProductCategoryRelation;
import com.ruoyi.common.core.domain.entity.test.helper.ProductCategoryRelationWrapperHelper;
import com.ruoyi.common.core.domain.entity.test.helper.ProductWrapperHelper;
import com.ruoyi.common.core.domain.entity.test.mapper.ProductCategoryMapper;
import com.ruoyi.common.core.domain.entity.test.mapper.ProductCategoryRelationMapper;
import com.ruoyi.common.core.domain.entity.test.mapper.ProductMapper;
import com.ruoyi.common.core.domain.entity.test.wrapper.ProductCategoryRelationQuery;
import com.ruoyi.common.core.domain.entity.test.wrapper.ProductQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 商品测试控制
@ -31,7 +48,10 @@ public class ProductTestController extends BaseController {
@Autowired
private ProductMapper productMapper;
@Autowired
private ProductCategoryMapper productCategoryMapper;
@Autowired
private ProductCategoryRelationMapper productCategoryRelationMapper;
/**
@ -39,76 +59,190 @@ public class ProductTestController extends BaseController {
*/
@ApiOperation("测试插入商品")
@PostMapping("/insertProduct")
public void insertProduct() {
public AjaxResult<Long> insertProduct() {
Product product = new Product();
product.setProductCode("M10001");
product.setProductName("测试商品");
product.setUnitPrice(BigDecimal.valueOf(2.5));
product.setFreight(BigDecimal.valueOf(1.5));
product.setCreateDateTime(new Date());
product.setUpdateDateTime(new Date());
String productCode = "P" + RandomUtil.randomNumbers(12);
product.setProductCode(productCode);
product.setProductName("海口一年级语文");
product.setUnitPrice(BigDecimal.valueOf(12.5));
product.setFreight(BigDecimal.valueOf(8));
productMapper.insert(product);
List<Long> ids = new ArrayList<>();
ids.add(7l);
ids.add(22l);
ids.add(29l);
// 查出所有需要关联的商品分类
List<ProductCategory> categoryList = productCategoryMapper.listByIds(ids);
Long productId = product.getId();
// 遍历商品分类集合,每次创建一条商品与分类关联关系的实体
List<ProductCategoryRelation> relations = new ArrayList<>();
for (ProductCategory productCategory : categoryList) {
ProductCategoryRelation relation = new ProductCategoryRelation();
relation.setProductId(productId);
relation.setProductCategoryId(productCategory.getId());
relations.add(relation);
}
// 保存中间表
productCategoryRelationMapper.insertBatch(relations);
return AjaxResult.success(productId);
}
/**
* 测试插入
*/
@ApiOperation("测试插入商品分类")
@PostMapping("/insertProductCategory")
public void insertProductCategory() {
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryName("一年级");
productCategory.setCategoryType("Title");
@ApiOperation("测试插入商品库存")
@PostMapping("/insertProductInventory")
public void insertProductInventory() {
}
//
//
// /**
// * 测试查询
// */
// @ApiOperation("测试查询")
// @GetMapping("/queryTest")
// public AjaxResult<Product> query(@Validated Product bo) {
// Product product = productMapper.findById(bo.getId());
// return new AjaxResult<Product>().setData(product);
// }
@ApiOperation("测试插入商品分类")
@PostMapping("/insertProductCategory")
public AjaxResult<Void> insertProductCategory() {
List<ProductCategory> categories = new ArrayList<>();
// /**
// * 测试查询2根据实体类字段
// */
// @ApiOperation("测试查询2")
// @GetMapping("/query2Test")
// public AjaxResult<List<Product>> query2(@Validated Product bo) {
// ProductQuery productQuery = productMapper.query().where.eqByEntity(bo).end();
// List<Product> products = productMapper.listEntity(productQuery);
// log.info("query2 rows = {}", CollUtil.size(products));
// return new AjaxResult<List<Product>>().setData(products);
// }
//
// /**
// * 测试删除
// */
// @ApiOperation("测试删除")
// @DeleteMapping("/deleteTest")
// public AjaxResult<Void> delete(@Validated Product bo) {
// return toAjax(productMapper.deleteById(bo.getId()));
// }
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryName("适用阶段");
productCategory.setCategoryType("Title");
productCategory.setLevelNo(1);
productCategory.setSortNum(1);
categories.add(productCategory);
ProductCategory productCategory1 = new ProductCategory();
productCategory1.setCategoryName("适用区域");
productCategory1.setCategoryType("Title");
productCategory1.setLevelNo(1);
productCategory1.setSortNum(2);
categories.add(productCategory1);
ProductCategory productCategory2 = new ProductCategory();
productCategory2.setCategoryName("教辅类型");
productCategory2.setCategoryType("Title");
productCategory2.setLevelNo(1);
productCategory2.setSortNum(3);
categories.add(productCategory2);
return toAjax(productCategoryMapper.insertBatch(categories));
}
@ApiOperation("测试插入商品分类(根据父级)")
@PostMapping("/insertProductCategoryByParent")
public AjaxResult<Void> insertProductCategoryByParent(@Validated ProductCategory productCategory) {
return toAjax(productCategoryMapper.insert(productCategory));
}
@ApiOperation("根据分类查询")
@GetMapping("/queryByCategory")
public AjaxResult<JSONArray> queryByCategory(@Validated String categoryIdsString) {
String[] categoryIds = categoryIdsString.split(",");
ProductQuery productQuery = productMapper.query();
ProductWrapperHelper.QueryWhere queryWhere = productQuery.where;
for (String categoryId : categoryIds) {
queryWhere.exists(productCategoryRelationMapper.query().where.productId().apply("= product.id").productCategoryId()
.eq(Long.parseLong(categoryId)).end());
}
List<Product> products = productMapper.listEntity(queryWhere.end());
return AjaxResult.success(JSONUtil.parseArray(products));
}
@ApiOperation("查询商品所属分类")
@GetMapping("/queryProductBelongCategory")
public AjaxResult<JSONArray> queryProductBelongCategory(@Validated Long productId) {
Product product = productMapper.findById(productId);
List<ProductCategory> productCategoryList = product.findProductCategoryList();
return AjaxResult.success(JSONUtil.parseArray(productCategoryList));
}
@ApiOperation("查询分类树")
@GetMapping("/queryCategoryTree")
public AjaxResult<JSONObject> queryCategoryTree(@Validated Long productCategoryId) {
ProductCategory productCategory = productCategoryMapper.findById(productCategoryId);
JSONObject respBody = buildCategory(productCategory);
return AjaxResult.success(respBody);
}
private JSONObject buildCategory(ProductCategory productCategory) {
JSONObject categoryJSON = new JSONObject();
Long id = productCategory.getId();
Integer levelNo = productCategory.getLevelNo();
Long parentId = productCategory.getParentId();
String categoryName = productCategory.getCategoryName();
String categoryType = productCategory.getCategoryType();
categoryJSON.set("id", id);
categoryJSON.set("parentId", parentId);
categoryJSON.set("levelNo", levelNo);
categoryJSON.set("categoryName", categoryName);
categoryJSON.set("categoryType", categoryType);
List<ProductCategory> childList = productCategory.findProductCategoryList();
if (CollUtil.isNotEmpty(childList)) {
JSONArray childArray = new JSONArray();
for (ProductCategory childCategory : childList) {
JSONObject childJSON = buildCategory(childCategory);
childArray.add(childJSON);
}
categoryJSON.set("child", childArray);
}
return categoryJSON;
}
/**
* 测试查询
*/
@ApiOperation("测试查询")
@GetMapping("/queryTest")
public AjaxResult<Product> query(@Validated Product bo) {
Product product = productMapper.findById(bo.getId());
return new AjaxResult<Product>().setData(product);
}
/**
* 测试查询2根据实体类字段
*/
@ApiOperation("测试查询2")
@GetMapping("/query2Test")
public AjaxResult<List<Product>> query2(@Validated Product bo) {
ProductQuery productQuery = productMapper.query().where.eqByEntity(bo).end();
List<Product> products = productMapper.listEntity(productQuery);
log.info("query2 rows = {}", CollUtil.size(products));
return new AjaxResult<List<Product>>().setData(products);
}
/**
* 测试删除
*/
@ApiOperation("测试删除")
@DeleteMapping("/deleteTest")
public AjaxResult<Void> delete(@Validated Product bo) {
return toAjax(productMapper.deleteById(bo.getId()));
}
/**
* 测试删除2
*/
@ApiOperation("测试删除2根据相关参数")
@DeleteMapping("/delete2Test")
public AjaxResult<Void> delete2(@Validated Product bo) {
ProductQuery productQuery = productMapper.query().where.eqByEntity(bo).end();
int row = productMapper.delete(productQuery);
return toAjax(row);
}
// /**
// * 测试删除2
// */
// @ApiOperation("测试删除2根据相关参数")
// @DeleteMapping("/delete2Test")
// public AjaxResult<Void> delete2(@Validated Product bo) {
// ProductQuery productQuery = productMapper.query().where.eqByEntity(bo).end();
// List<Product> products = productMapper.listEntity(productQuery);
// int row = productMapper.delete(productQuery);
// return toAjax(row);
// }
//
// static final String url =
// "jdbc:mysql://localhost:3306/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true";
// "jdbc:mysql://localhost:3306/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
// &useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true";
// static final String basePackage = "com.ruoyi.common.core.domain.entity.test";
// /**