mybatis_generator-gui生成代码的一些总结

有道地址: https://note.youdao.com/ynoteshare1/index.html?id=d7d96fe0d846cc6d8700dbe727d282fd&type=note
对springboot整合mybatis项目中的mapper.xml手写也可以从github上直接拉取mybatis_generator-gui在本地直接跑起来反向生成mappers mapper entity这三个部分,这里主要说明用工具自动生成代码中要注意的细节。


  1. 首先看一下代码运行起来的ui
    1> 点击 数据库连接,会出现下面这张图,不用多说,填写自己要连接数据库的信息:
    mybatis_generator-gui生成代码的一些总结_第1张图片

    (注意:连接服务器后不要急着双击打开,可以右键来编辑要连接的数据库)
    2> 生成具体代码需要填写的信息
    mybatis_generator-gui生成代码的一些总结_第2张图片
    a> java实体类名:
    数据库用abs_copy这种明明规则,工具可以自己转化成AbsCopy,要不然就自己重新命名。
    b> 主键可以选填,但是填与不填将来在代码中会有如下代码不一样:
    加了主键:

      <insert id="insert" keyColumn="name" keyProperty="name" parameterType="com.szzt.csp.entity.AbsCopy" useGeneratedKeys="true">
     insert into abs_copy (age, `add`, nick
       )
     values (#{age,jdbcType=INTEGER}, #{add,jdbcType=VARCHAR}, #{nick,jdbcType=VARCHAR}
       )
    
```

不加主键:

  <insert id="insert" parameterType="com.szzt.csp.entity.AbsCopy">
 insert into abs_copy (`name`, age, `add`, 
   nick)
 values (#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{add,jdbcType=VARCHAR}, 
   #{nick,jdbcType=VARCHAR})
</insert>   

建议:设计表的时候有主键生成代码就写着,没有就不要乱加,以免后面在用mapper的时候出错。
c> 其他信息就如图所示找到自己的路径,注意勾选 使用example 来生成响应的分页信息,然后生成代码就可以了。


  1. 生成的两个entity
    Abs 和 AbsExample:
    Abs.java比较简单,有

    set  get 方法
    public boolean equals(Object that){}
    public int hashCode(){}
    public String toString(){}
    

    几个方法也都常用。
    AbsExample.java的内容比较多,也是实际项目中用的比较多的一个:
    里面主要是 设置增删改查的变量,以及分页排序的参数

        protected String orderByClause;
    
     protected boolean distinct;
    
     protected List<Criteria> oredCriteria;
    
     private Integer limit;
    
     private Long offset;
      public AbsCopyExample() {
         oredCriteria = new ArrayList<Criteria>();
     }
    
     public void setOrderByClause(String orderByClause) {
         this.orderByClause = orderByClause;
     }
    
     public String getOrderByClause() {
         return orderByClause;
     }
    
     public void setDistinct(boolean distinct) {
         this.distinct = distinct;
     }
    
     public boolean isDistinct() {
         return distinct;
     }
    
     public List<Criteria> getOredCriteria() {
         return oredCriteria;
     }
    
     public void or(Criteria criteria) {
         oredCriteria.add(criteria);
     }
    
     public Criteria or() {
         Criteria criteria = createCriteriaInternal();
         oredCriteria.add(criteria);
         return criteria;
     }
    
     public Criteria createCriteria() {
         Criteria criteria = createCriteriaInternal();
         if (oredCriteria.size() == 0) {
             oredCriteria.add(criteria);
         }
         return criteria;
     }
    
     protected Criteria createCriteriaInternal() {
         Criteria criteria = new Criteria();
         return criteria;
     }
    
     public void clear() {
         oredCriteria.clear();
         orderByClause = null;
         distinct = false;
     }
    
     public void setLimit(Integer limit) {
         this.limit = limit;
     }
    
     public Integer getLimit() {
         return limit;
     }
    
     public void setOffset(Long offset) {
         this.offset = offset;
     }
    
     public Long getOffset() {
         return offset;
     }
    
     protected abstract static class GeneratedCriteria {
         protected List<Criterion> criteria;
    
         protected GeneratedCriteria() {
             super();
             criteria = new ArrayList<Criterion>();
         }
    
         public boolean isValid() {
             return criteria.size() > 0;
         }
    
         public List<Criterion> getAllCriteria() {
             return criteria;
         }
    
         public List<Criterion> getCriteria() {
             return criteria;
         }
    
         protected void addCriterion(String condition) {
             if (condition == null) {
                 throw new RuntimeException("Value for condition cannot be null");
             }
             criteria.add(new Criterion(condition));
         }
    
         protected void addCriterion(String condition, Object value, String property) {
             if (value == null) {
                 throw new RuntimeException("Value for " + property + " cannot be null");
             }
             criteria.add(new Criterion(condition, value));
         }
    
         protected void addCriterion(String condition, Object value1, Object value2, String property) {
             if (value1 == null || value2 == null) {
                 throw new RuntimeException("Between values for " + property + " cannot be null");
             }
             criteria.add(new Criterion(condition, value1, value2));
         }
    
         public Criteria andNameIsNull() {
             addCriterion("`name` is null");
             return (Criteria) this;
         }
    
         public Criteria andNameIsNotNull() {
             addCriterion("`name` is not null");
             return (Criteria) this;
         }
    
         public Criteria andNameEqualTo(String value) {
             addCriterion("`name` =", value, "name");
             return (Criteria) this;
         }
    
         public Criteria andNameNotEqualTo(String value) {
             addCriterion("`name` <>", value, "name");
             return (Criteria) this;
         }
    
         public Criteria andNameGreaterThan(String value) {
             addCriterion("`name` >", value, "name");
             return (Criteria) this;
         }
    
         public Criteria andNameGreaterThanOrEqualTo(String value) {
             addCriterion("`name` >=", value, "name");
             return (Criteria) this;
         }
    
         public Criteria andNameLessThan(String value) {
             addCriterion("`name` <", value, "name");
             return (Criteria) this;
         }
    
         public Criteria andNameLessThanOrEqualTo(String value) {
             addCriterion("`name` <=", value, "name");
             return (Criteria) this;
         }
    
         public Criteria andNameLike(String value) {
             addCriterion("`name` like", value, "name");
             return (Criteria) this;
         }
    
         public Criteria andNameNotLike(String value) {
             addCriterion("`name` not like", value, "name");
             return (Criteria) this;
         }
    
         public Criteria andNameIn(List<String> values) {
             addCriterion("`name` in", values, "name");
             return (Criteria) this;
         }
    
         public Criteria andNameNotIn(List<String> values) {
             addCriterion("`name` not in", values, "name");
             return (Criteria) this;
         }
    
         public Criteria andNameBetween(String value1, String value2) {
             addCriterion("`name` between", value1, value2, "name");
             return (Criteria) this;
         }
    
         public Criteria andNameNotBetween(String value1, String value2) {
             addCriterion("`name` not between", value1, value2, "name");
             return (Criteria) this;
         }
    
         public Criteria andAgeIsNull() {
             addCriterion("age is null");
             return (Criteria) this;
         }
    
         public Criteria andAgeIsNotNull() {
             addCriterion("age is not null");
             return (Criteria) this;
         }
    
         public Criteria andAgeEqualTo(Integer value) {
             addCriterion("age =", value, "age");
             return (Criteria) this;
         }
    
         public Criteria andAgeNotEqualTo(Integer value) {
             addCriterion("age <>", value, "age");
             return (Criteria) this;
         }
    
         public Criteria andAgeGreaterThan(Integer value) {
             addCriterion("age >", value, "age");
             return (Criteria) this;
         }
    
         public Criteria andAgeGreaterThanOrEqualTo(Integer value) {
             addCriterion("age >=", value, "age");
             return (Criteria) this;
         }
    
         public Criteria andAgeLessThan(Integer value) {
             addCriterion("age <", value, "age");
             return (Criteria) this;
         }
    
         public Criteria andAgeLessThanOrEqualTo(Integer value) {
             addCriterion("age <=", value, "age");
             return (Criteria) this;
         }
    
         public Criteria andAgeIn(List<Integer> values) {
             addCriterion("age in", values, "age");
             return (Criteria) this;
         }
    
         public Criteria andAgeNotIn(List<Integer> values) {
             addCriterion("age not in", values, "age");
             return (Criteria) this;
         }
    
         public Criteria andAgeBetween(Integer value1, Integer value2) {
             addCriterion("age between", value1, value2, "age");
             return (Criteria) this;
         }
    
         public Criteria andAgeNotBetween(Integer value1, Integer value2) {
             addCriterion("age not between", value1, value2, "age");
             return (Criteria) this;
         }
    
         public Criteria andAddIsNull() {
             addCriterion("`add` is null");
             return (Criteria) this;
         }
    
         public Criteria andAddIsNotNull() {
             addCriterion("`add` is not null");
             return (Criteria) this;
         }
    
         public Criteria andAddEqualTo(String value) {
             addCriterion("`add` =", value, "add");
             return (Criteria) this;
         }
    
         public Criteria andAddNotEqualTo(String value) {
             addCriterion("`add` <>", value, "add");
             return (Criteria) this;
         }
    
         public Criteria andAddGreaterThan(String value) {
             addCriterion("`add` >", value, "add");
             return (Criteria) this;
         }
    
         public Criteria andAddGreaterThanOrEqualTo(String value) {
             addCriterion("`add` >=", value, "add");
             return (Criteria) this;
         }
    
         public Criteria andAddLessThan(String value) {
             addCriterion("`add` <", value, "add");
             return (Criteria) this;
         }
    
         public Criteria andAddLessThanOrEqualTo(String value) {
             addCriterion("`add` <=", value, "add");
             return (Criteria) this;
         }
    
         public Criteria andAddLike(String value) {
             addCriterion("`add` like", value, "add");
             return (Criteria) this;
         }
    
         public Criteria andAddNotLike(String value) {
             addCriterion("`add` not like", value, "add");
             return (Criteria) this;
         }
    
         public Criteria andAddIn(List<String> values) {
             addCriterion("`add` in", values, "add");
             return (Criteria) this;
         }
    
         public Criteria andAddNotIn(List<String> values) {
             addCriterion("`add` not in", values, "add");
             return (Criteria) this;
         }
    
         public Criteria andAddBetween(String value1, String value2) {
             addCriterion("`add` between", value1, value2, "add");
             return (Criteria) this;
         }
    
         public Criteria andAddNotBetween(String value1, String value2) {
             addCriterion("`add` not between", value1, value2, "add");
             return (Criteria) this;
         }
    
         public Criteria andNickIsNull() {
             addCriterion("nick is null");
             return (Criteria) this;
         }
    
         public Criteria andNickIsNotNull() {
             addCriterion("nick is not null");
             return (Criteria) this;
         }
    
         public Criteria andNickEqualTo(String value) {
             addCriterion("nick =", value, "nick");
             return (Criteria) this;
         }
    
         public Criteria andNickNotEqualTo(String value) {
             addCriterion("nick <>", value, "nick");
             return (Criteria) this;
         }
    
         public Criteria andNickGreaterThan(String value) {
             addCriterion("nick >", value, "nick");
             return (Criteria) this;
         }
    
         public Criteria andNickGreaterThanOrEqualTo(String value) {
             addCriterion("nick >=", value, "nick");
             return (Criteria) this;
         }
    
         public Criteria andNickLessThan(String value) {
             addCriterion("nick <", value, "nick");
             return (Criteria) this;
         }
    
         public Criteria andNickLessThanOrEqualTo(String value) {
             addCriterion("nick <=", value, "nick");
             return (Criteria) this;
         }
    
         public Criteria andNickLike(String value) {
             addCriterion("nick like", value, "nick");
             return (Criteria) this;
         }
    
         public Criteria andNickNotLike(String value) {
             addCriterion("nick not like", value, "nick");
             return (Criteria) this;
         }
    
         public Criteria andNickIn(List<String> values) {
             addCriterion("nick in", values, "nick");
             return (Criteria) this;
         }
    
         public Criteria andNickNotIn(List<String> values) {
             addCriterion("nick not in", values, "nick");
             return (Criteria) this;
         }
    
         public Criteria andNickBetween(String value1, String value2) {
             addCriterion("nick between", value1, value2, "nick");
             return (Criteria) this;
         }
    
         public Criteria andNickNotBetween(String value1, String value2) {
             addCriterion("nick not between", value1, value2, "nick");
             return (Criteria) this;
         }
     }
    
     /**
      */
     public static class Criteria extends GeneratedCriteria {
    
         protected Criteria() {
             super();
         }
     }
    
     public static class Criterion {
         private String condition;
    
         private Object value;
    
         private Object secondValue;
    
         private boolean noValue;
    
         private boolean singleValue;
    
         private boolean betweenValue;
    
         private boolean listValue;
    
         private String typeHandler;
    
         public String getCondition() {
             return condition;
         }
    
         public Object getValue() {
             return value;
         }
    
         public Object getSecondValue() {
             return secondValue;
         }
    
         public boolean isNoValue() {
             return noValue;
         }
    
         public boolean isSingleValue() {
             return singleValue;
         }
    
         public boolean isBetweenValue() {
             return betweenValue;
         }
    
         public boolean isListValue() {
             return listValue;
         }
    
         public String getTypeHandler() {
             return typeHandler;
         }
    
         protected Criterion(String condition) {
             super();
             this.condition = condition;
             this.typeHandler = null;
             this.noValue = true;
         }
    
         protected Criterion(String condition, Object value, String typeHandler) {
             super();
             this.condition = condition;
             this.value = value;
             this.typeHandler = typeHandler;
             if (value instanceof List<?>) {
                 this.listValue = true;
             } else {
                 this.singleValue = true;
             }
         }
    
         protected Criterion(String condition, Object value) {
             this(condition, value, null);
         }
    
         protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
             super();
             this.condition = condition;
             this.value = value;
             this.secondValue = secondValue;
             this.typeHandler = typeHandler;
             this.betweenValue = true;
         }
    
         protected Criterion(String condition, Object value, Object secondValue) {
             this(condition, value, secondValue, null);
         }
     }
    

    代码中比较绕的是用or连接两个查询条件,还有就是熟练运用那些大于 小于 不等于 null notnull等

  2. 代码中insert 和 insertSelective的区别:
    详见: https://blog.csdn.net/Com_ma/article/details/91463928

  3. 生成的mapper
    每次代码会生成 MyBatisBaseDao 和 自己的mapper, 自己的mapper extends MyBatisBaseDao , 自己在自己的mapper中可以用@SELECT标签写自己的sql,
    MyBatisBaseDao中定义了一些通用的接口:

    public interface MyBatisBaseDao<Model, PK extends Serializable, E> {
     long countByExample(E example);
    
     int deleteByExample(E example);
    
     int deleteByPrimaryKey(PK id);
    
     int insert(Model record);
    
     int insertSelective(Model record);
    
     List<Model> selectByExample(E example);
    
     Model selectByPrimaryKey(PK id);
    
     int updateByExampleSelective(@Param("record") Model record, @Param("example") E example);
    
     int updateByExample(@Param("record") Model record, @Param("example") E example);
    
     int updateByPrimaryKeySelective(Model record);
    
     int updateByPrimaryKey(Model record);
    }
    

    当然也可以在MyBatisBaseDao中写sql。

    总结 :
    工具生成的distinct自己感觉是假的,想要sql中用distinct,还是自己动手写sql比较合适,用工具生成的代码就这些,个人觉的就能完成基本的增删改查,对于业务逻辑稍微复杂的还要借助mapper.xml 或者自己独立封装sql。

你可能感兴趣的:(java,Java,springboot项目专栏)