求指教:Property 'mapperLocations' was not specified or no matching resources found
剛接觸 ssm 框架, 然后今天在看了老師的視頻之后,自己動手跑單元測試的時候出現:
Property 'mapperLocations' was not specified or no matching resources found
下面是幾個主要的配置文件
spring-dao.xml
<?xml?version="1.0"?encoding="UTF-8"?>
<beans?xmlns="http://www.springframework.org/schema/beans"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
???????xmlns:context="http://www.springframework.org/schema/context"
???????xmlns:aop="http://www.springframework.org/schema/aop"
???????xsi:schemaLocation="http://www.springframework.org/schema/beans
???http://www.springframework.org/schema/beans/spring-beans.xsd
???????http://www.springframework.org/schema/context
???????http://www.springframework.org/schema/context/spring-context.xsd
???????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop.xsd">
???????
<!--?配置整合?mybatis?過程?-->
<!--?配置數據庫相關參數properties的屬性:${url}?-->
<context:property-placeholder?location="classpath:jdbc.properties"/>
<!--?數據庫連接池?-->
<bean?id="dataSource"?class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--?配置連接池屬性?-->
<property?name="driverClass"?value="${driver}"></property>
<property?name="jdbcUrl"?value="${url}"></property>
<property?name="user"?value="${username}"></property>
<property?name="password"?value="${password}"></property>
<!--?c3p0連接池的私有屬性?-->
<property?name="maxPoolSize"?value="30"></property>
<property?name="minPoolSize"?value="10"></property>
<!--?關閉連接后不自動?commit?-->
<property?name="autoCommitOnClose"?value="false"></property>
<!--?獲取連接超時時間?-->
<property?name="checkoutTimeout"?value="1000"></property>
<!--?當獲取連接失敗重試次數?-->
<property?name="acquireRetryAttempts"?value="2"></property>
</bean>
<!--?約定大于配置?-->
<!--?配置?sqlSessionFactory?對象?-->
<bean?id="sqlSessionFactory"?class="org.mybatis.spring.SqlSessionFactoryBean">
<!--?注入數據庫連接池?-->
<property?name="dataSource"?ref="dataSource"></property>
<!--?配置?mybatis?全局配置文件?mybatis-config.xml?-->
<property?name="configLocation"?value="classpath:mybatis-config.xml"></property>
<!--?掃描?entity?包,使用別名?com.chenzhijun.top.entity.Seckill->Seckill?-->
<property?name="typeAliasesPackage"?value="com.chenzhijun.top.entity.Seckill;com.chenzhijun.top.entity.SuccessKilled"></property>
<!--?掃描?sql?配置文件:?mapper?需要的?xml?文件?-->
<property?name="mapperLocations"?value="classpath:mapper/*.xml"></property>
</bean>
<!--?配置掃描?Dao?接口包,動態實現?Dao?接口并注入到?spring?容器中?-->
<bean?class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--?注入?sqlSessionFactory?-->
<property?name="sqlSessionFactoryBeanName"?value="sqlSessionFactory"></property><!--?防止提前使用?SQLSessionFactory?可能?jdbc?的配置文件未加載-->
<!--?<property?name="annotationClass"?value="org.springframework.stereotype.Repository"/>?-->
<!--?給出需要掃描的?dao?接口包?-->
<property?name="basePackage"?value="com.chenzhijun.top.dao"></property>
</bean>
</beans>mybatis-config.xml
<?xml?version="1.0"?encoding="UTF-8"?> <!DOCTYPE?configuration?PUBLIC?"-//mybatis.org//DTD?Config?3.0//EN"?"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--?configure?global?attribute?--> <settings> <!--?use?jdbc?getGeneratedKeys?to?get?the?database?primary?key?auto_increment--> <setting?name="useGeneratedKeys"?value="true"/> <!--?使用列表名替換列名,默認為?true?--> <setting?name="useColumnLabel"?value="true"/> <!--?開啟駝峰命名轉換:?Table(create_time)->Entity(createTiem)?--> <setting?name="mapUnderscoreToCamelCase"?value="true"/> </settings> </configuration>
SeckillDao.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.chenzhijun.top.dao.SeckillDao">
<update?id="reduceNumber">
<!--?具體?sql?-->
update
seckill
set
number=number-1
where
seckilled_id?=?#{seckilled}?
and?start_time?<![CDATA[?<=?]]>?#{killTime}
and?end_time?>=?#{killTiem}
and?number>0;
</update>
<select?id="queryById"?resultMap="Seckill"?parameterType="long"><!--?多個參數可以不給?parameterType?-->
<!--?select?seckill_id?as?seckillId?-->
select?seckill_id,name,number,start_time,end_time,create_time
from?seckill
where?seckill_id=#{seckillID}
</select>
<select?id="queryAll"?resultMap="Seckill">
select?seckill_id,name,number,start_time,end_time,create_time
from?seckill
order?by?create_time?desc
limit?#{offset},#{limit}
</select>
</mapper>SeckillTest.java
package?com.chenzhijun.top.test;
import?javax.annotation.Resource;
import?org.junit.Test;
import?org.junit.runner.RunWith;
import?org.springframework.beans.factory.annotation.Autowired;
import?org.springframework.test.context.ContextConfiguration;
import?org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import?com.chenzhijun.top.dao.SeckillDao;
import?com.chenzhijun.top.dao.SuccessKilledDao;
import?com.chenzhijun.top.entity.Seckill;
import?com.chenzhijun.top.entity.SuccessKilled;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:spring/spring-dao.xml")
public?class?SeckillDaoTest?{
@Resource
private?SeckillDao?seckillDao;
@Test
public?void?testQueryById()?{
long?id=1000L;
Seckill?seckill=seckillDao.queryById(id);
System.out.println(seckill.getName());
System.out.println(seckill.toString());
}
}SeckillDao.java
package?com.chenzhijun.top.dao;
import?java.sql.Date;
import?java.util.List;
import?org.springframework.stereotype.Repository;
import?com.chenzhijun.top.entity.Seckill;
public?interface?SeckillDao?{
/**
?*?減庫存
?*?@param?seckillId
?*?@param?killTime?
?*?@return??影響行數>1,表示更新的記錄數
?*/
int?reduceNumber(long?seckillId,Date?killTime);
/**
?*?根據id查詢秒殺對象
?*?@param?seckillId
?*?@return
?*/
Seckill?queryById(long?seckillId);
/**
?*?根據偏移量查詢秒殺商品列表
?*?@param?offet
?*?@param?limit
?*?@return
?*/
List<Seckill>?queryAll(int?offet,int?limit);
}主要的代碼如上, 請教一下各位前輩,是不是哪里配置出錯了?還是我寫錯了? ?多謝前輩指導..
2016-09-28
返回值類型不正確。
2016-08-14
自己解決. 其實就是 resultMap 和 resultType... ? 初學者真是大意