Java mabatis逆向工程maven版

正常情况下在用mybatis逆向工程时,在resources节点下配置generatorConfig.xml  文件尤为重要

我们就拿这个xml配置文件为例 数据库用的是mysql 也可以运用其他的数据库

下面我们就会对一些配置来说明一下






    

        
        
        
        

        
        
        
        

        
            
            
            
            
        

        
        
        

        
            
            
            
        

        
        
            
            
        

        
        
        

        
        
        

        
        
        

例如我们在数据库中建了这几张表

Java mabatis逆向工程maven版_第1张图片

我们在src/main/java文件下会自动生成dao接口文件和mapperxml文件

那我们看context节点下会有id属性必填  targetRuntime的属性可填可不填  默认为MyBatis3 也可能是MyBatis3Simple 还可能是ibatis2  但是自动生成的效果截然不同我们就拿前MyBatis3  和  MyBatis3Simple 来举例子 两者生成的pojo有着很大的差异

Java mabatis逆向工程maven版_第2张图片Java mabatis逆向工程maven版_第3张图片

 
        
        
        



  
    
    
    
    
    
    
  
  
    
    
      
        
          
            
              
                
                  and ${criterion.condition}
                
                
                  and ${criterion.condition} #{criterion.value}
                
                
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                
                
                  and ${criterion.condition}
                  
                    #{listItem}
                  
                
              
            
          
        
      
    
  
  
    
    
      
        
          
            
              
                
                  and ${criterion.condition}
                
                
                  and ${criterion.condition} #{criterion.value}
                
                
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                
                
                  and ${criterion.condition}
                  
                    #{listItem}
                  
                
              
            
          
        
      
    
  
  
    
    id, `key`, `value`, `status`, remark
  
  
  
  
    
    delete from sys_config
    where id = #{id,jdbcType=BIGINT}
  
  
    
    delete from sys_config
    
      
    
  
  
    
    
      SELECT LAST_INSERT_ID()
    
    insert into sys_config (id, `key`, `value`, 
      `status`, remark)
    values (#{id,jdbcType=BIGINT}, #{key,jdbcType=VARCHAR}, #{value,jdbcType=VARCHAR}, 
      #{status,jdbcType=TINYINT}, #{remark,jdbcType=VARCHAR})
  
  
    
    
      SELECT LAST_INSERT_ID()
    
    insert into sys_config
    
      id,
      
        `key`,
      
      
        `value`,
      
      
        `status`,
      
      
        remark,
      
    
    
      #{id,jdbcType=BIGINT},
      
        #{key,jdbcType=VARCHAR},
      
      
        #{value,jdbcType=VARCHAR},
      
      
        #{status,jdbcType=TINYINT},
      
      
        #{remark,jdbcType=VARCHAR},
      
    
  
  
  
    
    update sys_config
    
      
        id = #{record.id,jdbcType=BIGINT},
      
      
        `key` = #{record.key,jdbcType=VARCHAR},
      
      
        `value` = #{record.value,jdbcType=VARCHAR},
      
      
        `status` = #{record.status,jdbcType=TINYINT},
      
      
        remark = #{record.remark,jdbcType=VARCHAR},
      
    
    
      
    
  
  
    
    update sys_config
    set id = #{record.id,jdbcType=BIGINT},
      `key` = #{record.key,jdbcType=VARCHAR},
      `value` = #{record.value,jdbcType=VARCHAR},
      `status` = #{record.status,jdbcType=TINYINT},
      remark = #{record.remark,jdbcType=VARCHAR}
    
      
    
  
  
    
    update sys_config
    
      
        `key` = #{key,jdbcType=VARCHAR},
      
      
        `value` = #{value,jdbcType=VARCHAR},
      
      
        `status` = #{status,jdbcType=TINYINT},
      
      
        remark = #{remark,jdbcType=VARCHAR},
      
    
    where id = #{id,jdbcType=BIGINT}
  
  
    
    update sys_config
    set `key` = #{key,jdbcType=VARCHAR},
      `value` = #{value,jdbcType=VARCHAR},
      `status` = #{status,jdbcType=TINYINT},
      remark = #{remark,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  

加上分隔符的好处就是在查询数据数据库时有时怕关键字的冲突,有反引号就可以避免因为关键字而引发的冲突 避免一些不必要的麻烦  建议开发的时候大家给加上

 
        
        
        

通过plugin 节点下生成的pojo就是实现Serializable接口,继承Object 里面的toString 和 equals方法 

这个根据开发的时候需要可加可不加

import java.io.Serializable;

public class SysConfig implements Serializable {
    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column sys_config.id
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    private Long id;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column sys_config.key
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    private String key;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column sys_config.value
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    private String value;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column sys_config.status
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    private Byte status;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column sys_config.remark
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    private String remark;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database table sys_config
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    private static final long serialVersionUID = 1L;

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column sys_config.id
     *
     * @return the value of sys_config.id
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    public Long getId() {
        return id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column sys_config.id
     *
     * @param id the value for sys_config.id
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column sys_config.key
     *
     * @return the value of sys_config.key
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    public String getKey() {
        return key;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column sys_config.key
     *
     * @param key the value for sys_config.key
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    public void setKey(String key) {
        this.key = key == null ? null : key.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column sys_config.value
     *
     * @return the value of sys_config.value
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    public String getValue() {
        return value;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column sys_config.value
     *
     * @param value the value for sys_config.value
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    public void setValue(String value) {
        this.value = value == null ? null : value.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column sys_config.status
     *
     * @return the value of sys_config.status
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    public Byte getStatus() {
        return status;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column sys_config.status
     *
     * @param status the value for sys_config.status
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    public void setStatus(Byte status) {
        this.status = status;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column sys_config.remark
     *
     * @return the value of sys_config.remark
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    public String getRemark() {
        return remark;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column sys_config.remark
     *
     * @param remark the value for sys_config.remark
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    public void setRemark(String remark) {
        this.remark = remark == null ? null : remark.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table sys_config
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        SysConfig other = (SysConfig) that;
        return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
            && (this.getKey() == null ? other.getKey() == null : this.getKey().equals(other.getKey()))
            && (this.getValue() == null ? other.getValue() == null : this.getValue().equals(other.getValue()))
            && (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus()))
            && (this.getRemark() == null ? other.getRemark() == null : this.getRemark().equals(other.getRemark()));
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table sys_config
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
        result = prime * result + ((getKey() == null) ? 0 : getKey().hashCode());
        result = prime * result + ((getValue() == null) ? 0 : getValue().hashCode());
        result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode());
        result = prime * result + ((getRemark() == null) ? 0 : getRemark().hashCode());
        return result;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table sys_config
     *
     * @mbg.generated Sat Jun 23 22:17:31 CST 2018
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", key=").append(key);
        sb.append(", value=").append(value);
        sb.append(", status=").append(status);
        sb.append(", remark=").append(remark);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }
}

对于时间戳的解释


            
            
            
            
        

如果取消时间戳的情况下我们每次生成的文件都会如果加上时间戳就会在每次修改代码时都会有时间的注释,在上传到服务器的时候我们就会对每次的改动做出明确的判断

是否自动生成注释

如果为true就会不生成注释  注意 我们在mapper.xml文件中一定要注意这个问题 如果自动生成之前的之前已有的xml代码还有有所保留,生成几次就会复制多少遍之前自动生成的代码,从而给开发的时候造成影响,而dao中的接口,和pojo中的类不会叠加生成,这是比较头疼的地方



你可能感兴趣的:(java,maven,MyBatis)