在 WSO2 ESB 5.0.0 中使用 MyBatis 框架
一、导包
1、在 WSO2 ESB 5.0.0 lib文件夹中导入 jar 包
2、在 Eclipse 项目中导入 jar 包
二、准备 Mybatis 的配置文件
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/TestMapper.xml" ></mapper>
</mappers>
</configuration>
三、准备实体类
package entity;
import java.io.Serializable;
public class TestMode implements Serializable{
private String name,address,sex,phone;
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
四、准备实体对应的 mapper 映射 xml 文件
TestMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="TestMapper">
<select id="countById" parameterType="java.lang.String" resultType="int">
select count(1) as num from mode where id = #{id}
</select>
<select id="findUserByName" parameterType="java.lang.String" resultType="Map">
select * from mode where name like '%${value}%'
</select>
</mapper>
五、准备一个获取 sqlsession 的工具类
package util;
import java.io.IOException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisSessionUtil {
static SqlSessionFactory sqlSessionFactory = null;
static {
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
try {
sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("SqlMapConfig.xml"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSession() {
return sqlSessionFactory.openSession();
}
}
六、准备 dao
package dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import util.MybatisSessionUtil;
public class TestDao {
private SqlSession session = MybatisSessionUtil.getSession();
public int countById(String id){
int rint = session.selectOne("TestMapper.countById", id);
session.commit();
return rint;
}
public List<Map<String,Object>> findUserByName(String name){
List<Map<String,Object>> list = session.selectList("TestMapper.findUserByName", name);
session.commit();
return list;
}
}
七、Mediator 类
package com;
import java.util.List;
import java.util.Map;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import dao.TestDao;
public class TestMyModel extends AbstractMediator {
private String property_name;
public String getProperty_name() {
return property_name;
}
public void setProperty_name(String property_name) {
this.property_name = property_name;
}
public boolean mediate(MessageContext context) {
JSONArray js = new JSONArray();
try{
TestDao tdao = new TestDao();
List<Map<String,Object>> list = tdao.findUserByName("a");
int count = tdao.countById("441012198009245079");
System.out.println("count: " + count);
System.out.println("id\tname\taddress\tsex\tphone\t");
for(int i = 0; i < list.size(); i++){
Map<String,Object> tm = list.get(i);
System.out.println(tm.get("id") + "\t"
+ tm.get("name") + "\t"
+ tm.get("address") + "\t"
+ tm.get("sex") + "\t"
+ tm.get("phone") + "\t");
}
String jsonstr = JSON.toJSONString(list);
js = JSONArray.parseArray(jsonstr);
log.info("jsonstr: " + jsonstr);
}catch(Exception e){
log.error("Mybatis Exception!", e);
}
String resp = "<DATA>\n" +
" <DATAINFOS>\n" +
" <PUUID>11111</PUUID>\n" +
" <SYNCODE>ZWY</SYNCODE>\n" +
" </DATAINFOS>\n" +
" </DATA>";
context.setProperty("resp", resp);
return true;
}
}
八、数据库表
九、完整目录结构
十、Postman 测试
Postman 请求
控制台输出
详细输出日志:
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 728566657.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@2b6d0b81]
==> Preparing: select * from mode where name like '%a%'
==> Parameters:
<== Columns: id, name, address, sex, phone, marriage, mobilephone
<== Row: 441011198009235056, a, , M, 33, Y, 18533446708
<== Row: 441012198009245079, a, , M, 18533446731, N, 18533446731
<== Row: 441013198009255102, a, , F, 8233936, Y,
<== Row: 441014198009265125, a, , M, , N, 18533446777
<== Row: 441015198009285148, a, , F, , Y, 18533446800
<== Total: 5
==> Preparing: select count(1) as num from mode where id = ?
==> Parameters: 441012198009245079(String)
<== Columns: num
<== Row: 1
<== Total: 1
count: 1
id name address sex phone
441011198009235056 a M 33
441012198009245079 a M 18533446731
441013198009255102 a F 8233936
441014198009265125 a M
441015198009285148 a F
[2021-08-04 11:48:24,396] INFO - TestMyModel jsonstr: [{"address":"","phone":"33","marriage":"Y","sex":"M","mobilephone":"18533446708","name":"a","id":"441011198009235056"},{"address":"","phone":"18533446731","marriage":"N","sex":"M","mobilephone":"18533446731","name":"a","id":"441012198009245079"},{"address":"","phone":"8233936","marriage":"Y","sex":"F","mobilephone":"","name":"a","id":"441013198009255102"},{"address":"","phone":"","marriage":"N","sex":"M","mobilephone":"18533446777","name":"a","id":"441014198009265125"},{"address":"","phone":"","marriage":"Y","sex":"F","mobilephone":"18533446800","name":"a","id":"441015198009285148"}]
|