栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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、完成

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/130037.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号