开发设计ibatis 文件 pubMessageDraft.xml 写sqlPubMessageDraftDO写Model

PubMessageDraftDao写方法 : public List<PubMessageDraftDO> listByType(String pubUid, String type, int size, int offset) {

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("type", type);

        params.put("pubUid", pubUid);

        params.put("size", size);

        params.put("offset", offset);

        return (List<PubMessageDraftDO>) this.getSqlMapClientTemplate().queryForList("pubMessageDraft.listByType", params);

    }

    public List<PubMessageDraftDO> listByIds(String pubUid,List<String> idList){

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("pubUid", pubUid);

        params.put("idList", idList);

        return (List<PubMessageDraftDO>) this.getSqlMapClientTemplate().queryForList("pubMessageDraft.listByIds", params);

      

    }

 

    public PubMessageDraftDO getById(String id, String pubUid) {

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("id", id);

        params.put("pubUid", pubUid);

        return (PubMessageDraftDO) this.getSqlMapClientTemplate().queryForObject("pubMessageDraft.getById", params);

    }

 

    public void insert(PubMessageDraftDO pubMessageDraftDO) {

        this.getSqlMapClientTemplate().insert("pubMessageDraft.insert", pubMessageDraftDO);

    }

    public void update(PubMessageDraftDO pubMessageDraftDO) {

        this.getSqlMapClientTemplate().insert("pubMessageDraft.update", pubMessageDraftDO);

    }

    public void delete(String id, String pubUid) {

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("id", id);

        params.put("pubUid", pubUid);

        this.getSqlMapClientTemplate().delete("pubMessageDraft.delete", params);

    }

}

 

单元测试设计:

需要的配置文件: daoTextContext.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" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

 

<import resource="classpath:dataSource.xml" />

<import resource="classpath*:META-INF/spring/pp-dao.xml" />

</beans>

--------------

 

daoSource.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"

       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

 

    <bean id="propertyConfigurer_pp" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="order" value="1"/>

        <property name="ignoreUnresolvablePlaceholders" value="true"/>

        <property name="locations">

         <list>

                <value>classpath:jtester.properties</value>

           </list>       

        </property>

    </bean>

 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="com.mysql.jdbc.Driver" />

<property name="url" value="${jdbc.url}" />

<property name="username" value="${jdbc.username}"/>

<property name="password" value="${jdbc.password}" />

        <property name="maxActive"><value>20</value></property>

        <property name="initialSize"><value>3</value></property>

        <property name="maxWait"><value>60000</value></property>

        <property name="maxIdle"><value>20</value></property> <!-- 可以和maxActive保持一致 -->

        <property name="minIdle"><value>3</value></property>  <!-- 可以和initialSize保持一致 -->

        <property name="removeAbandoned"><value>true</value></property>

        <property name="removeAbandonedTimeout"><value>180</value></property>

<property name="timeBetweenEvictionRunsMillis"><value>60000</value></property>

<property name="minEvictableIdleTimeMillis"><value>1800000</value></property>

<property name="connectionProperties"><value>clientEncoding=UTF-8</value></property>

</bean>

 

    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

       <property name="configLocations">

       <list>

        <value>classpath:META-INF/ibatis/pp-sql-map-config.xml</value>

       </list>

        </property>

        <property name="dataSource" ref="dataSource"/>

    </bean>

 

    <bean id="sqlDaoBaseSupport" abstract="true">

        <property name="sqlMapClient" ref="sqlMapClient"/>

    </bean>

 

    <!-- Transaction manager for a single JDBC DataSource -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource"/>

    </bean>

 

</beans>

------------

jtester.properties

----------------------

# dbFit database test use

database.type=mysql

database.url=jdbc:mysql://10.20.153.58:3306/bbltest?useUnicode=true&characterEncoding=UTF-8

database.userName=test

database.password=test

database.driverClassName=com.mysql.jdbc.Driver

database.only.testdb.allowing=false

jdbc.url=jdbc:mysql://10.20.153.58:3306/bbltest?useUnicode=true&characterEncoding=UTF-8

jdbc.username=test

jdbc.password=test

 

DatabaseModule.Transactional.value.default=rollback

 

## jtester-ext custom configuration

# webx configuration

SpringModule.applicationContextFactory.implClassName=com.alibaba.china.testerx.jtester.ext.spring.JTesterxApplicationContextFactory

# database configuration

unitils.module.database.className=com.alibaba.china.testerx.jtester.ext.database.module.DatabasexModule

# lazy-init configuration

unitils.module.spring.className=com.alibaba.china.testerx.jtester.ext.spring.JTesterxSpringModule

# dbfit configuration

unitils.module.dbfit.className=com.alibaba.china.testerx.jtester.ext.database.module.DbFitxModule

 

------------

pp-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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"

default-lazy-init="true">

 

<bean id="pubMessageDao" class="com.laiwang.pp.dal.dao.PubMessageDao" parent="sqlDaoBaseSupport" />

<bean id="pubMessageDetailDao" class="com.laiwang.pp.dal.dao.PubMessageDetailDao" parent="sqlDaoBaseSupport" />

<bean id="pubMessageDraftDao" class="com.laiwang.pp.dal.dao.PubMessageDraftDao" parent="sqlDaoBaseSupport" />

<bean id="pubReplayRuleDao" class="com.laiwang.pp.dal.dao.PubReplayRuleDao" parent="sqlDaoBaseSupport" />

<bean id="pubSessionMessageDao" class="com.laiwang.pp.dal.dao.PubSessionMessageDao" parent="sqlDaoBaseSupport" />

<bean id="pubSessionDao" class="com.laiwang.pp.dal.dao.PubSessionDao" parent="sqlDaoBaseSupport" />

</beans>

----------

 

测试用例:

---------------

package com.alibaba.laiwang.pp;

 

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

 

import junit.framework.Assert;

 

import org.junit.Before;

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.SpringJUnit4Cla***unner;

 

import com.laiwang.pp.dal.dao.PubMessageDraftDao;

import com.laiwang.pp.dal.dataobject.PubMessageDraftDO;

 

 

@RunWith(SpringJUnit4Cla***unner.class)

@ContextConfiguration(locations = "classpath:daoTestContext.xml")

public class pubMessageDraftTest {

    @Autowired

    private PubMessageDraftDao        pubMessageDraftDao;

 

    @Before

    public void before() {

       

    }

 

    @Test

    public void test() {

        //insert

        PubMessageDraftDO pubMessageDraft = new PubMessageDraftDO();

        pubMessageDraft.setId("id1");

        pubMessageDraft.setPubUid("pubUid1");

        pubMessageDraft.setMsgType("msgType");

        pubMessageDraft.setAttachment("p_w_upload1");

        pubMessageDraft.setGmtCreate(new Date());

        pubMessageDraft.setGmtModified(new Date());

        pubMessageDraftDao.insert(pubMessageDraft);

        PubMessageDraftDO pubInsert = pubMessageDraftDao.getById("id1","pubUid1");   

        Assert.assertNotNull(pubInsert);

        

        //update

        pubMessageDraft.setMsgType("msgType111");

        pubMessageDraft.setGmtModified(new Date());

        pubMessageDraft.setPubUid("pubUid2");

        pubMessageDraftDao.update(pubMessageDraft);

        

        //getById

        PubMessageDraftDO pub = pubMessageDraftDao.getById("id2","pubUid2");     

        Assert.assertEquals("p_w_upload1", pub.getAttachment());

        Assert.assertEquals("msgType111", pub.getMsgType());

        Assert.assertNotNull(pub.getGmtCreate());

        Assert.assertNotNull(pub.getGmtModified());

        

        //listByType

       List<PubMessageDraftDO>  pubListByType= pubMessageDraftDao.listByType("pubUid2", "p_w_upload1", 0, 10);

       for (PubMessageDraftDO lb: pubListByType ){

           Assert.assertEquals("p_w_upload1", lb.getAttachment());

           Assert.assertEquals("msgType111",lb.getMsgType());

       }

      

       //listByIds

       List<String> idList= new ArrayList();

       idList.add("id2");

       List<PubMessageDraftDO>  listByIds= pubMessageDraftDao.listByIds("pubUid2", idList);

       for(PubMessageDraftDO lbs:listByIds){

           Assert.assertEquals("p_w_upload1", lbs.getAttachment());

           Assert.assertEquals("msgType111",lbs.getMsgType());   

       }

       

       

       //delete 

       pubMessageDraftDao.delete("id1","pubUid1");

       PubMessageDraftDO pubDelete = pubMessageDraftDao.getById("id1","pubUid1"); 

       Assert.assertNull(pubDelete);

    }

}