Skip to content

fit2cloud/mybatis-tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

1. com.fit2cloud.tools.mybatis.SqlCriterionGeneratorPlugin

What's this

A mybatis generator plugin that generates a method to add sql formatted criterion to the MBG generated criteria.

Why to use

Hanlde complex query conditons(e.g. including both 'and' and 'or') in a fluent way.

When to use

Consider some employees need to be filtered by name and other conditions, with the default criteria you may have to do this by tedious code.

Criteria criteria1 = example.or();
criteria1.andCountryEqualTo("USA");
criteria1.andStateEqualTo("California");
criteria1.andCityEqualTo("LA");
criteria1.andMaleEqualTo("Female");

criteria1.andFirstNameLike("%Ava%");

Criteria criteria2 = example.or();
criteria2.andCountryEqualTo("USA");
criteria2.andStateEqualTo("California");
criteria2.andCityEqualTo("LA");
criteria2.andMaleEqualTo("Female");

criteria2.andLastNameLike("%Ava%");

mapper.selectByExamle(example);

/*the query will be 
select * from employee where (country = 'USA' and state = 'California' and city = 'LA' and male = 'Female'
and first_name like '%Ava%') or (country = 'USA' and state = 'California' and city = 'LA' and male = 'Female'
and last_name like '%Ava%')
*/

With this generator plugin a new method called andSqlCriterion will be generated in the Criteria class.

public Criteria andSqlCriterion(String value) {
    addCriterion("(" + value + ")");
    return (Criteria) this;
}

Then above scenario can be implemented like this

Criteria criteria = example.or();
criteria.andCountryEqualTo("USA");
criteria.andStateEqualTo("California");
criteria.andCityEqualTo("LA");
criteria.andMaleEqualTo("Female");

criteria.andSqlCriterion("first_name like '%Ava%' or last_name like '%Ava%'");

mapper.selectByExamle(example);

/*the query will be 
select * from employee where country = 'USA' and state = 'California' and city = 'LA' and male = 'Female'
and ( first_name like '%Ava%' or last_name like '%Ava%')
*/
How to use

Simply mvn package the project and add the plugin in your generatorConfig.xml(maybe some other file you named).

<plugin type="com.fit2cloud.tools.mybatis.SqlCriterionGeneratorPlugin" />
Reference

Supplied Plugins by MBG

2. com.fit2cloud.tools.mybatis.swagger.ImportSwaggerPlugin

What's this

A mybatis generator plugin that adds Swagger Annotations for the fields of Java model based on table.

When to use

You develop a restful API which is described by swagger, the models might be generated by MBG and you want to add swagger annotations.

In such case, if you added the annotation manually then they would be removed when regenerating the models :(, but this plugin will help you :).

Assume you have a table user_key

CREATE TABLE `user_key` (
  `id` VARCHAR(50) NOT NULL DEFAULT '' COMMENT 'user_key ID',
  `user_id` VARCHAR(50) NOT NULL COMMENT '用户ID',
  `access_key` VARCHAR(50) NOT NULL COMMENT 'access_key',
  `secret_key` VARCHAR(50) NOT NULL COMMENT 'secret key',
  `create_time` BIGINT(13) NOT NULL COMMENT '创建时间',
  `status` VARCHAR(10) DEFAULT NULL COMMENT '状态',
  PRIMARY KEY (`id`),
  UNIQUE KEY `IDX_AK` (`access_key`),
  KEY `IDX_USER_ID` (`user_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4

With this generator you can get the model with annotations like this.

import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;

public class UserKey implements Serializable {
    
    @ApiModelProperty("user_key ID")
    private String id;

    @ApiModelProperty(value = "用户ID", required = true)
    private String userId;

    @ApiModelProperty(value = "access_key", required = true)
    private String accessKey;

    @ApiModelProperty(value = "secret key", required = true)
    private String secretKey;

    @ApiModelProperty("创建时间")
    private Long createTime;

    @ApiModelProperty("状态")
    private String status;
    
    // getter and setters..
}

Then you will get a swagger model.

swagger model

How to use

Simply mvn package the project and add the plugin in your generatorConfig.xml(maybe some other file you named).

<!-- this class add import(import io.swagger.annotations.ApiModelProperty;) in the model -->
<plugin type="com.fit2cloud.tools.mybatis.swagger.ImportSwaggerPlugin" />
<!-- this class add annotation(@ApiModelProperty) for the fields -->
<commentGenerator type="com.fit2cloud.tools.mybatis.swagger.ApiModelPropertyAnnotationGenerator">
    <property name="suppressDate" value="true"/>
</commentGenerator>