首页 新闻 会员 周边

关于Mybatis和spring整合的时候测试单元测试出现的问题。

0
悬赏园豆:50 [已解决问题] 解决于 2018-12-05 12:19

首先贴出出现异常的日志:如下

spring的配置如下

<?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"
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">
<!-- 配置整合mybatis过程 -->
<!-- 1.配置数据库相关参数 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- properties的属性:#{url} -->

<!-- 2.数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 配置连接池属性 -->
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.pass}"></property>
        <!-- c3p0连接池的私有属性  -->
        
        <!-- 在小的项目默认是私有属性就已经够用 -->
        <property name="maxPoolSize" value="30"></property>
        <!-- 默认是15 -->
        <property name="minPoolSize" value="10"></property>
        <!-- 默认是3 -->
        <property name="autoCommitOnClose" value="false"></property>
        <!-- 关闭不自动commit 默认值是false -->
        <property name="checkoutTimeout" value="1000"></property>
        
        <property name="acquireRetryAttempts" value="2"></property>
        <!-- 当获取连接失败重试的次数 -->
        
</bean>

<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置mybatis全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
        <!-- 扫描entity包,使用别名 -->
        <property name="typeAliasesPackage" value="org.seckill.entity"></property>
        <!-- 扫描sql配置文件:mapper需要的Xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>         
</bean>

<!-- 4.配置扫描DAO接口包,目的是动态实现Dao接口并注入到Spring容器中 -->
 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!-- 有可能开始的时候拿到的JDBC的数据还没有初始化,采用BeanName的方式后处理。 -->
        
        <property name="basePackage" value="org.seckill.dao"></property>
</bean>

</beans>

mybatis配置如下:

<?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>
<!-- 配置myBatis全局的属性 -->
<settings>
<!-- 默认是false:底层都是使用JDBC,意思是使用JDBC的getGeneratedKeys获得数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true"></setting>

        <!-- 使用列名替换列名 默认是:true 
            select name as title from table;
        -->
        <setting name="useColumnLabel" value="true"></setting>
        <!-- 开启驼峰命名转换:  -->
        <setting name="mapUnderscoreToCamelCase" value="true"></setting>
    </settings>

</configuration>

SeckillDao的mybatis配置如下:

<?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="org.seckill.dao.SeckillDao">
<!-- 目的:为DAO提供sql语句配置,所以第一个就是命名空间 -->

<update id="reduceNumber">
        update
            seckill
        set
            number=number-1
        where seckill_id = #{seckillId}
        and start_time <![CDATA[<= ]]>#{killTime}
        and end_time >= #{killTime}
        and number>0;
</update>

<select id="queryById" resultType="SecKill" parameterType="long">
        select seckill_id,name,number,start_time,end_time,create_time
        from seckill
        where seckill_id=#{seckillId};
</select>

<select id="queryAll" resultType="SecKill">
        select seckill_id,name,number,start_time,end_time,create_time
        from seckill
        order by create_time desc
        limit #{offset},#{limit};

</select>

</mapper>

接口SeckillDao

package org.seckill.dao;

import java.sql.Date;
import java.util.List;

import org.seckill.entity.SecKill;

public interface SeckillDao {

/*
 * 减库存函数
 * 如果返回的数字>1,表示影响的行数。,返回0更新语句可能没有成功
 */

public int reduceNumber(long seckillId,Date killTime);

/*
 * 查询接口
 */
public SecKill queryById(long seckillId);

/*
 * 根据偏移量查询秒杀商品列表
 */
public List<SecKill> queryAll(int offset,int limit);

}

Seckill的Entity

public class SecKill {
private long seckillId;

private String name;

private int number;

private Date startTime;

private Date endTime;

private Date createTime;

.......................
get和set方法等
.........................

}

测试类:
/*

  • 配置spring和Junit整合,Junit启动时加载springIOc
  • SPring-test,jUnit
    */
    @RunWith(SpringJUnit4ClassRunner.class)
    //告诉Junit Spring配置文件
    @ContextConfiguration({"classpath:spring/springDao-config.xml"})
    public class SeckillDaoTest {

    //注入Dao依赖

    @Resource
    private SeckillDao seckillDao;

    @Test
    public void testQueryById() {
    long id=1000;

     SecKill secKill=seckillDao.queryById(id);
     System.out.println(secKill.getName());
     System.out.println(secKill);
     //fail("Not yet implemented");

    }
    }

轻抚丶两袖风尘的主页 轻抚丶两袖风尘 | 初学一级 | 园豆:59
提问于:2018-12-05 11:50
< >
分享
最佳答案
0

是缺少jboss依赖,在pom配置jbooss就可以了。如下

<!-- https://mvnrepository.com/artifact/org.jboss/jboss-vfs -->
    <dependency>
        <groupId>org.jboss</groupId>
        <artifactId>jboss-vfs</artifactId>
        <version>3.2.11.Final</version>
    </dependency>
轻抚丶两袖风尘 | 初学一级 |园豆:59 | 2018-12-05 12:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册