zoukankan      html  css  js  c++  java
  • mybatis的dao注入失败

    转载自https://blog.csdn.net/hanpenghu/article/details/83897618

    springboot maven资源路径配置 resource路径配置, 解决mybatis的xml放在java路径而没有放在resource路径下报错的问题

    我是这样配置的

    1.  
      <build>
    2.  
      <!--resources配置解决mybatis 的mapperXml配置在java路径不被扫描的问题 -->
    3.  
      <resources>
    4.  
      <resource>
    5.  
      <directory>src/main/java</directory>
    6.  
      </resource>
    7.  
      <resource>
    8.  
      <directory>src/main/resources</directory>
    9.  
      </resource>
    10.  
      </resources>
    11.  
      <plugins>
    12.  
      <plugin>
    13.  
      <groupId>org.springframework.boot</groupId>
    14.  
      <artifactId>spring-boot-maven-plugin</artifactId>
    15.  
      </plugin>
    16.  
      <!--跳过测试 -->
    17.  
      <plugin>
    18.  
      <groupId>org.apache.maven.plugins</groupId>
    19.  
      <artifactId>maven-surefire-plugin</artifactId>
    20.  
      <configuration>
    21.  
      <skipTests>true</skipTests>
    22.  
      </configuration>
    23.  
      </plugin>
    24.  
      </plugins>
    25.  
      </build>
    26.  
       
    27.  
       
    28.  
      </project>

    然后在资源文件 application.properites里面配置mybatis的xml路径

    1.  
      server.port=8081
    2.  
       
    3.  
      #必须有
    4.  
      mybatis.mapper-locations=classpath*:com/hanhan/dao/xmlMapper/*.xml
    5.  
      #必须有
    6.  
      logging.config=classpath:logback.xml
    7.  
      #必须有
    8.  
      spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    9.  
      #必须有
    10.  
      spring.datasource.url=jdbc:mysql://127.0.0.1:3307/ipace?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
    11.  
      #必须有
    12.  
      spring.datasource.username=root
    13.  
      #必须用
    14.  
      spring.datasource.password=root
    15.  
       
    16.  
       
    17.  
      #32位随机字符串
    18.  
      #dd9008e5ec8608c9eb9d57ab764651eb
    19.  
      rand.str = ${random.value}
    20.  
      #36位,比32位的多了4个-
    21.  
      #0cf88531-9569-44c4-926e-2528dd5948e2
    22.  
      rand.uuid = ${random.uuid}
    23.  
      #随机int类型,主意有负数
    24.  
      rand.intid = ${random.int}
    25.  
      #随机long类型
    26.  
      rand.longid = ${random.long}
    27.  
      #100以内的随机int类型
    28.  
      rand.number = ${random.int(100)}
    29.  
      #0-10亿范围内的随机int类型
    30.  
      rand.range = ${random.int[0,1000000000]}
    31.  
       

    java的接口mapper路径扫描在启动类上配置 

    1.  
      import org.mybatis.spring.annotation.MapperScan;
    2.  
      import org.springframework.boot.SpringApplication;
    3.  
      import org.springframework.boot.autoconfigure.SpringBootApplication;
    4.  
      import org.springframework.scheduling.annotation.EnableScheduling;
    5.  
       
    6.  
      @SpringBootApplication(scanBasePackages = {"com.hanhan","hanhan"})
    7.  
      @MapperScan({"com.hanhan.dao"})
    8.  
      @EnableScheduling
    9.  
      public class BeetltestApplication {
    10.  
       
    11.  
       
    12.  
      public static void main(String[] args) {
    13.  
      SpringApplication.run(BeetltestApplication.class, args);
    14.  
      }
    15.  

    贴完别人的  说下我的问题,

    首先是引入springboot集成的mybatis的jar包

    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
    </dependency>

    其次是applicatioin的配置文件配置数据库的连接等

    spring:
      datasource:
        name: feibi
        url: jdbc:mysql://****/i***
        username: ***
        password: ***
        # 使用druid数据源
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        maxActive: 20
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20

    applicatioin里还要配置mybatis的扫描xml文件位置

    mybatis:
      mapper-locations: classpath:mybatis/*.xml
      config-location: classpath:conf/mybatis-config.xml
      check-config-location: true

    最后是启动类需要加入mapperscan

    @MapperScan("com.*.*.dao")
  • 相关阅读:
    【总结】机器学习里需要知道的12堂课
    【转载】The Mathematics Of Beauty
    Facebook的朋友推荐系统
    【转载】Netflix: System Architectures for Personalization and Recommendation
    aDev第13期#个性化推荐技术#总结(Part II:江申@百度)
    mahout 0.7关于随机选择数据的一个bug
    Dirichlet Process 和 Dirichlet Process Mixture模型
    aDev第13期#个性化推荐技术#总结(Part I:袁全@一淘)
    httpHandler实现.Net无后缀名Web访问
    还在使用Application_Start做定时器的,你们伤不起!
  • 原文地址:https://www.cnblogs.com/heroinss/p/9984473.html
Copyright © 2011-2022 走看看