spring-mybatis整合的dao單元測試
阿新 • • 發佈:2017-11-13
app iba 不一致 映射 xxx perm destroy 3.1 那種
在做spring-mybatis整合項目時對Dao所對應的 XXX.xml進行單元測試。整合時是spring和mybatis的整合,所以mybatis.xml文件就沒有了,
對應的配置文件為spring-mybatis.xml文件,這是以spring配置文件的形式寫的。下圖是項目的結構,使用maven創建。
spring-mybatis.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 自動掃描service--> <context:component-scan base-package="cn.BBS.service" /> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:config/db.properties" /> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 指定連接數據庫的驅動 --> <property name="driverClass" value="${jdbc.driver}"/> <!-- 指定連接數據庫的URL --> <property name="jdbcUrl" value="${jdbc.url}"/> <!-- 指定連接數據庫的用戶名 --> <property name="user" value="${jdbc.username}"/> <!-- 指定連接數據庫的密碼 --> <property name="password" value="${jdbc.password}"/> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自動掃描mapping.xml文件--> <property name="mapperLocations" value="classpath:cn/BBS/mapper/*.xml"></property> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.BBS.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"> </property> </bean> </beans>
對應的db.properties文件如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/bbsdb?characterEncoding=UTF-8&
useUnicode=TRUE&autoReconnect=true
jdbc.username=root
jdbc.password=123456
Dao類,該類是個接口類 : AdminMapper.java如下
package cn.BBS.dao; import cn.BBS.po.Admin; import java.sql.SQLException; import java.util.List; import java.util.Map; public interface AdminMapper { Admin selectAdmin(String ID); List<Admin> selectAllAdmin(); int updateAdmin(Admin admin); int delectAdmin(int id); int updatePermissions(Map map); int updatePassword(Map map); int insertAdmin(Admin admin); }
AdminMapper.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="cn.BBS.dao.AdminMapper" > <resultMap id="BaseResultMap" type="cn.BBS.po.Admin" > <id column="ID" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="realName" property="realname" jdbcType="VARCHAR" /> <result column="Card" property="card" jdbcType="VARCHAR" /> <result column="picture" property="picture" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="permissions" property="permissions" jdbcType="INTEGER" /> </resultMap> <sql id="Base_Column_List" > ID, name, realName, Card, picture, password, permissions </sql> <sql id="Base_Column_List_1" > ID, name, realName, Card, picture, permissions </sql> <select id="selectAdmin" resultMap="BaseResultMap" parameterType="String" > SELECT <include refid="Base_Column_List"></include> FROM admin WHERE ID=#{ID} OR name = #{ID} </select> <select id="selectAllAdmin" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List_1"></include> FROM admin </select> <update id="updateAdmin" parameterType="cn.BBS.po.Admin"> UPDATE admin SET name =#{name}, realName = #{realname}, picture = #{picture}, password = #{password}, permissions = #{permissions} WHERE ID= #{id} </update> <update id="updatePermissions" parameterType="java.util.Map"> UPDATE admin SET permissions = #{permissions} WHERE ID= #{id} </update> <update id="updatePassword" parameterType="java.util.Map"> UPDATE admin SET password = #{password} WHERE ID= #{id} </update> <delete id="delectAdmin" parameterType="Integer"> delete from admin where ID = #{id,jdbcType=INTEGER} </delete> <insert id="insertAdmin" parameterType="cn.BBS.po.Admin"> insert into admin (ID, name, realName, Card, picture, password, permissions) values (#{id},#{name},#{realname},#{card},#{picture},#{password},#{permissions}) <selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id"> SELECT LAST_INSERT_ID() AS ID </selectKey> </insert> </mapper>
由於該項目使用mybatis對應的XXX.xml文件如AdminMapper.xml來對實現Dao的CRUD等操作。所以需要對該xml文件進行測試。DAO接口類不用測試。
下面是測試類AdminMapperXmlDaoTest:
/**
*
*/
package cn.BBS.Mapperdao.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
import cn.BBS.dao.AdminMapper;
import cn.BBS.po.Admin;
/**
* 作者:星朵 時間:Nov 10, 2017 類名:AdminMapperXmlDaoTest
*/
@ContextConfiguration(locations = { "/config/db.properties" })
public class AdminMapperXmlDaoTest {
@Autowired
private AdminMapper adminMapper;
// private static String sourcePath = "/config/db.properties";
private static String xmlePath = "/config/Spring/spring-mybatis.xml";
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
@Transactional
public void updateAdminTest() {
Admin admin = new Admin();
admin.setId(112);
admin.setName("sage");
admin.setPassword("123456");
admin.setPermissions(1);
admin.setPicture("mypciture");
admin.setRealname("星朵");
admin.setCard("121153");
//下面解釋我的疑惑。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlePath);
adminMapper = applicationContext.getBean(AdminMapper.class);
adminMapper.insertAdmin(admin);
}
}
本來根據網上一般的做法,使用SqlSessionFactory來創建SqlSession來進行操作的。但是那些是基於mybatis.xml的,這個文件使用的文件格式根據其版本有兩種:DOCTYPE和
scheme格式的。如果是前者,則使用SqlsessionFactory來創建Sqlsession;如果是後者則使用SqlsessionBean。由於我的項目的配置文件是spring-mybatis.xml的,與第一
中情況不一致,使用SqlsessionFactory創建Sqlsession會出錯。
提示是:Cause: org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null".
所以我放棄了第一種做法,對於第二種做法我也嘗試著去做,可能是本人的水平有限,結果也出錯。無奈之下,就是用了上面代碼那種做法,結果卻意外的通過了。
結果如下:
無論網上的做法怎麽樣,我最後總結出來的測試步驟是:
(1)加載配置文件,這裏的是spring-mybatis.xml文件;
(2)給bean實例化內容。
(3)從spring管理容器中獲取bean,這裏的bean已經有spring註入管理了;
(4)調取dao接口的方法驗證對應的xml映射文件中的CRUD功能()。
以上是我對於這種測試的一些感悟。當然由於水平有限,其中有不當解釋也不足為奇。
下面是參考的博文地址:https://www.cnblogs.com/fenglanglang/p/6007653.html
spring-mybatis整合的dao單元測試