A plugin for MyBatis Generator to use Lombok annotations and table annotations
快速開始
初始化sql
數(shù)據(jù)庫的建表sql
CREATE TABLE `user` (
`id` bigint(20) DEFAULT NULL COMMENT '自增id',
`name` varchar(255) DEFAULT NULL COMMENT '姓名',
`mobile` bigint(255) DEFAULT NULL COMMENT '手機(jī)號'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用戶表';
使用前
正常如果不適用插件的話生成帶注釋的bean是這樣的:
package entity;
public class User {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.id
*
* @mbg.generated Sun Feb 02 14:17:08 CST 2020
*/
private Long id;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.name
*
* @mbg.generated Sun Feb 02 14:17:08 CST 2020
*/
private String name;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.mobile
*
* @mbg.generated Sun Feb 02 14:17:08 CST 2020
*/
private Long mobile;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.id
*
* @return the value of user.id
*
* @mbg.generated Sun Feb 02 14:17:08 CST 2020
*/
public Long getId() {
return id;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.id
*
* @param id the value for user.id
*
* @mbg.generated Sun Feb 02 14:17:08 CST 2020
*/
public void setId(Long id) {
this.id = id;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.name
*
* @return the value of user.name
*
* @mbg.generated Sun Feb 02 14:17:08 CST 2020
*/
public String getName() {
return name;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.name
*
* @param name the value for user.name
*
* @mbg.generated Sun Feb 02 14:17:08 CST 2020
*/
public void setName(String name) {
this.name = name;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.mobile
*
* @return the value of user.mobile
*
* @mbg.generated Sun Feb 02 14:17:08 CST 2020
*/
public Long getMobile() {
return mobile;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.mobile
*
* @param mobile the value for user.mobile
*
* @mbg.generated Sun Feb 02 14:17:08 CST 2020
*/
public void setMobile(Long mobile) {
this.mobile = mobile;
}
}
使用后
使用插件后生成帶lombok注解與數(shù)據(jù)庫注釋的bean:
package entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 用戶表
* @author tsn
* @date 2020-02-02
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder(toBuilder = true)
public class User {
/**
* 自增id
*/
private Long id;
/**
* 姓名
*/
private String name;
/**
* 手機(jī)號
*/
private Long mobile;
}
pom.xml配置
在pom.xml
內(nèi)引入
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${mybatis.generator.version}</version>
<configuration>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>com.github.supergaga</groupId>
<artifactId>generator</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
generator的config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="example"
targetRuntime="MyBatis3Simple"
defaultModelType="flat">
<commentGenerator type="com.edison.CommentGenerator">
<property name="author" value="tsn"/>
<property name="allArgsConstructor" value="false"/>
<property name="noArgsConstructor" value="false"/>
<property name="toString" value="false"/>
<property name="builder.toBuilder" value="true"/>
<property name="builder" value="true"/>
</commentGenerator>
</context>
<!-- 數(shù)據(jù)庫連接,庫表關(guān)系财著,生成位置 等等配置... -->
</generatorConfiguration>
在idea中使用插件
在idea右側(cè)的maven中有使用插件的按鈕
插件源碼
插件代碼已經(jīng)上傳到github上,歡迎提意見!