问题:SpringBoot整合myBatis时报错:找不到bean
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-04-09 15:01:59.078 ERROR 7664 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean of type 'com.example.demo.mapper.UserMapper' that could not be found.
Action:
Consider defining a bean of type 'com.example.demo.mapper.UserMapper' in your configuration.
Process finished with exit code 0
目录结构:
Controller代码:
package com.example.demo.controller;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
public class UserController {
@Resource
private UserService userService;
@ResponseBody
@RequestMapping("/getUserInfo")
public String getUserInfo(Long userid){
return userService.getUserInfo(userid);
}
}
Service代码:
package com.example.demo.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public String getUserInfo(Long userid) {
List<Map> userInfoList = userMapper.selectUser(userid);
return JSONArray.toJSONString(userInfoList);
}
}
Mpper代码:
package com.example.demo.mapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
import java.util.Map;
@Mapper
public interface UserMapper {
List<Map> selectUser(Long userid);
}
xml代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.mapper.UserMapper" >
<select id="selectUser" resultType="java.util.Map" >
select
USERID,REGTEL
from user USER
where USERID = #{userid,jdbcType=BIGINT}
</select>
</mapper>
application.properties代码:
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
spring.datasource.url = jdbc:mysql://localhost:3307/dbo?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
#端口
server.port=8888
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
我的解决方法 :因为报错为找不到名为UserMapper的bean,所以感觉是UserMapper里的问题。在网上找了一些方法,都没解决这个问题,偶尔看到有人的Mapper里是添加了注解“@Mapper”的,尝试添加上,运行成功,问题解决。
之后又查了注解“@Mapper”,发现只需要在启动类“Application”加上注解
“@MapperScan(basePackages={"com.example.demo.mapper"})”就可以扫描到mapper包了
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = {"com.example.demo.mapper"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}