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.呵呵.
相關文章
- Springboot進階-JDBC、Druid、Mybatis、Swagger、SpringMVC、MailSpring BootJDBCUIMyBatisSwaggerSpringMVCAI
- Django 進階之 celeryDjango
- 前端進階之困前端
- javascript 進階之 - PromiseJavaScriptPromise
- Java 之 JDBCJavaJDBC
- Java之JDBCJavaJDBC
- 高階前端的進階——CSS之flex前端CSSFlex
- JavaScript進階之繼承JavaScript繼承
- JavaScript進階之原型鏈JavaScript原型
- Java中JDBC進階教程之資料庫的隔離級別!JavaJDBC資料庫
- 使用Jmeter進行效能測試實戰:詳解HTTP請求和JDBC request進階篇JMeterHTTPJDBC
- Java 之 JDBC(二)JavaJDBC
- 前端基礎之jQuery進階前端jQuery
- JavaScript進階之(一) this指標JavaScript指標
- Android自我進階——JAVA之JVMAndroidJavaJVM
- pytest進階之fixture函式函式
- 【Android進階】RecyclerView之ItemDecoration(一)AndroidView
- Java進階之詳談ExceptionJavaException
- Python進階之物件導向Python物件
- hibernate進行JDBC批量新增JDBC
- jmeter之JDBC類元件JMeterJDBC元件
- 攻防世界web高手進階之blgdelWeb
- flutter的進階之路之常用元件Flutter元件
- 【Flutter】開發之進階Widget(三)Flutter
- 【Flutter】開發之進階Widget(五)Flutter
- JAVA進階之IO模型深入解析Java模型
- Asp.NetCore之AutoMapper進階篇ASP.NETNetCoreAPP
- 【Android進階】RecyclerView之快取(二)AndroidView快取
- Go進階之網路程式設計Go程式設計
- Java多執行緒之進階篇Java執行緒
- python爬蟲進階必備之代理Python爬蟲
- Docker進階與實踐之七:LibcontainerDockerAI
- Python爬蟲進階之APP逆向(三)Python爬蟲APP
- JavaScript進階之函式柯里化JavaScript函式
- 【Android進階】RecyclerView之繪製流程(三)AndroidView
- 前端進階之 JS 抽象語法樹前端JS抽象語法樹
- react進階元件之Render Props小結React元件
- 前端進階之 Javascript 抽象語法樹前端JavaScript抽象語法樹
- Linux ACL 許可權之進階篇Linux