weixin_43296748
API的Porter
2019-03-26 10:49

springBoot使用注解注入失败

  • spring

useMapper的xml文件

 <mapper namespace="com.auto.dao.UserMapper">
    <select id="findByName" parameterType="String" resultType="User">
        select * from t_user where username=#{username}
    </select>
 </mapper>

mapper接口代码

public interface UserMapper {
    public User findByName(String username);
}

springboot启动类的代码

@SpringBootApplication
@MapperScan("com.auto.dao")
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

pom.xml文件配置

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
  </parent>
  <groupId>com.auto</groupId>
  <artifactId>MavenDay01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
        <!-- spirngBoot启动器 -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <!-- jstl -->
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
    </dependency>

user的实体类

@Repository
public class User {
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + "]";
    }

}

这是properties的配置文件

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mysql
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.auto.entity  实体类和mapper.xml文件都在这包里面
server.port=9090

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

我写了个service接口,然后写了个实现类重写findByName方法返回userMapper中方法,在实现类中注入了userMapper接口,这里无法使用@Autowired注入

@Service
@Transactional
public class UserServiceImpl implements UserService{
    @Resource
    UserMapper userMapper;
    @Override
    public User findByName(String username) {

        return userMapper.findByName(username);
    }
}

报错

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.auto.dao.UserMapper.findByName

总是报找不到接口,加上注解标记也没用,换成在接口上用@mapper注解也不行,是什么问题,我在网上看说有什么依赖包冲突问题,但我换了几个版本也不行;

问题已经解决:
问题出在没有在properties文件中添加mapper.xml文件的扫描路径
解决办法:在properties文件中加上
mybatis.mapper-locations=com/.../.../*.xml 这里是mapper.xml文件的路径

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答