总结:mybatis-generator两种使用方式pom-plugin、main方法

1. 使用Maven plugin生成

  • pom.xml


    4.0.0

    cn.xiaows
    app
    0.0.1-SNAPSHOT
    app

    
        1.8
    

    
        
        
        
            tk.mybatis
            mapper-spring-boot-starter
            1.2.4
        
    

    
        
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.4
                
                    ${basedir}/src/main/resources/mybatis-generator.xml
                    true
                    true
                
                
                    
                        mysql
                        mysql-connector-java
                        5.1.46
                    
                    
                    
                        tk.mybatis
                        mapper-generator
                        1.0.0
                    
                
            
        
    



  • src/main/resources/mybatis-generator.xml




    
        
        

        

        

        
        
        
        
        
        

        
  • 生成命令:
    mybatis-generator:generate -f pom.xml

2. 使用main方法生成

  • pom.xml


    4.0.0
    cn.xiaows
    app
    0.0.1-SNAPSHOT
    app

    
        1.8
    

    
        
        
            mysql
            mysql-connector-java
            5.1.46
        

        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.4
            compile
            true
        
        
        
        
            tk.mybatis
            mapper-spring-boot-starter
            1.2.4
        
    



  • MybatisGeneratorMain.java
package cn.xiaows.app.util;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MybatisGeneratorMain {

    public static void main(String[] args) throws Exception {
        List warnings = new ArrayList<>();
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(new File("app-logs-web/src/main/resources/mybatis-generator.xml"));
        // Configuration config = cp.parseConfiguration(ClassLoader.getSystemResourceAsStream("mybatis-generator.xml"));
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        for (String warning : warnings) {
            System.out.println(warning);
        }
    }
}


  • mybatis-generator.xml




    
        
        

        

        
        

        
        
        
        
        
        

        

  • MyMapper.java
package cn.xiaows.app.util;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

/**
 * 继承自己的MyMapper
 * 特别注意,该接口不能被扫描到,否则会出错
 */
@Deprecated
public interface MyMapper extends Mapper, MySqlMapper {

}

  • UserMapper.java
package cn.xiaows.app.dao;

import cn.xiaows.app.entity.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 Sat Jan 05 19:19:00 CST 2019
     */
    int deleteByPrimaryKey(Integer id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user
     *
     * @mbg.generated Sat Jan 05 19:19:00 CST 2019
     */
    int insert(User record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user
     *
     * @mbg.generated Sat Jan 05 19:19:00 CST 2019
     */
    User selectByPrimaryKey(Integer id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user
     *
     * @mbg.generated Sat Jan 05 19:19:00 CST 2019
     */
    List selectAll();

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user
     *
     * @mbg.generated Sat Jan 05 19:19:00 CST 2019
     */
    int updateByPrimaryKey(User record);
}

  • UserMapper.xml



  
    
    
    
    
  
  
    
    delete from user
    where id = #{id,jdbcType=INTEGER}
  
  
    
    insert into user (id, name, age
      )
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}
      )
  
  
    
    update user
    set name = #{name,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  
  
  


  • springboot启动类:AppLogApplication.java
package cn.xiaows.app;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// import tk.mybatis.spring.annotation.MapperScan;// 不推荐使用 容易搞混!!!

/**
    如果dao.UserMapper extends MyMapper
    dao.UserMapper就不需要添加@org.apache.ibatis.annotations.Mapper注解
    就必须添加@tk.mybatis.spring.annotation.MapperScan("cn.xiaows.app.dao")该注解

    如果dao.UserMapper没有添加Mapper注解
    则这里需要添加MapperScan("cn.xiaows.app.dao")
 */
@SpringBootApplication
@MapperScan("cn.xiaows.app.dao")
public class AppLogApplication {

    public static void main(String[] args) {
        SpringApplication.run(AppLogsWebApplication.class, args);
    }

}

你可能感兴趣的:(总结:mybatis-generator两种使用方式pom-plugin、main方法)