刚接触 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); }
主要的代码如上, 请教一下各位前辈,是不是哪里配置出错了?还是我写错了? 多谢前辈指导..
返回值类型不正确。
自己解决. 其实就是 resultMap 和 resultType... 初学者真是大意