Explorar el Código

生成器支持 Lombok

= hace 8 años
padre
commit
57a3240a70

+ 5 - 1
build.gradle

@@ -29,8 +29,11 @@ tasks.withType(JavaCompile) {
 
 repositories {
     mavenLocal()
+    maven { url 'http://repo.spring.io/plugins-release' }
+    maven { url "https://plugins.gradle.org/m2/" }
+    maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
     mavenCentral()
-    maven { url "http://repo.maven.apache.org/maven2" }
+    jcenter()
 }
 
 task sourcesJar(type: Jar, dependsOn: classes) {
@@ -106,4 +109,5 @@ dependencies {
     testCompile("org.springframework:spring-webmvc:${springVersion}")
     testCompile("org.aspectj:aspectjweaver:1.8.9")
     testCompile("javax.servlet:servlet-api:2.5")
+    testCompile("org.projectlombok:lombok:1.16.16")
 }

+ 7 - 0
pom.xml

@@ -67,6 +67,7 @@
         <junit.version>4.12</junit.version>
         <velocity.version>1.7</velocity.version>
         <h2.version>1.4.194</h2.version>
+        <lombok.version>1.16.16</lombok.version>
     </properties>
 
     <dependencies>
@@ -237,6 +238,12 @@
             <version>${h2.version}</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>${lombok.version}</artifactId>
+            <version>1.16.16</version>
+            <scope>test</scope>
+        </dependency>
         <!-- test end -->
     </dependencies>
 

+ 8 - 9
src/main/java/com/baomidou/mybatisplus/generator/AutoGenerator.java

@@ -139,14 +139,13 @@ public class AutoGenerator extends AbstractGenerator {
             }
             // Boolean类型is前缀处理
             if ( config.getStrategyConfig().isEntityBooleanColumnRemoveIsPrefix() ) {
-                for ( TableField field : tableInfo.getFields() ) {
-                    if ( field.getPropertyType().equalsIgnoreCase( "boolean" ) ) {
-                        if ( field.getPropertyName().indexOf( "is" ) != -1 ) {
-                            String noIsPropertyName = field.getPropertyName()
-                                                           .substring( 2, field.getPropertyName().length() );
-                            String firstChar        = noIsPropertyName.substring( 0, 1 ).toLowerCase();
-                            String afterChar        = noIsPropertyName.substring( 1, noIsPropertyName.length() );
-                            field.setPropertyName( config.getStrategyConfig(), firstChar + afterChar );
+                for (TableField field : tableInfo.getFields()) {
+                    if (field.getPropertyType().equalsIgnoreCase("boolean")) {
+                        if (field.getPropertyName().indexOf("is") != -1) {
+                            String noIsPropertyName = field.getPropertyName().substring(2, field.getPropertyName().length());
+                            String firstChar = noIsPropertyName.substring(0, 1).toLowerCase();
+                            String afterChar = noIsPropertyName.substring(1, noIsPropertyName.length());
+                            field.setPropertyName(config.getStrategyConfig(), firstChar + afterChar);
                         }
                     }
                 }
@@ -154,7 +153,7 @@ public class AutoGenerator extends AbstractGenerator {
             // RequestMapping 连字符风格 user-info
             if ( config.getStrategyConfig().isControllerMappingHyphenStyle() ) {
                 ctx.put("controllerMappingHyphenStyle", config.getStrategyConfig().isControllerMappingHyphenStyle());
-                ctx.put("controllerMappingHyphen", StringUtils.camelToHyphen( tableInfo.getEntityPath() ));
+                ctx.put("controllerMappingHyphen", StringUtils.camelToHyphen(tableInfo.getEntityPath()));
             }
             
             ctx.put("restControllerStyle", config.getStrategyConfig().isRestControllerStyle());

+ 11 - 11
src/main/java/com/baomidou/mybatisplus/generator/config/StrategyConfig.java

@@ -118,7 +118,7 @@ public class StrategyConfig {
      *      <code>@Controller</code> -> <code>@RestController</code>
      * </pre>
      */
-    private boolean restControllerStyle          = false;
+    private boolean restControllerStyle = false;
     /**
      * 驼峰转连字符
      * <pre>
@@ -126,7 +126,7 @@ public class StrategyConfig {
      * </pre>
      */
     private boolean controllerMappingHyphenStyle = false;
-   
+
 
     public void setDbColumnUnderline(boolean dbColumnUnderline) {
         DB_COLUMN_UNDERLINE = dbColumnUnderline;
@@ -141,7 +141,7 @@ public class StrategyConfig {
      * @return
      */
     public boolean isCapitalModeNaming(String word) {
-        return isCapitalMode && StringUtils.isCapitalMode( word);
+        return isCapitalMode && StringUtils.isCapitalMode(word);
     }
 
     /**
@@ -281,35 +281,35 @@ public class StrategyConfig {
         this.entityBuilderModel = entityBuilderModel;
     }
 
-    public boolean isEntityLombokModel () {
+    public boolean isEntityLombokModel() {
         return entityLombokModel;
     }
 
-    public void setEntityLombokModel ( boolean entityLombokModel ) {
+    public void setEntityLombokModel(boolean entityLombokModel) {
         this.entityLombokModel = entityLombokModel;
     }
 
-    public boolean isEntityBooleanColumnRemoveIsPrefix () {
+    public boolean isEntityBooleanColumnRemoveIsPrefix() {
         return entityBooleanColumnRemoveIsPrefix;
     }
 
-    public void setEntityBooleanColumnRemoveIsPrefix ( boolean entityBooleanColumnRemoveIsPrefix ) {
+    public void setEntityBooleanColumnRemoveIsPrefix(boolean entityBooleanColumnRemoveIsPrefix) {
         this.entityBooleanColumnRemoveIsPrefix = entityBooleanColumnRemoveIsPrefix;
     }
 
-    public boolean isRestControllerStyle () {
+    public boolean isRestControllerStyle() {
         return restControllerStyle;
     }
 
-    public void setRestControllerStyle ( boolean restControllerStyle ) {
+    public void setRestControllerStyle(boolean restControllerStyle) {
         this.restControllerStyle = restControllerStyle;
     }
 
-    public boolean isControllerMappingHyphenStyle () {
+    public boolean isControllerMappingHyphenStyle() {
         return controllerMappingHyphenStyle;
     }
 
-    public void setControllerMappingHyphenStyle ( boolean controllerMappingHyphenStyle ) {
+    public void setControllerMappingHyphenStyle(boolean controllerMappingHyphenStyle) {
         this.controllerMappingHyphenStyle = controllerMappingHyphenStyle;
     }
 }

+ 1 - 0
src/main/resources/templates/entity.java.vm

@@ -5,6 +5,7 @@ import ${pkg};
 #end
 #if(${entityLombokModel})
 import lombok.Data;
+import lombok.experimental.Accessors;
 #end
 
 /**

+ 5 - 3
src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java

@@ -67,11 +67,11 @@ public class MysqlGenerator {
 
         // 数据源配置
         DataSourceConfig dsc = new DataSourceConfig();
-        dsc.setDbType( DbType.MYSQL);
+        dsc.setDbType(DbType.MYSQL);
         dsc.setTypeConvert(new MySqlTypeConvert() {
             // 自定义数据库表字段类型转换【可选】
             @Override
-            public DbColumnType processTypeConvert( String fieldType) {
+            public DbColumnType processTypeConvert(String fieldType) {
                 System.out.println("转换类型:" + fieldType);
                 // if ( fieldType.toLowerCase().contains( "tinyint" ) ) {
                 //    return DbColumnType.BOOLEAN;
@@ -90,7 +90,7 @@ public class MysqlGenerator {
         // strategy.setCapitalMode(true);// 全局大写命名
         // strategy.setDbColumnUnderline(true);//全局下划线命名
         strategy.setTablePrefix(new String[]{"bmd_", "mp_"});// 此处可以修改为您的表前缀
-        strategy.setNaming( NamingStrategy.underline_to_camel);// 表名生成策略
+        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
         // strategy.setInclude(new String[] { "user" }); // 需要生成的表
         // strategy.setExclude(new String[]{"test"}); // 排除生成的表
         // 自定义实体父类
@@ -115,6 +115,8 @@ public class MysqlGenerator {
         // strategy.setEntityLombokModel(true);
         // Boolean类型字段是否移除is前缀处理
         // strategy.setEntityBooleanColumnRemoveIsPrefix(true);
+        // strategy.setRestControllerStyle(true);
+        // strategy.setControllerMappingHyphenStyle(true);
         mpg.setStrategy(strategy);
 
         // 包配置

+ 5 - 32
src/test/java/com/baomidou/mybatisplus/test/h2/entity/persistent/H2Addr.java

@@ -4,6 +4,9 @@ import com.baomidou.mybatisplus.annotations.TableField;
 import com.baomidou.mybatisplus.annotations.TableId;
 import com.baomidou.mybatisplus.annotations.TableName;
 
+import lombok.Data;
+import lombok.experimental.Accessors;
+
 /**
  * <p>
  * h2address entity.
@@ -12,6 +15,8 @@ import com.baomidou.mybatisplus.annotations.TableName;
  * @author yuxiaobin
  * @date 2017/5/25
  */
+@Data
+@Accessors(chain = true)
 @TableName("h2address")
 public class H2Addr {
 
@@ -24,36 +29,4 @@ public class H2Addr {
     @TableField("test_id")
     private Long testId;
 
-    public Long getAddrId() {
-        return addrId;
-    }
-
-    public void setAddrId(Long addrId) {
-        this.addrId = addrId;
-    }
-
-    public String getAddrName() {
-        return addrName;
-    }
-
-    public void setAddrName(String addrName) {
-        this.addrName = addrName;
-    }
-
-    public Long getTestId() {
-        return testId;
-    }
-
-    public void setTestId(Long testId) {
-        this.testId = testId;
-    }
-
-    @Override
-    public String toString() {
-        return "H2Addr{" +
-                "addrId=" + addrId +
-                ", addrName='" + addrName + '\'' +
-                ", testId=" + testId +
-                '}';
-    }
 }

+ 5 - 68
src/test/java/com/baomidou/mybatisplus/test/h2/entity/persistent/H2User.java

@@ -24,6 +24,9 @@ import com.baomidou.mybatisplus.annotations.TableName;
 import com.baomidou.mybatisplus.annotations.Version;
 import com.baomidou.mybatisplus.enums.FieldStrategy;
 
+import lombok.Data;
+import lombok.experimental.Accessors;
+
 /**
  * <p>
  * 测试用户类
@@ -32,6 +35,8 @@ import com.baomidou.mybatisplus.enums.FieldStrategy;
  * @author hubin sjy
  */
 /* 表名 value 注解【 驼峰命名可无 】, resultMap 注解测试【 映射 xml 的 resultMap 内容 】 */
+@Data
+@Accessors(chain = true)
 @TableName
 public class H2User implements Serializable {
 
@@ -101,72 +106,4 @@ public class H2User implements Serializable {
         this.testType = testType;
     }
 
-    public Long getId() {
-        return id;
-    }
-
-    public void setId(Long id) {
-        this.id = id;
-    }
-
-    public String getName() {
-        return name;
-    }
-
-    public void setName(String name) {
-        this.name = name;
-    }
-
-    public BigDecimal getPrice() {
-        return price;
-    }
-
-    public void setPrice(BigDecimal price) {
-        this.price = price;
-    }
-
-    public Integer getAge() {
-        return age;
-    }
-
-    public void setAge(Integer age) {
-        this.age = age;
-    }
-
-    public Integer getTestType() {
-        return testType;
-    }
-
-    public void setTestType(Integer testType) {
-        this.testType = testType;
-    }
-
-    public String getDesc() {
-        return desc;
-    }
-
-    public void setDesc(String desc) {
-        this.desc = desc;
-    }
-
-    public Integer getVersion() {
-        return version;
-    }
-
-    public void setVersion(Integer version) {
-        this.version = version;
-    }
-
-    @Override
-    public String toString() {
-        return "H2User{" +
-                "id=" + id +
-                ", name='" + name + '\'' +
-                ", age=" + age +
-                ", price=" + price +
-                ", testType=" + testType +
-                ", desc='" + desc + '\'' +
-                ", version=" + version +
-                '}';
-    }
 }

+ 5 - 77
src/test/java/com/baomidou/mybatisplus/test/h2/entity/persistent/H2UserDateVersion.java

@@ -25,6 +25,9 @@ import com.baomidou.mybatisplus.annotations.TableName;
 import com.baomidou.mybatisplus.annotations.Version;
 import com.baomidou.mybatisplus.enums.FieldStrategy;
 
+import lombok.Data;
+import lombok.experimental.Accessors;
+
 /**
  * <p>
  * 测试用户类
@@ -33,6 +36,8 @@ import com.baomidou.mybatisplus.enums.FieldStrategy;
  * @author hubin sjy
  */
 /* 表名 value 注解【 驼峰命名可无 】, resultMap 注解测试【 映射 xml 的 resultMap 内容 】 */
+@Data
+@Accessors(chain = true)
 @TableName("h2user")
 public class H2UserDateVersion implements Serializable {
 
@@ -104,81 +109,4 @@ public class H2UserDateVersion implements Serializable {
         this.testType = testType;
     }
 
-    public Long getId() {
-        return id;
-    }
-
-    public void setId(Long id) {
-        this.id = id;
-    }
-
-    public String getName() {
-        return name;
-    }
-
-    public void setName(String name) {
-        this.name = name;
-    }
-
-    public BigDecimal getPrice() {
-        return price;
-    }
-
-    public void setPrice(BigDecimal price) {
-        this.price = price;
-    }
-
-    public Integer getAge() {
-        return age;
-    }
-
-    public void setAge(Integer age) {
-        this.age = age;
-    }
-
-    public Integer getTestType() {
-        return testType;
-    }
-
-    public void setTestType(Integer testType) {
-        this.testType = testType;
-    }
-
-    public String getDesc() {
-        return desc;
-    }
-
-    public void setDesc(String desc) {
-        this.desc = desc;
-    }
-
-    public Integer getVersion() {
-        return version;
-    }
-
-    public void setVersion(Integer version) {
-        this.version = version;
-    }
-
-    public Date getTestDate() {
-        return testDate;
-    }
-
-    public void setTestDate(Date testDate) {
-        this.testDate = testDate;
-    }
-
-    @Override
-    public String toString() {
-        return "H2UserDateVersion{" +
-                "id=" + id +
-                ", name='" + name + '\'' +
-                ", age=" + age +
-                ", price=" + price +
-                ", testType=" + testType +
-                ", desc='" + desc + '\'' +
-                ", version=" + version +
-                ", testDate=" + testDate +
-                '}';
-    }
 }

+ 5 - 68
src/test/java/com/baomidou/mybatisplus/test/h2/entity/persistent/H2UserLogicDelete.java

@@ -10,6 +10,9 @@ import com.baomidou.mybatisplus.annotations.TableName;
 import com.baomidou.mybatisplus.enums.FieldStrategy;
 import com.baomidou.mybatisplus.enums.IdType;
 
+import lombok.Data;
+import lombok.experimental.Accessors;
+
 /**
  * <p>
  * 测试逻辑删除实体
@@ -18,6 +21,8 @@ import com.baomidou.mybatisplus.enums.IdType;
  * @author yuxiaobin
  * @date 2017/6/15
  */
+@Data
+@Accessors(chain = true)
 @TableName("h2user")
 public class H2UserLogicDelete {
 
@@ -50,72 +55,4 @@ public class H2UserLogicDelete {
     @TableField(value = "last_updated_dt")
     private Timestamp lastUpdatedDt;
 
-
-    public static long getSerialVersionUID() {
-        return serialVersionUID;
-    }
-
-    public Long getId() {
-        return id;
-    }
-
-    public void setId(Long id) {
-        this.id = id;
-    }
-
-    public String getName() {
-        return name;
-    }
-
-    public void setName(String name) {
-        this.name = name;
-    }
-
-    public Integer getAge() {
-        return age;
-    }
-
-    public void setAge(Integer age) {
-        this.age = age;
-    }
-
-    public BigDecimal getPrice() {
-        return price;
-    }
-
-    public void setPrice(BigDecimal price) {
-        this.price = price;
-    }
-
-    public Integer getTestType() {
-        return testType;
-    }
-
-    public void setTestType(Integer testType) {
-        this.testType = testType;
-    }
-
-    public String getDesc() {
-        return desc;
-    }
-
-    public void setDesc(String desc) {
-        this.desc = desc;
-    }
-
-    public Integer getVersion() {
-        return version;
-    }
-
-    public void setVersion(Integer version) {
-        this.version = version;
-    }
-
-    public Timestamp getLastUpdatedDt() {
-        return lastUpdatedDt;
-    }
-
-    public void setLastUpdatedDt(Timestamp lastUpdatedDt) {
-        this.lastUpdatedDt = lastUpdatedDt;
-    }
 }

+ 5 - 77
src/test/java/com/baomidou/mybatisplus/test/h2/entity/persistent/H2UserMetaObj.java

@@ -26,6 +26,9 @@ import com.baomidou.mybatisplus.annotations.Version;
 import com.baomidou.mybatisplus.enums.FieldStrategy;
 import com.baomidou.mybatisplus.enums.IdType;
 
+import lombok.Data;
+import lombok.experimental.Accessors;
+
 /**
  * <p>
  * 测试用户类
@@ -34,6 +37,8 @@ import com.baomidou.mybatisplus.enums.IdType;
  * @author hubin sjy
  */
 /* 表名 value 注解【 驼峰命名可无 】, resultMap 注解测试【 映射 xml 的 resultMap 内容 】 */
+@Data
+@Accessors(chain = true)
 @TableName("h2user")
 public class H2UserMetaObj implements Serializable {
 
@@ -105,81 +110,4 @@ public class H2UserMetaObj implements Serializable {
         this.testType = testType;
     }
 
-    public Long getId() {
-        return id;
-    }
-
-    public void setId(Long id) {
-        this.id = id;
-    }
-
-    public String getName() {
-        return name;
-    }
-
-    public void setName(String name) {
-        this.name = name;
-    }
-
-    public BigDecimal getPrice() {
-        return price;
-    }
-
-    public void setPrice(BigDecimal price) {
-        this.price = price;
-    }
-
-    public Integer getAge() {
-        return age;
-    }
-
-    public void setAge(Integer age) {
-        this.age = age;
-    }
-
-    public Integer getTestType() {
-        return testType;
-    }
-
-    public void setTestType(Integer testType) {
-        this.testType = testType;
-    }
-
-    public String getDesc() {
-        return desc;
-    }
-
-    public void setDesc(String desc) {
-        this.desc = desc;
-    }
-
-    public Integer getVersion() {
-        return version;
-    }
-
-    public void setVersion(Integer version) {
-        this.version = version;
-    }
-
-    public Timestamp getLastUpdatedDt() {
-        return lastUpdatedDt;
-    }
-
-    public void setLastUpdatedDt(Timestamp lastUpdatedDt) {
-        this.lastUpdatedDt = lastUpdatedDt;
-    }
-
-    @Override
-    public String toString() {
-        return "H2UserMetaObj{" +
-                "id=" + id +
-                ", name='" + name + '\'' +
-                ", age=" + age +
-                ", price=" + price +
-                ", testType=" + testType +
-                ", desc='" + desc + '\'' +
-                ", version=" + version +
-                ", lastUpdatedDt=" + lastUpdatedDt +
-                '}';
-    }
 }

+ 5 - 68
src/test/java/com/baomidou/mybatisplus/test/h2/entity/persistent/H2UserNoVersion.java

@@ -23,6 +23,9 @@ import com.baomidou.mybatisplus.annotations.TableId;
 import com.baomidou.mybatisplus.annotations.TableName;
 import com.baomidou.mybatisplus.enums.FieldStrategy;
 
+import lombok.Data;
+import lombok.experimental.Accessors;
+
 /**
  * <p>
  * 测试用户类
@@ -31,6 +34,8 @@ import com.baomidou.mybatisplus.enums.FieldStrategy;
  * @author hubin sjy
  */
 /* 表名 value 注解【 驼峰命名可无 】, resultMap 注解测试【 映射 xml 的 resultMap 内容 】 */
+@Data
+@Accessors(chain = true)
 @TableName("h2user")
 public class H2UserNoVersion implements Serializable {
 
@@ -99,72 +104,4 @@ public class H2UserNoVersion implements Serializable {
         this.testType = testType;
     }
 
-    public Long getId() {
-        return id;
-    }
-
-    public void setId(Long id) {
-        this.id = id;
-    }
-
-    public String getName() {
-        return name;
-    }
-
-    public void setName(String name) {
-        this.name = name;
-    }
-
-    public BigDecimal getPrice() {
-        return price;
-    }
-
-    public void setPrice(BigDecimal price) {
-        this.price = price;
-    }
-
-    public Integer getAge() {
-        return age;
-    }
-
-    public void setAge(Integer age) {
-        this.age = age;
-    }
-
-    public Integer getTestType() {
-        return testType;
-    }
-
-    public void setTestType(Integer testType) {
-        this.testType = testType;
-    }
-
-    public String getDesc() {
-        return desc;
-    }
-
-    public void setDesc(String desc) {
-        this.desc = desc;
-    }
-
-    public Integer getVersion() {
-        return version;
-    }
-
-    public void setVersion(Integer version) {
-        this.version = version;
-    }
-
-    @Override
-    public String toString() {
-        return "H2User{" +
-                "id=" + id +
-                ", name='" + name + '\'' +
-                ", age=" + age +
-                ", price=" + price +
-                ", testType=" + testType +
-                ", desc='" + desc + '\'' +
-                ", version=" + version +
-                '}';
-    }
 }