其实原理很简单很简单...
下面是修改后的模板代码:
#set($paramName = $helper.getParamName($field, $project))#if($field.modifierStatic)static ###end$classname set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) { #if ($field.name == $paramName) #if (!$field.modifierStatic) this.## #else $classname.## #end #end $field.name = $paramName; return this;}
自动生成的类代码:
public class User { private String userName; private Date createTime; private String password; public User() { } public String getUserName() { return userName; } public User setUserName(String userName) { this.userName = userName; return this; } public Date getCreateTime() { return createTime; } public User setCreateTime(Date createTime) { this.createTime = createTime; return this; } public String getPassword() { return password; } public User setPassword(String password) { this.password = password; return this; }}
实测效果,是不是很爽:
public class Test { public static void main(String[] args) { User tom = new User() .setUserName("tom") .setCreateTime(new Date()) .setPassword("123"); }}