第十四篇:SpringBoot2.x整合MyBatisGenerator

在这里简单介绍一下如何整合Mybatis自动生成代码的插件MybatisGenerator

引入插件

需要在pom.xml文件中的中加入以下设置

            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.5
                
                
                
                    mysql
                    mysql-connector-java
                    5.1.28
                    runtime
                
                
                
                
                    
                    src/main/resources/generatorConfig.xml
                    
                    
                    true
                
            

引入依赖后右侧的maven project侧边栏中的Plugins选项会多出一个mybatis-generator

第十四篇:SpringBoot2.x整合MyBatisGenerator_第1张图片
image.png

设置配置文件

接下来在resources文件夹中创建一个generatorConfig.xml文件用于自动构建代码的配置




    

        
        
            
            
            
            
        

        
        

        
        
        
        
            
            
            
            
            
            
            
            
        

        
        
            
            
        

        
        
            
            
        

        
        

运行插件

配置就结束了,最后运行一下,这里找到在Maven Projectmybatis-generator:generator选项,双击运行

第十四篇:SpringBoot2.x整合MyBatisGenerator_第2张图片
image.png

最后的成果

User.java

package priv.gabriel.model;

import java.util.Date;

public class User {
    private Integer id;

    private String username;

    private String pword;

    private Integer age;

    private Date utime;

    private Date ctime;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPword() {
        return pword;
    }

    public void setPword(String pword) {
        this.pword = pword == null ? null : pword.trim();
    }

    public Integer getAge() {
        return age;
    }

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

    public Date getUtime() {
        return utime;
    }

    public void setUtime(Date utime) {
        this.utime = utime;
    }

    public Date getCtime() {
        return ctime;
    }

    public void setCtime(Date ctime) {
        this.ctime = ctime;
    }
}

UserMapper.xml




  
    
    
    
    
    
    
  
  
    
      
        
          
            
              
                
                  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, username, pword, age, utime, ctime
  
  
  
  
    delete from user
    where id = #{id,jdbcType=INTEGER}
  
  
    delete from user
    
      
    
  
  
    insert into user (id, username, pword, 
      age, utime, ctime)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pword,jdbcType=VARCHAR}, 
      #{age,jdbcType=INTEGER}, #{utime,jdbcType=DATE}, #{ctime,jdbcType=DATE})
  
  
    insert into user
    
      
        id,
      
      
        username,
      
      
        pword,
      
      
        age,
      
      
        utime,
      
      
        ctime,
      
    
    
      
        #{id,jdbcType=INTEGER},
      
      
        #{username,jdbcType=VARCHAR},
      
      
        #{pword,jdbcType=VARCHAR},
      
      
        #{age,jdbcType=INTEGER},
      
      
        #{utime,jdbcType=DATE},
      
      
        #{ctime,jdbcType=DATE},
      
    
  
  
  
    update user
    
      
        id = #{record.id,jdbcType=INTEGER},
      
      
        username = #{record.username,jdbcType=VARCHAR},
      
      
        pword = #{record.pword,jdbcType=VARCHAR},
      
      
        age = #{record.age,jdbcType=INTEGER},
      
      
        utime = #{record.utime,jdbcType=DATE},
      
      
        ctime = #{record.ctime,jdbcType=DATE},
      
    
    
      
    
  
  
    update user
    set id = #{record.id,jdbcType=INTEGER},
      username = #{record.username,jdbcType=VARCHAR},
      pword = #{record.pword,jdbcType=VARCHAR},
      age = #{record.age,jdbcType=INTEGER},
      utime = #{record.utime,jdbcType=DATE},
      ctime = #{record.ctime,jdbcType=DATE}
    
      
    
  
  
    update user
    
      
        username = #{username,jdbcType=VARCHAR},
      
      
        pword = #{pword,jdbcType=VARCHAR},
      
      
        age = #{age,jdbcType=INTEGER},
      
      
        utime = #{utime,jdbcType=DATE},
      
      
        ctime = #{ctime,jdbcType=DATE},
      
    
    where id = #{id,jdbcType=INTEGER}
  
  
    update user
    set username = #{username,jdbcType=VARCHAR},
      pword = #{pword,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER},
      utime = #{utime,jdbcType=DATE},
      ctime = #{ctime,jdbcType=DATE}
    where id = #{id,jdbcType=INTEGER}
  
  
    
    
    
    
    
    
  
  
    
      
        
          
            
              
                
                  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, username, pword, age, utime, ctime
  
  
  
  
    delete from user
    where id = #{id,jdbcType=INTEGER}
  
  
    delete from user
    
      
    
  
  
    insert into user (id, username, pword, 
      age, utime, ctime)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pword,jdbcType=VARCHAR}, 
      #{age,jdbcType=INTEGER}, #{utime,jdbcType=DATE}, #{ctime,jdbcType=DATE})
  
  
    insert into user
    
      
        id,
      
      
        username,
      
      
        pword,
      
      
        age,
      
      
        utime,
      
      
        ctime,
      
    
    
      
        #{id,jdbcType=INTEGER},
      
      
        #{username,jdbcType=VARCHAR},
      
      
        #{pword,jdbcType=VARCHAR},
      
      
        #{age,jdbcType=INTEGER},
      
      
        #{utime,jdbcType=DATE},
      
      
        #{ctime,jdbcType=DATE},
      
    
  
  
  
    update user
    
      
        id = #{record.id,jdbcType=INTEGER},
      
      
        username = #{record.username,jdbcType=VARCHAR},
      
      
        pword = #{record.pword,jdbcType=VARCHAR},
      
      
        age = #{record.age,jdbcType=INTEGER},
      
      
        utime = #{record.utime,jdbcType=DATE},
      
      
        ctime = #{record.ctime,jdbcType=DATE},
      
    
    
      
    
  
  
    update user
    set id = #{record.id,jdbcType=INTEGER},
      username = #{record.username,jdbcType=VARCHAR},
      pword = #{record.pword,jdbcType=VARCHAR},
      age = #{record.age,jdbcType=INTEGER},
      utime = #{record.utime,jdbcType=DATE},
      ctime = #{record.ctime,jdbcType=DATE}
    
      
    
  
  
    update user
    
      
        username = #{username,jdbcType=VARCHAR},
      
      
        pword = #{pword,jdbcType=VARCHAR},
      
      
        age = #{age,jdbcType=INTEGER},
      
      
        utime = #{utime,jdbcType=DATE},
      
      
        ctime = #{ctime,jdbcType=DATE},
      
    
    where id = #{id,jdbcType=INTEGER}
  
  
    update user
    set username = #{username,jdbcType=VARCHAR},
      pword = #{pword,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER},
      utime = #{utime,jdbcType=DATE},
      ctime = #{ctime,jdbcType=DATE}
    where id = #{id,jdbcType=INTEGER}
  
  
    
    
    
    
    
    
  
  
    
      
        
          
            
              
                
                  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, username, pword, age, utime, ctime
  
  
  
  
    delete from user
    where id = #{id,jdbcType=INTEGER}
  
  
    delete from user
    
      
    
  
  
    insert into user (id, username, pword, 
      age, utime, ctime)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pword,jdbcType=VARCHAR}, 
      #{age,jdbcType=INTEGER}, #{utime,jdbcType=DATE}, #{ctime,jdbcType=DATE})
  
  
    insert into user
    
      
        id,
      
      
        username,
      
      
        pword,
      
      
        age,
      
      
        utime,
      
      
        ctime,
      
    
    
      
        #{id,jdbcType=INTEGER},
      
      
        #{username,jdbcType=VARCHAR},
      
      
        #{pword,jdbcType=VARCHAR},
      
      
        #{age,jdbcType=INTEGER},
      
      
        #{utime,jdbcType=DATE},
      
      
        #{ctime,jdbcType=DATE},
      
    
  
  
  
    update user
    
      
        id = #{record.id,jdbcType=INTEGER},
      
      
        username = #{record.username,jdbcType=VARCHAR},
      
      
        pword = #{record.pword,jdbcType=VARCHAR},
      
      
        age = #{record.age,jdbcType=INTEGER},
      
      
        utime = #{record.utime,jdbcType=DATE},
      
      
        ctime = #{record.ctime,jdbcType=DATE},
      
    
    
      
    
  
  
    update user
    set id = #{record.id,jdbcType=INTEGER},
      username = #{record.username,jdbcType=VARCHAR},
      pword = #{record.pword,jdbcType=VARCHAR},
      age = #{record.age,jdbcType=INTEGER},
      utime = #{record.utime,jdbcType=DATE},
      ctime = #{record.ctime,jdbcType=DATE}
    
      
    
  
  
    update user
    
      
        username = #{username,jdbcType=VARCHAR},
      
      
        pword = #{pword,jdbcType=VARCHAR},
      
      
        age = #{age,jdbcType=INTEGER},
      
      
        utime = #{utime,jdbcType=DATE},
      
      
        ctime = #{ctime,jdbcType=DATE},
      
    
    where id = #{id,jdbcType=INTEGER}
  
  
    update user
    set username = #{username,jdbcType=VARCHAR},
      pword = #{pword,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER},
      utime = #{utime,jdbcType=DATE},
      ctime = #{ctime,jdbcType=DATE}
    where id = #{id,jdbcType=INTEGER}
  
  
    
    
    
    
    
    
  
  
    
      
        
          
            
              
                
                  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, username, pword, age, utime, ctime
  
  
  
  
    delete from user
    where id = #{id,jdbcType=INTEGER}
  
  
    delete from user
    
      
    
  
  
    insert into user (id, username, pword, 
      age, utime, ctime)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pword,jdbcType=VARCHAR}, 
      #{age,jdbcType=INTEGER}, #{utime,jdbcType=DATE}, #{ctime,jdbcType=DATE})
  
  
    insert into user
    
      
        id,
      
      
        username,
      
      
        pword,
      
      
        age,
      
      
        utime,
      
      
        ctime,
      
    
    
      
        #{id,jdbcType=INTEGER},
      
      
        #{username,jdbcType=VARCHAR},
      
      
        #{pword,jdbcType=VARCHAR},
      
      
        #{age,jdbcType=INTEGER},
      
      
        #{utime,jdbcType=DATE},
      
      
        #{ctime,jdbcType=DATE},
      
    
  
  
  
    update user
    
      
        id = #{record.id,jdbcType=INTEGER},
      
      
        username = #{record.username,jdbcType=VARCHAR},
      
      
        pword = #{record.pword,jdbcType=VARCHAR},
      
      
        age = #{record.age,jdbcType=INTEGER},
      
      
        utime = #{record.utime,jdbcType=DATE},
      
      
        ctime = #{record.ctime,jdbcType=DATE},
      
    
    
      
    
  
  
    update user
    set id = #{record.id,jdbcType=INTEGER},
      username = #{record.username,jdbcType=VARCHAR},
      pword = #{record.pword,jdbcType=VARCHAR},
      age = #{record.age,jdbcType=INTEGER},
      utime = #{record.utime,jdbcType=DATE},
      ctime = #{record.ctime,jdbcType=DATE}
    
      
    
  
  
    update user
    
      
        username = #{username,jdbcType=VARCHAR},
      
      
        pword = #{pword,jdbcType=VARCHAR},
      
      
        age = #{age,jdbcType=INTEGER},
      
      
        utime = #{utime,jdbcType=DATE},
      
      
        ctime = #{ctime,jdbcType=DATE},
      
    
    where id = #{id,jdbcType=INTEGER}
  
  
    update user
    set username = #{username,jdbcType=VARCHAR},
      pword = #{pword,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER},
      utime = #{utime,jdbcType=DATE},
      ctime = #{ctime,jdbcType=DATE}
    where id = #{id,jdbcType=INTEGER}
  

UserMapper.java

package priv.gabriel.dao;

import java.util.List;
import org.apache.ibatis.annotations.Param;
import priv.gabriel.model.User;
import priv.gabriel.model.UserExample;

public interface UserMapper {
    long countByExample(UserExample example);

    int deleteByExample(UserExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    List selectByExample(UserExample example);

    User selectByPrimaryKey(Integer id);

    int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);

    int updateByExample(@Param("record") User record, @Param("example") UserExample example);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

可以看到已经帮我们生产了关于CRUD的绝大代码,再结合之前的通用mapper就可以解决绝大部分的问题啦

你可能感兴趣的:(第十四篇:SpringBoot2.x整合MyBatisGenerator)