{"id":928,"date":"2023-03-11T17:25:17","date_gmt":"2023-03-11T09:25:17","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=928"},"modified":"2023-04-29T15:45:57","modified_gmt":"2023-04-29T07:45:57","slug":"mybatis-mybatis-generator-custom-plugin","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/mybatis-mybatis-generator-custom-plugin\/","title":{"rendered":"MyBatis mybatis-generator\u81ea\u5b9a\u4e49plugin"},"content":{"rendered":"<h2>\u5b57\u6bb5\u6dfb\u52a0\u6ce8\u91ca<\/h2>\n<p>generatorConfig.xml\u6dfb\u52a0\u81ea\u5b9a\u4e49plugin\u793a\u4f8b<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-xml\">&lt;!-- \u751f\u6210\u7684\u4ee3\u7801\u53bb\u6389\u6ce8\u91ca --&gt;\n&lt;commentGenerator type=&quot;com.lianpay.globalpay.remit.common.plugin.CommentGenerator&quot;&gt;\n    &lt;property name=&quot;suppressAllComments&quot; value=&quot;true&quot; \/&gt;\n    &lt;property name=&quot;suppressDate&quot; value=&quot;true&quot;\/&gt;\n&lt;\/commentGenerator&gt;<\/code><\/pre>\n<pre><code class=\"language-java\">\/**\n * \u751f\u6210model\u4e2d\uff0c\u5b57\u6bb5\u6dfb\u52a0\u6ce8\u91ca\n *\/\npublic class CommentGenerator extends DefaultCommentGenerator {\n\n    @Override\n    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {\n        super.addFieldComment(field, introspectedTable, introspectedColumn);\n        if (introspectedColumn.getRemarks() != null &amp;&amp; !introspectedColumn.getRemarks().equals(&quot;&quot;)) {\n            field.addJavaDocLine(&quot;\/**&quot;);\n            field.addJavaDocLine(&quot; * &quot; + introspectedColumn.getRemarks());\n            addJavadocTag(field, false);\n            field.addJavaDocLine(&quot; *\/&quot;);\n        }\n    }\n\n}<\/code><\/pre>\n<h2>\u52a0\u5bc6\u914d\u7f6e\u6587\u4ef6<\/h2>\n<pre><code class=\"language-java\">\/**\n * \u652f\u6301\u52a0\u5bc6\u914d\u7f6e\u6587\u4ef6\u63d2\u4ef6\n *\/\npublic class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {\n    private Logger logger = LoggerFactory.getLogger(this.getClass());\n\n    private String[] propertyNames = {\n        &quot;master.password&quot;, &quot;slave.password&quot;, &quot;generator.jdbc.password&quot;, &quot;master.redis.password&quot;\n    };\n\n    \/**\n     * \u89e3\u5bc6\u6307\u5b9apropertyName\u7684\u52a0\u5bc6\u5c5e\u6027\u503c\n     * @param propertyName\n     * @param propertyValue\n     * @return\n     *\/\n    @Override\n    protected String convertProperty(String propertyName, String propertyValue) {\n        logger.info(&quot;Decrypt &quot; + propertyName + &quot;=&quot; + propertyValue + &quot; error!&quot;);\n        for (String p : propertyNames) {\n            if (p.equalsIgnoreCase(propertyName)) {\n                return AESUtil.AESDecode(propertyValue);\n            }\n        }\n        return super.convertProperty(propertyName, propertyValue);\n    }\n}<\/code><\/pre>\n<h2>Example\u7c7b\u548cmodel\u7c7b\u5b9e\u73b0\u5e8f\u5217\u5316\u63d2\u4ef6<\/h2>\n<pre><code class=\"language-java\">\/**\n * Example\u7c7b\u548cmodel\u7c7b\u5b9e\u73b0\u5e8f\u5217\u5316\u63d2\u4ef6\n *\/\npublic class SerializablePlugin extends PluginAdapter {\n    private FullyQualifiedJavaType serializable = new FullyQualifiedJavaType(&quot;java.io.Serializable&quot;);\n    private FullyQualifiedJavaType gwtSerializable = new FullyQualifiedJavaType(&quot;com.google.gwt.user.client.rpc.IsSerializable&quot;);\n    private boolean addGWTInterface;\n    private boolean suppressJavaInterface;\n\n    public SerializablePlugin() {\n    }\n\n    public boolean validate(List&lt;String&gt; warnings) {\n        return true;\n    }\n\n    public void setProperties(Properties properties) {\n        super.setProperties(properties);\n        this.addGWTInterface = Boolean.valueOf(properties.getProperty(&quot;addGWTInterface&quot;)).booleanValue();\n        this.suppressJavaInterface = Boolean.valueOf(properties.getProperty(&quot;suppressJavaInterface&quot;)).booleanValue();\n    }\n\n    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {\n        this.makeSerializable(topLevelClass, introspectedTable);\n        return true;\n    }\n\n    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {\n        this.makeSerializable(topLevelClass, introspectedTable);\n        return true;\n    }\n\n    public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {\n        this.makeSerializable(topLevelClass, introspectedTable);\n        return true;\n    }\n\n    protected void makeSerializable(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {\n        if(this.addGWTInterface) {\n            topLevelClass.addImportedType(this.gwtSerializable);\n            topLevelClass.addSuperInterface(this.gwtSerializable);\n        }\n\n        if(!this.suppressJavaInterface) {\n            topLevelClass.addImportedType(this.serializable);\n            topLevelClass.addSuperInterface(this.serializable);\n            Field field = new Field();\n            field.setFinal(true);\n            field.setInitializationString(&quot;1L&quot;);\n            field.setName(&quot;serialVersionUID&quot;);\n            field.setStatic(true);\n            field.setType(new FullyQualifiedJavaType(&quot;long&quot;));\n            field.setVisibility(JavaVisibility.PRIVATE);\n            this.context.getCommentGenerator().addFieldComment(field, introspectedTable);\n            topLevelClass.addField(field);\n        }\n    }\n\n    \/**\n     * \u6dfb\u52a0\u7ed9Example\u7c7b\u5e8f\u5217\u5316\u7684\u65b9\u6cd5\n     * @param topLevelClass\n     * @param introspectedTable\n     * @return\n     *\/\n    @Override\n    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,IntrospectedTable introspectedTable){\n        makeSerializable(topLevelClass, introspectedTable);\n\n        for (InnerClass innerClass : topLevelClass.getInnerClasses()) {\n            if (&quot;GeneratedCriteria&quot;.equals(innerClass.getType().getShortName())) {\n                innerClass.addSuperInterface(serializable);\n            }\n            if (&quot;Criteria&quot;.equals(innerClass.getType().getShortName())) {\n                innerClass.addSuperInterface(serializable);\n            }\n            if (&quot;Criterion&quot;.equals(innerClass.getType().getShortName())) {\n                innerClass.addSuperInterface(serializable);\n            }\n        }\n\n        return true;\n    }\n}<\/code><\/pre>\n<h2>MySQL\u5206\u9875\u63d2\u4ef6<\/h2>\n<pre><code class=\"language-java\">\/**\n * MySQL\u5206\u9875\u63d2\u4ef6\n *\/\npublic class PaginationPlugin extends PluginAdapter {\n\n    @Override\n    public boolean validate(List&lt;String&gt; list) {\n        return true;\n    }\n\n    \/**\n     * \u4e3a\u6bcf\u4e2aExample\u7c7b\u6dfb\u52a0limit\u548coffset\u5c5e\u6027\u548cset\u3001get\u65b9\u6cd5\n     *\/\n    @Override\n    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {\n\n        PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper();\n\n        Field limit = new Field();\n        limit.setName(&quot;limit&quot;);\n        limit.setVisibility(JavaVisibility.PRIVATE);\n        limit.setType(integerWrapper);\n        topLevelClass.addField(limit);\n\n        Method setLimit = new Method();\n        setLimit.setVisibility(JavaVisibility.PUBLIC);\n        setLimit.setName(&quot;setLimit&quot;);\n        setLimit.addParameter(new Parameter(integerWrapper, &quot;limit&quot;));\n        setLimit.addBodyLine(&quot;this.limit = limit;&quot;);\n        topLevelClass.addMethod(setLimit);\n\n        Method getLimit = new Method();\n        getLimit.setVisibility(JavaVisibility.PUBLIC);\n        getLimit.setReturnType(integerWrapper);\n        getLimit.setName(&quot;getLimit&quot;);\n        getLimit.addBodyLine(&quot;return limit;&quot;);\n        topLevelClass.addMethod(getLimit);\n\n        Field offset = new Field();\n        offset.setName(&quot;offset&quot;);\n        offset.setVisibility(JavaVisibility.PRIVATE);\n        offset.setType(integerWrapper);\n        topLevelClass.addField(offset);\n\n        Method setOffset = new Method();\n        setOffset.setVisibility(JavaVisibility.PUBLIC);\n        setOffset.setName(&quot;setOffset&quot;);\n        setOffset.addParameter(new Parameter(integerWrapper, &quot;offset&quot;));\n        setOffset.addBodyLine(&quot;this.offset = offset;&quot;);\n        topLevelClass.addMethod(setOffset);\n\n        Method getOffset = new Method();\n        getOffset.setVisibility(JavaVisibility.PUBLIC);\n        getOffset.setReturnType(integerWrapper);\n        getOffset.setName(&quot;getOffset&quot;);\n        getOffset.addBodyLine(&quot;return offset;&quot;);\n        topLevelClass.addMethod(getOffset);\n\n        return true;\n    }\n\n    \/**\n     * \u4e3aMapper.xml\u7684selectByExample\u6dfb\u52a0limit,offset\n     *\/\n    @Override\n    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,\n                                                                     IntrospectedTable introspectedTable) {\n\n        XmlElement ifLimitNotNullElement = new XmlElement(&quot;if&quot;);\n        ifLimitNotNullElement.addAttribute(new Attribute(&quot;test&quot;, &quot;limit != null&quot;));\n\n        XmlElement ifOffsetNotNullElement = new XmlElement(&quot;if&quot;);\n        ifOffsetNotNullElement.addAttribute(new Attribute(&quot;test&quot;, &quot;offset != null&quot;));\n        ifOffsetNotNullElement.addElement(new TextElement(&quot;limit ${offset}, ${limit}&quot;));\n        ifLimitNotNullElement.addElement(ifOffsetNotNullElement);\n\n        XmlElement ifOffsetNullElement = new XmlElement(&quot;if&quot;);\n        ifOffsetNullElement.addAttribute(new Attribute(&quot;test&quot;, &quot;offset == null&quot;));\n        ifOffsetNullElement.addElement(new TextElement(&quot;limit ${limit}&quot;));\n        ifLimitNotNullElement.addElement(ifOffsetNullElement);\n\n        element.addElement(ifLimitNotNullElement);\n\n        return true;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b57\u6bb5\u6dfb\u52a0\u6ce8\u91ca generatorConfig.xml\u6dfb\u52a0\u81ea\u5b9a\u4e49plugin\u793a\u4f8b &lt;!&#8211; \u751f\u6210\u7684\u4ee3\u7801\u53bb\u6389 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[111],"class_list":["post-928","post","type-post","status-publish","format-standard","hentry","category-mybatis","tag-mybatis"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=928"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/928\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}