mule進階之ibatis component

iteye_6308發表於2009-03-18

mule的jdbc transport功能過於簡單, 今天的需求是把ibatis整合進來, 作為一個service的component, 以增強持久層功能.
mule可以直接引用spring的配置檔案, 方法如下:

    <spring:beans>
        <spring:import resource="applicationContext.xml" />
        <spring:import resource="applicationContext-ibatis.xml" />
    </spring:beans> 

     作為一個演示,我需要往一個vm:queue中寫入訊息,component(由spring bean充當) 得到訊息, 並作為查詢引數 從資料庫查詢資料並返回.
    model定義如下:

        <model name="databaseModel">
        <service name="databaseUMO">
            <!-- any number of endpoints can be added to an inbound router -->
            <inbound>
                <vm:inbound-endpoint path="query" />
            </inbound>
            <component>
                <method-entry-point-resolver>
                    <include-entry-point method="getUser" />
                </method-entry-point-resolver>
                <spring-object bean="userDao"></spring-object>
            </component>
        </service>
    </model>

    
    mule中關於component的xsd很不友好, component的子項居然是一個序列, 次序不能顛倒.
    現在的任務就是完成userDao 的構建.
    首先給出dao的介面:

  public interface Dao {
   
    public Object save(String sqlId, Object parameterObject);

    public int delete(String sqlId, Object parameterObject);

    public int update(String sqlId, Object parameterObject);

    public List query(String sqlId, Object parameterObject);

    public Object queryObject(String sqlId, Object parameterObject);

    public Connection getConn();
} 
 


userDao如下:

public interface UserDao extends Dao {
    public List getUsers();
    public User getUser(Long userId);
    public void saveUser(User user);
    public void removeUser(Long userId);
} 

 
userDao的ibatis實現:

public class UserDaoiBatis extends BaseDao implements UserDao {
    private DataFieldMaxValueIncrementer incrementer;
   
    public void setIncrementer(DataFieldMaxValueIncrementer incrementer) {
        this.incrementer = incrementer;
    }

    public List getUsers() {
        return getSqlMapClientTemplate().queryForList("getUsers", null);
    }

    public User getUser(Long id) {
        User user =
            (User) getSqlMapClientTemplate().queryForObject("getUser", id);

        if (user == null) {
            throw new ObjectRetrievalFailureException(User.class, id);
        }

        return user;
    }

    public void saveUser(User user) {
        if (user.getId() == null) {
            Long id = new Long(incrementer.nextLongValue());
            user.setId(id);

            // To use iBatis's <selectKey> feature, which is db-specific, comment
            // out the above two lines and use the line below instead
           
            // Long id = (Long) getSqlMapClientTemplate().insert("addUser", user);
            getSqlMapClientTemplate().insert("addUser", user);
            logger.info("new User id set to: " + id);
        } else {
            getSqlMapClientTemplate().update("updateUser", user);
        }
    }

    public void removeUser(Long id)  {
        getSqlMapClientTemplate().update("deleteUser", id);
    }
}
 


在spring配置檔案中, 裝配userDao:

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

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="30"/>
        <property name="maxIdle" value="10"/>
        <property name="maxWait" value="1000"/>
        <property name="defaultAutoCommit" value="true"/>
    </bean>
   
    <!-- Transaction manager for iBATIS DAOs -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
   
    <!-- SqlMap setup for iBATIS Database Layer -->
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:/co/iplatform/dao/sql-map-config.xml"/>
    </bean>

    <bean id="userIncrementer" class="org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer">
        <property name="dataSource" ref="dataSource"/>
        <property name="incrementerName" value="user_sequence"/>
        <property name="columnName" value="value"/>
    </bean>
   
    <bean id="userDao" class="co.iplatform.dao.UserDaoiBatis">
        <property name="incrementer" ref="userIncrementer"/>
        <property name="sqlMapClient" ref="sqlMapClient"/>
    </bean>
   
    <!-- Add additional DAO definitions here -->
</beans>

 

ibatis的配置檔案長大很標準,就不貼了。
寫一個userDao的測試, 以確保dao正常工作:

public abstract class BaseDaoTestCase extends AbstractTransactionalDataSourceSpringContextTests {
    protected final Log log = logger;
    private ApplicationContext ctx;

    protected String[] getConfigLocations() {
        setAutowireMode(AUTOWIRE_BY_NAME);
        String[] paths = {"classpath*: applicationContext*.xml" };
        return paths;
    }
}
 

 

public class UserDaoTest extends BaseDaoTestCase {
    private User user = null;
    private UserDao dao = null;

    public void setUserDao(UserDao userDao) {
        this.dao = userDao;
    }

    public void testGetUsers() {
        user = new User();
        user.setFirstName("li");
        user.setLastName("chunlei");
        dao.saveUser(user);
        System.out.println("size--"+dao.getUsers().size());
        assertTrue(dao.getUsers().size() >= 1);
    }

    public void testSaveUser() throws Exception {
        user = new User();
        user.setFirstName("li");
        user.setLastName("chunlei");

        dao.saveUser(user);
        assertTrue("primary key assigned", user.getId() != null);

        assertNotNull(user.getFirstName());
    }

    public void testAddAndRemoveUser() throws Exception {
        user = new User();
        user.setFirstName("feng");
        user.setLastName("Joy");

        dao.saveUser(user);

        assertNotNull(user.getId());
        assertTrue(user.getFirstName().equals("feng"));

        log.debug("removing user...");

        dao.removeUser(user.getId());
        endTransaction();

        try {
            user = dao.getUser(user.getId());
            fail("User found in database");
        } catch (DataAccessException dae) {
            log.debug("Expected exception: " + dae.getMessage());
            assertNotNull(dae);
        }
    }
}
 


一路綠燈.
最後,進行mule測試:

public class MyMuleClientTest
{
    public static void main(String[] args) throws MuleException
    {
        // create mule
        MuleContext muleContext;
        String config = "mule-database-config.xml";
        muleContext = new DefaultMuleContextFactory().createMuleContext(config);
        muleContext.start();
        // creat mule client
        MuleClient client = new MuleClient();
        MuleMessage response = client.send("vm://query", new Long(11), null);       
        System.out.println("response = " + response.getPayload());
    }

}
 

 

相關文章