Springboot通用mapper和mybatis-generator代码示例

实现功能:根据数据库中的表,自动生成model、dao和对应的xml文件。xml中实现通用mapper中CURD功能

1、引入依赖

  
    1.3.7
    4.1.5
  

  
    
    
      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      2.1.3
    
    
    
      tk.mybatis
      mapper
      ${tk.mybatis.version}
    
    
    
      org.mybatis.generator
      mybatis-generator-core
      ${mybatis.generator.version}
    

   
   
      mysql
      mysql-connector-java
    
      
    
      com.alibaba
      druid-spring-boot-starter
      1.1.10
    
  

   
    

      
      
      
        org.mybatis.generator
        mybatis-generator-maven-plugin
        ${mybatis.generator.version}
        
          
          true
          
          true
        
        
          
            tk.mybatis
            mapper
            ${tk.mybatis.version}
          
        
      

    
  

2、配置通用mapper

方式1:配置要扫描的mapper

@SpringBootApplication
@MapperScan(basePackages = "com.liang.mapper")
public class MyappApplication {
  public static void main(String[] args) {
    SpringApplication.run(MyappApplication.class, args);
  }

}

方式2:扫描的mapper

@Configuration
public class MybatisConfig {
  @Bean(name = "mapperScannerConfigurer")
  public MapperScannerConfigurer MapperScannerConfigurer1() {
    MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
    mapperScannerConfigurer.setBasePackage("com.liang.mapper");
    Properties properties = new Properties();
    properties.setProperty("notEmpty", "false");
    properties.setProperty("IDENTITY", "MYSQL");
    properties.setProperty("mappers", Mapper.class.getName());
    mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
    mapperScannerConfigurer.setProperties(properties);
    return mapperScannerConfigurer;
  }
}

3、application.yaml中配置mybatis

#Mybatis(注意不是 mybatis.config-location 这个配置)
mybatis:
 mapper-locations: classpath:mapper/*.xml
 type-aliases-package: com.liang.pojo
 configuration-properties: {
  notEmpty: false,
  IDENTITY: 'MYSQL',
  mappers: "tk.mybatis.mapper.common.Mapper"
 }

4、添加generatorConfig.xml





  
  

  

    
    
    

    
    

    
    
      
    

    
    

    
    
      
    

    
    
      
      
    

    
    
      
    

    
    
      
    

    
    
    
    

5、完成

Springboot通用mapper和mybatis-generator代码示例_第1张图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Springboot通用mapper和mybatis-generator代码示例)