springboot多模块化整合mybatis,mapper自动注入失败问题及解决

作者:Kevinten10 时间:2022-02-20 08:06:29 

springboot多模块化整合mybatis,mapper自动注入失败

问题

启动类添加@MapperScan或@ComponentScan,mapper类添加@Mapper或@Repository

==> Consider defining a bean of type 'com.ten.mapper.UserMapper' in your configuration.

Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required in spring mock mvc test for spring boot with mybatis

解决

手动装配dataSource

启动类:

package com.ten;
import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;

@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@MapperScan("com.ten.mapper")
class LcWebApplication {
   public static void main(String[] args) {
       SpringApplication.run(LcWebApplication.class, args);
   }

@Autowired
   private Environment env;

//destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用.
   @Bean
   public DataSource dataSource() {
       DruidDataSource dataSource = new DruidDataSource();
       dataSource.setUrl(env.getProperty("spring.datasource.url"));
       dataSource.setUsername(env.getProperty("spring.datasource.username"));//用户名
       dataSource.setPassword(env.getProperty("spring.datasource.password"));//密码
       dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
       dataSource.setInitialSize(2);//初始化时建立物理连接的个数
       dataSource.setMaxActive(20);//最大连接池数量
       dataSource.setMinIdle(0);//最小连接池数量
       dataSource.setMaxWait(60000);//获取连接时最大等待时间,单位毫秒。
       dataSource.setValidationQuery("SELECT 1");//用来检测连接是否有效的sql
       dataSource.setTestOnBorrow(false);//申请连接时执行validationQuery检测连接是否有效
       dataSource.setTestWhileIdle(true);//建议配置为true,不影响性能,并且保证安全性。
       dataSource.setPoolPreparedStatements(false);//是否缓存preparedStatement,也就是PSCache
       return dataSource;
   }
}

启动类配置文件:

spring:
   datasource:
       name: test
       url: jdbc:mysql://localhost:3306/db
       username: root
       password: root
       # 使用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

mybatis:
 mapperLocations: classpath*:mapper/*.xml
 typeAliasesPackage: com.ten.entity

启动类依赖

<!--web-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <!--rest-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-data-rest</artifactId>
       </dependency>
       <!-- mybatis -->
       <dependency>
           <groupId>org.mybatis.spring.boot</groupId>
           <artifactId>mybatis-spring-boot-starter</artifactId>
       </dependency>
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
       </dependency>
       <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>druid</artifactId>
       </dependency>

DAO类

@Repository
public interface UserMapper {
   String getTest(String test);
}

DAO类依赖

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

目录结构:

springboot多模块化整合mybatis,mapper自动注入失败问题及解决

springboot mapper注入失败的一种原因

今天启动项目报错----mapper注入失败。细细查找一番发现是时间类型的问题。

具体情况是

数据库有个字段的类型是datetime,但是实体类里的类型我写成了LocalDateTime,结果当然是jdbctype对不上,导致mapper注入不进去。

解决办法

实体类型定义成Date。

LocalDateTime其实是一种时间转换工具,不要定义为实体的类型。 实体类是时间的话,类型一般是Date或者timestamp。 

来源:https://blog.csdn.net/wsh596823919/article/details/81231187

标签:springboot,mybatis,mapper,自动注入
0
投稿

猜你喜欢

  • java 读取本地文件实例详解

    2023-08-12 20:41:32
  • C#实现Base64处理的加密解密,编码解码示例

    2023-07-15 12:11:31
  • 详解关于SpringBoot的外部化配置使用记录

    2023-08-10 03:54:54
  • RocketMQ4.5.2 修改mqnamesrv 和 mqbroker的日志路径操作

    2023-11-28 14:03:18
  • Java并发编程预防死锁过程详解

    2023-11-09 15:33:58
  • 微信第三方登录Android实现代码

    2023-07-27 08:05:49
  • Spring Cloud Ribbon配置详解

    2023-11-25 01:32:50
  • Java对象类型的判断详解

    2023-07-26 09:55:07
  • Android编程实现WebView添加进度条的方法

    2023-07-06 03:16:46
  • IP查询系统的异步回调案例

    2023-11-10 18:22:24
  • 基于idea 的 Java中的get/set方法之优雅的写法

    2023-11-26 20:22:50
  • Hibernate一级缓存和二级缓存详解

    2023-11-16 11:58:11
  • java实现新浪微博Oauth接口发送图片和文字的方法

    2023-11-29 01:43:04
  • java实现哈弗曼编码与反编码实例分享(哈弗曼算法)

    2023-11-25 04:54:05
  • 解决Eclipse创建android项目无法正常预览布局文件问题的方法

    2023-07-31 09:51:12
  • 详细解读JAVA多线程实现的三种方式

    2022-01-14 04:35:31
  • 解决grails服务端口冲突的办法(grails修改端口号)

    2023-09-12 01:00:03
  • Java concurrency线程池之线程池原理(二)_动力节点Java学院整理

    2023-11-28 23:43:18
  • Spring注解之@Lazy注解使用解析

    2023-08-28 23:12:23
  • spring webflux自定义netty 参数解析

    2023-07-26 18:38:25
  • asp之家 软件编程 m.aspxhome.com