根本简介Mybatis-Tiny是什么?Mybatis-Tiny是一个基于Mybatis框架的一层极简的扩大,它旨在应用DSL的形式对单表进行CRUD操作,相似于Mybatis-Plus框架,但它绝不是反复造轮子!区别于别的相似框架(如Mybatis-Plus、Fluent-Mybatis等)的实现形式,它采纳一种逆向曲线救国的实现形式,通过较少的代码,极简的扩大实现了相似于他们大多数的性能,齐全满足日常开发中对单表的各种CRUD操作。
我的项目地址:https://github.com/penggle/my...
疾速入门Talk is cheap,show me the code!插入操作ProductBaseInfo productBase = ...;List<ProductSaleSpec> productSaleSpecs = ...;productBaseInfoMapper.insert(productBase);//基于JDBC-Batch个性的批量插入操作。productSaleSpecMapper.batchUpdate(productSaleSpecs, productSaleSpec -> productSaleSpecMapper.insert(productSaleSpec));//打印日志: - ==> Preparing: INSERT INTO t_product_base_info( product_id, product_name, product_url, product_tags, product_type, audit_status, online_status, shop_id, remark, create_time, update_time ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) - ==> Parameters: null, 24期免息【当天发】Huawei/华为Mate40 5G手机官网旗舰店50pro直降mate40e官网30副品4G鸿蒙副品30全网通(String), https://detail.tmall.com/item.htm?id=633658852628(String), ["手机通信","手机","手机"](String), 1(Integer), 0(Integer), 1(Integer), 111212422(Long), null, 2022-04-27 00:43:42(String), 2022-04-27 00:43:42(String) - <== Updates: 1 - ==> Preparing: INSERT INTO t_product_sale_spec( product_id, spec_no, spec_name, spec_index, remark, create_time, update_time ) VALUES ( ?, ?, ?, ?, ?, ?, ? ) - ==> Parameters: 1(Long), 101(String), 4G全网通(String), 1(Integer), null, 2022-04-27 00:43:42(String), 2022-04-27 00:43:42(String) - ==> Parameters: 1(Long), 102(String), 5G全网通(String), 2(Integer), null, 2022-04-27 00:43:42(String), 2022-04-27 00:43:42(String) - ==> Parameters: 1(Long), 201(String), 亮彩色(String), 1(Integer), null, 2022-04-27 00:43:42(String), 2022-04-27 00:43:42(String) - ==> Parameters: 1(Long), 202(String), 釉红色(String), 2(Integer), null, 2022-04-27 00:43:42(String), 2022-04-27 00:43:42(String) - ==> Parameters: 1(Long), 203(String), 秘银色(String), 3(Integer), null, 2022-04-27 00:43:42(String), 2022-04-27 00:43:42(String) - ==> Parameters: 1(Long), 204(String), 夏日胡杨(String), 4(Integer), null, 2022-04-27 00:43:42(String), 2022-04-27 00:43:42(String) - ==> Parameters: 1(Long), 205(String), 秋日胡杨(String), 5(Integer), null, 2022-04-27 00:43:42(String), 2022-04-27 00:43:42(String) - ==> Parameters: 1(Long), 301(String), 8+128GB(String), 1(Integer), null, 2022-04-27 00:43:42(String), 2022-04-27 00:43:42(String) - ==> Parameters: 1(Long), 302(String), 8+256GB(String), 2(Integer), null, 2022-04-27 00:43:42(String), 2022-04-27 00:43:42(String)更新操作//依据ID更新ProductBaseInfo productBase = ...;Map<String,Object> updateColumns1 = MapLambdaBuilder.of(productBase) //取productBase实例中对应字段的值 .with(ProductBaseInfo::getProductName) .with(ProductBaseInfo::getRemark) //如果productBase实例中对应字段的值为空值(null|空串|空数组|空集合)则取default值"1" .withDefault(ProductBaseInfo::getProductType, 1) //疏忽productBase实例中对应字段的值,只取override值"0" .withOverride(ProductBaseInfo::getAuditStatus, 0) .withOverride(ProductBaseInfo::getOnlineStatus, 0) .withOverride(ProductBaseInfo::getUpdateTime, DateTimeUtils.formatNow()) .build();productBaseInfoMapper.updateById(productBase.getProductId(), updateColumns1);//实现了畛域实体的identity()办法则productBase.identity()与productBase.getProductId()是等效的//productBaseInfoMapper.updateById(productBase.identity(), updateColumns);//依据条件更新Map<String,Object> updateColumns2 = MapLambdaBuilder.<ProductBaseInfo>ofEmpty() .withOverride(ProductBaseInfo::getOnlineStatus, 0) .withOverride(ProductBaseInfo::getUpdateTime, DateTimeUtils.formatNow()) .build();QueryCriteria<ProductBaseInfo> updateCriteria2 = LambdaQueryCriteria.ofSupplier(ProductBaseInfo::new) .eq(ProductBaseInfo::getProductType, 1) .in(ProductBaseInfo::getAuditStatus, 0, 1) .limit(5);productBaseInfoMapper.updateByCriteria(updateCriteria2, updateColumns2);//批量更新List<ProductSaleStock> productSaleStocks = ...;String nowTime = DateTimeUtils.formatNow();productSaleStockMapper.batchUpdate(productSaleStocks, productSaleStock -> { Map<String,Object> updateColumns = MapLambdaBuilder.of(productSaleStock) .withOverride(ProductSaleStock::getSellPrice, productSaleStock.getSellPrice() - productSaleStock.getSellPrice() % 100) .withOverride(ProductSaleStock::getUpdateTime, nowTime) .build(); //new一个联结主键实例 ID productSaleStockId = new ID() .addKey(ProductSaleStock::getProductId, productSaleStock.getProductId()) .addKey(ProductSaleStock::getProductId, productSaleStock.getSpecNo()); productSaleStockMapper.updateById(productSaleStockId, updateColumns); //或者实现了畛域实体的identity()办法,则能够如下间接调用 //productSaleStockMapper.updateById(productSaleStock.identity(), updateColumns);});查问操作//依据ID查ProductBaseInfo productBase1 = productBaseInfoMapper.selectById(1L);ProductBaseInfo productBase2 = productBaseInfoMapper.selectById(10L, new QueryColumns(ProductBaseInfo::getProductId, ProductBaseInfo::getProductName, ProductBaseInfo::getAuditStatus, ProductBaseInfo::getOnlineStatus));ID id = new ID().addKey(ProductSaleSpec::getProductId, 1L).addKey(ProductSaleSpec::getSpecNo, "101");ProductSaleSpec productSaleSpec = productSaleSpecMapper.selectById(id);//依据多个ID查问List<ProductBaseInfo> productBases = productBaseInfoMapper.selectListByIds(Arrays.asList(5L, 6L, 7L, 8L, 9L));//依据多个联结主键查问实体对象列表List<ID> ids = new ArrayList<>();ids.add(new ID().addKey(ProductSaleSpec::getProductId, 1L).addKey(ProductSaleSpec::getSpecNo, "101"));ids.add(new ID().addKey(ProductSaleSpec::getProductId, 1L).addKey(ProductSaleSpec::getSpecNo, "102"));ids.add(new ID().addKey(ProductSaleSpec::getProductId, 1L).addKey(ProductSaleSpec::getSpecNo, "103"));List<ProductSaleSpec> productSaleSpecs = productSaleSpecMapper.selectListByIds(ids); - ==> Preparing: SELECT product_id AS productId, spec_no AS specNo, spec_name AS specName, spec_index AS specIndex, remark AS remark, DATE_FORMAT(create_time, '%Y-%m-%d %T') AS createTime, DATE_FORMAT(update_time, '%Y-%m-%d %T') AS updateTime FROM t_product_sale_spec WHERE (product_id = ? AND spec_no = ?) OR (product_id = ? AND spec_no = ?) OR (product_id = ? AND spec_no = ?) - ==> Parameters: 1(Long), 101(String), 1(Long), 102(String), 1(Long), 103(String) - <== Total: 2//依据条件查问QueryCriteria<ProductSaleSpec> queryCriteria1 = LambdaQueryCriteria.ofSupplier(ProductSaleSpec::new) .eq(ProductSaleSpec::getProductId, 1L) .eq(ProductSaleSpec::getSpecNo, "101");ProductSaleSpec productSaleSpec = productSaleSpecMapper.selectByCriteria(queryCriteria1);ProductSaleStock queryRequest1 = ...;QueryCriteria<ProductSaleStock> queryCriteria2 = LambdaQueryCriteria.of(queryRequest1) .eq(ProductSaleStock::getProductId) .likeRight(ProductSaleStock::getSpecNo) .between(ProductSaleStock::getStock, queryRequest1.getMinStock(), queryRequest1.getMaxStock()) .orderBy(OrderBy.desc(ProductSaleStock::getSellPrice));List<ProductSaleStock> productStocks = productSaleStockMapper.selectListByCriteria(queryCriteria2);QueryCriteria<ProductBaseInfo> queryCriteria3 = LambdaQueryCriteria.of(queryRequest2) .and(nestedCriteria -> nestedCriteria.like(ProductBaseInfo::getProductName, "华为") .or().like(ProductBaseInfo::getProductName, "HUAWEI")) .eq(ProductBaseInfo::getProductType) .eq(ProductBaseInfo::getOnlineStatus) .in(ProductBaseInfo::getAuditStatus, queryRequest.getAuditStatuses().toArray()) .orderBy(OrderBy.desc(ProductBaseInfo::getCreateTime)) .dynamic(true); //主动过滤掉为空值(null|空串|空数组|空集合)的查问参数List<ProductBaseInfo> productBases1 = productBaseInfoMapper.selectListByCriteria(queryCriteria3);//分页查问1Page page = Page.of(1, 10);QueryCriteria<ProductBaseInfo> queryCriteria4 = LambdaQueryCriteria.of(queryRequest) .likeRight(ProductBaseInfo::getProductName) .eq(ProductBaseInfo::getProductType) .eq(ProductBaseInfo::getOnlineStatus) .in(ProductBaseInfo::getAuditStatus, queryRequest.getAuditStatuses().toArray()) .orderBy(page.getOrderBys()) .dynamic(true); //主动过滤掉为空值(null|空串|空数组|空集合)的查问参数(条件)List<ProductBaseInfo> productBases2 = productBaseInfoMapper.selectPageListByCriteria(queryCriteria4, new RowBounds(page.offset(), page.limit()));//设置总记录数page.setTotalRowCount(productBaseInfoMapper.selectPageCountByCriteria(queryCriteria4));//分页查问2(等效与下面)Page page = Page.of(2, 10);List<ProductBaseInfo> productBases2 = EntityMapperHelper.selectEntityObjectListByPage(productBaseInfoMapper, queryCriteria4, page);删除操作//依据ID删除productBaseInfoMapper.deleteById(2L);productExtraInfoMapper.deleteById(2L);//依据条件删除QueryCriteria<ProductSaleSpec> queryCriteria1 = LambdaQueryCriteria.ofSupplier(ProductSaleSpec::new) .eq(ProductSaleSpec::getProductId, 2L) .limit(5);productSaleSpecMapper.deleteByCriteria(queryCriteria1);更多示例请见:https://github.com/penggle/my...个性及限度反对繁多主键或联结主键,繁多主键时主键策略反对:IDENTITY(数据库自增的),SEQUENCE(基于序列的),NONE(无,客户端本人设置主键)
...