springboot多模块化整合mybatis,出现mapper自动注入失败问题
2018-07-27 09:12
836 查看
问题:
启动类添加@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
启动类:
[code]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; } }
启动类配置文件:
[code]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
启动类依赖
[code] <!--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类
[code]@Repository public interface UserMapper { String getTest(String test); }
DAO类依赖
[code]<!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency>
目录结构:
阅读更多
相关文章推荐
- spring mybatis 整合后mapper接口注入失败问题
- springMVC mybatis整合dao接口(mapper接口) spring注入失败
- 关于IDEA工具在springboot整合mybatis中出现的Invalid bound statement (not found)问题
- Springboot整合mybatis出现无法注入mapper接口的问题处理
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案 标签: spring 201
- Spring Boot整合mybatis并自动生成mapper和实体实例解析
- springMVC mybatis整合dao接口(mapper接口) spring注入失败
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案
- 在idea里使用SpringBoot整合MyBatis时遇到的Mapper扫描不到的问题
- SpringBoot+Mybatis整合报,service注入失败,dao注入失败,
- 关于spring boot整合mybatis使用oracle数据库出现could not load:oracle.jdbc.driver.OracleDriver问题的终极解决方案
- spring-boot集成mybatis问题1:IntellijIdea中j解决自动生成代码xml配置文件中出现“uri is not registered”问题
- springboot整合Mybatis扫描不到mapper问题
- springboot与mybatis整合dao层不能注入的问题
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案
- SpringBoot整合Mybatis扫描不到Mapper的问题
- Spring 整合 MyBatis,Mapper注入失败
- springboot整合mybatis mapper注入时显示could not autowire的解决
- Maven项目中,关于Spring Boot 整合MyBatis时,Service层无法找到mapper接口的问题解决
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案