mule進階之ibatis component
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());
}
}
相關文章
- mule進階之jdbc transport:JDBC
- Vuejs進階知識(十八)【component 進階知識】VueJS
- Mule 入門之:環境搭建
- Dagger 2 系列(六) -- 進階篇:Component 依賴、@SubComponent 與多 Component 下的 Scope 使用限制
- Django 進階之 celeryDjango
- javascript 進階之 - PromiseJavaScriptPromise
- IOS 進階之 WKWebViewiOSWebView
- 前端進階之困前端
- Pytest高階進階之Fixture
- 高階前端的進階——CSS之flex前端CSSFlex
- Mule+Spring+jbpmSpring
- JavaScript進階之繼承JavaScript繼承
- JavaScript進階之原型鏈JavaScript原型
- JavaScript進階之陣列JavaScript陣列
- JavaScript進階之函式JavaScript函式
- Linq之Lambda進階
- React 高階應用 -- 高階元件 Heigher Order ComponentReact元件
- Java進階之詳談ExceptionJavaException
- JavaScript進階之(一) this指標JavaScript指標
- c#進階之泛型C#泛型
- 前端基礎之jQuery進階前端jQuery
- 【Android進階】RecyclerView之ItemDecoration(一)AndroidView
- flutter的進階之路之常用元件Flutter元件
- pytest進階之fixture函式函式
- 攻防世界web高手進階之blgdelWeb
- JAVA進階之IO模型深入解析Java模型
- Android自我進階——JAVA之JVMAndroidJavaJVM
- 博弈論進階之SG函式函式
- 博弈論進階之Multi-SG
- 博弈論進階之Every-SG
- python進階之socket詳解Python
- Python進階之物件導向Python物件
- JavaScript進階之基礎語法JavaScript
- RecyclerView進階之層疊列表(上)View
- node爬蟲進階之——登入爬蟲
- RecyclerView進階之層疊列表(下)View
- java進階之自定義註解Java
- vim進階之新增ctags和cscope