mule3.4.0和Jdbc整合使用
<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/jdbc http://www.mulesoft.org/schema/mule/jdbc/current/mule-jdbc.xsd http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd"> <vm:connector name="vmQueue"/> <endpoint address="vm://query" name="query" exchange-pattern="one-way"/><!-- 建源 --> <spring:bean id="dataSource" destroy-method="close"> <spring:property name="driverClassName" value="com.mysql.jdbc.Driver" /> <spring:property name="url" value="jdbc:mysql://localhost/dbtest2" /> <spring:property name="username" value="root" /> <spring:property name="password" value="root" /> <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> <!-- 建sql 句 --> <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 ref="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="insertUser"/> </pass-through-router> </outbound> </service> </model> </mule>
?
测试代码:
?
?
import java.util.HashMap;import java.util.Map;import org.mule.api.MuleContext;import org.mule.api.MuleMessage;import org.mule.api.client.LocalMuleClient;import org.mule.context.DefaultMuleContextFactory;/** * <p>mule 3.4.0 和jdbc的整合使用 * 简单需求如下:向一个vm:queue发送map消息, mule根据map信息, 动态执行sql, 并返回数据. insert 插入语句. * <p> * * 创建日期 2013-8-17<br> * @author $Author$<br> * @version $Revision$ $Date$ * @since 3.0.0 */public class MuleJdbcMain { public static void main(String[] args) { try { String configFile = "mule-jdbc-config-service.xml"; MuleContext context =new DefaultMuleContextFactory().createMuleContext(configFile); context.start(); LocalMuleClient client = context.getClient(); Map<String,Object> muleParam=new HashMap<String,Object>(); muleParam.put("id", "789"); muleParam.put("firstName", "jms"); muleParam.put("lastName", "abcdef"); MuleMessage response = client.send("vm://query", muleParam, null); System.out.println("执行成功!"); } catch (Exception e) { e.printStackTrace(); } }}?
?
?