Spring Boot 整合Mybatis 之 Mapper插件(自动生成XML及Mapper代码)

pom文件 主要代码

 


        
            mysql
            mysql-connector-java
        

        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.9
        


 


    
        


 
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.5
                
                    
                    
                        mysql
                        mysql-connector-java
                        5.1.47
                    
                    
                        tk.mybatis
                        mapper
                        3.4.4
                    
                
                
                    
                        Generate MyBatis Artifacts
                        package
                        
                            generate
                        
                    
                
                
                    
                    true
                    
                    true
                    
                    src/main/resources/mybatis-generator/mybatis-generator.xml
                
            


        
    

 

 

mybatis-generator.xml(resources目录下面   在pom文件中指定)

 





    
    
    
    
    

    
    
    


        
        
        
        
        
        
        

        
        
        
        
        

        

        
        
        
        
        
        

        
        
            
             
        

        
        
        
        
            
            
        

        
        
            
            
        
        
        
            
        
        
        
            
        
        
        
        
            
            
        

实体类 User.java(生成代码前)

package com.wangs.model;

import java.io.Serializable;

public class User implements Serializable {

    private String id;

    private String name;

    private String password;

    private String mobile;

    private Integer age;

    private String dis;

    private static final long serialVersionUID = 1L;

}

指向生成代码命令:mvn mybatis-generator:generate 

IDEA工具可使用

Spring Boot 整合Mybatis 之 Mapper插件(自动生成XML及Mapper代码)_第1张图片

执行成功:

Spring Boot 整合Mybatis 之 Mapper插件(自动生成XML及Mapper代码)_第2张图片

以下为生成代码:

代码结构:

Spring Boot 整合Mybatis 之 Mapper插件(自动生成XML及Mapper代码)_第3张图片

UserMapper.java
package com.wangs.mapper;

import com.wangs.model.User;
import java.util.List;

public interface UserMapper {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user
     *
     * @mbg.generated
     */
    int deleteByPrimaryKey(String id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user
     *
     * @mbg.generated
     */
    int insert(User record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user
     *
     * @mbg.generated
     */
    User selectByPrimaryKey(String id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user
     *
     * @mbg.generated
     */
    List selectAll();

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user
     *
     * @mbg.generated
     */
    int updateByPrimaryKey(User record);
}

 

User.java

package com.wangs.model;

import java.io.Serializable;

public class User implements Serializable {
    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column user.id
     *
     * @mbg.generated
     */
    private String id;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column user.name
     *
     * @mbg.generated
     */
    private String name;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column user.password
     *
     * @mbg.generated
     */
    private String password;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column user.mobile
     *
     * @mbg.generated
     */
    private String mobile;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column user.age
     *
     * @mbg.generated
     */
    private Integer age;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column user.dis
     *
     * @mbg.generated
     */
    private String dis;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database table user
     *
     * @mbg.generated
     */
    private static final long serialVersionUID = 1L;

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column user.id
     *
     * @return the value of user.id
     *
     * @mbg.generated
     */
    public String getId() {
        return id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column user.id
     *
     * @param id the value for user.id
     *
     * @mbg.generated
     */
    public void setId(String id) {
        this.id = id == null ? null : id.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column user.name
     *
     * @return the value of user.name
     *
     * @mbg.generated
     */
    public String getName() {
        return name;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column user.name
     *
     * @param name the value for user.name
     *
     * @mbg.generated
     */
    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column user.password
     *
     * @return the value of user.password
     *
     * @mbg.generated
     */
    public String getPassword() {
        return password;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column user.password
     *
     * @param password the value for user.password
     *
     * @mbg.generated
     */
    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column user.mobile
     *
     * @return the value of user.mobile
     *
     * @mbg.generated
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column user.mobile
     *
     * @param mobile the value for user.mobile
     *
     * @mbg.generated
     */
    public void setMobile(String mobile) {
        this.mobile = mobile == null ? null : mobile.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column user.age
     *
     * @return the value of user.age
     *
     * @mbg.generated
     */
    public Integer getAge() {
        return age;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column user.age
     *
     * @param age the value for user.age
     *
     * @mbg.generated
     */
    public void setAge(Integer age) {
        this.age = age;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column user.dis
     *
     * @return the value of user.dis
     *
     * @mbg.generated
     */
    public String getDis() {
        return dis;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column user.dis
     *
     * @param dis the value for user.dis
     *
     * @mbg.generated
     */
    public void setDis(String dis) {
        this.dis = dis == null ? null : dis.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user
     *
     * @mbg.generated
     */
    @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(", name=").append(name);
        sb.append(", password=").append(password);
        sb.append(", mobile=").append(mobile);
        sb.append(", age=").append(age);
        sb.append(", dis=").append(dis);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }
}

 

UserMapper.xml




  
    
    
    
    
    
    
    
  
  
    
    delete from user
    where id = #{id,jdbcType=VARCHAR}
  
  
    
    
      SELECT LAST_INSERT_ID()
    
    insert into user (`name`, `password`, mobile, 
      age, dis)
    values (#{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}, 
      #{age,jdbcType=INTEGER}, #{dis,jdbcType=VARCHAR})
  
  
    
    update user
    set `name` = #{name,jdbcType=VARCHAR},
      `password` = #{password,jdbcType=VARCHAR},
      mobile = #{mobile,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER},
      dis = #{dis,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  
  
  

 

你可能感兴趣的:(StringBoot)