MyBatis使用逆向工程生成文件(保姆级教程)

1. 下载逆向工程项目

点击链接下载逆向工程压缩文件

解压文件

导入到IDEA中

2. 配置依赖

导入到IDEA后项目的结构如下:

MyBatis使用逆向工程生成文件(保姆级教程)_第1张图片

这不是Maven项目,所以依赖需要我们手动添加一下,添加方法如下:

  • 点击项目名

  • 按 Ctrl + Shift + Alt + S

  • 点击Modules,然后点击Dependencies,下面显示的就是项目的依赖

    MyBatis使用逆向工程生成文件(保姆级教程)_第2张图片

  • 点击右侧的 + 号

    MyBatis使用逆向工程生成文件(保姆级教程)_第3张图片

  • 点击要添加的依赖,选择 Add Selected

    MyBatis使用逆向工程生成文件(保姆级教程)_第4张图片

  • 点击Apply关闭即可

    MyBatis使用逆向工程生成文件(保姆级教程)_第5张图片

注:项目中仅含有的数据库链接驱动为MySQL和Oracle的,其他数据库需要单独添加配置依赖,步骤相同

3. 编写配置文件

这是模拟的数据库表,数据库名称 test 表名 person

MyBatis使用逆向工程生成文件(保姆级教程)_第6张图片

打开配置文件

MyBatis使用逆向工程生成文件(保姆级教程)_第7张图片

一般需要配置一下内容:

MyBatis使用逆向工程生成文件(保姆级教程)_第8张图片

  • 数据库连接驱动

    MySQL 8.0 及以上的版本

    driverClass="com.mysql.cj.jdbc.Driver"
    

    MySQL 8.0 以下版本

    driverClass="com.mysql.jdbc.Driver"
    
  • 数据库连接URL

    connectionURL="jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false"
    
  • 数据库连接用户名和密码

    userId="你的用户名"
    password="你的密码"
    
  • 实体类、Mapper文件和接口的生成位置

    MyBatis使用逆向工程生成文件(保姆级教程)_第9张图片

    需要其他配置可按照提示自行修改

4. 启动项目

配置无误后,点击项目Main方法入口类,点击运行

MyBatis使用逆向工程生成文件(保姆级教程)_第10张图片

如果下面输出日志打印窗口表明一切正常

MyBatis使用逆向工程生成文件(保姆级教程)_第11张图片

此时项目的路径中已成功生成对应的文件

MyBatis使用逆向工程生成文件(保姆级教程)_第12张图片

生成成功后就可以将文件拷贝到对应的项目中去使用

附:生成效果参考

实体类

package com.demo.domain;

public class Person {
     
    private Long id;

    private String address;

    private Integer age;

    private String name;

    public Long getId() {
     
        return id;
    }

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

    public String getAddress() {
     
        return address;
    }

    public void setAddress(String address) {
     
        this.address = address == null ? null : address.trim();
    }

    public Integer getAge() {
     
        return age;
    }

    public void setAge(Integer age) {
     
        this.age = age;
    }

    public String getName() {
     
        return name;
    }

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

实体查询类

package com.demo.domain;

import java.util.ArrayList;
import java.util.List;

public class PersonExample {
     
    protected String orderByClause;

    protected boolean distinct;

    protected List<Criteria> oredCriteria;

    public PersonExample() {
     
        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;
    }

    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 andIdIsNull() {
     
            addCriterion("id is null");
            return (Criteria) this;
        }

        public Criteria andIdIsNotNull() {
     
            addCriterion("id is not null");
            return (Criteria) this;
        }

        public Criteria andIdEqualTo(Long value) {
     
            addCriterion("id =", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdNotEqualTo(Long value) {
     
            addCriterion("id <>", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdGreaterThan(Long value) {
     
            addCriterion("id >", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdGreaterThanOrEqualTo(Long value) {
     
            addCriterion("id >=", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdLessThan(Long value) {
     
            addCriterion("id <", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdLessThanOrEqualTo(Long value) {
     
            addCriterion("id <=", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdIn(List<Long> values) {
     
            addCriterion("id in", values, "id");
            return (Criteria) this;
        }

        public Criteria andIdNotIn(List<Long> values) {
     
            addCriterion("id not in", values, "id");
            return (Criteria) this;
        }

        public Criteria andIdBetween(Long value1, Long value2) {
     
            addCriterion("id between", value1, value2, "id");
            return (Criteria) this;
        }

        public Criteria andIdNotBetween(Long value1, Long value2) {
     
            addCriterion("id not between", value1, value2, "id");
            return (Criteria) this;
        }

        public Criteria andAddressIsNull() {
     
            addCriterion("address is null");
            return (Criteria) this;
        }

        public Criteria andAddressIsNotNull() {
     
            addCriterion("address is not null");
            return (Criteria) this;
        }

        public Criteria andAddressEqualTo(String value) {
     
            addCriterion("address =", value, "address");
            return (Criteria) this;
        }

        public Criteria andAddressNotEqualTo(String value) {
     
            addCriterion("address <>", value, "address");
            return (Criteria) this;
        }

        public Criteria andAddressGreaterThan(String value) {
     
            addCriterion("address >", value, "address");
            return (Criteria) this;
        }

        public Criteria andAddressGreaterThanOrEqualTo(String value) {
     
            addCriterion("address >=", value, "address");
            return (Criteria) this;
        }

        public Criteria andAddressLessThan(String value) {
     
            addCriterion("address <", value, "address");
            return (Criteria) this;
        }

        public Criteria andAddressLessThanOrEqualTo(String value) {
     
            addCriterion("address <=", value, "address");
            return (Criteria) this;
        }

        public Criteria andAddressLike(String value) {
     
            addCriterion("address like", value, "address");
            return (Criteria) this;
        }

        public Criteria andAddressNotLike(String value) {
     
            addCriterion("address not like", value, "address");
            return (Criteria) this;
        }

        public Criteria andAddressIn(List<String> values) {
     
            addCriterion("address in", values, "address");
            return (Criteria) this;
        }

        public Criteria andAddressNotIn(List<String> values) {
     
            addCriterion("address not in", values, "address");
            return (Criteria) this;
        }

        public Criteria andAddressBetween(String value1, String value2) {
     
            addCriterion("address between", value1, value2, "address");
            return (Criteria) this;
        }

        public Criteria andAddressNotBetween(String value1, String value2) {
     
            addCriterion("address not between", value1, value2, "address");
            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 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 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);
        }
    }
}

Mapper接口类

package com.demo.mapper;

import com.demo.domain.Person;
import com.demo.domain.PersonExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface PersonMapper {
     
    int countByExample(PersonExample example);

    int deleteByExample(PersonExample example);

    int deleteByPrimaryKey(Long id);

    int insert(Person record);

    int insertSelective(Person record);

    List<Person> selectByExample(PersonExample example);

    Person selectByPrimaryKey(Long id);

    int updateByExampleSelective(@Param("record") Person record, @Param("example") PersonExample example);

    int updateByExample(@Param("record") Person record, @Param("example") PersonExample example);

    int updateByPrimaryKeySelective(Person record);

    int updateByPrimaryKey(Person record);
}

对应的XML文件



<mapper namespace="com.demo.mapper.PersonMapper" >
  <resultMap id="BaseResultMap" type="com.demo.domain.Person" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="address" property="address" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
  resultMap>
  <sql id="Example_Where_Clause" >
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  foreach>
                when>
              choose>
            foreach>
          trim>
        if>
      foreach>
    where>
  sql>
  <sql id="Update_By_Example_Where_Clause" >
    <where >
      <foreach collection="example.oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  foreach>
                when>
              choose>
            foreach>
          trim>
        if>
      foreach>
    where>
  sql>
  <sql id="Base_Column_List" >
    id, address, age, name
  sql>
  <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.demo.domain.PersonExample" >
    select
    <if test="distinct" >
      distinct
    if>
    <include refid="Base_Column_List" />
    from person
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    if>
  select>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from person
    where id = #{id,jdbcType=BIGINT}
  select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from person
    where id = #{id,jdbcType=BIGINT}
  delete>
  <delete id="deleteByExample" parameterType="com.demo.domain.PersonExample" >
    delete from person
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    if>
  delete>
  <insert id="insert" parameterType="com.demo.domain.Person" >
    insert into person (id, address, age, 
      name)
    values (#{id,jdbcType=BIGINT}, #{address,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, 
      #{name,jdbcType=VARCHAR})
  insert>
  <insert id="insertSelective" parameterType="com.demo.domain.Person" >
    insert into person
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      if>
      <if test="address != null" >
        address,
      if>
      <if test="age != null" >
        age,
      if>
      <if test="name != null" >
        name,
      if>
    trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=BIGINT},
      if>
      <if test="address != null" >
        #{address,jdbcType=VARCHAR},
      if>
      <if test="age != null" >
        #{age,jdbcType=INTEGER},
      if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      if>
    trim>
  insert>
  <select id="countByExample" parameterType="com.demo.domain.PersonExample" resultType="java.lang.Integer" >
    select count(*) from person
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    if>
  select>
  <update id="updateByExampleSelective" parameterType="map" >
    update person
    <set >
      <if test="record.id != null" >
        id = #{record.id,jdbcType=BIGINT},
      if>
      <if test="record.address != null" >
        address = #{record.address,jdbcType=VARCHAR},
      if>
      <if test="record.age != null" >
        age = #{record.age,jdbcType=INTEGER},
      if>
      <if test="record.name != null" >
        name = #{record.name,jdbcType=VARCHAR},
      if>
    set>
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    if>
  update>
  <update id="updateByExample" parameterType="map" >
    update person
    set id = #{record.id,jdbcType=BIGINT},
      address = #{record.address,jdbcType=VARCHAR},
      age = #{record.age,jdbcType=INTEGER},
      name = #{record.name,jdbcType=VARCHAR}
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    if>
  update>
  <update id="updateByPrimaryKeySelective" parameterType="com.demo.domain.Person" >
    update person
    <set >
      <if test="address != null" >
        address = #{address,jdbcType=VARCHAR},
      if>
      <if test="age != null" >
        age = #{age,jdbcType=INTEGER},
      if>
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      if>
    set>
    where id = #{id,jdbcType=BIGINT}
  update>
  <update id="updateByPrimaryKey" parameterType="com.demo.domain.Person" >
    update person
    set address = #{address,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER},
      name = #{name,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  update>
mapper>

个人公众号

我的个人公众号已经开通了,正在按计划建设中,以后的文章会第一时间发布在公众号中,同时也给大家准备了亿点学习资料,关注公众号后台发送:学习资料

你可能感兴趣的:(框架,mybatis,java)