weixin_43296748
API的Porter
采纳率33.3%
2019-03-26 10:49 阅读 4.5k
已采纳

springBoot使用注解注入失败

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条回答 默认 最新

  • 已采纳
    qq_35929243 qq_35929243 2019-03-26 10:56

    是否可以提供更多的代码? 比如UserMapper.xml? 在mapper接口中是否使用@param 注解?如果没有使用, 可能会导致找不到绑定参数的异常
    如果使用的是idea 开发工具,请观察target 目录下的mapper 是否已经编译(mapper.xml是否存在) 如果没有编译, 请在pom 的build 中加入以下节点

    <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
    </resources>
    
    

    (这段依赖包含在build中)加入之后请重新Build

    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
            </resources>
        </build>
    
    
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources
    
    # mapper 实现类路径
    mybatis.mapper-locations=com/xdz/mybatis/mapper/impl/*.xml 
    
    # mapper对应entity配置
    mybatis.typeAliasesPackage=com.xdz.mybatis.entity   
    
    
    点赞 评论 复制链接分享
  • sinat_30356443 云锦の 2019-03-26 18:15

    useMapper.xml中resultType="User" 的User在xml中定义了吗?

    点赞 1 评论 复制链接分享

相关推荐