## XML支持
beetlsql提供一個子模塊支持`sql-xml` XML,語法上借鑒了mybatis,這是beetlsql的擴展包,因此需要額外引入
```xml
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>sql-xml</artifactId>
<version>3.23.11-RELEASE</version>
</dependency>
```
如下是一個xml文件定義
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<beetlsql>
<sql id="select">
<!-- 測試sql -->
select * from sys_user
<where>
<if test="has(user) and user.name=='a'">
and name='3'
</if>
<bind value="1+99" export="newValue"/>
<isNotEmpty value="'1'">
and name='5'
</isNotEmpty>
and name = #{newValue}
and name in
<foreach items="[1,2,3]" var="id,status" open="(" close=")" separator=",">
#{id+status.index}
</foreach>
<include refid="commonWhere"/>
</where>
</sql>
<resultMap id="simpleMap">
<result property="id" column="id"/>
<result property="myName" column="user_name"/>
</resultMap>
<resultMap id="complexMap">
<result property="id" column="id"/>
<association property="info" >
<result property="name" column="name"/>
<result property="age" column="age"/>
</association>
</resultMap>
<resultMap id="complexListMap">
<result property="id" column="id"/>
<collection property="listInfo" >
<result property="name" column="name"/>
<result property="age" column="age"/>
</collection>
</resultMap>
</beetlsql>
```
> 參考源碼sql-test下的代碼QuickXMLTestBeetlSQL,以及對應的xml文件user.xml
因為xml支持并非xml核心功能,因此需要一定步奏引入和配置
1) sqlLoader使用XMLClasspathLoader,可以通過配置或者api設定
```java
ConnectionSource source = ....
SQLManagerBuilder builder = new SQLManagerBuilder(source);
builder.setNc(new UnderlinedNameConversion());
builder.setDbStyle(new H2Style());
builder.setInters(new Interceptor[]{new DebugInterceptor()});
builder.setSqlLoader(new XMLClasspathLoader("sql"));
sqlManager = builder.build();
```
2. 同創建md文件那樣,創建xml文件,格式可以參考如上例子
3. 如果提供的xml tag不夠用,可以完全自定義tag,以if標簽實現為例子,用beetl的標簽實現,參考http://www.hmoore.net/xiandafu/beetl3_guide/2138969 了解如何定義標簽
```java
public class IfTag extends Tag{
@Override
public void render() {
if (!containHtmlAttribute("test")) {
throw new IllegalArgumentException("缺少 test屬性");
}
Object value = this.getHtmlAttribute("test");
if (!(value instanceof Boolean)) {
throw new IllegalArgumentException("期望test表達式運算結果是boolean類型");
}
if ((Boolean) value) {
this.doBodyRender();
}
}
}
```
4. 調用XMLBeetlSQL,初始化xml支持,如果你定義了xml標簽,調用
```java
XMLBeetlSQL.config(sqlManager);
//可選,自定義tag
XMLBeetlSQL.regisregisterTagter(sqlManager,"yourTag",YourTag.clas);
```
初始化XML支持后,就可以像使用md文件那樣使用XML文件
5. 如果有自定義映射,可以在xml文件定義resultMap,并通過@ResultProvider(XMLConfigMapper.class) 和@XMLMapper(resource=sqlId) 來指定對象的映射文件
```java
@Test
public void testForeach(){
Map map = new HashMap();
map.put("ids", Arrays.asList(1,2));
//訪問的是user.xml文件下的 testFoeach 標簽
List<User> list = sqlManager.select(SqlId.of("user.testForeach"),User.class,map);
Assert.assertTrue(list.size()==2);
}
@Test
public void testComplexXMLMapping(){
String sql = "select id,name,age from sys_user";
List<MyXMLComplexUser> list = sqlManager.execute(new SQLReady(sql),MyXMLComplexUser.class);
Assert.assertTrue(list.get(0) instanceof MyXMLComplexUser );
}
@ResultProvider(XMLConfigMapper.class)
@XMLMapper(resource="user.complexMap")
@Data
public static class MyXMLComplexUser {
private Long id;
private MyXMLComplexUserInfo info;
}
@Data
public static class MyXMLComplexUserInfo {
private String name;
private Integer age;
}
```
user.complexMap對應了user.xml的映射配置
```xml
<resultMap id="complexMap">
<result property="id" column="id"/>
<association property="info" >
<result property="name" column="name"/>
<result property="age" column="age"/>
</association>
</resultMap>
```
- BeetlSQL 3 指南
- 數據訪問框架
- 適合用戶
- 編譯源碼
- 直接看代碼
- 閑大賦介紹
- BeetlSQL3 特點
- 數據庫訪問工具的痛點
- BeetlSQL3 例子
- 基礎例子
- 結果集映射
- 翻頁查詢
- 演示like,batchUpdate,in 操作
- 自動fetch
- 多數據庫
- 代碼生成框架
- 安裝BeetlSQL
- 直接安裝
- 框架集成
- 編譯源碼
- 快速開始
- 環境準備
- 環境搭建
- 使用BeetlSQL
- 按照主鍵查尋
- 更新
- 按照模板查詢
- 執行SQL
- 執行模板SQL
- 使用Query
- 使用Mapper
- 使用模板文件
- SQLManager
- 內置語句
- 內置查詢API
- template查詢
- 更新操作
- 執行SQL
- 執行模板SQL
- 把SQL放到文件里
- 翻頁查詢
- SQLResult
- Stream查詢
- 存儲過程調用
- NameConversion(重要)
- DBStyle
- Inerceptor
- SQLManagerExtend
- 強制使用數據源
- Mapper
- 實現Mapper
- @Sql
- @Update @BatchUpdate
- @Template
- 參數名稱
- 參數返回值
- 執行SQL文件
- 翻頁查詢
- @SqlProvider
- @SpringData
- @SubQuery
- @InheritMapper
- @Call
- StreamData
- Default Method
- 定義自己的BaseMapper
- 限制Java代碼中SQL長度
- 數據模型
- POJO
- 不嚴格的POJO
- 交集(重要)
- @Table 注解
- @Column 注解
- 主鍵
- RowMapper
- ResultSetMapper
- Json配置映射
- Json自動映射
- XML支持
- 自動Fetch
- AttributeConvert
- BeanConvert
- 枚舉
- 混合模型
- Map模型
- 動態模型
- 模型其他注解
- 安全擴展注解
- BeetlSql 單表查詢工具Query
- Query使用方式和風格介紹
- Query主要操作簡介
- 查詢器獲取
- SELECT簡單的條件查詢
- 復雜的條件查詢
- 查詢字段智能處理
- 健壯的變量
- 自定義實現
- INSERT操作
- UPDATE操作
- DELETE操作
- single查詢和unique
- COUNT查詢
- GROUP分組查詢和Having子句
- 分頁查詢
- ORDER BY 排序
- page分頁查詢
- 方法調用順序
- Markdown文件
- Beetl 入門
- 定界符號
- 變量
- 算數表達式
- 邏輯表達式
- 控制語句
- 訪問變量屬性
- 判斷對象非空(重要)
- 調用方法
- 自定義方法
- 內置方法
- 標簽功能
- 附錄
- Idea 插件
- 代碼生成
- 多庫使用
- 業務主從庫
- 主從庫延遲問題
- 多個業務庫
- 合并多個業務庫 1
- 合并多個業務庫 2
- 單表多租戶
- 每個租戶一個表
- 多庫多租戶
- 使用ShardingSphere
- Saga(試驗)
- 關于Saga的一些認識
- 關于隔離性
- BeetlSQL Saga
- SagaMapper
- Saga 多庫事務實現
- Saga 微服務 實現
- 配置 Saga Server
- Swagger
- BeetlSQL 性能
- 測試方法
- 最新
- 2021-11-21
- 2020-9-28
- Spring 快速開始
- 環境準備
- 環境搭建
- SpringBoot 快速開始
- 環境準備
- 環境搭建
- 擴展BeetlSQL3
- 完整配置
- BaseMapper定制
- 代碼生成
- SQLExecutor定制
- 第三方
- MetadataManager
- JFinal集成
- ExecuteContext
- 參考ACT
- 數據庫表到Java對象
- Solon
- NameConversion
- ViewType
- RowMapper
- ResultSetMapper
- AttributeConvert
- BeanConvert
- Fetch
- 代碼生成
- 擴展BeetlSQL3
- BaseMapper定制
- SQLExecutor定制
- MetadataManager
- ExecuteContext
- 數據庫表到Java對象
- NameConversion
- ViewType
- RowMapper
- ResultSetMapper
- AttributeConvert
- BeanConvert
- Fetch
- 代碼生成
- BeetlSQL 多數據庫支持
- 多庫之間的不同
- 跨庫支持實現
- DBStyle
- MySqlStyle 例子
- H2Style例子
- ClickHouseStyle例子
- HBaseStyle例子
- DruidStyle例子
- MetadataManager
- BeanProcessor
- 與BeetlSQL2的區別
- 使用區別