开发者社区> 问答> 正文

Spring boot整合Mybatis 不能调用dao层代码? 400 报错

Spring boot整合Mybatis 不能调用dao层代码? 400 报错

我在使用Spring boot 整合Mybatis时,在junit中运行代码时,当运行到调用dao层的代码时就报错,报错信息如下:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.beBianMin.beBianMin.dao.LinkHouseDao.queryLinkHouseInfoByImgUrl
	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:227)
	at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:49)
	at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)
	at com.sun.proxy.$Proxy68.queryLinkHouseInfoByImgUrl(Unknown Source)
	at com.beBianMin.beBianMin.BeBianMinApplicationTests.contextLoads(BeBianMinApplicationTests.java:40)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

但是我配置都写了啊application.properties文件如下:

spring.datasource.url=jdbc:mysql://localhost:3306/beBianMin?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
# thymeleaf
spring:
  thymeleaf:
    prefix: classpath:/templates/
# logger
logging:
    config: classpath:logback.xml   
#mybatis
mybatis:
	#mapper 文件路径
	mapper-locations:classpath*:/mapper/**Mapper.xml

项目结构如下:

BeBianMinApplicationTests.java如下:

package com.beBianMin.beBianMin;

import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.beBianMin.beBianMin.dao.LinkHouseDao;
import com.beBianMin.beBianMin.entity.LinkHouseInfo;
import com.beBianMin.beBianMin.service.LianjiaService;


@RunWith(SpringRunner.class)
@SpringBootTest
@MapperScan(value = "com.beBianMin.beBianMin.dao")
// 这样才能找到mapper文件
public class BeBianMinApplicationTests {
	
	@Autowired
	private LianjiaService lianjiaService;

	@Autowired
	private LinkHouseDao linkHouseDao;
	
	@Test
	public void contextLoads() {
		System.out.println("heheh");
		LinkHouseInfo linkHouseInfo = new LinkHouseInfo();
		String url = "https://m.lianjia.com/bj/chengjiao/101102829123.html";
		try {
			lianjiaService.initLinkHouserMsg4ChengJiao(linkHouseInfo, url);
			Map<String, Object> resultMap = ReflexObjectUtil
					.getKeyAndValue(linkHouseInfo);
			System.out.println(resultMap);

			linkHouseDao.queryLinkHouseInfoByImgUrl("adsfds");
//			linkHouseDao.addLinkHouseInfo(linkHouseInfo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

LinkHouseDao内容如下:

package com.beBianMin.beBianMin.dao;

import org.apache.ibatis.annotations.Mapper;

import com.beBianMin.beBianMin.entity.LinkHouseInfo;
@Mapper
public interface LinkHouseDao {
	/**
	 * 根据大图URL查询房源信息
	 * @param imgUrl 大图url
	 * @return
	 */
	public LinkHouseInfo queryLinkHouseInfoByImgUrl(String imgUrl);

	/**
	 * 新增链家房源信息
	 * @param linkHouseInfo
	 */
	public void addLinkHouseInfo(LinkHouseInfo linkHouseInfo);

}

mapper文件如下:

<?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对应一个dao -->
<mapper namespace="com.beBianMin.beBianMin.dao.LinkHouseDao">
	<resultMap id="selecMap" type="com.beBianMin.beBianMin.entity.LinkHouseInfo">
		<id column="id" property="id" jdbcType="INTEGER" />
		<result column="houseUrl" property="houseUrl" jdbcType="VARCHAR" />
		<result column="houseDesc" property="houseDesc" jdbcType="VARCHAR" />
		<result column="imgUrl" property="imgUrl" jdbcType="VARCHAR" />
		<result column="startPrice" property="startPrice" jdbcType="VARCHAR" />
		<result column="finalPrice" property="finalPrice" jdbcType="VARCHAR" />
		<result column="dataSource" property="dataSource" jdbcType="VARCHAR" />
		<result column="sellDate" property="sellDate" jdbcType="VARCHAR" />
		<result column="oriented" property="oriented" jdbcType="VARCHAR" />
		<result column="houseUrl" property="houseUrl" jdbcType="VARCHAR" />
		<result column="floor" property="floor" jdbcType="VARCHAR" />
		<result column="buildType" property="buildType" jdbcType="VARCHAR" />
		<result column="elevator" property="elevator" jdbcType="VARCHAR" />
		<result column="decorationType" property="decorationType" jdbcType="VARCHAR" />
		<result column="buildingAge" property="buildingAge" jdbcType="VARCHAR" />
		<result column="houseNature" property="houseNature" jdbcType="VARCHAR" />
		<result column="houseType" property="houseType" jdbcType="VARCHAR" />
		<result column="housePlan" property="housePlan" jdbcType="VARCHAR" />
		<result column="communityName" property="communityName" jdbcType="VARCHAR" />
		<result column="budget" property="budget" jdbcType="VARCHAR" />
		<result column="currHouseType" property="currHouseType" jdbcType="VARCHAR" />
		<result column="avgPrice" property="avgPrice" jdbcType="VARCHAR" />
		<result column="createDate" property="createDate" jdbcType="VARCHAR" />
		<!-- <result column="categoryName" property="categoryName" />
		<association property="user" column="userid"
			select="com.blog.dao.UserDao.findUserById" /> -->
	</resultMap>
	
	<!-- 根据大图url查询房源信息 -->
	<select id="queryLinkHouseInfoByImgUrl" parameterType="string"
		resultType="com.beBianMin.beBianMin.entity.LinkHouseInfo">
		select *
		from
		bm_link_house_info
		where imgUrl = #{imgUrl}
	</select>

	<!-- 根据大图url新增房源信息 -->
	<insert id="addLinkHouseInfo" parameterType="com.beBianMin.beBianMin.entity.LinkHouseInfo"
		useGeneratedKeys="true" keyProperty="id">
		insert into
		bm_link_house_info(houseUrl,houseDesc,imgUrl,startPrice,finalPrice,dataSource,sellDate,oriented,
		floor,buildType,elevator,decorationType,buildingAge,houseNature,houseType,housePlan,communityName,budget,
		currHouseType,avgPrice,createDate)
		values(
		#{houseUrl},#{houseDesc},#{imgUrl},#{startPrice},#{finalPrice},#{dataSource},#{sellDate},#{oriented},
		#{floor},#{buildType},#{elevator},#{decorationType},#{buildingAge},#{houseNature},#{houseType},#{housePlan},#{communityName},#{budget},
		#{currHouseType},#{avgPrice},#{createDate}
		)
	</insert>


</mapper>

一开始我以为是dao没有注入进来(null),但是我在调试模式下看了下,dao不是null,已经被实例化了,但是在执行dao的代码时就一直报错。

急求各位大神到底出错在哪个地方了

原文链接:https://www.blog-china.cn/blog/liuzaiqingshan/home/38/1546272071137 

展开
收起
爱吃鱼的程序员 2020-06-04 15:20:43 757 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    可能pom里少了resource配置######回复 @! @#$RT : 现在有了######回复 @! @#$RT :问题找到了,是我的application.properties文件内容的格式不对,我写成了yml格式的配置了,改成properties格式内容就可以了######看一下target下有生成对应的文件吗######楼上说的对,你pom有问题,编译后,classpath下mapper被过滤了,你看看target下有没?没有改下pom文件就ok了

    2020-06-04 16:04:27
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题) 立即下载
Spring Cloud 微服务核心组件集 mica 的设计思路 立即下载
workshop专场-微服务专场-开发者动手实践营-微服务-Spring Cloud Alibaba 微服务全家桶体验 立即下载

相关实验场景

更多