🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 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的标签实现,参考https://www.kancloud.cn/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> ```