温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot2 多模块项目中mybatis如何使用

发布时间:2021-07-08 16:33:35 来源:亿速云 阅读:314 作者:Leah 栏目:大数据

这篇文章将为大家详细讲解有关springboot2 多模块项目中mybatis如何使用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

首先创建多模块项目让项目成功运行并跑起来

可以参考文章创建多模块项目 Go!!!

项目分3个子模块分别是,父级megatron

  1. megatron-api
    项目启动类

    package com.megatron.module;
    
    import com.megatron.utils.IPUtils;
    import org.mybatis.spring.annotation.MapperScan;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @MapperScan("com.megatron.module.dal.mapper") //扫描指定包中的接口
    public class MegatronLogApiApplication {
    	public static Logger logger = LoggerFactory.getLogger(MegatronLogApiApplication.class);
    	public static void main(String[] args) {
    		System.setProperty("local-ip", IPUtils.getLocalIp());
    		SpringApplication.run(MegatronLogApiApplication.class, args);
    	}
    
    }


    application.yml

    spring:
      datasource:
        name: mysql
        type: com.alibaba.druid.pool.DruidDataSource
        druid:
          filter: stat
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://127.0.0.1:3306/megatron?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
          username: root
          password:
          #配置初始化大小/最小/最大
          initial-size: 1
          min-idle: 1
          max-active: 20
          #获取连接等待超时时间
          max-wait: 60000
          #间隔多久进行一次检测,检测需要关闭的空闲连接
          time-between-eviction-runs-millis: 60000
          #一个连接在池中最小生存的时间
          min-evictable-idle-time-millis: 300000
          validation-query: SELECT 'x'
          test-while-idle: true
          test-on-borrow: false
          test-on-return: false
          #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
          pool-prepared-statements: false
          max-pool-prepared-statement-per-connection-size: 20
    logging:
      config: classpath:log4j2-test.yml
    mybatis:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.megatron.module.dal.entity



     

  2. megatron-impl
    mybatis需要用到的pom

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.1</version>
        <scope>compile</scope>
    </dependency>
    
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    
    <!-- 分页插件 -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.5</version>
    </dependency>
    <!-- alibaba的druid数据库连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.9</version>
    </dependency>

    springboot2 多模块项目中mybatis如何使用
    红框内容是通过mybatis插件生成代码参考插件Go!!!

  3. 最后可以看到结果可以正常输出
    springboot2 多模块项目中mybatis如何使用
     

关于springboot2 多模块项目中mybatis如何使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI