mule進階之jdbc transport:
繼續在mule的xml汪洋中遨遊.
簡單需求如下:向一個vm:queue傳送map訊息, mule根據map資訊, 動態執行sql, 並返回資料.
select 的查詢mule預設返回map資料.
配置檔案:my-mule-jdbc-config.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns="http://www.mulesource.org/schema/mule/core/2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.mulesource.com/schema/mule/jdbc/2.1" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.1" xsi:schemaLocation=" http://www.mulesource.com/schema/mule/jdbc/2.1 http://www.mulesource.com/schema/mule/jdbc/2.1/mule-jdbc-ee.xsd http://www.mulesource.org/schema/mule/core/2.1 http://www.mulesource.org/schema/mule/core/2.1/mule.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.mulesource.org/schema/mule/vm/2.1 http://www.mulesource.org/schema/mule/vm/2.1/mule-vm.xsd"> <spring:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <spring:property name="driverClassName" value="com.mysql.jdbc.Driver" /> <spring:property name="url" value="jdbc:mysql://192.168.10.120/sand_res" /> <spring:property name="username" value="username" /> <spring:property name="password" value="888" /> <spring:property name="maxActive" value="30" /> <spring:property name="maxIdle" value="10" /> <spring:property name="maxWait" value="1000" /> <spring:property name="defaultAutoCommit" value="true" /> </spring:bean> <jdbc:connector name="jdbcConnector" dataSource-ref="dataSource"> <jdbc:query key="selectUser" value="SELECT first_name,last_name FROM app_user where first_name=#[map-payload:firstName]" /> <jdbc:query key="insertUser" value="insert into app_user (id,first_name,last_name ) values(#[map-payload:id], #[map-payload:firstName], #[map-payload:lastName])" /> </jdbc:connector> <!-- The Mule model initialises and manages your UMO components --> <model name="databaseModel"> <service name="insertUMO"> <!-- any number of endpoints can be added to an inbound router --> <inbound> <vm:inbound-endpoint path="query"/> </inbound> <!-- An outbound router can have one or more router configurations that can be invoked depending on business rules, message contents, headers or any other criteria. The pass-through-router is a router that automatically passes on every message it receives --> <outbound> <pass-through-router> <jdbc:outbound-endpoint queryKey="selectUser" synchronous="true"/> </pass-through-router> </outbound> </service> </model> </mule>
注意: 如果mule採用2.1, jdbc transport的namespase字尾為com, 而不是org, 如果寫錯,IDE不會提示,程式異常也很奇怪,讓我折騰了一個下午:(
測試程式:
public class MyMuleClientTest
{
public static void main(String[] args) throws MuleException
{
// create mule
MuleContext muleContext;
String config = "my-mule-jdbc-config.xml";
muleContext = new DefaultMuleContextFactory().createMuleContext(config);
muleContext.start();
// creat mule client
MuleClient client = new MuleClient();
Map map = new HashMap();
map.put("firstName", "feng");
MuleMessage response = client.send("vm://query", map, null);
System.out.println("response = " + response.getPayload());
}
}
執行的sql為:
SELECT first_name,last_name FROM app_user where first_name="feng"
insert的執行類似,只需修改如下:
<outbound> <pass-through-router> <jdbc:outbound-endpoint queryKey="insertUser" synchronous="true"/> </pass-through-router> </outbound>
感覺mule的jdbc transtort, 就是微縮版的ibatis.呵呵.
相關文章
- mule進階之ibatis componentBAT
- Hive學習之六 《Hive進階— —hive jdbc》 詳解HiveJDBC
- Mule 入門之:環境搭建
- Django 進階之 celeryDjango
- javascript 進階之 - PromiseJavaScriptPromise
- IOS 進階之 WKWebViewiOSWebView
- 前端進階之困前端
- Pytest高階進階之Fixture
- 高階前端的進階——CSS之flex前端CSSFlex
- JavaScript進階之繼承JavaScript繼承
- JavaScript進階之原型鏈JavaScript原型
- JavaScript進階之陣列JavaScript陣列
- JavaScript進階之函式JavaScript函式
- Linq之Lambda進階
- Mule+Spring+jbpmSpring
- Java進階之詳談ExceptionJavaException
- JavaScript進階之(一) this指標JavaScript指標
- c#進階之泛型C#泛型
- 前端基礎之jQuery進階前端jQuery
- Java中JDBC進階教程之資料庫的隔離級別!JavaJDBC資料庫
- Springboot進階-JDBC、Druid、Mybatis、Swagger、SpringMVC、MailSpring BootJDBCUIMyBatisSwaggerSpringMVCAI
- Java 之 JDBCJavaJDBC
- Java之JDBCJavaJDBC
- Java之JDBC①JavaJDBC
- 使用Jmeter進行效能測試實戰:詳解HTTP請求和JDBC request進階篇JMeterHTTPJDBC
- 【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