光辉飞翔

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

                    XsqlBuilder用于可以动态构造sql语句,避免在构造sql时使用过多的 if 判断,与SafeSqlProcesser集成提供防止sql注入攻击,与DataModifier集成完成数据类型的转换
动态构造sql示例:

Java代码 复制代码 收藏代码
  1. String xsql = "select * from user where 1=1 
  2.          /~ and username = {username} ~/ 
  3.          /~ and password = {password} ~/ 
  4.          /~ and age = [age] ~/ 
  5.          /~ and sex = [sex] ~/" 
  6.  
  7. Map filters = new HashMap(); 
  8. filters.put("username", "badqiu"); 
  9. filters.put("age", "12"); 
  10. filters.put("sex", ""); 
  11.  
  12. XsqlFilterResult result = xsqlBuilder.applyFilters(xsql,filters); 
   
       String xsql = "select * from user where 1=1
                /~ and username = {username} ~/
                /~ and password = {password} ~/
                /~ and age = [age] ~/
                /~ and sex = [sex] ~/"

        Map filters = new HashMap();
        filters.put("username", "badqiu");
        filters.put("age", "12");
        filters.put("sex", "");

        XsqlFilterResult result = xsqlBuilder.applyFilters(xsql,filters);

         构造生成的结果result.getXsql()将会等于

Sql代码 复制代码 收藏代码
  1. select * fromuserwhere 1=1 and username={username} and age=12 
select * from user where 1=1 and username={username} and age=12

被过滤删除的段:        /~ and password = {password} ~/这一段由于在filters中password不存在而没有被构造出来         /~ and sex = [sex] ~/由于sex的值为空串也没有被构造出来

最后result.acceptedFilters值

Java代码 复制代码 收藏代码
  1. Map acceptedFilters = result.getAcceptedFilters(); 
  2. 会等于: 
  3. {username=badqiu} 
Map acceptedFilters = result.getAcceptedFilters();
会等于:
{username=badqiu}

相关符号介绍:
       /~ segment... ~/ 为一个条件代码块         {key} 过滤器中起标记作用的key,作为后面可以替换为sql的?,或是hql的:username标记         [key] 将直接替换为key value
数据类型转换示例:
select * from user where and 1=1 /~ age={age?int} ~/ 将会将Map filters中key=age的值转换为int类型
项目地址:http://code.google.com/p/rapid-xsqlbuilder/
下载地址:http://rapid-xsqlbuilder.googlecode.com/files/xsqlbuilder-1.0.zip

posted on   光辉飞翔  阅读(1935)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
 
点击右上角即可分享
微信分享提示