mybatis generator生成entity类时如何生成构造方法和toString方法?

如题,使用mybatis generator默认生成的entity(数据库表的实体类)文件只包含get/set,如何可以生成的时候同时生成有参/无参的构造方法和重写toString方法呢?每次都手写太麻烦了,谢谢!

package com.example.baseproject.entity;

public class User {

    //默认生成
    private Integer id;

    private String name;

    private String email;

    private String cellphone;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    public String getCellphone() {
        return cellphone;
    }

    public void setCellphone(String cellphone) {
        this.cellphone = cellphone == null ? null : cellphone.trim();
    }

    //以下手动生成
    @Override
    public String toString() {
        return "User{" + "id=" + id + ", name='" + name + '\'' + ", email='" + email + '\'' + ", cellphone='" + cellphone + '\'' + '}';
    }

    public User(Integer id, String name, String email, String cellphone) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.cellphone = cellphone;
    }

    public User() {
    }
}
阅读 1.8k
1 个回答

mybatis generator有一个官方的org.mybatis.generator.plugins.ToStringPlugin,但是它生成的代码可能和你需要的有些许出入

你可以更具它的代码(开源的)自己实现一个CustomToStringPlugin 我看了下更据你的需求改动非常小

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题