This is a lightweight MyBatis plug-in, published on GitHub has played a good JAR package, 25.2KB, import project can be used, support to create data tables, custom query, paging query and other common database operations. The project makes full use of Java’s reflection mechanism and generics mechanism, which is a rare tool in the process of work and learning.

The characteristics of

  • Small size, simple code, simple to use
  • Create data tables and indexes
  • Supports primary keys and auto-add primary keys
  • Supports field description, non-empty constraints, unique constraints, and set length
  • Supports paging query, custom condition query, and statistical query
  • Supports common add, delete, modify, and query operations

Depend on the package

<dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
  
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
Copy the code

The code structure

  • Mybatis: Mybatis general Mapper related
    • Annotation: Annotation is relevant
      • Fieldattribute. Java: Field annotations that identify member variables as database fields and set constraints such as length, non-null, query fields, index fields, etc
      • Keyattribute. Java: primary key annotation that identifies the primary key of a member variable
      • Sortattribute. Java: Sorting annotation that identifies member variables as sorting fields
      • Tableattribute. Java: Data table annotation that identifies the table name and description information corresponding to the entity class
    • Provider: generates related Sql statements
      • Basecreateprovider. Java: a data table creation statement generator that supports index generation
      • Basedeleteprovider. Java: delete statement generator that supports deletion by ID, primary key, and custom conditions
      • Baseinsertprovider. Java: insert statement generator that supports auto-increment of primary keysinsertoperation
      • Baseselectprovider. Java: select statement generator, which supports paging query, statistical query, and custom condition query
      • Baseupdateprovider. Java: Update statement generator that supports data modification based on ID and primary key
    • Baseentity. Java: The parent of all entity classes, providing extensions for custom query criteria, paging queries, and sorting
    • Baseexception. Java: Custom exception, an exception thrown during the general Mapper operation
    • Basemapper. Java: A parent of all Mapper classes that provides general Mapper functionality
    • Sqlfieldreader. Java: Sql field parsing class used to parse custom annotations in entity classes for generating Sql statements
    • Typecast. Java: Type conversion, used to convert Java data types to the corresponding MySql data types
  • Util: Utility class dependent
    • Console. Java: log output tool class, used to output related information in the Console and log files
    • Objectutils. Java: Object utility class, used for null-value determination of objects
    • Stringutils.java: String utility class for generating strings of a specified format and for various manipulation of strings

use

The entity class inherits BaseEntity and uses annotations

@TableAttribute(name = "user_info",comment = "User Information Sheet")
public class UserInfo   extends BaseEntity {
  	// The annotation is auto-increment primary key
    @KeyAttribute(autoIncr = true)
  	// The annotations are database fields
    @FieldAttribute
    private int id;
		
  	// Set field comments, non-null constraints, as query conditions
    @FieldAttribute(value = "User Type",notNull = true,isCondition = true)
    private Integer type;


    @FieldAttribute(value = "Password",length = 200, isDetailed = true)
    private String password;
		
  	// Set field comment, non-null constraint, field length, is index field
    @FieldAttribute(value = "Email",notNull = true,length = 200,isIndex = true)
    private String email;

    @FieldAttribute
  	@SortAttrtibute
    private Date createTime = new Date();

    @FieldAttribute(value = "User Account Status",isCondition = true)
    private Integer status ;

    @FieldAttribute(value = "Delete or not? 1 means delete.",isCondition = true)
    privateInteger isDelete; . . }Copy the code

The Dao layer inherits from BaseMapper

@Mapper
public interface UserMapper extends BaseMapper<UserInfo> {}Copy the code

call

    /** * Query user * by type and mailbox@param type
     * @param email
     * @return* /
    private UserInfo getByEmailAndType(int type,String email){
        UserInfo userInfo = new UserInfo();
      	// Set the search condition. 1 Email
        userInfo.setEmail(email);
      	// Set the query condition 2 Type
        userInfo.setType(type);
      	// Set the use of And to join multiple query conditions
        userInfo.setBaseKyleUseAnd(true);
      	// Execute the query and return the result
        List<UserInfo> list = userMapper.baseSelectList(userInfo);
        if(list ! =null && list.size() > 0) {return list.get(0);
        }
        return null;
    }
Copy the code

To obtain

Project address: github.com/tianlanland…