From 9bf077bc44a889d0ee7fe45263cf42715fadf112 Mon Sep 17 00:00:00 2001 From: "zhenyin.szy" Date: Thu, 15 Oct 2020 17:06:02 +0800 Subject: [PATCH 1/4] update version --- generator-dao/pom.xml | 4 ++-- generator-gui/pom.xml | 4 ++-- generator-plugin/pom.xml | 4 ++-- pom.xml | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/generator-dao/pom.xml b/generator-dao/pom.xml index cafa2e0..edb144b 100644 --- a/generator-dao/pom.xml +++ b/generator-dao/pom.xml @@ -6,12 +6,12 @@ com.dingtalk.mybatis mybatis-generator-tddl - 1.0.0-SNAPSHOT + 1.0.1-SNAPSHOT ../pom.xml org.dingtalk generator-dao - 1.0-SNAPSHOT + 1.0.1-SNAPSHOT 用于存储gui执行的页面保存数据,便于用户存储多个项目的不同配置 diff --git a/generator-gui/pom.xml b/generator-gui/pom.xml index c25ace7..c9394ae 100644 --- a/generator-gui/pom.xml +++ b/generator-gui/pom.xml @@ -6,13 +6,13 @@ com.dingtalk.mybatis mybatis-generator-tddl - 1.0.0-SNAPSHOT + 1.0.1-SNAPSHOT ../pom.xml com.dingtalk.mybatis generator-gui - 1.0-SNAPSHOT + 1.0.1-SNAPSHOT 界面配置工具 diff --git a/generator-plugin/pom.xml b/generator-plugin/pom.xml index 6f2be1c..ab5f2af 100644 --- a/generator-plugin/pom.xml +++ b/generator-plugin/pom.xml @@ -6,12 +6,12 @@ com.dingtalk.mybatis mybatis-generator-tddl - 1.0.0-SNAPSHOT + 1.0.1-SNAPSHOT ../pom.xml org.dingtalk generator-plugin - 1.0-SNAPSHOT + 1.0.1-SNAPSHOT generator自定义调整方法 diff --git a/pom.xml b/pom.xml index 1a67059..10fb046 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.dingtalk.mybatis mybatis-generator-tddl pom - 1.0.0-SNAPSHOT + 1.0.1-SNAPSHOT mybatis-generator-tddl From 87cede4889dbfd154b436924955c518b492b2d85 Mon Sep 17 00:00:00 2001 From: "zhenyin.szy" Date: Thu, 15 Oct 2020 19:58:16 +0800 Subject: [PATCH 2/4] add batchInsert plugin --- .../demo/dao/TableOneUniqueKeyMapper.java | 2 + .../demo/dao/TableTwoUniqueKeyMapper.java | 2 + .../demo/dao/TableWithIdentifyMapper.java | 38 ++ .../demo/domain/TableOneUniqueKey.java | 2 +- .../domain/TableOneUniqueKeyCriteria.java | 12 +- .../demo/domain/TableTwoUniqueKey.java | 2 +- .../domain/TableTwoUniqueKeyCriteria.java | 12 +- .../demo/domain/TableWithIdentify.java | 58 ++ .../domain/TableWithIdentifyCriteria.java | 616 ++++++++++++++++++ .../generator/plugins/BasePlugin.java | 22 - .../generator/plugins/BatchInsertPlugin.java | 75 +++ .../plugins/DeleteByUniqueKeyPlugin.java | 6 - .../plugins/SelectByUniqueKeyPlugin.java | 8 - .../UpdateByUniqueKeySelectivePlugin.java | 6 - .../plugins/constant/PluginConstants.java | 4 - .../generator/plugins/util/ColumnUtils.java | 18 + .../util/XmlElementGeneratorTools.java | 117 +++- .../demo/dao/TableOneUniqueKeyMapper.xml | 42 +- .../demo/dao/TableTwoUniqueKeyMapper.xml | 42 +- .../demo/dao/TableWithIdentifyMapper.xml | 348 ++++++++++ .../resources/generatorConfigMyBatis3.xml | 4 + .../src/main/resources/mybatis-config.xml | 3 +- .../classes/generatorConfigMyBatis3.xml | 4 + .../target/classes/mybatis-config.xml | 3 +- .../demo/dao/TableWithIdentifyMapperTest.java | 50 ++ .../dao/TableWithIdentifyMapperTest.class | Bin 0 -> 2348 bytes 26 files changed, 1401 insertions(+), 95 deletions(-) create mode 100644 generator-plugin/src/main/java/com/dingtalk/demo/dao/TableWithIdentifyMapper.java create mode 100644 generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentify.java create mode 100644 generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentifyCriteria.java create mode 100644 generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/ColumnUtils.java create mode 100644 generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableWithIdentifyMapper.xml create mode 100644 generator-test/src/test/java/com/dingtalk/demo/dao/TableWithIdentifyMapperTest.java create mode 100644 generator-test/target/test-classes/com/dingtalk/demo/dao/TableWithIdentifyMapperTest.class diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.java b/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.java index 4d2d990..1c57d2c 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.java @@ -33,4 +33,6 @@ public interface TableOneUniqueKeyMapper { int updateByUniqueKeySelective(TableOneUniqueKey record); int deleteByUniqueKey(@Param("orgId") Long orgId, @Param("code") String code); + + int batchInsert(@Param("list") List list); } \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.java b/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.java index fbf1bbb..a854b27 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.java @@ -33,4 +33,6 @@ public interface TableTwoUniqueKeyMapper { int updateByUniqueKeySelective(TableTwoUniqueKey record); int deleteByUniqueKey(@Param("orgId") Long orgId, @Param("cid") String cid); + + int batchInsert(@Param("list") List list); } \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableWithIdentifyMapper.java b/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableWithIdentifyMapper.java new file mode 100644 index 0000000..5b6f338 --- /dev/null +++ b/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableWithIdentifyMapper.java @@ -0,0 +1,38 @@ +package com.dingtalk.demo.dao; + +import com.dingtalk.demo.domain.TableWithIdentify; +import com.dingtalk.demo.domain.TableWithIdentifyCriteria; +import java.util.List; +import org.apache.ibatis.annotations.Param; + +public interface TableWithIdentifyMapper { + long countByExample(TableWithIdentifyCriteria example); + + int deleteByExample(TableWithIdentifyCriteria example); + + int deleteByPrimaryKey(Long id); + + int insert(TableWithIdentify record); + + int insertSelective(TableWithIdentify record); + + List selectByExample(TableWithIdentifyCriteria example); + + TableWithIdentify selectByPrimaryKey(Long id); + + int updateByExampleSelective(@Param("record") TableWithIdentify record, @Param("example") TableWithIdentifyCriteria example); + + int updateByExample(@Param("record") TableWithIdentify record, @Param("example") TableWithIdentifyCriteria example); + + int updateByPrimaryKeySelective(TableWithIdentify record); + + int updateByPrimaryKey(TableWithIdentify record); + + TableWithIdentify selectByUniqueKey(@Param("orgId") Long orgId, @Param("code") String code); + + int updateByUniqueKeySelective(TableWithIdentify record); + + int deleteByUniqueKey(@Param("orgId") Long orgId, @Param("code") String code); + + int batchInsert(@Param("list") List list); +} \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKey.java b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKey.java index cc3826b..fc767ac 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKey.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKey.java @@ -52,7 +52,7 @@ public class TableOneUniqueKey implements Serializable { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_one_unique_key * - * @mbg.generated Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 */ private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKeyCriteria.java b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKeyCriteria.java index 081c29a..aaa1d3d 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKeyCriteria.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKeyCriteria.java @@ -9,7 +9,7 @@ public class TableOneUniqueKeyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_one_unique_key * - * @mbg.generated Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 */ protected String orderByClause; @@ -17,7 +17,7 @@ public class TableOneUniqueKeyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_one_unique_key * - * @mbg.generated Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 */ protected boolean distinct; @@ -25,7 +25,7 @@ public class TableOneUniqueKeyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_one_unique_key * - * @mbg.generated Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 */ protected List oredCriteria; @@ -86,7 +86,7 @@ public void clear() { * This class was generated by MyBatis Generator. * This class corresponds to the database table table_one_unique_key * - * @mbg.generated Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 */ protected abstract static class GeneratedCriteria { protected List criteria; @@ -514,7 +514,7 @@ public Criteria andCidNotBetween(String value1, String value2) { * This class was generated by MyBatis Generator. * This class corresponds to the database table table_one_unique_key * - * @mbg.generated do_not_delete_during_merge Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated do_not_delete_during_merge Thu Oct 15 19:45:26 CST 2020 */ public static class Criteria extends GeneratedCriteria { protected Criteria() { @@ -526,7 +526,7 @@ protected Criteria() { * This class was generated by MyBatis Generator. * This class corresponds to the database table table_one_unique_key * - * @mbg.generated Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 */ public static class Criterion { private String condition; diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKey.java b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKey.java index 2d42e0c..dbe2591 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKey.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKey.java @@ -52,7 +52,7 @@ public class TableTwoUniqueKey implements Serializable { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_two_unique_key * - * @mbg.generated Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 */ private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKeyCriteria.java b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKeyCriteria.java index 032b91b..9f3a2db 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKeyCriteria.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKeyCriteria.java @@ -9,7 +9,7 @@ public class TableTwoUniqueKeyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_two_unique_key * - * @mbg.generated Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 */ protected String orderByClause; @@ -17,7 +17,7 @@ public class TableTwoUniqueKeyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_two_unique_key * - * @mbg.generated Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 */ protected boolean distinct; @@ -25,7 +25,7 @@ public class TableTwoUniqueKeyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_two_unique_key * - * @mbg.generated Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 */ protected List oredCriteria; @@ -86,7 +86,7 @@ public void clear() { * This class was generated by MyBatis Generator. * This class corresponds to the database table table_two_unique_key * - * @mbg.generated Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 */ protected abstract static class GeneratedCriteria { protected List criteria; @@ -514,7 +514,7 @@ public Criteria andCidNotBetween(String value1, String value2) { * This class was generated by MyBatis Generator. * This class corresponds to the database table table_two_unique_key * - * @mbg.generated do_not_delete_during_merge Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated do_not_delete_during_merge Thu Oct 15 19:45:26 CST 2020 */ public static class Criteria extends GeneratedCriteria { protected Criteria() { @@ -526,7 +526,7 @@ protected Criteria() { * This class was generated by MyBatis Generator. * This class corresponds to the database table table_two_unique_key * - * @mbg.generated Thu Oct 15 16:54:39 CST 2020 + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 */ public static class Criterion { private String condition; diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentify.java b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentify.java new file mode 100644 index 0000000..5922862 --- /dev/null +++ b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentify.java @@ -0,0 +1,58 @@ +package com.dingtalk.demo.domain; + +import java.io.Serializable; +import java.util.Date; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +/** + * 子增长测试 + * @author szy + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@ToString +public class TableWithIdentify implements Serializable { + /** + * id + */ + private Long id; + + /** + * 创建时间 + */ + private Date gmtCreate; + + /** + * 修改时间 + */ + private Date gmtModified; + + /** + * 多群主键 + */ + private Long orgId; + + /** + * 会话ID + */ + private String cid; + + /** + * 多码 + */ + private String code; + + /** + * This field was generated by MyBatis Generator. + * This field corresponds to the database table table_with_identify + * + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + */ + private static final long serialVersionUID = 1L; +} \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentifyCriteria.java b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentifyCriteria.java new file mode 100644 index 0000000..15dba2e --- /dev/null +++ b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentifyCriteria.java @@ -0,0 +1,616 @@ +package com.dingtalk.demo.domain; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class TableWithIdentifyCriteria { + /** + * This field was generated by MyBatis Generator. + * This field corresponds to the database table table_with_identify + * + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + */ + protected String orderByClause; + + /** + * This field was generated by MyBatis Generator. + * This field corresponds to the database table table_with_identify + * + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + */ + protected boolean distinct; + + /** + * This field was generated by MyBatis Generator. + * This field corresponds to the database table table_with_identify + * + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + */ + protected List oredCriteria; + + public TableWithIdentifyCriteria() { + oredCriteria = new ArrayList<>(); + } + + public void setOrderByClause(String orderByClause) { + this.orderByClause = orderByClause; + } + + public String getOrderByClause() { + return orderByClause; + } + + public void setDistinct(boolean distinct) { + this.distinct = distinct; + } + + public boolean isDistinct() { + return distinct; + } + + public List getOredCriteria() { + return oredCriteria; + } + + public void or(Criteria criteria) { + oredCriteria.add(criteria); + } + + public Criteria or() { + Criteria criteria = createCriteriaInternal(); + oredCriteria.add(criteria); + return criteria; + } + + public Criteria createCriteria() { + Criteria criteria = createCriteriaInternal(); + if (oredCriteria.size() == 0) { + oredCriteria.add(criteria); + } + return criteria; + } + + protected Criteria createCriteriaInternal() { + Criteria criteria = new Criteria(); + return criteria; + } + + public void clear() { + oredCriteria.clear(); + orderByClause = null; + distinct = false; + } + + /** + * This class was generated by MyBatis Generator. + * This class corresponds to the database table table_with_identify + * + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + */ + protected abstract static class GeneratedCriteria { + protected List criteria; + + protected GeneratedCriteria() { + super(); + criteria = new ArrayList<>(); + } + + public boolean isValid() { + return criteria.size() > 0; + } + + public List getAllCriteria() { + return criteria; + } + + public List getCriteria() { + return criteria; + } + + protected void addCriterion(String condition) { + if (condition == null) { + throw new RuntimeException("Value for condition cannot be null"); + } + criteria.add(new Criterion(condition)); + } + + protected void addCriterion(String condition, Object value, String property) { + if (value == null) { + throw new RuntimeException("Value for " + property + " cannot be null"); + } + criteria.add(new Criterion(condition, value)); + } + + protected void addCriterion(String condition, Object value1, Object value2, String property) { + if (value1 == null || value2 == null) { + throw new RuntimeException("Between values for " + property + " cannot be null"); + } + criteria.add(new Criterion(condition, value1, value2)); + } + + public Criteria andIdIsNull() { + addCriterion("id is null"); + return (Criteria) this; + } + + public Criteria andIdIsNotNull() { + addCriterion("id is not null"); + return (Criteria) this; + } + + public Criteria andIdEqualTo(Long value) { + addCriterion("id =", value, "id"); + return (Criteria) this; + } + + public Criteria andIdNotEqualTo(Long value) { + addCriterion("id <>", value, "id"); + return (Criteria) this; + } + + public Criteria andIdGreaterThan(Long value) { + addCriterion("id >", value, "id"); + return (Criteria) this; + } + + public Criteria andIdGreaterThanOrEqualTo(Long value) { + addCriterion("id >=", value, "id"); + return (Criteria) this; + } + + public Criteria andIdLessThan(Long value) { + addCriterion("id <", value, "id"); + return (Criteria) this; + } + + public Criteria andIdLessThanOrEqualTo(Long value) { + addCriterion("id <=", value, "id"); + return (Criteria) this; + } + + public Criteria andIdIn(List values) { + addCriterion("id in", values, "id"); + return (Criteria) this; + } + + public Criteria andIdNotIn(List values) { + addCriterion("id not in", values, "id"); + return (Criteria) this; + } + + public Criteria andIdBetween(Long value1, Long value2) { + addCriterion("id between", value1, value2, "id"); + return (Criteria) this; + } + + public Criteria andIdNotBetween(Long value1, Long value2) { + addCriterion("id not between", value1, value2, "id"); + return (Criteria) this; + } + + public Criteria andGmtCreateIsNull() { + addCriterion("gmt_create is null"); + return (Criteria) this; + } + + public Criteria andGmtCreateIsNotNull() { + addCriterion("gmt_create is not null"); + return (Criteria) this; + } + + public Criteria andGmtCreateEqualTo(Date value) { + addCriterion("gmt_create =", value, "gmtCreate"); + return (Criteria) this; + } + + public Criteria andGmtCreateNotEqualTo(Date value) { + addCriterion("gmt_create <>", value, "gmtCreate"); + return (Criteria) this; + } + + public Criteria andGmtCreateGreaterThan(Date value) { + addCriterion("gmt_create >", value, "gmtCreate"); + return (Criteria) this; + } + + public Criteria andGmtCreateGreaterThanOrEqualTo(Date value) { + addCriterion("gmt_create >=", value, "gmtCreate"); + return (Criteria) this; + } + + public Criteria andGmtCreateLessThan(Date value) { + addCriterion("gmt_create <", value, "gmtCreate"); + return (Criteria) this; + } + + public Criteria andGmtCreateLessThanOrEqualTo(Date value) { + addCriterion("gmt_create <=", value, "gmtCreate"); + return (Criteria) this; + } + + public Criteria andGmtCreateIn(List values) { + addCriterion("gmt_create in", values, "gmtCreate"); + return (Criteria) this; + } + + public Criteria andGmtCreateNotIn(List values) { + addCriterion("gmt_create not in", values, "gmtCreate"); + return (Criteria) this; + } + + public Criteria andGmtCreateBetween(Date value1, Date value2) { + addCriterion("gmt_create between", value1, value2, "gmtCreate"); + return (Criteria) this; + } + + public Criteria andGmtCreateNotBetween(Date value1, Date value2) { + addCriterion("gmt_create not between", value1, value2, "gmtCreate"); + return (Criteria) this; + } + + public Criteria andGmtModifiedIsNull() { + addCriterion("gmt_modified is null"); + return (Criteria) this; + } + + public Criteria andGmtModifiedIsNotNull() { + addCriterion("gmt_modified is not null"); + return (Criteria) this; + } + + public Criteria andGmtModifiedEqualTo(Date value) { + addCriterion("gmt_modified =", value, "gmtModified"); + return (Criteria) this; + } + + public Criteria andGmtModifiedNotEqualTo(Date value) { + addCriterion("gmt_modified <>", value, "gmtModified"); + return (Criteria) this; + } + + public Criteria andGmtModifiedGreaterThan(Date value) { + addCriterion("gmt_modified >", value, "gmtModified"); + return (Criteria) this; + } + + public Criteria andGmtModifiedGreaterThanOrEqualTo(Date value) { + addCriterion("gmt_modified >=", value, "gmtModified"); + return (Criteria) this; + } + + public Criteria andGmtModifiedLessThan(Date value) { + addCriterion("gmt_modified <", value, "gmtModified"); + return (Criteria) this; + } + + public Criteria andGmtModifiedLessThanOrEqualTo(Date value) { + addCriterion("gmt_modified <=", value, "gmtModified"); + return (Criteria) this; + } + + public Criteria andGmtModifiedIn(List values) { + addCriterion("gmt_modified in", values, "gmtModified"); + return (Criteria) this; + } + + public Criteria andGmtModifiedNotIn(List values) { + addCriterion("gmt_modified not in", values, "gmtModified"); + return (Criteria) this; + } + + public Criteria andGmtModifiedBetween(Date value1, Date value2) { + addCriterion("gmt_modified between", value1, value2, "gmtModified"); + return (Criteria) this; + } + + public Criteria andGmtModifiedNotBetween(Date value1, Date value2) { + addCriterion("gmt_modified not between", value1, value2, "gmtModified"); + return (Criteria) this; + } + + public Criteria andOrgIdIsNull() { + addCriterion("org_id is null"); + return (Criteria) this; + } + + public Criteria andOrgIdIsNotNull() { + addCriterion("org_id is not null"); + return (Criteria) this; + } + + public Criteria andOrgIdEqualTo(Long value) { + addCriterion("org_id =", value, "orgId"); + return (Criteria) this; + } + + public Criteria andOrgIdNotEqualTo(Long value) { + addCriterion("org_id <>", value, "orgId"); + return (Criteria) this; + } + + public Criteria andOrgIdGreaterThan(Long value) { + addCriterion("org_id >", value, "orgId"); + return (Criteria) this; + } + + public Criteria andOrgIdGreaterThanOrEqualTo(Long value) { + addCriterion("org_id >=", value, "orgId"); + return (Criteria) this; + } + + public Criteria andOrgIdLessThan(Long value) { + addCriterion("org_id <", value, "orgId"); + return (Criteria) this; + } + + public Criteria andOrgIdLessThanOrEqualTo(Long value) { + addCriterion("org_id <=", value, "orgId"); + return (Criteria) this; + } + + public Criteria andOrgIdIn(List values) { + addCriterion("org_id in", values, "orgId"); + return (Criteria) this; + } + + public Criteria andOrgIdNotIn(List values) { + addCriterion("org_id not in", values, "orgId"); + return (Criteria) this; + } + + public Criteria andOrgIdBetween(Long value1, Long value2) { + addCriterion("org_id between", value1, value2, "orgId"); + return (Criteria) this; + } + + public Criteria andOrgIdNotBetween(Long value1, Long value2) { + addCriterion("org_id not between", value1, value2, "orgId"); + return (Criteria) this; + } + + public Criteria andCidIsNull() { + addCriterion("cid is null"); + return (Criteria) this; + } + + public Criteria andCidIsNotNull() { + addCriterion("cid is not null"); + return (Criteria) this; + } + + public Criteria andCidEqualTo(String value) { + addCriterion("cid =", value, "cid"); + return (Criteria) this; + } + + public Criteria andCidNotEqualTo(String value) { + addCriterion("cid <>", value, "cid"); + return (Criteria) this; + } + + public Criteria andCidGreaterThan(String value) { + addCriterion("cid >", value, "cid"); + return (Criteria) this; + } + + public Criteria andCidGreaterThanOrEqualTo(String value) { + addCriterion("cid >=", value, "cid"); + return (Criteria) this; + } + + public Criteria andCidLessThan(String value) { + addCriterion("cid <", value, "cid"); + return (Criteria) this; + } + + public Criteria andCidLessThanOrEqualTo(String value) { + addCriterion("cid <=", value, "cid"); + return (Criteria) this; + } + + public Criteria andCidLike(String value) { + addCriterion("cid like", value, "cid"); + return (Criteria) this; + } + + public Criteria andCidNotLike(String value) { + addCriterion("cid not like", value, "cid"); + return (Criteria) this; + } + + public Criteria andCidIn(List values) { + addCriterion("cid in", values, "cid"); + return (Criteria) this; + } + + public Criteria andCidNotIn(List values) { + addCriterion("cid not in", values, "cid"); + return (Criteria) this; + } + + public Criteria andCidBetween(String value1, String value2) { + addCriterion("cid between", value1, value2, "cid"); + return (Criteria) this; + } + + public Criteria andCidNotBetween(String value1, String value2) { + addCriterion("cid not between", value1, value2, "cid"); + return (Criteria) this; + } + + public Criteria andCodeIsNull() { + addCriterion("code is null"); + return (Criteria) this; + } + + public Criteria andCodeIsNotNull() { + addCriterion("code is not null"); + return (Criteria) this; + } + + public Criteria andCodeEqualTo(String value) { + addCriterion("code =", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeNotEqualTo(String value) { + addCriterion("code <>", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeGreaterThan(String value) { + addCriterion("code >", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeGreaterThanOrEqualTo(String value) { + addCriterion("code >=", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeLessThan(String value) { + addCriterion("code <", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeLessThanOrEqualTo(String value) { + addCriterion("code <=", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeLike(String value) { + addCriterion("code like", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeNotLike(String value) { + addCriterion("code not like", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeIn(List values) { + addCriterion("code in", values, "code"); + return (Criteria) this; + } + + public Criteria andCodeNotIn(List values) { + addCriterion("code not in", values, "code"); + return (Criteria) this; + } + + public Criteria andCodeBetween(String value1, String value2) { + addCriterion("code between", value1, value2, "code"); + return (Criteria) this; + } + + public Criteria andCodeNotBetween(String value1, String value2) { + addCriterion("code not between", value1, value2, "code"); + return (Criteria) this; + } + } + + /** + * This class was generated by MyBatis Generator. + * This class corresponds to the database table table_with_identify + * + * @mbg.generated do_not_delete_during_merge Thu Oct 15 19:45:26 CST 2020 + */ + public static class Criteria extends GeneratedCriteria { + protected Criteria() { + super(); + } + } + + /** + * This class was generated by MyBatis Generator. + * This class corresponds to the database table table_with_identify + * + * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + */ + public static class Criterion { + private String condition; + + private Object value; + + private Object secondValue; + + private boolean noValue; + + private boolean singleValue; + + private boolean betweenValue; + + private boolean listValue; + + private String typeHandler; + + public String getCondition() { + return condition; + } + + public Object getValue() { + return value; + } + + public Object getSecondValue() { + return secondValue; + } + + public boolean isNoValue() { + return noValue; + } + + public boolean isSingleValue() { + return singleValue; + } + + public boolean isBetweenValue() { + return betweenValue; + } + + public boolean isListValue() { + return listValue; + } + + public String getTypeHandler() { + return typeHandler; + } + + protected Criterion(String condition) { + super(); + this.condition = condition; + this.typeHandler = null; + this.noValue = true; + } + + protected Criterion(String condition, Object value, String typeHandler) { + super(); + this.condition = condition; + this.value = value; + this.typeHandler = typeHandler; + if (value instanceof List) { + this.listValue = true; + } else { + this.singleValue = true; + } + } + + protected Criterion(String condition, Object value) { + this(condition, value, null); + } + + protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { + super(); + this.condition = condition; + this.value = value; + this.secondValue = secondValue; + this.typeHandler = typeHandler; + this.betweenValue = true; + } + + protected Criterion(String condition, Object value, Object secondValue) { + this(condition, value, secondValue, null); + } + } +} \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BasePlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BasePlugin.java index f3b42c4..320d9b2 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BasePlugin.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BasePlugin.java @@ -40,7 +40,6 @@ public class BasePlugin extends PluginAdapter { */ List uniqueKey; - IntrospectedColumn autoIncrementColumn; @Override @@ -54,8 +53,6 @@ public void initialized(IntrospectedTable introspectedTable) { this.uks = null; try { initUniqueKey(introspectedTable); - - initAutoIncrement(introspectedTable); } catch (SQLException e) { e.printStackTrace(); } @@ -68,27 +65,8 @@ public void initialized(IntrospectedTable introspectedTable) { this.uniqueKey = (List) uk; } - Object autoIncrementColumn = introspectedTable.getAttribute(PluginConstants.TABLE_COLUMN_AUTO_INCREMENT); - if(null != autoIncrementColumn){ - this.autoIncrementColumn = (IntrospectedColumn)autoIncrementColumn; - } } - /** - * 获取automentIncrement字段 - * @param it - * @throws SQLException - */ - private void initAutoIncrement(IntrospectedTable it) throws SQLException{ - TableConfiguration tc = it.getTableConfiguration(); - List columns = it.getAllColumns(); - Optional incrementCol = - columns.stream().filter(item -> item.isAutoIncrement()).findFirst(); - if(incrementCol.isPresent()){ - it.setAttribute(PluginConstants.TABLE_COLUMN_AUTO_INCREMENT,incrementCol.get()); - } - - } /** * 增加unique key 特殊属性 * diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BatchInsertPlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BatchInsertPlugin.java index 6bb389f..7aac91e 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BatchInsertPlugin.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BatchInsertPlugin.java @@ -1,8 +1,83 @@ package com.dingtalk.generator.plugins; +import com.dingtalk.generator.plugins.util.ColumnUtils; +import com.dingtalk.generator.plugins.util.JavaElementGeneratorTools; +import com.dingtalk.generator.plugins.util.XmlElementGeneratorTools; +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; +import org.mybatis.generator.api.dom.java.Interface; +import org.mybatis.generator.api.dom.java.JavaVisibility; +import org.mybatis.generator.api.dom.java.Method; +import org.mybatis.generator.api.dom.java.Parameter; +import org.mybatis.generator.api.dom.xml.Attribute; +import org.mybatis.generator.api.dom.xml.Document; +import org.mybatis.generator.api.dom.xml.TextElement; +import org.mybatis.generator.api.dom.xml.VisitableElement; +import org.mybatis.generator.api.dom.xml.XmlElement; +import org.mybatis.generator.codegen.mybatis3.ListUtilities; + /** * batchInsert Plugin * @author suyin */ public class BatchInsertPlugin extends BasePlugin { + /** + * 方法名 + */ + public static final String METHOD_BATCH_INSERT = "batchInsert"; + @Override + public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) { + + // 1. batchInsert + FullyQualifiedJavaType listType = FullyQualifiedJavaType.getNewListInstance(); + listType.addTypeArgument(introspectedTable.getRules().calculateAllFieldsClass()); + Method mBatchInsert = JavaElementGeneratorTools.generateMethod( + METHOD_BATCH_INSERT, + JavaVisibility.DEFAULT, + FullyQualifiedJavaType.getIntInstance(), + new Parameter(listType, "list", "@Param(\"list\")") + + ); +// commentGenerator.addGeneralMethodComment(mBatchInsert, introspectedTable); + // interface 增加方法 + JavaElementGeneratorTools.addMethodToInterface(interfaze, mBatchInsert); + return super.clientGenerated(interfaze, introspectedTable); + } + + @Override + public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) { + // 1. batchInsert + XmlElement answer = new XmlElement("insert"); + answer.addAttribute(new Attribute("id", METHOD_BATCH_INSERT)); + // 参数类型 + answer.addAttribute(new Attribute("parameterType", "map")); + // 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated) +// commentGenerator.addComment(answer); + + // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer +// XmlElementGeneratorTools.useGeneratedKeys(answer, introspectedTable); + + answer.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime())); + for (VisitableElement element : + XmlElementGeneratorTools.generateKeys(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), true)) { + answer.addElement(element); + } + + // 添加foreach节点 + XmlElement foreachElement = new XmlElement("foreach"); + foreachElement.addAttribute(new Attribute("collection", "list")); + foreachElement.addAttribute(new Attribute("item", "item")); + foreachElement.addAttribute(new Attribute("separator", ",")); + + for (VisitableElement element : XmlElementGeneratorTools.generateValues(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "item.")) { + foreachElement.addElement(element); + } + + // values 构建 + answer.addElement(new TextElement("values")); + answer.addElement(foreachElement); + + XmlElementGeneratorTools.addElementWithBestPosition(document.getRootElement(), answer); + return true; + } } diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/DeleteByUniqueKeyPlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/DeleteByUniqueKeyPlugin.java index cc994e9..f0dd1a7 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/DeleteByUniqueKeyPlugin.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/DeleteByUniqueKeyPlugin.java @@ -30,12 +30,6 @@ public class DeleteByUniqueKeyPlugin extends BasePlugin { public static final String METHOD_DELETE_BY_UNIQUE_KEY = "deleteByUniqueKey"; - - @Override - public void initialized(IntrospectedTable introspectedTable) { - super.initialized(introspectedTable); - } - @Override public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) { if(null == uniqueKey || uniqueKey.size() == 0){ diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/SelectByUniqueKeyPlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/SelectByUniqueKeyPlugin.java index 94250c4..5a68021 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/SelectByUniqueKeyPlugin.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/SelectByUniqueKeyPlugin.java @@ -14,7 +14,6 @@ import org.mybatis.generator.api.dom.xml.XmlElement; import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; -import java.util.List; import java.util.stream.Collectors; import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; @@ -30,13 +29,6 @@ public class SelectByUniqueKeyPlugin extends BasePlugin { */ public static final String METHOD_SELECT_BY_UNIQUE_KEY = "selectByUniqueKey"; - - @Override - public void initialized(IntrospectedTable introspectedTable) { - super.initialized(introspectedTable); - } - - @Override public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) { if(null == uniqueKey || uniqueKey.size() == 0){ diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpdateByUniqueKeySelectivePlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpdateByUniqueKeySelectivePlugin.java index 0bae791..75bab29 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpdateByUniqueKeySelectivePlugin.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpdateByUniqueKeySelectivePlugin.java @@ -31,12 +31,6 @@ public class UpdateByUniqueKeySelectivePlugin extends BasePlugin { public static final String METHOD_UPDATE_BY_UNIQUE_KEY_SELECTIVE = "updateByUniqueKeySelective"; - @Override - public void initialized(IntrospectedTable introspectedTable) { - super.initialized(introspectedTable); - } - - @Override public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) { if(null == uniqueKey || uniqueKey.size() == 0){ diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/constant/PluginConstants.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/constant/PluginConstants.java index 130cd16..7a01c02 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/constant/PluginConstants.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/constant/PluginConstants.java @@ -15,10 +15,6 @@ public class PluginConstants { */ public static final String TABLE_PROPERTY_UNIQUE_KEY = "uniqueKey"; - /** - * 用于指定生成insert、update语句的自增长字段 - */ - public static final String TABLE_COLUMN_AUTO_INCREMENT = "autoIncrement"; /** * 主键在数据库schema中的IndexName名称,FIXME 此处只验证了mysql的的默认主键名称 diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/ColumnUtils.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/ColumnUtils.java new file mode 100644 index 0000000..8984070 --- /dev/null +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/ColumnUtils.java @@ -0,0 +1,18 @@ +package com.dingtalk.generator.plugins.util; + +import org.mybatis.generator.api.IntrospectedColumn; +import org.mybatis.generator.codegen.mybatis3.ListUtilities; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * Column 工具类 + * @author suyin + */ +public class ColumnUtils { + + public static List removeIdentityAndGeneratedAlwaysColumns(List allColumns) { + return ListUtilities.removeIdentityAndGeneratedAlwaysColumns(allColumns).stream().filter(it -> !it.isAutoIncrement()).collect(Collectors.toList()); + } +} diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/XmlElementGeneratorTools.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/XmlElementGeneratorTools.java index 7ea00d5..f4fd818 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/XmlElementGeneratorTools.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/XmlElementGeneratorTools.java @@ -16,20 +16,28 @@ package com.dingtalk.generator.plugins.util; +import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.dom.OutputUtilities; import org.mybatis.generator.api.dom.xml.Attribute; +import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.VisitableElement; import org.mybatis.generator.api.dom.xml.XmlElement; +import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; import java.util.List; /** * --------------------------------------------------------------------------- * Xml 节点生成工具 + * + * @author suyin * @see org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator * --------------------------------------------------------------------------- - * @author suyin */ public class XmlElementGeneratorTools { @@ -61,6 +69,7 @@ public static XmlElement getBlobColumnListElement(IntrospectedTable introspected /** * 在最佳位置添加节点 + * * @param rootElement * @param element */ @@ -106,8 +115,10 @@ public static void addElementWithBestPosition(XmlElement rootElement, XmlElement } } } + /** * 找出节点ID值 + * * @param element * @return */ @@ -119,4 +130,108 @@ private static String getIdFromElement(XmlElement element) { } return null; } + + /** + * 生成keys Ele + * + * @param columns + * @param bracket 是否需要括号() + * @return + */ + public static List generateKeys(List columns, boolean bracket) { + return generateCommColumns(columns, null, bracket, 1); + } + + + /** + * 通用遍历columns + * + * @param columns + * @param prefix + * @param bracket 是否需要括号() + * @param type 1:key,2:value,3:set + * @return + */ + private static List generateCommColumns(List columns, String prefix, boolean bracket, int type) { + return generateCommColumns(columns, prefix, bracket, type, false); + } + + /** + * 通用遍历columns + * + * @param columns + * @param prefix + * @param bracket 是否需要括号() + * @param type 1:key,2:value,3:set + * @param upsert + * @return + */ + private static List generateCommColumns(List columns, String prefix, boolean bracket, int type, boolean upsert) { + List list = new ArrayList<>(); + + + StringBuilder sb = new StringBuilder(bracket ? "(" : ""); + Iterator columnIterator = columns.iterator(); + while (columnIterator.hasNext()) { + IntrospectedColumn introspectedColumn = columnIterator.next(); + + switch (type) { + case 3: + sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn)); + sb.append(" = "); + sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix)); + break; + case 2: + sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix)); + break; + case 1: + sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn)); + break; + } + + if (columnIterator.hasNext()) { + sb.append(", "); + } + + // 保持和官方一致 80 进行换行 + if (type == 1 || type == 2) { + if (sb.length() > 80) { + list.add(new TextElement(sb.toString())); + sb.setLength(0); + OutputUtilities.xmlIndent(sb, 1); + } + } else { + list.add(new TextElement(sb.toString())); + sb.setLength(0); + } + } + if (sb.length() > 0 || bracket) { + list.add(new TextElement(sb.append(bracket ? ")" : "").toString())); + } + + return list; + } + + + + /** + * 生成values Ele + * @param columns + * @param prefix + * @return + */ + public static List generateValues(List columns, String prefix) { + return generateValues(columns, prefix, true); + } + /** + * 生成values Ele + * @param columns + * @param prefix + * @param bracket + * @return + */ + public static List generateValues(List columns, String prefix, boolean bracket) { + return generateCommColumns(columns, prefix, bracket, 2); + } + } diff --git a/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.xml b/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.xml index 6b843ca..7a4c5c5 100644 --- a/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.xml +++ b/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.xml @@ -5,7 +5,7 @@ @@ -18,7 +18,7 @@ @@ -52,7 +52,7 @@ @@ -86,7 +86,7 @@ org_id, code, id, gmt_create, gmt_modified, cid @@ -94,7 +94,7 @@ select @@ -113,7 +113,7 @@ select @@ -125,7 +125,7 @@ delete from table_one_unique_key where org_id = #{orgId,jdbcType=BIGINT} @@ -135,7 +135,7 @@ delete from table_one_unique_key @@ -146,7 +146,7 @@ insert into table_one_unique_key (org_id, code, id, gmt_create, gmt_modified, cid @@ -159,7 +159,7 @@ insert into table_one_unique_key @@ -207,7 +207,7 @@ select count(*) from table_one_unique_key @@ -218,7 +218,7 @@ update table_one_unique_key @@ -249,7 +249,7 @@ update table_one_unique_key set org_id = #{record.orgId,jdbcType=BIGINT}, @@ -266,7 +266,7 @@ update table_one_unique_key @@ -290,7 +290,7 @@ update table_one_unique_key set id = #{id,jdbcType=BIGINT}, @@ -331,10 +331,20 @@ delete from table_one_unique_key where org_id = #{orgId,jdbcType=BIGINT} and code = #{code,jdbcType=VARCHAR} + + insert into table_one_unique_key + (org_id, code, id, gmt_create, gmt_modified, cid) + values + + (#{item.orgId,jdbcType=BIGINT}, #{item.code,jdbcType=VARCHAR}, #{item.id,jdbcType=BIGINT}, + #{item.gmtCreate,jdbcType=TIMESTAMP}, #{item.gmtModified,jdbcType=TIMESTAMP}, #{item.cid,jdbcType=VARCHAR} + ) + + \ No newline at end of file diff --git a/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.xml b/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.xml index b5a7e82..e09db5c 100644 --- a/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.xml +++ b/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.xml @@ -5,7 +5,7 @@ @@ -18,7 +18,7 @@ @@ -52,7 +52,7 @@ @@ -86,7 +86,7 @@ org_id, code, id, gmt_create, gmt_modified, cid @@ -94,7 +94,7 @@ select @@ -113,7 +113,7 @@ select @@ -125,7 +125,7 @@ delete from table_two_unique_key where org_id = #{orgId,jdbcType=BIGINT} @@ -135,7 +135,7 @@ delete from table_two_unique_key @@ -146,7 +146,7 @@ insert into table_two_unique_key (org_id, code, id, gmt_create, gmt_modified, cid @@ -159,7 +159,7 @@ insert into table_two_unique_key @@ -207,7 +207,7 @@ select count(*) from table_two_unique_key @@ -218,7 +218,7 @@ update table_two_unique_key @@ -249,7 +249,7 @@ update table_two_unique_key set org_id = #{record.orgId,jdbcType=BIGINT}, @@ -266,7 +266,7 @@ update table_two_unique_key @@ -290,7 +290,7 @@ update table_two_unique_key set id = #{id,jdbcType=BIGINT}, @@ -331,10 +331,20 @@ delete from table_two_unique_key where org_id = #{orgId,jdbcType=BIGINT} and cid = #{cid,jdbcType=VARCHAR} + + insert into table_two_unique_key + (org_id, code, id, gmt_create, gmt_modified, cid) + values + + (#{item.orgId,jdbcType=BIGINT}, #{item.code,jdbcType=VARCHAR}, #{item.id,jdbcType=BIGINT}, + #{item.gmtCreate,jdbcType=TIMESTAMP}, #{item.gmtModified,jdbcType=TIMESTAMP}, #{item.cid,jdbcType=VARCHAR} + ) + + \ No newline at end of file diff --git a/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableWithIdentifyMapper.xml b/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableWithIdentifyMapper.xml new file mode 100644 index 0000000..6a97e0e --- /dev/null +++ b/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableWithIdentifyMapper.xml @@ -0,0 +1,348 @@ + + + + + + + + + + + + + + + + + + + + + + and ${criterion.condition} + + + and ${criterion.condition} #{criterion.value} + + + and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} + + + and ${criterion.condition} + + #{listItem} + + + + + + + + + + + + + + + + + + + and ${criterion.condition} + + + and ${criterion.condition} #{criterion.value} + + + and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} + + + and ${criterion.condition} + + #{listItem} + + + + + + + + + + + + id, gmt_create, gmt_modified, org_id, cid, code + + + + + + delete from table_with_identify + where id = #{id,jdbcType=BIGINT} + + + + delete from table_with_identify + + + + + + + insert into table_with_identify (id, gmt_create, gmt_modified, + org_id, cid, code) + values (#{id,jdbcType=BIGINT}, #{gmtCreate,jdbcType=TIMESTAMP}, #{gmtModified,jdbcType=TIMESTAMP}, + #{orgId,jdbcType=BIGINT}, #{cid,jdbcType=VARCHAR}, #{code,jdbcType=VARCHAR}) + + + + insert into table_with_identify + + + id, + + + gmt_create, + + + gmt_modified, + + + org_id, + + + cid, + + + code, + + + + + #{id,jdbcType=BIGINT}, + + + #{gmtCreate,jdbcType=TIMESTAMP}, + + + #{gmtModified,jdbcType=TIMESTAMP}, + + + #{orgId,jdbcType=BIGINT}, + + + #{cid,jdbcType=VARCHAR}, + + + #{code,jdbcType=VARCHAR}, + + + + + + + update table_with_identify + + + id = #{record.id,jdbcType=BIGINT}, + + + gmt_create = #{record.gmtCreate,jdbcType=TIMESTAMP}, + + + gmt_modified = #{record.gmtModified,jdbcType=TIMESTAMP}, + + + org_id = #{record.orgId,jdbcType=BIGINT}, + + + cid = #{record.cid,jdbcType=VARCHAR}, + + + code = #{record.code,jdbcType=VARCHAR}, + + + + + + + + + update table_with_identify + set id = #{record.id,jdbcType=BIGINT}, + gmt_create = #{record.gmtCreate,jdbcType=TIMESTAMP}, + gmt_modified = #{record.gmtModified,jdbcType=TIMESTAMP}, + org_id = #{record.orgId,jdbcType=BIGINT}, + cid = #{record.cid,jdbcType=VARCHAR}, + code = #{record.code,jdbcType=VARCHAR} + + + + + + + update table_with_identify + + + gmt_create = #{gmtCreate,jdbcType=TIMESTAMP}, + + + gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}, + + + org_id = #{orgId,jdbcType=BIGINT}, + + + cid = #{cid,jdbcType=VARCHAR}, + + + code = #{code,jdbcType=VARCHAR}, + + + where id = #{id,jdbcType=BIGINT} + + + + update table_with_identify + set gmt_create = #{gmtCreate,jdbcType=TIMESTAMP}, + gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}, + org_id = #{orgId,jdbcType=BIGINT}, + cid = #{cid,jdbcType=VARCHAR}, + code = #{code,jdbcType=VARCHAR} + where id = #{id,jdbcType=BIGINT} + + + + update table_with_identify + + + id = #{id,jdbcType=BIGINT}, + + + gmt_create = #{gmtCreate,jdbcType=TIMESTAMP}, + + + gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}, + + + cid = #{cid,jdbcType=VARCHAR}, + + + where org_id = #{orgId,jdbcType=BIGINT} + and code = #{code,jdbcType=VARCHAR} + + + + delete from table_with_identify + where org_id = #{orgId,jdbcType=BIGINT} + and code = #{code,jdbcType=VARCHAR} + + + insert into table_with_identify + (id, gmt_create, gmt_modified, org_id, cid, code) + values + + (#{item.id,jdbcType=BIGINT}, #{item.gmtCreate,jdbcType=TIMESTAMP}, #{item.gmtModified,jdbcType=TIMESTAMP}, + #{item.orgId,jdbcType=BIGINT}, #{item.cid,jdbcType=VARCHAR}, #{item.code,jdbcType=VARCHAR} + ) + + + \ No newline at end of file diff --git a/generator-plugin/src/main/resources/generatorConfigMyBatis3.xml b/generator-plugin/src/main/resources/generatorConfigMyBatis3.xml index dc0a98f..75f5e28 100644 --- a/generator-plugin/src/main/resources/generatorConfigMyBatis3.xml +++ b/generator-plugin/src/main/resources/generatorConfigMyBatis3.xml @@ -41,6 +41,9 @@ + + + @@ -100,6 +103,7 @@
+
+ \ No newline at end of file diff --git a/generator-plugin/target/classes/generatorConfigMyBatis3.xml b/generator-plugin/target/classes/generatorConfigMyBatis3.xml index dc0a98f..75f5e28 100644 --- a/generator-plugin/target/classes/generatorConfigMyBatis3.xml +++ b/generator-plugin/target/classes/generatorConfigMyBatis3.xml @@ -41,6 +41,9 @@ + + + @@ -100,6 +103,7 @@
+
+ \ No newline at end of file diff --git a/generator-test/src/test/java/com/dingtalk/demo/dao/TableWithIdentifyMapperTest.java b/generator-test/src/test/java/com/dingtalk/demo/dao/TableWithIdentifyMapperTest.java new file mode 100644 index 0000000..144b77d --- /dev/null +++ b/generator-test/src/test/java/com/dingtalk/demo/dao/TableWithIdentifyMapperTest.java @@ -0,0 +1,50 @@ +package com.dingtalk.demo.dao; + +import com.dingtalk.demo.MapperBaseTest; +import com.dingtalk.demo.domain.TableOneUniqueKey; +import com.dingtalk.demo.domain.TableWithIdentify; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * CREATE TABLE `table_with_identify` ( + * `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id', + * `gmt_create` datetime NOT NULL COMMENT '创建时间', + * `gmt_modified` datetime NOT NULL COMMENT '修改时间', + * `org_id` bigint unsigned NOT NULL COMMENT '多群主键', + * `cid` varchar(32) NOT NULL COMMENT '会话ID', + * `code` varchar(32) NOT NULL COMMENT '多码', + * PRIMARY KEY (`id`), + * UNIQUE KEY `uk_org_code` (`org_id`,`code`) + * ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='自增长测试'; + */ +public class TableWithIdentifyMapperTest extends MapperBaseTest { + + @Resource + TableWithIdentifyMapper tableWithIdentifyMapper; + + List columns = new ArrayList<>(); + + @Before + public void init() { + for (int i = 0; i < 10; i++) { + columns.add(TableWithIdentify.builder() + .cid("cid" + i) + .code("code" + i).gmtCreate(new Date()).gmtModified(new Date()).orgId(1L + i) + .build() + ); + } + } + + @Test + public void testBatchInsert() { + int n = tableWithIdentifyMapper.batchInsert(columns); + System.out.println(n); + } +} diff --git a/generator-test/target/test-classes/com/dingtalk/demo/dao/TableWithIdentifyMapperTest.class b/generator-test/target/test-classes/com/dingtalk/demo/dao/TableWithIdentifyMapperTest.class new file mode 100644 index 0000000000000000000000000000000000000000..46004d099cbe30958fb9b54d2aae116f3eefbc35 GIT binary patch literal 2348 zcmb_eTUQ%Z6#h;K3>k)tP@pZfH&GL`OtE+?7==`7YA8q$q+aStW(Wh58JtYI2c{^O)7}x{erT z6F7%Abi9eTG`y{&2eZAnsn$6q=9PFyM;h;Hcuz+^X4S)j5{o)AxTUoBm00S-ZG51@ zW&3akA108)vW~l0(XguFBZ2Wy7F>JN33sxV?S;@nz_|>3j(^D(@ zZndoJh9Wz&8al3-b1ETUCh|_nlVLTm1=3Hwo^J=zFH6TeiE}Q^U_JAW=Y(m2u4HOM zAfE9ph8@f~o_)7kF4#eIJb~eyUzF~K3>((cbn0#mitw-$fY~cd}l^4wSp6IP#mOjo^AdqH@V`d78@| zN~CP3RT&JtfJuQ7rPrxm2m*Pa_%?6}mo=;z$b@#EWFk zz$QLXV#~lie5&Cy12^%xz`$|nTQa2G7r1ZWOWfDM>;)7JSm5?HN*Z$Md) zL$FU*oB=ZbpQMfl5ZG!9`xHVGVWLzHGl9)k6WDCziDroEK9X}fS^Ah|*|(f+$7UJo zJsuSy=X)i##{=nB?UilbtmK_mBGqex?B=!2((wpj)AiH$SNQQe&u?suKMoiOJWs8M>qV|m;{GXsPKT*m-r nFpAfjsp;8BI6#=!BM?UwnW0CFwo2Z>$6R&hPV Date: Fri, 16 Oct 2020 14:43:09 +0800 Subject: [PATCH 3/4] add limit upsert forupdate plugin --- generator-plugin/generator-plugin.iml | 1 + generator-plugin/pom.xml | 4 + .../demo/dao/TableOneUniqueKeyMapper.java | 6 + .../demo/dao/TableTwoUniqueKeyMapper.java | 6 + .../demo/dao/TableWithIdentifyMapper.java | 6 + .../demo/domain/TableOneUniqueKey.java | 2 +- .../domain/TableOneUniqueKeyCriteria.java | 45 ++++- .../demo/domain/TableTwoUniqueKey.java | 2 +- .../domain/TableTwoUniqueKeyCriteria.java | 45 ++++- .../demo/domain/TableWithIdentify.java | 4 +- .../domain/TableWithIdentifyCriteria.java | 45 ++++- .../generator/plugins/BasePlugin.java | 30 ++++ .../generator/plugins/BatchInsertPlugin.java | 14 +- .../plugins/DeleteByUniqueKeyPlugin.java | 22 +-- .../generator/plugins/LimitPlugin.java | 162 +++++++++++++++++ .../SelectByUniqueKeyForUpdatePlugin.java | 127 +++++++++++++ .../plugins/SelectByUniqueKeyPlugin.java | 24 +-- .../UpdateByUniqueKeySelectivePlugin.java | 22 +-- .../plugins/UpsertByUniqueKeyPlugin.java | 149 +++++++++++++++ .../plugins/constant/PluginConstants.java | 5 +- .../plugins/selectByExampleForUpdate.java | 104 +++++++++++ .../generator/plugins/util/BeanUtils.java | 25 +++ .../util/JavaElementGeneratorTools.java | 15 +- .../util/XmlElementGeneratorTools.java | 170 ++++++++++++++++++ .../demo/dao/TableOneUniqueKeyMapper.xml | 121 ++++++++++--- .../demo/dao/TableTwoUniqueKeyMapper.xml | 121 ++++++++++--- .../demo/dao/TableWithIdentifyMapper.xml | 133 ++++++++++++-- .../resources/generatorConfigMyBatis3.xml | 10 +- .../src/main/resources/mybatis-config.xml | 2 +- .../classes/generatorConfigMyBatis3.xml | 10 +- .../target/classes/mybatis-config.xml | 2 +- generator-test/generator-test.iml | 13 +- generator-test/pom.xml | 21 ++- .../common/log/MybatisInterceptor.java | 136 ++++++++++++++ .../demo/dao/TableOneUniqueKeyMapperTest.java | 70 ++++++++ .../demo/dao/TableTwoUniqueKeyMapperTest.java | 1 - .../demo/dao/TableWithIdentifyMapperTest.java | 16 +- .../test/resources/spring/spring-dao-test.xml | 16 +- .../test/resources/test-mybatis-config.xml | 17 ++ .../common/log/MybatisInterceptor.class | Bin 0 -> 6489 bytes .../dao/TableOneUniqueKeyMapperTest.class | Bin 3027 -> 5689 bytes .../dao/TableTwoUniqueKeyMapperTest.class | Bin 2210 -> 2210 bytes .../dao/TableWithIdentifyMapperTest.class | Bin 2348 -> 2562 bytes .../test-classes/spring/spring-dao-test.xml | 16 +- .../test-classes/test-mybatis-config.xml | 17 ++ pom.xml | 40 ++--- 46 files changed, 1597 insertions(+), 200 deletions(-) create mode 100644 generator-plugin/src/main/java/com/dingtalk/generator/plugins/LimitPlugin.java create mode 100644 generator-plugin/src/main/java/com/dingtalk/generator/plugins/SelectByUniqueKeyForUpdatePlugin.java create mode 100644 generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpsertByUniqueKeyPlugin.java create mode 100644 generator-plugin/src/main/java/com/dingtalk/generator/plugins/selectByExampleForUpdate.java create mode 100644 generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/BeanUtils.java create mode 100644 generator-test/src/main/java/com/dingtalk/common/log/MybatisInterceptor.java create mode 100644 generator-test/src/test/resources/test-mybatis-config.xml create mode 100644 generator-test/target/classes/com/dingtalk/common/log/MybatisInterceptor.class create mode 100644 generator-test/target/test-classes/test-mybatis-config.xml diff --git a/generator-plugin/generator-plugin.iml b/generator-plugin/generator-plugin.iml index ccb5fde..3a842eb 100644 --- a/generator-plugin/generator-plugin.iml +++ b/generator-plugin/generator-plugin.iml @@ -12,6 +12,7 @@ + diff --git a/generator-plugin/pom.xml b/generator-plugin/pom.xml index ab5f2af..fb9edee 100644 --- a/generator-plugin/pom.xml +++ b/generator-plugin/pom.xml @@ -22,6 +22,10 @@ lombok provided + + com.google.code.gson + gson + org.mybatis.generator mybatis-generator-core diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.java b/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.java index 1c57d2c..b285b15 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.java @@ -18,6 +18,8 @@ public interface TableOneUniqueKeyMapper { List selectByExample(TableOneUniqueKeyCriteria example); + TableOneUniqueKey selectByExampleForUpdate(TableOneUniqueKeyCriteria example); + TableOneUniqueKey selectByPrimaryKey(@Param("orgId") Long orgId, @Param("code") String code); int updateByExampleSelective(@Param("record") TableOneUniqueKey record, @Param("example") TableOneUniqueKeyCriteria example); @@ -30,9 +32,13 @@ public interface TableOneUniqueKeyMapper { TableOneUniqueKey selectByUniqueKey(@Param("orgId") Long orgId, @Param("code") String code); + TableOneUniqueKey selectByUniqueKeyForUpdate(@Param("orgId") Long orgId, @Param("code") String code); + int updateByUniqueKeySelective(TableOneUniqueKey record); int deleteByUniqueKey(@Param("orgId") Long orgId, @Param("code") String code); int batchInsert(@Param("list") List list); + + int upsertByUniqueKey(TableOneUniqueKey record); } \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.java b/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.java index a854b27..969e042 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.java @@ -18,6 +18,8 @@ public interface TableTwoUniqueKeyMapper { List selectByExample(TableTwoUniqueKeyCriteria example); + TableTwoUniqueKey selectByExampleForUpdate(TableTwoUniqueKeyCriteria example); + TableTwoUniqueKey selectByPrimaryKey(@Param("orgId") Long orgId, @Param("code") String code); int updateByExampleSelective(@Param("record") TableTwoUniqueKey record, @Param("example") TableTwoUniqueKeyCriteria example); @@ -30,9 +32,13 @@ public interface TableTwoUniqueKeyMapper { TableTwoUniqueKey selectByUniqueKey(@Param("orgId") Long orgId, @Param("cid") String cid); + TableTwoUniqueKey selectByUniqueKeyForUpdate(@Param("orgId") Long orgId, @Param("cid") String cid); + int updateByUniqueKeySelective(TableTwoUniqueKey record); int deleteByUniqueKey(@Param("orgId") Long orgId, @Param("cid") String cid); int batchInsert(@Param("list") List list); + + int upsertByUniqueKey(TableTwoUniqueKey record); } \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableWithIdentifyMapper.java b/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableWithIdentifyMapper.java index 5b6f338..dbfdc78 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableWithIdentifyMapper.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/dao/TableWithIdentifyMapper.java @@ -18,6 +18,8 @@ public interface TableWithIdentifyMapper { List selectByExample(TableWithIdentifyCriteria example); + TableWithIdentify selectByExampleForUpdate(TableWithIdentifyCriteria example); + TableWithIdentify selectByPrimaryKey(Long id); int updateByExampleSelective(@Param("record") TableWithIdentify record, @Param("example") TableWithIdentifyCriteria example); @@ -30,9 +32,13 @@ public interface TableWithIdentifyMapper { TableWithIdentify selectByUniqueKey(@Param("orgId") Long orgId, @Param("code") String code); + TableWithIdentify selectByUniqueKeyForUpdate(@Param("orgId") Long orgId, @Param("code") String code); + int updateByUniqueKeySelective(TableWithIdentify record); int deleteByUniqueKey(@Param("orgId") Long orgId, @Param("code") String code); int batchInsert(@Param("list") List list); + + int upsertByUniqueKey(TableWithIdentify record); } \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKey.java b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKey.java index fc767ac..911e34a 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKey.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKey.java @@ -52,7 +52,7 @@ public class TableOneUniqueKey implements Serializable { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_one_unique_key * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:21 CST 2020 */ private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKeyCriteria.java b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKeyCriteria.java index aaa1d3d..4daa253 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKeyCriteria.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableOneUniqueKeyCriteria.java @@ -9,7 +9,7 @@ public class TableOneUniqueKeyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_one_unique_key * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:21 CST 2020 */ protected String orderByClause; @@ -17,7 +17,7 @@ public class TableOneUniqueKeyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_one_unique_key * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:21 CST 2020 */ protected boolean distinct; @@ -25,10 +25,14 @@ public class TableOneUniqueKeyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_one_unique_key * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:21 CST 2020 */ protected List oredCriteria; + protected Integer start; + + protected Integer limit; + public TableOneUniqueKeyCriteria() { oredCriteria = new ArrayList<>(); } @@ -80,13 +84,42 @@ public void clear() { oredCriteria.clear(); orderByClause = null; distinct = false; + start = null; + limit = null; + } + + public void setStart(Integer start) { + this.start = start; + } + + public Integer getStart() { + return this.start; + } + + public void setLimit(Integer limit) { + this.limit = limit; + } + + public Integer getLimit() { + return this.limit; + } + + public TableOneUniqueKeyCriteria limit(Integer limit) { + this.limit = limit; + return this; + } + + public TableOneUniqueKeyCriteria limit(Integer start, Integer limit) { + this.start = start; + this.limit = limit; + return this; } /** * This class was generated by MyBatis Generator. * This class corresponds to the database table table_one_unique_key * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:21 CST 2020 */ protected abstract static class GeneratedCriteria { protected List criteria; @@ -514,7 +547,7 @@ public Criteria andCidNotBetween(String value1, String value2) { * This class was generated by MyBatis Generator. * This class corresponds to the database table table_one_unique_key * - * @mbg.generated do_not_delete_during_merge Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated do_not_delete_during_merge Fri Oct 16 14:39:21 CST 2020 */ public static class Criteria extends GeneratedCriteria { protected Criteria() { @@ -526,7 +559,7 @@ protected Criteria() { * This class was generated by MyBatis Generator. * This class corresponds to the database table table_one_unique_key * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:21 CST 2020 */ public static class Criterion { private String condition; diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKey.java b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKey.java index dbe2591..caf9687 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKey.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKey.java @@ -52,7 +52,7 @@ public class TableTwoUniqueKey implements Serializable { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_two_unique_key * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:21 CST 2020 */ private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKeyCriteria.java b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKeyCriteria.java index 9f3a2db..b7e80b9 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKeyCriteria.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableTwoUniqueKeyCriteria.java @@ -9,7 +9,7 @@ public class TableTwoUniqueKeyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_two_unique_key * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:21 CST 2020 */ protected String orderByClause; @@ -17,7 +17,7 @@ public class TableTwoUniqueKeyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_two_unique_key * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:21 CST 2020 */ protected boolean distinct; @@ -25,10 +25,14 @@ public class TableTwoUniqueKeyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_two_unique_key * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:21 CST 2020 */ protected List oredCriteria; + protected Integer start; + + protected Integer limit; + public TableTwoUniqueKeyCriteria() { oredCriteria = new ArrayList<>(); } @@ -80,13 +84,42 @@ public void clear() { oredCriteria.clear(); orderByClause = null; distinct = false; + start = null; + limit = null; + } + + public void setStart(Integer start) { + this.start = start; + } + + public Integer getStart() { + return this.start; + } + + public void setLimit(Integer limit) { + this.limit = limit; + } + + public Integer getLimit() { + return this.limit; + } + + public TableTwoUniqueKeyCriteria limit(Integer limit) { + this.limit = limit; + return this; + } + + public TableTwoUniqueKeyCriteria limit(Integer start, Integer limit) { + this.start = start; + this.limit = limit; + return this; } /** * This class was generated by MyBatis Generator. * This class corresponds to the database table table_two_unique_key * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:21 CST 2020 */ protected abstract static class GeneratedCriteria { protected List criteria; @@ -514,7 +547,7 @@ public Criteria andCidNotBetween(String value1, String value2) { * This class was generated by MyBatis Generator. * This class corresponds to the database table table_two_unique_key * - * @mbg.generated do_not_delete_during_merge Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated do_not_delete_during_merge Fri Oct 16 14:39:21 CST 2020 */ public static class Criteria extends GeneratedCriteria { protected Criteria() { @@ -526,7 +559,7 @@ protected Criteria() { * This class was generated by MyBatis Generator. * This class corresponds to the database table table_two_unique_key * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:21 CST 2020 */ public static class Criterion { private String condition; diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentify.java b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentify.java index 5922862..79a4ae2 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentify.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentify.java @@ -9,7 +9,7 @@ import lombok.ToString; /** - * 子增长测试 + * 自增长测试 * @author szy */ @Data @@ -52,7 +52,7 @@ public class TableWithIdentify implements Serializable { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_with_identify * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:22 CST 2020 */ private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentifyCriteria.java b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentifyCriteria.java index 15dba2e..00ce407 100644 --- a/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentifyCriteria.java +++ b/generator-plugin/src/main/java/com/dingtalk/demo/domain/TableWithIdentifyCriteria.java @@ -9,7 +9,7 @@ public class TableWithIdentifyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_with_identify * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:22 CST 2020 */ protected String orderByClause; @@ -17,7 +17,7 @@ public class TableWithIdentifyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_with_identify * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:22 CST 2020 */ protected boolean distinct; @@ -25,10 +25,14 @@ public class TableWithIdentifyCriteria { * This field was generated by MyBatis Generator. * This field corresponds to the database table table_with_identify * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:22 CST 2020 */ protected List oredCriteria; + protected Integer start; + + protected Integer limit; + public TableWithIdentifyCriteria() { oredCriteria = new ArrayList<>(); } @@ -80,13 +84,42 @@ public void clear() { oredCriteria.clear(); orderByClause = null; distinct = false; + start = null; + limit = null; + } + + public void setStart(Integer start) { + this.start = start; + } + + public Integer getStart() { + return this.start; + } + + public void setLimit(Integer limit) { + this.limit = limit; + } + + public Integer getLimit() { + return this.limit; + } + + public TableWithIdentifyCriteria limit(Integer limit) { + this.limit = limit; + return this; + } + + public TableWithIdentifyCriteria limit(Integer start, Integer limit) { + this.start = start; + this.limit = limit; + return this; } /** * This class was generated by MyBatis Generator. * This class corresponds to the database table table_with_identify * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:22 CST 2020 */ protected abstract static class GeneratedCriteria { protected List criteria; @@ -514,7 +547,7 @@ public Criteria andCodeNotBetween(String value1, String value2) { * This class was generated by MyBatis Generator. * This class corresponds to the database table table_with_identify * - * @mbg.generated do_not_delete_during_merge Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated do_not_delete_during_merge Fri Oct 16 14:39:22 CST 2020 */ public static class Criteria extends GeneratedCriteria { protected Criteria() { @@ -526,7 +559,7 @@ protected Criteria() { * This class was generated by MyBatis Generator. * This class corresponds to the database table table_with_identify * - * @mbg.generated Thu Oct 15 19:45:26 CST 2020 + * @mbg.generated Fri Oct 16 14:39:22 CST 2020 */ public static class Criterion { private String condition; diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BasePlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BasePlugin.java index 320d9b2..9d0f62e 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BasePlugin.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BasePlugin.java @@ -41,6 +41,12 @@ public class BasePlugin extends PluginAdapter { List uniqueKey; + /** + * 严格按照数据库顺序的allColumns + */ + List allColumns; + + @Override public boolean validate(List warnings) { @@ -53,6 +59,7 @@ public void initialized(IntrospectedTable introspectedTable) { this.uks = null; try { initUniqueKey(introspectedTable); + initAllColumns(introspectedTable); } catch (SQLException e) { e.printStackTrace(); } @@ -67,6 +74,29 @@ public void initialized(IntrospectedTable introspectedTable) { } + /** + * 按照数据库顺序的all columns + * @param it + */ + private void initAllColumns(IntrospectedTable it) throws SQLException { + Connection connection = getConnection(); + DatabaseMetaData metaData = connection.getMetaData(); + TableConfiguration tc = it.getTableConfiguration(); + ActualTableName tb = getTableInfo(metaData, tc); + // uks Map> + Map> uks = new HashMap<>(5); + ResultSet rs = metaData.getColumns(tb.getCatalog(), tb.getSchema(), tb.getTableName(),null); + allColumns = new ArrayList<>(); + while (rs.next()) { + // @see https://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getColumns(java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String) + Optional columnOptional = it.getColumn(rs.getString("COLUMN_NAME")); + if(!columnOptional.isPresent()){ + continue; + } + allColumns.add(columnOptional.get()); + } + } + /** * 增加unique key 特殊属性 * diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BatchInsertPlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BatchInsertPlugin.java index 7aac91e..b9bc10e 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BatchInsertPlugin.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/BatchInsertPlugin.java @@ -35,6 +35,7 @@ public boolean clientGenerated(Interface interfaze, IntrospectedTable introspect METHOD_BATCH_INSERT, JavaVisibility.DEFAULT, FullyQualifiedJavaType.getIntInstance(), + true, new Parameter(listType, "list", "@Param(\"list\")") ); @@ -52,14 +53,15 @@ public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable intr // 参数类型 answer.addAttribute(new Attribute("parameterType", "map")); // 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated) -// commentGenerator.addComment(answer); + context.getCommentGenerator().addComment(answer); // 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer // XmlElementGeneratorTools.useGeneratedKeys(answer, introspectedTable); answer.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime())); for (VisitableElement element : - XmlElementGeneratorTools.generateKeys(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), true)) { + XmlElementGeneratorTools.generateKeys(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(allColumns), + true)) { answer.addElement(element); } @@ -69,9 +71,11 @@ public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable intr foreachElement.addAttribute(new Attribute("item", "item")); foreachElement.addAttribute(new Attribute("separator", ",")); - for (VisitableElement element : XmlElementGeneratorTools.generateValues(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "item.")) { - foreachElement.addElement(element); - } + + XmlElement valuesTrimElement = new XmlElement("trim"); + foreachElement.addElement(valuesTrimElement); + valuesTrimElement.addElement(XmlElementGeneratorTools.generateValuesSelective(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(allColumns), "item.", true) ); + // values 构建 answer.addElement(new TextElement("values")); diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/DeleteByUniqueKeyPlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/DeleteByUniqueKeyPlugin.java index f0dd1a7..009b641 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/DeleteByUniqueKeyPlugin.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/DeleteByUniqueKeyPlugin.java @@ -12,6 +12,7 @@ import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.Document; import org.mybatis.generator.api.dom.xml.TextElement; +import org.mybatis.generator.api.dom.xml.VisitableElement; import org.mybatis.generator.api.dom.xml.XmlElement; import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; @@ -41,6 +42,7 @@ public boolean clientGenerated(Interface interfaze, IntrospectedTable introspect METHOD_DELETE_BY_UNIQUE_KEY, JavaVisibility.PUBLIC, new FullyQualifiedJavaType("int"), + true, uniqueKey.stream().map(it -> new Parameter(it.getFullyQualifiedJavaType(),it.getJavaProperty(), "@Param(\""+it.getJavaProperty()+"\")" )).collect(Collectors.toList()) @@ -89,23 +91,9 @@ public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable intr sb.append("delete from "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime()); answer.addElement(new TextElement(sb.toString())); - - boolean and = false; - for (IntrospectedColumn introspectedColumn : uniqueKey) { - sb.setLength(0); - if (and) { - sb.append(" and "); //$NON-NLS-1$ - } else { - sb.append("where "); //$NON-NLS-1$ - and = true; - } - - sb.append(MyBatis3FormattingUtilities - .getEscapedColumnName(introspectedColumn)); - sb.append(" = "); //$NON-NLS-1$ - sb.append(MyBatis3FormattingUtilities - .getParameterClause(introspectedColumn)); - answer.addElement(new TextElement(sb.toString())); + // where + for (VisitableElement where :XmlElementGeneratorTools.generateWheres(uniqueKey)) { + answer.addElement(where); } XmlElementGeneratorTools.addElementWithBestPosition(document.getRootElement(), answer); return true; diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/LimitPlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/LimitPlugin.java new file mode 100644 index 0000000..63c03a0 --- /dev/null +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/LimitPlugin.java @@ -0,0 +1,162 @@ +package com.dingtalk.generator.plugins; + +import com.dingtalk.generator.plugins.util.JavaElementGeneratorTools; +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.dom.java.Field; +import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; +import org.mybatis.generator.api.dom.java.JavaVisibility; +import org.mybatis.generator.api.dom.java.Method; +import org.mybatis.generator.api.dom.java.Parameter; +import org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper; +import org.mybatis.generator.api.dom.java.TopLevelClass; +import org.mybatis.generator.api.dom.xml.Attribute; +import org.mybatis.generator.api.dom.xml.TextElement; +import org.mybatis.generator.api.dom.xml.XmlElement; + +import java.util.List; + +/** + * 分页插件 + * @author suyin + */ +public class LimitPlugin extends BasePlugin { + + @Override + public boolean validate(List warnings) { + // 只支持MYSQL的limit 处理 + if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false + && "com.mysql.cj.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false) { + return false; + } + return super.validate(warnings); + } + + /** + * ModelExample Methods 生成 + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param topLevelClass + * @param introspectedTable + * @return + */ + @Override + public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { + PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper(); + // 添加offset和rows字段 + Field offsetField = JavaElementGeneratorTools.generateField( + "start", + JavaVisibility.PROTECTED, + integerWrapper, + null + ); + // commentGenerator.addFieldComment(offsetField, introspectedTable); + topLevelClass.addField(offsetField); + + Field rowsField = JavaElementGeneratorTools.generateField( + "limit", + JavaVisibility.PROTECTED, + integerWrapper, + null + ); +// commentGenerator.addFieldComment(rowsField, introspectedTable); + topLevelClass.addField(rowsField); + System.out.println("(MySQL分页插件):" + topLevelClass.getType().getShortName() + "增加start和limit字段"); + + // 增加getter && setter 方法 + Method mSetOffset = JavaElementGeneratorTools.generateSetterMethod(offsetField); +// commentGenerator.addGeneralMethodComment(mSetOffset, introspectedTable); + topLevelClass.getMethods().add(mSetOffset); + + Method mGetOffset = JavaElementGeneratorTools.generateGetterMethod(offsetField); +// commentGenerator.addGeneralMethodComment(mGetOffset, introspectedTable); + topLevelClass.getMethods().add(mGetOffset); + + Method mSetRows = JavaElementGeneratorTools.generateSetterMethod(rowsField); +// commentGenerator.addGeneralMethodComment(mSetRows, introspectedTable); + topLevelClass.getMethods().add(mSetRows); + + Method mGetRows = JavaElementGeneratorTools.generateGetterMethod(rowsField); +// commentGenerator.addGeneralMethodComment(mGetRows, introspectedTable); + topLevelClass.getMethods().add(mGetRows); + + System.out.println("(MySQL分页插件):" + topLevelClass.getType().getShortName() + "增加offset和rows的getter和setter实现。"); + + // 提供几个快捷方法 + Method setLimit = JavaElementGeneratorTools.generateMethod( + "limit", + JavaVisibility.PUBLIC, + topLevelClass.getType(), + false, + new Parameter(integerWrapper, "limit") + ); +// commentGenerator.addGeneralMethodComment(setLimit, introspectedTable); + setLimit = JavaElementGeneratorTools.generateMethodBody( + setLimit, + "this.limit = limit;", + "return this;" + ); + topLevelClass.getMethods().add(setLimit); + + Method setLimit2 = JavaElementGeneratorTools.generateMethod( + "limit", + JavaVisibility.PUBLIC, + topLevelClass.getType(), + false, + new Parameter(integerWrapper, "start"), + new Parameter(integerWrapper, "limit") + ); +// commentGenerator.addGeneralMethodComment(setLimit2, introspectedTable); + setLimit2 = JavaElementGeneratorTools.generateMethodBody( + setLimit2, + "this.start = start;", + "this.limit = limit;", + "return this;" + ); + topLevelClass.getMethods().add(setLimit2); + System.out.println("(MySQL分页插件):" + topLevelClass.getType().getShortName() + "增加limit方法。"); + + + // !!! clear 方法增加 offset 和 rows的清理 + List methodList = topLevelClass.getMethods(); + for (Method method : methodList) { + if (method.getName().equals("clear")) { + method.addBodyLine("start = null;"); + method.addBodyLine("limit = null;"); + } + } + return true; + } + + @Override + public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + this.generateLimitElement(element); + return true; + } + + @Override + public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { + this.generateLimitElement(element); + return true; + } + + /** + * 生成limit节点 + * @param element + */ + private void generateLimitElement(XmlElement element) { + XmlElement ifLimitNotNullElement = new XmlElement("if"); + ifLimitNotNullElement.addAttribute(new Attribute("test", "limit != null")); + + XmlElement ifOffsetNotNullElement = new XmlElement("if"); + ifOffsetNotNullElement.addAttribute(new Attribute("test", "start != null")); + ifOffsetNotNullElement.addElement(new TextElement("limit ${start}, ${limit}")); + ifLimitNotNullElement.addElement(ifOffsetNotNullElement); + + XmlElement ifOffsetNullElement = new XmlElement("if"); + ifOffsetNullElement.addAttribute(new Attribute("test", "start == null")); + ifOffsetNullElement.addElement(new TextElement("limit ${limit}")); + ifLimitNotNullElement.addElement(ifOffsetNullElement); + + element.addElement(ifLimitNotNullElement); + } + +} diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/SelectByUniqueKeyForUpdatePlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/SelectByUniqueKeyForUpdatePlugin.java new file mode 100644 index 0000000..992aafa --- /dev/null +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/SelectByUniqueKeyForUpdatePlugin.java @@ -0,0 +1,127 @@ +package com.dingtalk.generator.plugins; + +import com.dingtalk.generator.plugins.util.JavaElementGeneratorTools; +import com.dingtalk.generator.plugins.util.XmlElementGeneratorTools; +import org.mybatis.generator.api.IntrospectedColumn; +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.dom.java.Interface; +import org.mybatis.generator.api.dom.java.JavaVisibility; +import org.mybatis.generator.api.dom.java.Method; +import org.mybatis.generator.api.dom.java.Parameter; +import org.mybatis.generator.api.dom.xml.Attribute; +import org.mybatis.generator.api.dom.xml.Document; +import org.mybatis.generator.api.dom.xml.TextElement; +import org.mybatis.generator.api.dom.xml.VisitableElement; +import org.mybatis.generator.api.dom.xml.XmlElement; +import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; + +import java.util.stream.Collectors; + +import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; + +/** + * select by uniqueKey, should assign unique key name + * @author suyin + */ +public class SelectByUniqueKeyForUpdatePlugin extends BasePlugin { + + /** + * Mapper.java 方法名 + */ + public static final String METHOD_SELECT_BY_UNIQUE_KEY_FOR_UPDATE = "selectByUniqueKeyForUpdate"; + + @Override + public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) { + if(null == uniqueKey || uniqueKey.size() == 0){ + System.err.println("generator selectByUniqueKey break ,cause by no unique can be found"); + return false; + } + // 方法生成 + Method selectMethod = JavaElementGeneratorTools.generateMethod( + METHOD_SELECT_BY_UNIQUE_KEY_FOR_UPDATE, + JavaVisibility.PUBLIC, + JavaElementGeneratorTools.getModelTypeWithBLOBs(introspectedTable), + true, + uniqueKey.stream().map(it -> new Parameter(it.getFullyQualifiedJavaType(),it.getJavaProperty(), + "@Param(\""+it.getJavaProperty()+"\")" + )).collect(Collectors.toList()) + ); + + // 注释生成 + // commentGenerator.addGeneralMethodComment(selectOneMethod, introspectedTable); + + // 构造interface + JavaElementGeneratorTools.addMethodToInterface(interfaze, selectMethod); + return super.clientGenerated(interfaze, introspectedTable); + } + + /** + * 生成XML文件 + * @see org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.SelectByPrimaryKeyElementGenerator + * @param document + * @param introspectedTable + * @return + */ + @Override + public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) { + + if(null == uniqueKey || uniqueKey.size() == 0){ + return false; + } + // 生成查询语句 + XmlElement answer = new XmlElement("select"); + // 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated) + context.getCommentGenerator().addComment(answer); + + // 添加ID + answer.addAttribute(new Attribute("id", METHOD_SELECT_BY_UNIQUE_KEY_FOR_UPDATE)); + // 添加返回类型 + if (introspectedTable.getRules().generateResultMapWithBLOBs()) { + answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$ + introspectedTable.getResultMapWithBLOBsId())); + } else { + answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$ + introspectedTable.getBaseResultMapId())); + } + + // 添加参数类型 + String parameterType; + if (uniqueKey.size() == 1) { + parameterType = uniqueKey.get(0).getFullyQualifiedJavaType().getFullyQualifiedName(); + } else { + // PK fields are in the base class. If more than on PK + // field, then they are coming in a map. + parameterType = "map"; //$NON-NLS-1$ + } + answer.addAttribute(new Attribute("parameterType", parameterType)); + answer.addElement(new TextElement("select")); + + StringBuilder sb = new StringBuilder(); + if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) { + sb.append('\''); + sb.append(introspectedTable.getSelectByExampleQueryId()); + sb.append("' as QUERYID,"); + } + answer.addElement(new TextElement(sb.toString())); + + answer.addElement(XmlElementGeneratorTools.getBaseColumnListElement(introspectedTable)); + if (introspectedTable.hasBLOBColumns()) { + answer.addElement(new TextElement(",")); //$NON-NLS-1$ + answer.addElement(XmlElementGeneratorTools.getBlobColumnListElement(introspectedTable)); + } + // 添加from tableName + sb.setLength(0); + sb.append("from "); + sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()); + answer.addElement(new TextElement(sb.toString())); + // 添加where语句 + for (VisitableElement where :XmlElementGeneratorTools.generateWheres(uniqueKey)) { + answer.addElement(where); + } + + answer.addElement(new TextElement("for update")); + + XmlElementGeneratorTools.addElementWithBestPosition(document.getRootElement(), answer); + return true; + } +} diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/SelectByUniqueKeyPlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/SelectByUniqueKeyPlugin.java index 5a68021..27eab12 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/SelectByUniqueKeyPlugin.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/SelectByUniqueKeyPlugin.java @@ -11,6 +11,7 @@ import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.Document; import org.mybatis.generator.api.dom.xml.TextElement; +import org.mybatis.generator.api.dom.xml.VisitableElement; import org.mybatis.generator.api.dom.xml.XmlElement; import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; @@ -40,6 +41,7 @@ public boolean clientGenerated(Interface interfaze, IntrospectedTable introspect METHOD_SELECT_BY_UNIQUE_KEY, JavaVisibility.PUBLIC, JavaElementGeneratorTools.getModelTypeWithBLOBs(introspectedTable), + true, uniqueKey.stream().map(it -> new Parameter(it.getFullyQualifiedJavaType(),it.getJavaProperty(), "@Param(\""+it.getJavaProperty()+"\")" )).collect(Collectors.toList()) @@ -69,12 +71,12 @@ public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable intr // 生成查询语句 XmlElement answer = new XmlElement("select"); // 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated) -// commentGenerator.addComment(selectOneElement); + context.getCommentGenerator().addComment(answer); // 添加ID answer.addAttribute(new Attribute("id", METHOD_SELECT_BY_UNIQUE_KEY)); // 添加返回类型 - if (introspectedTable.getRules().generateResultMapWithBLOBs()) { + if (introspectedTable.hasBLOBColumns()) { answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$ introspectedTable.getResultMapWithBLOBsId())); } else { @@ -113,22 +115,8 @@ public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable intr sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()); answer.addElement(new TextElement(sb.toString())); // 添加where语句 - boolean and = false; - for (IntrospectedColumn introspectedColumn : uniqueKey) { - sb.setLength(0); - if (and) { - sb.append(" and "); //$NON-NLS-1$ - } else { - sb.append("where "); //$NON-NLS-1$ - and = true; - } - - sb.append(MyBatis3FormattingUtilities - .getAliasedEscapedColumnName(introspectedColumn)); - sb.append(" = "); //$NON-NLS-1$ - sb.append(MyBatis3FormattingUtilities - .getParameterClause(introspectedColumn)); - answer.addElement(new TextElement(sb.toString())); + for (VisitableElement where :XmlElementGeneratorTools.generateWheres(uniqueKey)) { + answer.addElement(where); } XmlElementGeneratorTools.addElementWithBestPosition(document.getRootElement(), answer); diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpdateByUniqueKeySelectivePlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpdateByUniqueKeySelectivePlugin.java index 75bab29..482e7c9 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpdateByUniqueKeySelectivePlugin.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpdateByUniqueKeySelectivePlugin.java @@ -12,6 +12,7 @@ import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.Document; import org.mybatis.generator.api.dom.xml.TextElement; +import org.mybatis.generator.api.dom.xml.VisitableElement; import org.mybatis.generator.api.dom.xml.XmlElement; import org.mybatis.generator.codegen.mybatis3.ListUtilities; import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; @@ -42,6 +43,7 @@ public boolean clientGenerated(Interface interfaze, IntrospectedTable introspect METHOD_UPDATE_BY_UNIQUE_KEY_SELECTIVE, JavaVisibility.PUBLIC, new FullyQualifiedJavaType("int"), + true, new Parameter(new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()), "record") ); @@ -85,7 +87,7 @@ public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable intr answer.addAttribute(new Attribute("parameterType", //$NON-NLS-1$ parameterType)); -// context.getCommentGenerator().addComment(answer); + context.getCommentGenerator().addComment(answer); // 增加update语句 StringBuilder sb = new StringBuilder(); @@ -118,22 +120,8 @@ public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable intr } // 增加where条件语句 - boolean and = false; - for (IntrospectedColumn introspectedColumn : uniqueKey) { - sb.setLength(0); - if (and) { - sb.append(" and "); //$NON-NLS-1$ - } else { - sb.append("where "); //$NON-NLS-1$ - and = true; - } - - sb.append(MyBatis3FormattingUtilities - .getEscapedColumnName(introspectedColumn)); - sb.append(" = "); //$NON-NLS-1$ - sb.append(MyBatis3FormattingUtilities - .getParameterClause(introspectedColumn)); - answer.addElement(new TextElement(sb.toString())); + for (VisitableElement where :XmlElementGeneratorTools.generateWheres(uniqueKey)) { + answer.addElement(where); } XmlElementGeneratorTools.addElementWithBestPosition(document.getRootElement(), answer); diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpsertByUniqueKeyPlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpsertByUniqueKeyPlugin.java new file mode 100644 index 0000000..bd4f7cc --- /dev/null +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpsertByUniqueKeyPlugin.java @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2017. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.dingtalk.generator.plugins; + +import com.dingtalk.generator.plugins.util.JavaElementGeneratorTools; +import com.dingtalk.generator.plugins.util.XmlElementGeneratorTools; +import org.mybatis.generator.api.IntrospectedColumn; +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; +import org.mybatis.generator.api.dom.java.Interface; +import org.mybatis.generator.api.dom.java.JavaVisibility; +import org.mybatis.generator.api.dom.java.Method; +import org.mybatis.generator.api.dom.java.Parameter; +import org.mybatis.generator.api.dom.xml.Attribute; +import org.mybatis.generator.api.dom.xml.Document; +import org.mybatis.generator.api.dom.xml.TextElement; +import org.mybatis.generator.api.dom.xml.VisitableElement; +import org.mybatis.generator.api.dom.xml.XmlElement; +import org.mybatis.generator.codegen.mybatis3.ListUtilities; +import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; + +import java.util.ArrayList; +import java.util.List; + +/** + * --------------------------------------------------------------------------- + * 存在即更新插件 + * --------------------------------------------------------------------------- + * --------------------------------------------------------------------------- + */ +public class UpsertByUniqueKeyPlugin extends BasePlugin { + public static final String METHOD_UPSERT_BY_UNIQUE_KEY = "upsertByUniqueKey"; // 方法名 + + /** + * {@inheritDoc} + */ + @Override + public boolean validate(List warnings) { + + // 插件使用前提是数据库为MySQL + if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false + && "com.mysql.cj.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false) { + warnings.add("itfsw:插件" + this.getClass().getTypeName() + "插件使用前提是数据库为MySQL!"); + return false; + } + return super.validate(warnings); + } + + /** + * Java Client Methods 生成 + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param interfaze + * @param introspectedTable + * @return + */ + @Override + public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) { + // ====================================== upsert ====================================== + Method mUpsert = JavaElementGeneratorTools.generateMethod( + METHOD_UPSERT_BY_UNIQUE_KEY, + JavaVisibility.DEFAULT, + FullyQualifiedJavaType.getIntInstance(), + true, + new Parameter(JavaElementGeneratorTools.getModelTypeWithoutBLOBs(introspectedTable), "record") + ); +// commentGenerator.addGeneralMethodComment(mUpsert, introspectedTable); + // interface 增加方法 + JavaElementGeneratorTools.addMethodToInterface(interfaze, mUpsert); + return super.clientGenerated(interfaze, introspectedTable); + } + + /** + * SQL Map Methods 生成 + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param document + * @param introspectedTable + * @return + */ + @Override + public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) { + this.generateXmlElement(document, introspectedTable); + + return super.sqlMapDocumentGenerated(document, introspectedTable); + } + + /** + * 生成xml + * @param document + * @param introspectedTable + */ + private void generateXmlElement(Document document, IntrospectedTable introspectedTable) { + List columns = ListUtilities.removeGeneratedAlwaysColumns( introspectedTable.getAllColumns()); + // ====================================== upsert ====================================== + XmlElement insertEle = new XmlElement("insert"); + insertEle.addAttribute(new Attribute("id", METHOD_UPSERT_BY_UNIQUE_KEY)); + + // 增加update标签的parameter + String parameterType; + if (introspectedTable.getRules().generateRecordWithBLOBsClass()) { + parameterType = introspectedTable.getRecordWithBLOBsType(); + } else { + parameterType = introspectedTable.getBaseRecordType(); + } + + insertEle.addAttribute(new Attribute("parameterType", //$NON-NLS-1$ + parameterType)); + context.getCommentGenerator().addComment(insertEle); + + + // insert + insertEle.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime())); + for (VisitableElement element : + XmlElementGeneratorTools.generateKeys(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(allColumns), + true)) { + insertEle.addElement(element); + } + + insertEle.addElement(new TextElement("values")); + insertEle.addElement(XmlElementGeneratorTools.generateValuesSelective(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(allColumns), null, true) ); + + insertEle.addElement(new TextElement("on duplicate key update ")); + // set + + List nonUniqueKeyColumns = new ArrayList<>(introspectedTable.getAllColumns()); + nonUniqueKeyColumns.removeAll(uniqueKey); + for (VisitableElement where :XmlElementGeneratorTools.generateSets(nonUniqueKeyColumns,null, false)) { + insertEle.addElement(where); + } + + + document.getRootElement().addElement(insertEle); + } + + +} \ No newline at end of file diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/constant/PluginConstants.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/constant/PluginConstants.java index 7a01c02..49fd6a1 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/constant/PluginConstants.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/constant/PluginConstants.java @@ -26,5 +26,8 @@ public class PluginConstants { */ public static final String ENUM_NAME = "Column"; - + /** + * DEFAULT VALUE EMPTY_STRING + */ + public static final String DEFAULT_VALUE_EMPTY_STRING = "''"; } diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/selectByExampleForUpdate.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/selectByExampleForUpdate.java new file mode 100644 index 0000000..3ca7cbe --- /dev/null +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/selectByExampleForUpdate.java @@ -0,0 +1,104 @@ +package com.dingtalk.generator.plugins; + +import com.dingtalk.generator.plugins.util.BeanUtils; +import com.dingtalk.generator.plugins.util.JavaElementGeneratorTools; +import com.dingtalk.generator.plugins.util.XmlElementGeneratorTools; +import org.mybatis.generator.api.IntrospectedTable; +import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; +import org.mybatis.generator.api.dom.java.Interface; +import org.mybatis.generator.api.dom.java.JavaVisibility; +import org.mybatis.generator.api.dom.java.Method; +import org.mybatis.generator.api.dom.java.Parameter; +import org.mybatis.generator.api.dom.xml.Attribute; +import org.mybatis.generator.api.dom.xml.Document; +import org.mybatis.generator.api.dom.xml.TextElement; +import org.mybatis.generator.api.dom.xml.VisitableElement; +import org.mybatis.generator.api.dom.xml.XmlElement; + +import java.lang.reflect.InvocationTargetException; +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; + +/** + * + * @author suyin + */ +public class selectByExampleForUpdate extends BasePlugin { + /** + * Mapper.java 方法名 + */ + public static final String METHOD_SELECT_BY_EXAMPLE_FOR_UPDATE = "selectByExampleForUpdate"; + + /** + * Mapper.java 方法名 + */ + public static final String METHOD_SELECT_BY_EXAMPLE_WITH_BLOBS_FOR_UPDATE = "selectByExampleWithBlobsForUpdate"; + + + @Override + public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) { + // 方法生成 + Method selectMethod = JavaElementGeneratorTools.generateMethod( + METHOD_SELECT_BY_EXAMPLE_FOR_UPDATE, + JavaVisibility.PUBLIC, + JavaElementGeneratorTools.getModelTypeWithBLOBs(introspectedTable), + true, + new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example") + ); + // 构造interface + JavaElementGeneratorTools.addMethodToInterface(interfaze, selectMethod); + return super.clientGenerated(interfaze, introspectedTable); + } + + + /** + * 生成XML文件 + * @see org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.SelectByExampleWithoutBLOBsElementGenerator + * @param document + * @param introspectedTable + * @return + */ + @Override + public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) { + + XmlElement answer = new XmlElement("select"); //$NON-NLS-1$ + + answer.addAttribute(new Attribute("id", //$NON-NLS-1$ + METHOD_SELECT_BY_EXAMPLE_FOR_UPDATE)); + // 添加返回类型 + if (introspectedTable.hasBLOBColumns()) { + answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$ + introspectedTable.getResultMapWithBLOBsId())); + } else { + answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$ + introspectedTable.getBaseResultMapId())); + } + answer.addAttribute(new Attribute("parameterType", introspectedTable.getExampleType())); //$NON-NLS-1$ + context.getCommentGenerator().addComment(answer); + answer.addElement(new TextElement("select")); //$NON-NLS-1$ + StringBuilder sb = new StringBuilder(); + if (stringHasValue(introspectedTable + .getSelectByExampleQueryId())) { + sb.append('\''); + sb.append(introspectedTable.getSelectByExampleQueryId()); + sb.append("' as QUERYID,"); //$NON-NLS-1$ + answer.addElement(new TextElement(sb.toString())); + } + answer.addElement(XmlElementGeneratorTools.getBaseColumnListElement(introspectedTable)); + + sb.setLength(0); + sb.append("from "); //$NON-NLS-1$ + sb.append(introspectedTable + .getAliasedFullyQualifiedTableNameAtRuntime()); + answer.addElement(new TextElement(sb.toString())); + answer.addElement(XmlElementGeneratorTools.getExampleIncludeElement(introspectedTable)); + + answer.addElement(new TextElement("for update")); + + XmlElementGeneratorTools.addElementWithBestPosition(document.getRootElement(), answer); + return true; + } + +} diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/BeanUtils.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/BeanUtils.java new file mode 100644 index 0000000..f2fd12f --- /dev/null +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/BeanUtils.java @@ -0,0 +1,25 @@ +package com.dingtalk.generator.plugins.util; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.mybatis.generator.api.dom.xml.Attribute; +import org.mybatis.generator.api.dom.xml.VisitableElement; +import org.mybatis.generator.api.dom.xml.XmlElement; + +public class BeanUtils { + + public static XmlElement cloneXmlElement(XmlElement ele) { + XmlElement answer = new XmlElement(ele.getName()); + if(ele.getElements() != null && ele.getElements().size() > 0){ + for (VisitableElement e : ele.getElements()){ + answer.addElement(e); + } + } + if(ele.getAttributes() != null && ele.getAttributes().size() > 0){ + for (Attribute attribute : ele.getAttributes()){ + answer.addAttribute(attribute); + } + } + return answer; + } +} diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/JavaElementGeneratorTools.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/JavaElementGeneratorTools.java index 58c6bd9..48d1fab 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/JavaElementGeneratorTools.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/JavaElementGeneratorTools.java @@ -85,7 +85,8 @@ public static Field generateField(String fieldName, JavaVisibility visibility, F * @param parameters 参数列表 * @return */ - public static Method generateMethod(String methodName, JavaVisibility visibility, FullyQualifiedJavaType returnType, Parameter... parameters) { + public static Method generateMethod(String methodName, JavaVisibility visibility, + FullyQualifiedJavaType returnType, boolean abstrat,Parameter... parameters) { List params = new ArrayList<>(); if (parameters != null) { for (Parameter parameter : parameters) { @@ -93,15 +94,17 @@ public static Method generateMethod(String methodName, JavaVisibility visibility } } - return generateMethod(methodName, visibility, returnType, params); + return generateMethod(methodName, visibility, returnType,abstrat, params); } - public static Method generateMethod(String methodName, JavaVisibility visibility, FullyQualifiedJavaType returnType, List parameters) { + public static Method generateMethod(String methodName, JavaVisibility visibility, + FullyQualifiedJavaType returnType, + boolean abstrat, List parameters) { Method method = new Method(methodName); method.setVisibility(visibility); method.setReturnType(returnType); - method.setAbstract(true); + method.setAbstract(abstrat); if (parameters != null) { for (Parameter parameter : parameters) { method.addParameter(parameter); @@ -136,6 +139,7 @@ public static Method generateSetterMethod(Field field) { "set" + FormatTools.upFirstChar(field.getName()), JavaVisibility.PUBLIC, null, + false, new Parameter(field.getType(), field.getName()) ); return generateMethodBody(method, "this." + field.getName() + " = " + field.getName() + ";"); @@ -150,7 +154,8 @@ public static Method generateGetterMethod(Field field) { Method method = generateMethod( "get" + FormatTools.upFirstChar(field.getName()), JavaVisibility.PUBLIC, - field.getType() + field.getType(), + false ); return generateMethodBody(method, "return this." + field.getName() + ";"); } diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/XmlElementGeneratorTools.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/XmlElementGeneratorTools.java index f4fd818..1c9e274 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/XmlElementGeneratorTools.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/util/XmlElementGeneratorTools.java @@ -16,6 +16,7 @@ package com.dingtalk.generator.plugins.util; +import com.dingtalk.generator.plugins.constant.PluginConstants; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.dom.OutputUtilities; @@ -234,4 +235,173 @@ public static List generateValues(List col return generateCommColumns(columns, prefix, bracket, 2); } + /** + * 生成values Ele + * @param columns + * @param prefix + * @param bracket + * @return + */ + public static List generateSets(List columns, String prefix, + boolean bracket) { + return generateCommColumns(columns, prefix, bracket, 3); + } + + /** + * 生成values Selective Ele + * @param columns + * @param prefix + * @param defaultVal 是否使用默认值,有默认值填充默认值,没有默认值,不写choose + * @return + */ + public static VisitableElement generateValuesSelective(List columns, String prefix, + boolean defaultVal) { + return generateValuesSelective(columns, prefix, true, defaultVal); + } + /** + * 生成values Selective Ele + * @param columns + * @param prefix + * @param bracket + * @param defaultVal 是否使用默认值,有默认值填充默认值,没有默认值,不写choose + * @return + */ + public static VisitableElement generateValuesSelective(List columns, String prefix, boolean bracket, + boolean defaultVal) { + return generateCommColumnsSelective(columns, prefix, bracket, 2, defaultVal); + } + + /** + * 通用遍历columns + * @param columns + * @param prefix + * @param bracket + * @param type 1:key,2:value,3:set + * @param defaultVal 是否使用默认值,有默认值填充默认值,没有默认值,不写choose + * @return + */ + private static VisitableElement generateCommColumnsSelective(List columns, String prefix, boolean bracket + , int type, boolean defaultVal) { + XmlElement trimEle = generateTrim(bracket); + for (IntrospectedColumn introspectedColumn : columns) { + generateSelectiveToTrimEleTo(trimEle, introspectedColumn, prefix, type, defaultVal); + } + return trimEle; + } + + /** + * 生成选择列到trim 节点 + * @param trimEle + * @param introspectedColumn + * @param prefix + * @param type 1:key,2:value,3:set + * @param defaultVal 是否使用默认值,有默认值填充默认值,没有默认值,不写choose + */ + private static void generateSelectiveToTrimEleTo(XmlElement trimEle, IntrospectedColumn introspectedColumn, String prefix + , int type , boolean defaultVal) { + if(defaultVal){ + + String defVal = introspectedColumn.getDefaultValue(); + if(introspectedColumn.isNullable() || null == defVal){ + // 如果没有默认值,那么直接写 x = ? + trimEle.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, + prefix) + ",")); + }else{ + /* + + + #{item.cid,jdbcType=VARCHAR}, + + + '', + + + */ + // 如果有默认值,写choose + XmlElement eleIf = new XmlElement("choose"); + XmlElement when = new XmlElement("when"); + eleIf.addElement(when); + when.addAttribute(new Attribute("test", introspectedColumn.getJavaProperty(prefix) + " != null")); + generateSelectiveCommColumnTo(when, introspectedColumn, prefix, type); + + XmlElement otherwise = new XmlElement("otherwise"); + eleIf.addElement(otherwise); + + otherwise.addElement(new TextElement(defVal.length() ==0 ? PluginConstants.DEFAULT_VALUE_EMPTY_STRING : defVal)); + trimEle.addElement(eleIf); + + } + }else { + XmlElement eleIf = new XmlElement("if"); + eleIf.addAttribute(new Attribute("test", introspectedColumn.getJavaProperty(prefix) + " != null")); + + generateSelectiveCommColumnTo(eleIf, introspectedColumn, prefix, type); + + trimEle.addElement(eleIf); + } + } + /** + * 生成 + * @param element + * @param introspectedColumn + * @param prefix + * @param type 1:key,2:value,3:set + */ + private static void generateSelectiveCommColumnTo(XmlElement element, IntrospectedColumn introspectedColumn, String prefix, int type) { + switch (type) { + case 3: + element.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ",")); + break; + case 2: + + element.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ",")); + break; + case 1: + element.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + ",")); + break; + } + } + + /** + * trim 节点 + * @param bracket + * @return + */ + private static XmlElement generateTrim(boolean bracket) { + XmlElement trimEle = new XmlElement("trim"); + if (bracket) { + trimEle.addAttribute(new Attribute("prefix", "(")); + trimEle.addAttribute(new Attribute("suffix", ")")); + trimEle.addAttribute(new Attribute("suffixOverrides", ",")); + } else { + trimEle.addAttribute(new Attribute("suffixOverrides", ",")); + } + return trimEle; + } + + + public static List generateWheres(List uniqueKey) { + List answer = new ArrayList<>(); + // 添加where语句 + boolean and = false; + StringBuilder sb = new StringBuilder(); + for (IntrospectedColumn introspectedColumn : uniqueKey) { + sb.setLength(0); + if (and) { + sb.append(" and "); //$NON-NLS-1$ + } else { + sb.append("where "); //$NON-NLS-1$ + and = true; + } + + sb.append(MyBatis3FormattingUtilities + .getAliasedEscapedColumnName(introspectedColumn)); + sb.append(" = "); //$NON-NLS-1$ + sb.append(MyBatis3FormattingUtilities + .getParameterClause(introspectedColumn)); + answer.add(new TextElement(sb.toString())); + } + + return answer; + } } diff --git a/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.xml b/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.xml index 7a4c5c5..3fe015b 100644 --- a/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.xml +++ b/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableOneUniqueKeyMapper.xml @@ -5,7 +5,7 @@ @@ -18,7 +18,7 @@ @@ -52,7 +52,7 @@ @@ -86,7 +86,7 @@ org_id, code, id, gmt_create, gmt_modified, cid @@ -94,7 +94,7 @@ select @@ -108,12 +108,34 @@ order by ${orderByClause} + + + limit ${start}, ${limit} + + + limit ${limit} + + + + + + select + + + from table_one_unique_key + where org_id = #{orgId,jdbcType=BIGINT} + and code = #{code,jdbcType=VARCHAR} + + + update table_one_unique_key @@ -331,20 +377,55 @@ delete from table_one_unique_key where org_id = #{orgId,jdbcType=BIGINT} and code = #{code,jdbcType=VARCHAR} + insert into table_one_unique_key - (org_id, code, id, gmt_create, gmt_modified, cid) + (id, gmt_create, gmt_modified, org_id, cid, code) values - (#{item.orgId,jdbcType=BIGINT}, #{item.code,jdbcType=VARCHAR}, #{item.id,jdbcType=BIGINT}, - #{item.gmtCreate,jdbcType=TIMESTAMP}, #{item.gmtModified,jdbcType=TIMESTAMP}, #{item.cid,jdbcType=VARCHAR} - ) + + + #{item.id,jdbcType=BIGINT}, + #{item.gmtCreate,jdbcType=TIMESTAMP}, + #{item.gmtModified,jdbcType=TIMESTAMP}, + #{item.orgId,jdbcType=BIGINT}, + #{item.cid,jdbcType=VARCHAR}, + #{item.code,jdbcType=VARCHAR}, + + + + + insert into table_one_unique_key + (id, gmt_create, gmt_modified, org_id, cid, code) + values + + #{id,jdbcType=BIGINT}, + #{gmtCreate,jdbcType=TIMESTAMP}, + #{gmtModified,jdbcType=TIMESTAMP}, + #{orgId,jdbcType=BIGINT}, + #{cid,jdbcType=VARCHAR}, + #{code,jdbcType=VARCHAR}, + + on duplicate key update + id = #{id,jdbcType=BIGINT}, + gmt_create = #{gmtCreate,jdbcType=TIMESTAMP}, + gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}, + cid = #{cid,jdbcType=VARCHAR} + \ No newline at end of file diff --git a/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.xml b/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.xml index e09db5c..0a78c9c 100644 --- a/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.xml +++ b/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableTwoUniqueKeyMapper.xml @@ -5,7 +5,7 @@ @@ -18,7 +18,7 @@ @@ -52,7 +52,7 @@ @@ -86,7 +86,7 @@ org_id, code, id, gmt_create, gmt_modified, cid @@ -94,7 +94,7 @@ select @@ -108,12 +108,34 @@ order by ${orderByClause} + + + limit ${start}, ${limit} + + + limit ${limit} + + + + + + select + + + from table_two_unique_key + where org_id = #{orgId,jdbcType=BIGINT} + and cid = #{cid,jdbcType=VARCHAR} + + + update table_two_unique_key @@ -331,20 +377,55 @@ delete from table_two_unique_key where org_id = #{orgId,jdbcType=BIGINT} and cid = #{cid,jdbcType=VARCHAR} + insert into table_two_unique_key - (org_id, code, id, gmt_create, gmt_modified, cid) + (id, gmt_create, gmt_modified, org_id, cid, code) values - (#{item.orgId,jdbcType=BIGINT}, #{item.code,jdbcType=VARCHAR}, #{item.id,jdbcType=BIGINT}, - #{item.gmtCreate,jdbcType=TIMESTAMP}, #{item.gmtModified,jdbcType=TIMESTAMP}, #{item.cid,jdbcType=VARCHAR} - ) + + + #{item.id,jdbcType=BIGINT}, + #{item.gmtCreate,jdbcType=TIMESTAMP}, + #{item.gmtModified,jdbcType=TIMESTAMP}, + #{item.orgId,jdbcType=BIGINT}, + #{item.cid,jdbcType=VARCHAR}, + #{item.code,jdbcType=VARCHAR}, + + + + + insert into table_two_unique_key + (id, gmt_create, gmt_modified, org_id, cid, code) + values + + #{id,jdbcType=BIGINT}, + #{gmtCreate,jdbcType=TIMESTAMP}, + #{gmtModified,jdbcType=TIMESTAMP}, + #{orgId,jdbcType=BIGINT}, + #{cid,jdbcType=VARCHAR}, + #{code,jdbcType=VARCHAR}, + + on duplicate key update + code = #{code,jdbcType=VARCHAR}, + id = #{id,jdbcType=BIGINT}, + gmt_create = #{gmtCreate,jdbcType=TIMESTAMP}, + gmt_modified = #{gmtModified,jdbcType=TIMESTAMP} + \ No newline at end of file diff --git a/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableWithIdentifyMapper.xml b/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableWithIdentifyMapper.xml index 6a97e0e..6cad08e 100644 --- a/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableWithIdentifyMapper.xml +++ b/generator-plugin/src/main/resources/com/dingtalk/demo/dao/TableWithIdentifyMapper.xml @@ -5,7 +5,7 @@ @@ -18,7 +18,7 @@ @@ -52,7 +52,7 @@ @@ -86,7 +86,7 @@ id, gmt_create, gmt_modified, org_id, cid, code @@ -94,7 +94,7 @@ select @@ -108,12 +108,34 @@ order by ${orderByClause} + + + limit ${start}, ${limit} + + + limit ${limit} + + + + + select @@ -306,7 +333,26 @@ where org_id = #{orgId,jdbcType=BIGINT} and code = #{code,jdbcType=VARCHAR} + + update table_with_identify @@ -329,20 +375,69 @@ delete from table_with_identify where org_id = #{orgId,jdbcType=BIGINT} and code = #{code,jdbcType=VARCHAR} + insert into table_with_identify (id, gmt_create, gmt_modified, org_id, cid, code) values - (#{item.id,jdbcType=BIGINT}, #{item.gmtCreate,jdbcType=TIMESTAMP}, #{item.gmtModified,jdbcType=TIMESTAMP}, - #{item.orgId,jdbcType=BIGINT}, #{item.cid,jdbcType=VARCHAR}, #{item.code,jdbcType=VARCHAR} - ) + + + #{item.id,jdbcType=BIGINT}, + #{item.gmtCreate,jdbcType=TIMESTAMP}, + #{item.gmtModified,jdbcType=TIMESTAMP}, + #{item.orgId,jdbcType=BIGINT}, + + + #{item.cid,jdbcType=VARCHAR}, + + + '' + + + #{item.code,jdbcType=VARCHAR}, + + + + + insert into table_with_identify + (id, gmt_create, gmt_modified, org_id, cid, code) + values + + #{id,jdbcType=BIGINT}, + #{gmtCreate,jdbcType=TIMESTAMP}, + #{gmtModified,jdbcType=TIMESTAMP}, + #{orgId,jdbcType=BIGINT}, + + + #{cid,jdbcType=VARCHAR}, + + + '' + + + #{code,jdbcType=VARCHAR}, + + on duplicate key update + id = #{id,jdbcType=BIGINT}, + gmt_create = #{gmtCreate,jdbcType=TIMESTAMP}, + gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}, + cid = #{cid,jdbcType=VARCHAR} + \ No newline at end of file diff --git a/generator-plugin/src/main/resources/generatorConfigMyBatis3.xml b/generator-plugin/src/main/resources/generatorConfigMyBatis3.xml index 75f5e28..6f152b0 100644 --- a/generator-plugin/src/main/resources/generatorConfigMyBatis3.xml +++ b/generator-plugin/src/main/resources/generatorConfigMyBatis3.xml @@ -41,9 +41,17 @@ - + + + + + + + + + diff --git a/generator-plugin/src/main/resources/mybatis-config.xml b/generator-plugin/src/main/resources/mybatis-config.xml index 5d782f2..ef30256 100644 --- a/generator-plugin/src/main/resources/mybatis-config.xml +++ b/generator-plugin/src/main/resources/mybatis-config.xml @@ -5,7 +5,7 @@ This file is generated by MyBatis Generator. This file is the shell of a Mapper Config file - in many cases you will need to add to this file before it is usable by MyBatis. - This file was generated on Thu Oct 15 19:45:26 CST 2020. + This file was generated on Fri Oct 16 14:39:22 CST 2020. --> diff --git a/generator-plugin/target/classes/generatorConfigMyBatis3.xml b/generator-plugin/target/classes/generatorConfigMyBatis3.xml index 75f5e28..6f152b0 100644 --- a/generator-plugin/target/classes/generatorConfigMyBatis3.xml +++ b/generator-plugin/target/classes/generatorConfigMyBatis3.xml @@ -41,9 +41,17 @@ - + + + + + + + + + diff --git a/generator-plugin/target/classes/mybatis-config.xml b/generator-plugin/target/classes/mybatis-config.xml index 5d782f2..ef30256 100644 --- a/generator-plugin/target/classes/mybatis-config.xml +++ b/generator-plugin/target/classes/mybatis-config.xml @@ -5,7 +5,7 @@ This file is generated by MyBatis Generator. This file is the shell of a Mapper Config file - in many cases you will need to add to this file before it is usable by MyBatis. - This file was generated on Thu Oct 15 19:45:26 CST 2020. + This file was generated on Fri Oct 16 14:39:22 CST 2020. --> diff --git a/generator-test/generator-test.iml b/generator-test/generator-test.iml index 8223b55..17f5bd1 100644 --- a/generator-test/generator-test.iml +++ b/generator-test/generator-test.iml @@ -26,11 +26,12 @@ - - - - - + + + + + + @@ -46,6 +47,8 @@ + + diff --git a/generator-test/pom.xml b/generator-test/pom.xml index d7be5b4..eb9ce5f 100644 --- a/generator-test/pom.xml +++ b/generator-test/pom.xml @@ -5,7 +5,7 @@ mybatis-generator-tddl com.dingtalk.mybatis - 1.0.0-SNAPSHOT + 1.0.1-SNAPSHOT 4.0.0 @@ -14,7 +14,7 @@ org.dingtalk generator-plugin - 1.0-SNAPSHOT + 1.0.1-SNAPSHOT @@ -52,8 +52,25 @@ spring-test 4.3.6.RELEASE + + org.dingtalk + generator-plugin + 1.0.1-SNAPSHOT + test + + + + org.apache.commons + commons-lang3 + + + + org.slf4j + slf4j-api + +
\ No newline at end of file diff --git a/generator-test/src/main/java/com/dingtalk/common/log/MybatisInterceptor.java b/generator-test/src/main/java/com/dingtalk/common/log/MybatisInterceptor.java new file mode 100644 index 0000000..cec6b0b --- /dev/null +++ b/generator-test/src/main/java/com/dingtalk/common/log/MybatisInterceptor.java @@ -0,0 +1,136 @@ +package com.dingtalk.common.log; + +import java.text.DateFormat; +import java.util.Date; +import java.util.List; +import java.util.Locale; +import java.util.Properties; + +import org.apache.commons.lang3.StringUtils; +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.ParameterMapping; +import org.apache.ibatis.plugin.Interceptor; +import org.apache.ibatis.plugin.Intercepts; +import org.apache.ibatis.plugin.Invocation; +import org.apache.ibatis.plugin.Plugin; +import org.apache.ibatis.plugin.Signature; +import org.apache.ibatis.reflection.MetaObject; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; +import org.apache.ibatis.type.TypeHandlerRegistry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @Description: mybatis拦截sql,并计算sql执行的具体时间 + * + * + */ +@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }), @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) }) +public class MybatisInterceptor implements Interceptor { + + /** + * LOGGER + */ + protected static final Logger LOGGER = LoggerFactory.getLogger(MybatisInterceptor.class); + + @Override + public Object intercept(Invocation invocation) throws Throwable { + MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; + Object parameter = null; + if (invocation.getArgs().length > 1) { + parameter = invocation.getArgs()[1]; + } + String sqlId = mappedStatement.getId(); + BoundSql boundSql = mappedStatement.getBoundSql(parameter); + Configuration configuration = mappedStatement.getConfiguration(); + Object returnValue = null; + long start = System.currentTimeMillis(); + try { + returnValue = invocation.proceed(); + } catch (Throwable e) { + throw e; + }finally{ + long end = System.currentTimeMillis(); + long time = (end - start); + String sql = getSql(configuration, boundSql, sqlId, time, returnValue); + LOGGER.info("【SQL】" + sql); + System.out.println("【SQL】" + sql); + } + return returnValue; + } + + public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId, long time, Object returnValue) { + String sql = showSql(configuration, boundSql); + StringBuilder str = new StringBuilder(100); + str.append(sqlId); + str.append(":"); + str.append(sql); + str.append(":"); + str.append(time); + str.append("ms"); + // str.append("ms,Return:"); + // String strObj = String.valueOf(returnValue); + // if (strObj.length() > 10240) { + // strObj = strObj.substring(0, 10240); + // } + // str.append(strObj); + return str.toString(); + } + + private static String getParameterValue(Object obj) { + String value = null; + if (obj instanceof String) { + value = "'" + obj.toString() + "'"; + } else if (obj instanceof Date) { + DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA); + value = "'" + formatter.format((Date) obj) + "'"; + } else { + if (obj != null) { + value = obj.toString(); + } else { + value = ""; + } + + } + return value; + } + + public static String showSql(Configuration configuration, BoundSql boundSql) { + Object parameterObject = boundSql.getParameterObject(); + List parameterMappings = boundSql.getParameterMappings(); + String sql = boundSql.getSql().replaceAll("[\\s]+", " "); + if (parameterMappings.size() > 0 && parameterObject != null) { + TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); + if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { + sql = StringUtils.replaceOnce(sql, "?", getParameterValue(parameterObject)); + } else { + MetaObject metaObject = configuration.newMetaObject(parameterObject); + for (ParameterMapping parameterMapping : parameterMappings) { + String propertyName = parameterMapping.getProperty(); + if (metaObject.hasGetter(propertyName)) { + Object obj = metaObject.getValue(propertyName); + sql = StringUtils.replaceOnce(sql, "?", getParameterValue(obj)); + } else if (boundSql.hasAdditionalParameter(propertyName)) { + Object obj = boundSql.getAdditionalParameter(propertyName); + sql = StringUtils.replaceOnce(sql, "?", getParameterValue(obj)); + } + } + } + } + return sql; + } + @Override + public Object plugin(Object target) { + return Plugin.wrap(target, this); + } + + @Override + public void setProperties(Properties properties) { + // TODO Auto-generated method stub + } +} diff --git a/generator-test/src/test/java/com/dingtalk/demo/dao/TableOneUniqueKeyMapperTest.java b/generator-test/src/test/java/com/dingtalk/demo/dao/TableOneUniqueKeyMapperTest.java index 63a5115..6a9fde7 100644 --- a/generator-test/src/test/java/com/dingtalk/demo/dao/TableOneUniqueKeyMapperTest.java +++ b/generator-test/src/test/java/com/dingtalk/demo/dao/TableOneUniqueKeyMapperTest.java @@ -2,12 +2,17 @@ import com.dingtalk.demo.MapperBaseTest; import com.dingtalk.demo.domain.TableOneUniqueKey; +import com.dingtalk.demo.domain.TableOneUniqueKeyCriteria; +import com.dingtalk.demo.domain.TableWithIdentify; +import com.dingtalk.demo.domain.TableWithIdentifyCriteria; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.Date; +import java.util.List; /** * DDL: @@ -31,6 +36,26 @@ public class TableOneUniqueKeyMapperTest extends MapperBaseTest { static TableOneUniqueKey tableOneUniqueKey = TableOneUniqueKey.builder().id(1L).cid("cid").code("code").gmtCreate(new Date()).gmtModified(new Date()).orgId(1L).build(); + + List batchs = new ArrayList<>(); + + int batchNum = 10; + @Before + public void init() { + for (int i = 5; i < 5 + batchNum; i++) { + batchs.add(TableOneUniqueKey.builder() + .id(Long.valueOf(i)) + .cid("cid" + i) + .code("code" + i) + .gmtCreate(new Date()) + .gmtModified(new Date()) + .orgId(1L + i) + .build() + ); + } + } + + @Test public void testInsert(){ @@ -70,4 +95,49 @@ public void testDeleteByUnikeyKey(){ tableOneUniqueKey.getCode()); Assert.assertNull(mapping); } + + + @Test + public void testBatchInsert() { + tableOneUniqueKeyMapper.deleteByExample(new TableOneUniqueKeyCriteria()); + int n = tableOneUniqueKeyMapper.batchInsert(batchs); + Assert.assertTrue(batchNum == n); + } + + @Test + public void testLimit(){ + int limit = 2; + TableOneUniqueKeyCriteria crt = new TableOneUniqueKeyCriteria(); + crt.setStart(0); + crt.setLimit(limit); + crt.createCriteria().andIdGreaterThan(2L); + List list = tableOneUniqueKeyMapper.selectByExample(crt); + Assert.assertTrue(limit == list.size()); + } + + @Test + public void testUpSertNotExist(){ + tableOneUniqueKeyMapper.deleteByUniqueKey(tableOneUniqueKey.getOrgId(), tableOneUniqueKey.getCode()); + tableOneUniqueKeyMapper.upsertByUniqueKey(tableOneUniqueKey); + TableOneUniqueKey res = tableOneUniqueKeyMapper.selectByUniqueKey(tableOneUniqueKey.getOrgId(), + tableOneUniqueKey.getCode()); + Assert.assertTrue(tableOneUniqueKey.getCid().equals(res.getCid())); + + } + @Test + public void testUpSertExist(){ + + String upVal = "UpCid"; + tableOneUniqueKeyMapper.deleteByUniqueKey(tableOneUniqueKey.getOrgId(), tableOneUniqueKey.getCode()); + tableOneUniqueKeyMapper.upsertByUniqueKey(tableOneUniqueKey); + TableOneUniqueKey resInsert = tableOneUniqueKeyMapper.selectByUniqueKey(tableOneUniqueKey.getOrgId(), + tableOneUniqueKey.getCode()); + Assert.assertTrue(tableOneUniqueKey.getCid().equals(resInsert.getCid())); + tableOneUniqueKey.setCid(upVal); + tableOneUniqueKeyMapper.upsertByUniqueKey(tableOneUniqueKey); + TableOneUniqueKey resUpdate = tableOneUniqueKeyMapper.selectByUniqueKey(tableOneUniqueKey.getOrgId(), + tableOneUniqueKey.getCode()); + Assert.assertTrue(upVal.equals(resUpdate.getCid())); + + } } diff --git a/generator-test/src/test/java/com/dingtalk/demo/dao/TableTwoUniqueKeyMapperTest.java b/generator-test/src/test/java/com/dingtalk/demo/dao/TableTwoUniqueKeyMapperTest.java index d9cdf84..219f6e2 100644 --- a/generator-test/src/test/java/com/dingtalk/demo/dao/TableTwoUniqueKeyMapperTest.java +++ b/generator-test/src/test/java/com/dingtalk/demo/dao/TableTwoUniqueKeyMapperTest.java @@ -1,7 +1,6 @@ package com.dingtalk.demo.dao; import com.dingtalk.demo.MapperBaseTest; -import com.dingtalk.demo.domain.TableOneUniqueKey; import com.dingtalk.demo.domain.TableTwoUniqueKey; import org.junit.Assert; import org.junit.Test; diff --git a/generator-test/src/test/java/com/dingtalk/demo/dao/TableWithIdentifyMapperTest.java b/generator-test/src/test/java/com/dingtalk/demo/dao/TableWithIdentifyMapperTest.java index 144b77d..a7fa63b 100644 --- a/generator-test/src/test/java/com/dingtalk/demo/dao/TableWithIdentifyMapperTest.java +++ b/generator-test/src/test/java/com/dingtalk/demo/dao/TableWithIdentifyMapperTest.java @@ -1,11 +1,11 @@ package com.dingtalk.demo.dao; import com.dingtalk.demo.MapperBaseTest; -import com.dingtalk.demo.domain.TableOneUniqueKey; import com.dingtalk.demo.domain.TableWithIdentify; +import com.dingtalk.demo.domain.TableWithIdentifyCriteria; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.Resource; import java.util.ArrayList; @@ -29,12 +29,13 @@ public class TableWithIdentifyMapperTest extends MapperBaseTest { @Resource TableWithIdentifyMapper tableWithIdentifyMapper; - List columns = new ArrayList<>(); + List batchs = new ArrayList<>(); + int batchNum = 10; @Before public void init() { - for (int i = 0; i < 10; i++) { - columns.add(TableWithIdentify.builder() + for (int i = 0; i < batchNum; i++) { + batchs.add(TableWithIdentify.builder() .cid("cid" + i) .code("code" + i).gmtCreate(new Date()).gmtModified(new Date()).orgId(1L + i) .build() @@ -44,7 +45,8 @@ public void init() { @Test public void testBatchInsert() { - int n = tableWithIdentifyMapper.batchInsert(columns); - System.out.println(n); + tableWithIdentifyMapper.deleteByExample(new TableWithIdentifyCriteria()); + int n = tableWithIdentifyMapper.batchInsert(batchs); + Assert.assertTrue(batchNum == n); } } diff --git a/generator-test/src/test/resources/spring/spring-dao-test.xml b/generator-test/src/test/resources/spring/spring-dao-test.xml index 4559ec5..1b1cf16 100644 --- a/generator-test/src/test/resources/spring/spring-dao-test.xml +++ b/generator-test/src/test/resources/spring/spring-dao-test.xml @@ -46,11 +46,11 @@ + - + + @@ -58,20 +58,28 @@ + + + + + + + - + + \ No newline at end of file diff --git a/generator-test/src/test/resources/test-mybatis-config.xml b/generator-test/src/test/resources/test-mybatis-config.xml new file mode 100644 index 0000000..b52aa4c --- /dev/null +++ b/generator-test/src/test/resources/test-mybatis-config.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/generator-test/target/classes/com/dingtalk/common/log/MybatisInterceptor.class b/generator-test/target/classes/com/dingtalk/common/log/MybatisInterceptor.class new file mode 100644 index 0000000000000000000000000000000000000000..7628705201f7f40210ac573fbe76cfa02a8efa96 GIT binary patch literal 6489 zcmb_g33yc175;B#c{4AM5QY#OMTLlv4MqV0Cx{>tNSXvBfD3Lflb7V-WM-I!#ctYG z?P{gAYHD3-X|cOv9fGuqU1)1erAAf-R4;#2svg3rhepOtG51n{8z+!w$@3O*OW!}7%Y733A{ z4`LJtRGf-O z(UTkQGtwLNzBp&NIi|-q>uFQIJCok*keLx^?TRIbBmHJ#FssL}iST7OnTW)bgOToC zeR|f+bR@DyI%cG@NqJ<@EW8Sw+h`f3r}Ws65iu?4NGhHiG!v1I#CD#UHIs?9rmn5} zc0Cf;6N8a;eOrxKmU=u!qF+FC((0@^OdCC!ZE#3B{-<#F5#;}pd3beTFltMHh(p@erK}hLoeb^u_0v!`C@*C9vCVfR)Tpa)$+y{{IEZ)qgFOPNs}> zc2^HGPoTD#f-*gsrm~VHMU+=#Apk{O9BDeEGJ$m)68AJq8v}6`tIV|Sq9`{`&F)GW zYxP8b+(>UQ22BQLm;32bFtV{yno1@Y?Jt-xr8wi*$TY0ABazFR@kkdB;>P-NE~9dX z&z6sqUG~4gvAj*evkHEz;5h}qBTcgeY$g-l*GL_uSD^Sjci)WoIZPTyV?q_x(z~OK$(GAA zSF}*|OigHF601ZLHDWR^4NcUFDVhk0sjM4K)QM@7I$y&zh%2IA6Vt^>nh1-NHF1iV z!J^W{sp2$EoQ@X-nok^n@l2(OnG&$)G%-swXyObpTN7u>(Ht>b5py-sD4MA7wKp+} zXx2oFXe~pqKti)C4J$S@#WqObj0waFeUCsxrBs2B3m2lm+)ANBu9w~KWs#L!=!)Wr z7}xb2SAu6pGnPRe^jT;53MeGDl~$EbYi!z5d8sl}C?R{Ex<=RgtD4JWUN3dMmn)$d zj~A=HmWsw9)50Sw_qxI;6_gatt_(Xsfhnw9*$wJ-MO5;m=ds3pA7)Y}1byLb)!;{Q00i34TOh(qT zRnAhKp%2qTn@Gy1EmtTMPf&SdhrD`bG76On7WP=*BOH8b2@fq& zMxl!9N+-vx6ieoHr3dlM=>`u|`thyi(;(J-OAfD@{pg%pyD0 z7of2sTVO59C`6}+eY_PIqTo$fFOrO$K8*cFkV0z=yZ3i3IBt}2aQjZ-;&71~>Zx~S zf4?caEL}ll=yK&HEu9 zIwB%9cS#SuRZqXyhk9zk&l>_gB zp4L3PQE%(KJbY0fU;I(Oh$^E{mINpfu!m~BS|2c$XkmZY7xva`VG0DJHT$3*MD?Zn zp@k;pQ4_A=Bag`kP+Nt4nBumUM`$mujH+SvvFK!ZN^O0uR2_0uhiuh#<5ky9pxRlZ z>hR>p!pZ~u1#Ag1-HoZ%=WT>wH|~J^^jPo>@MkHY1u9wz-#mgepZK4J2Aqv1EFkI& z`CP=^i>W6{Fx$|T;uY+NF9Y-2a^wRsOtqM$a1tE7ctl>p2Y_A zGT<7HU?VnBR|9`j+f2<~`f?Z-^CTZVI)qDc853dwe?i+qIfeF*;&R#&z!lVe4!p;} z?(<3oR~@I9K?PSU(CrBYeF|a<`nl+ZP+(lhzgMVcfLl33sDrY}=SZgQlQbj1&-Uh) zQPf5KG81b1_hVXA2`fypnv26qsNVhz`^(>Fj;H9wB<2pS4melqQC&f^q*gyfipckRV$O3fHS#OK?G(?)T6 zcd2-5^C8U0V`ll5Q1f9_OHJJF56$Xok^b9>JD8V6?D^g4oVTHdM-dEAcI4`KKqH2DXvKfMW6GnXP!KX;uaAhY2B&K4NK<#Mz@FijUm1K~g(i=shNLNJeWMsTVe z9B-!%KTo@vdUnwY!dlGFgHC?>kmm`T7#|f6;`Ow_Lp%3aS?T4uHsjlCZ{nIP-i7cT zr}2KPLRqI*DR{G0v)2K~P@~{_1#dZq6$;+U>6dZZOH?oLHX3giY?SXZIs7fnqd0f0 z;OvX$ddt~*TcXH8e%wei<^6OM-tOo*hdQL5io5Ej5*4DTLf{>EC$|RhuHuz9i|_JZ zFccj{+e1+0wI@ohy}3N$-JGk!dk7?`OyIrzOKm~b3fw|ScE|8O+?v7r@d3`o&>O?1 R@|D}97xE$(BwOlav$! z86+L)NV-eupnD`i*r7?fN4nCcdyjPQ@mHTdwC{I!I?0wyaUc9V{@v-fzV~~-_xpW6 z>Dh}{;LSnQVpt};MVhw;@ix3&`raYUX=&b>-OLlj z8N5rTy<5P%N8a8m+nx>LeR#AK@0SlB2;hSOd`Q6@Q|pWAU2%P9+!#*lx9B4s+R%`m zR1j{DBnHD#Bi?Unv4i2LK9~qcwM4k5_<}|Sb9bcUrZK4Z8YzRDZi>efre+$6cuGO2 z{eX5zI~>;B&hQRBl}INeI#Y&5R7t(hSQ|IBgoSaJ!K* zIabzf^v5+bozxXZ;IXgA_I z=k)1Gi!TM0?TLsM>(!Em@aJ^;%mIUtR~ExCIn_P7<&{qi6NI5)TDPf14l;5%W6A#T zfix$CoAv#Pq>vmmxvnjq(vz|(E=(odSDsP#>XaNK4x4Ym2%Oh-vZuaC;)&FQhr^mpdD5rilp~REX(j31Svwne?<9gW{z> zWI{^H(H?U8{pq1zEhga=eTOZdyUir2qP;RMa-HG3O&i&0io_h%1iP&qXf-G@uFU?P?C^d3K*bO7yn+=)j-vk&ZWTYmk5&8xKMmk#!o$y1`~tsJ@q#qJ!fyij zt%~2__bUE?KdSf>{;c9J0^qNL^ly~h&Y>1zI&CHC-Ib7*_VyqRRzj%L#wvM<9f0srlnFM;jJ~bQ%IhLlkH1&+iGyLoNx|l zv9#W`p8~DfUYnN?&LPlp`MynRSAoai5p!G3#O<$?S0z&Z;xy5aZ^B9Ge5dR#hi1}o zbxqr)hD*(ay+T11LohKDxxucY2Co2TauW3qnk`A4Nt@%&IzJ?j8?O|1<+Rd~h#LD1 zodo(>U8EfNEtM)*Iwj>1v{#FA`RsgJy7~^VG&R=l<{{dzn_aer%5j7`Y721OF2)T- zIGj9pc^ir5s*`;uQm`l=g=L{Igtfd5R2Wu)wM(dRh;)Z*rjT!qvd7F+VFAANa_mpV z5X*F~`fuXdAa!+`91>R9>rBU3)rxY@TaG`an-bbt1*qf>G|#>YlQIQSDmc!cMkkrXFQ=!zY|3YoKb$QFylS8T z2&S=!&?hM#*;Vdx>OqS~+oD@7fXSW#jW@)ue3vZMPvxj6utA6wS#7()Z2kX&X{TQ< za@0=BI6`so9xgeQ9ug(zX^@hW6G5uSj9i4XXL1Q+WQ~oJeD>Jy#dZ8T*TnB?KK}D& z0|htnt%T2;`7D)-4xf-q82h&H&4X69ypORhXCH5FY;WPGZSe99sIEH?rS1YeyUxRV z4!*h>!E^9`oFg8zvz^X)z%{g4g9_B4gP&A;B|6c?PYQ13t{!XG7l_8gzI16*sI=}0 z`1hT`K|Xz>C<&b$MIeLH3n-(lU}bp*YAAzg8BEWhBHKG7zjx*ZRC4aD4659>*}>3B z&DyCtgE?ck%7fcZD9YLYJS?ES7U&g(2sE%=irEOWT}Dcl6YvVW1S_$Z-^X>_hyjko zInTse%du*L_aKQElO7=_?YOvu?`81(8y@9f7@X2~e7ooIL~4R532tu)q#dE6>zIOH1klecD?8gF=)Yxo%&7Fl*x;W`GV zy{P?;`Xw3MAVc+ISV$6z&gxv!kinu17LVdZIj~EqUl2}!nyaGX=I}Y6fw~?$7{G3< zXB;;0*+Pao(Q7$TXG3zF2++;lcqyHjZ^@vU+_q36x6?821jp`h9Xstfwsum-7}^cR z9cdUvP0oYGln`h?K#NtlgC5*YBzu@sd+EU*%5oppK+8kE)~tHN?C%U4<| zp3nN!;K0ip!Hcve?4sjgN$k%K_vL=*bwDB%yTPAwo*I~%{*>f9rPX^4EwB{ zlHcbv^l|-4%tD0yUPQU0oOX6kD|^Jp>F`{{CM5tpfc*uY_Pd@w;zVG3p*$#>top~W zda_h#w~miB%$hYBtR0sR*HA~mVJ7VePm3cw;O->KBUs08%xyTD7pLuxr|TG}fg(w4 zxOgu*kvU$uu1Er39^?)iG9&0f=4awx?@(|FXRW+1@N+%c296WiJ(!CVSd5eO^Iojx zvkCVx^7k|AA7BhmG4>BK=BIE758)Vpa+HJ2J_C+B9C&dz4&Wdo+QiBjBO4yhR=ING z8Bj_UWozXQT9?1otYSgCh)MOTsARD!UZULk^f>0p37}XCm0x~=MJzlE`Ig|jhj?YY Ul5Zm{Hy`5}afZL7{u*Wf1u0;`rT_o{ literal 3027 zcmb_e`&Sc37`+n+8{#U8h!1L|6>WH~_$n2&l>mwcP+nI0uuC$=#mz1zyHxtJ@BSP8 z7y396+jH9A`$zTk&h8Q-NzSR~ketlS&V2LT`+eV?$^P^2AAbS3h@X<^##k#(;i_t` zCGjraQ_b~#7)M$)6RMd^VhT6n+@n6pByM6_ncY&0x78@4=4O=HtZL@e+?^!uVm^Td z)h{Nnr21tI_mVh{G4=4iYVNCMMKvF2_)x<~0!KY_)s}OPTym@@zPv3rXUz3=Srjlb zS+`*1EGO@o_G2R_3$BqfU1PEShEajz^SxoxFRaR9 zz+0d_<7Q2J*(_Qrj%c#kdt{O1g?f;ULtd06kH|@n{j^h(MNdFD{7nlS%(%t8vF4ML z5!oG4b_>#$S#Nx^l5PR5z>G7^0)fTSdd~D@VDVUPvLFHpKbV`&DZeU@hHW}|BjY-G z;wPg0!;ymtLhN=hmbIhwBo~6DPgyq8)7_w$it&&F((x8jI^MvWI?mv%hL3eTz$ZFP ztZK;W$U*9Oh`f$Rur#dccnn)ZL5BlZ$2y+qD5~X>+U3F5v4Kx@JcX@e6JKcfQpZ<# z#=2##oQ?&2&EjQQJRM7Fd?1L|_bl6(U|w{5gKu?wr<(8aOvexSQDFF$q^hE0?6rGB z>5rQw8JhIfn&cWNkY&hAZ?2eEMQTs=)|N@*dML_{mzEd2B8$&D4%R_yEIuBZ+)kmx zqbqPGwHvtVIYR2CJlm{5ub~>P4NAdC)f0b%Xh)TgL#0|R-}yiIuL4BH?;ND-rSOpL zH-o{f>&^PMP1#qY&aJNTgpI08Akz-KtZgcu9JyfQPFc2FFaf zI}skks>55Azc|aQwzkM8zG?F^sY_~w*F{KvvFJ1F&8ZdTIhqTj(N=xS&Z#T-Sk3Vq z-(J$E9Y+?^wsJc z9`*@!bw|&<&NcjyOJV(s! zK!yVb=PL{zL=3uW86=vJ>EA-@Zr~vT8w4IA*g2fQa20S@1vriiq_-RRVg>kb0ymS) zTnCa{&@*-MX9oJq*sqobUf=+eP}gdkBDGL1=tKyu@9C`_2d9#AvP4*IAN(Iin+$z%XB%@fLyh dlYs`i$>0=6S2!PIu0Lm=q{UsnXn%&*{{Z{)`egtB diff --git a/generator-test/target/test-classes/com/dingtalk/demo/dao/TableTwoUniqueKeyMapperTest.class b/generator-test/target/test-classes/com/dingtalk/demo/dao/TableTwoUniqueKeyMapperTest.class index 2cc31ddfd2c2066b844a309a32a486a70d391336..7ff5e98a0a0f503b07dda8dfb3e1f7b788970355 100644 GIT binary patch delta 76 zcmZ1^xJYn=8#|-qWOsH~Ruu+b2Gz+E+2aJYfh-*c0S0XbF$P@*DF(es9L@3y45ke7 c3``7K3=BY0n;{HHb1^V7STPtfgfegf0P7M7_5c6? delta 76 zcmZ1^xJYn=8#|-aWOsH~R#gUG2DQl(+2aItfGk}G0R|lgF$O&bDF*#X9L@5I45kbU c3``7K3=BY0n;{HHb1^V7STPtfgfegf0PN%n00000 diff --git a/generator-test/target/test-classes/com/dingtalk/demo/dao/TableWithIdentifyMapperTest.class b/generator-test/target/test-classes/com/dingtalk/demo/dao/TableWithIdentifyMapperTest.class index 46004d099cbe30958fb9b54d2aae116f3eefbc35..8a986423fe4b40fa9173cc7b74af86f1bbc46f1b 100644 GIT binary patch delta 996 zcmYL|SyK~H5QV=x6Ea~K77-DRisA-A4T6dWR6tNs+;KtNh9pAR4T*wM#Im&X&9d@n zm0G^=;)|t5kSc$GKg1uQmfCj^dC2MRGxv6%(=&Nj`lu}P?e~|jz#gV;Dv4M$*%(Cf z*+8>~gEogaY|&z45ed+$W1EH}8rp3-I2z!Xj>j!JZAysf;)I5iHeH<3*=Y@D3OLI- z-S)i21)DXLM+)fXqJ~R0m$_omV{!Gx!)JbfpPPyfWQ$d_FyN)9(<2JinH^V8vp?00 z8KO2jZB&$OVXH$mH5R=N*SMK|ZLEu#(L~JQ7OsXqhbS?NxI-(q6~5?LO!8woFB)|3 zy5V#xF&u7pQ^NK$;E-UzV$flTVTTb$9mW{9xZ{vy!eJwu6!qC_{?NuF$wVriOt=;) zhctH`?lEaG<#3+|irp_>?flSVJ~zvXWAWj5D&98LG3kzs562aa)!ANuZNXB9rkc*| zcmMXxSh7Dnm>x}}!mSe%@nlM2yI#_rOv^1__0^h7Y?38b%8N7QCu&esN#>WhSt7_% zB;O{P!FEB}kDw`>tW9u-RL8v<%0r(~p#_Yd&zPSw_(CQ2C-UBVmv;&ld1He3WK^*X z+9jMLP2?(J>LiL$X=`|GzljV(xFNGS82U)wTcQ$tbNCmqWJI7eKZhO6QINyQQMkkw zEwY(Kx_E&SIlLlA>Hln{9em=>QI=y>j`Dd{8(e;@)a;*^TC0iHT17x`E4G;5BGw~f zy;scNkUA#(plHG)tBje1qki#b?g3epzX$>{ITuLUvavI>Iz)WnG>${a|sm mm2zO8C$d3~2eh(U)w2A)8-+F4@8JQliC9DuOIpabKV=rCL`IMG!@+AXO1J91CK#iWeRGM6`k=egHp`G3vP!#rQD)v)pt4=YP)4JX`ywzVz+qm#;tq&t^Rd%|RQBM2K}9 zvN_BV%~6{w64e|tw$nhDf#WvaoY0)KsU=~89s{Rrdg(LXX#@Q=oZ+mY8>rzN=R+j9 zU~`c{&5&l*B}KDWj_Z~e^I5Iah3s@LU*4wDq*^h z#vJZ2rg0tal6DwJ()XCu+;_I97+QllO!e#kf@1&`zO$E9lb@nSRL_ZQ-WYscN z!m8^_)fGaK3U-B>3eGaMk`7l`z2eo`ky&?Wl{Sit%$8mGVI@n zCYhkoOtD0hMb`(KMSjb_-GoIGbPy&^Bl}l+%gsOg35(7SUq!2I-G7z1T(P9s-~q0P NwWWEt{92a@{RU#qj?e%A diff --git a/generator-test/target/test-classes/spring/spring-dao-test.xml b/generator-test/target/test-classes/spring/spring-dao-test.xml index 4559ec5..1b1cf16 100644 --- a/generator-test/target/test-classes/spring/spring-dao-test.xml +++ b/generator-test/target/test-classes/spring/spring-dao-test.xml @@ -46,11 +46,11 @@ + - + + @@ -58,20 +58,28 @@ + + + + + + + - + + \ No newline at end of file diff --git a/generator-test/target/test-classes/test-mybatis-config.xml b/generator-test/target/test-classes/test-mybatis-config.xml new file mode 100644 index 0000000..b52aa4c --- /dev/null +++ b/generator-test/target/test-classes/test-mybatis-config.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 10fb046..0aee5ca 100644 --- a/pom.xml +++ b/pom.xml @@ -64,21 +64,15 @@ - - com.github.rholder - guava-retrying - 2.0.0 - org.apache.commons - commons-text - 1.8 + commons-lang3 + 3.11 - - joda-time - joda-time - 2.9.1 + com.google.code.gson + gson + 2.8.6 org.projectlombok @@ -86,24 +80,6 @@ 1.18.2 - - commons-chain - commons-chain - 1.2 - - - - - com.typesafe - config - 1.3.4 - - - - net.jodah - typetools - 0.6.1 - com.google.guava guava @@ -118,6 +94,12 @@ 8.0.21 + + + org.slf4j + slf4j-api + 1.7.30 + From 7523d29f65c8365d6b049fda524f20e5969370e5 Mon Sep 17 00:00:00 2001 From: "zhenyin.szy" Date: Fri, 16 Oct 2020 14:43:44 +0800 Subject: [PATCH 4/4] add limit upsert forupdate plugin --- .../com/dingtalk/generator/plugins/UpsertByUniqueKeyPlugin.java | 1 - 1 file changed, 1 deletion(-) diff --git a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpsertByUniqueKeyPlugin.java b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpsertByUniqueKeyPlugin.java index bd4f7cc..fb54bd8 100644 --- a/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpsertByUniqueKeyPlugin.java +++ b/generator-plugin/src/main/java/com/dingtalk/generator/plugins/UpsertByUniqueKeyPlugin.java @@ -134,7 +134,6 @@ private void generateXmlElement(Document document, IntrospectedTable introspecte insertEle.addElement(new TextElement("on duplicate key update ")); // set - List nonUniqueKeyColumns = new ArrayList<>(introspectedTable.getAllColumns()); nonUniqueKeyColumns.removeAll(uniqueKey); for (VisitableElement where :XmlElementGeneratorTools.generateSets(nonUniqueKeyColumns,null, false)) {