Spring NamedParameterJdbcTemplate命名参数查询条件封装, NamedParameterJdbcTemplate查询封装...

本文介绍了如何使用Spring的NamedParameterJdbcTemplate进行命名参数查询条件的封装,提供了一个名为SqlBuffer的查询对象,简化了where、order by和limit子句的构建。通过示例展示了如何在Dao层使用该封装进行数据查询操作。
摘要由CSDN通过智能技术生成

Spring NamedParameterJdbcTemplate命名参数查询条件封装,

NamedParameterJdbcTemplate查询封装

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

©Copyright 蕃薯耀 2017年7月25日

http://www.cnblogs.com/fanshuyao/

由于在项目中想用Spring的NamedParameterJdbcTemplate,但又想写的sql查询简单一点,少一些,所以封装了Sql命名查询。使用此工具,主要是让where条件的查询及排序和分页简单点,现在使用的是mysql。

一、SqlBuffer查询对象封装(现在只能select语句的封装)

Java代码  
收藏代码
  1. import java.util.HashMap;  
  2. import java.util.Map;  
  3.   
  4. public class SqlBuffer {  
  5.       
  6.     /** 
  7.      * 空格 
  8.      */  
  9.     private final String SPACE = " ";  
  10.     /** 
  11.      * 冒号 
  12.      */  
  13.     private final String COLON = ":";  
  14.     /** 
  15.      * 逗号, 
  16.      */  
  17.     private final String COMMA = ",";  
  18.     /** 
  19.      * where关键字 
  20.      */  
  21.     private final String WHERE_SQL = "WHERE";  
  22.     /** 
  23.      * order by 关键字 
  24.      */  
  25.     private final String ORDER_BY_SQL = "ORDER BY";  
  26.     /** 
  27.      * limit关键字 
  28.      */  
  29.     private final String LIMIT_SQL = "LIMIT";  
  30.     /** 
  31.      * where 1=1条件 
  32.      */  
  33.     private final String WHERE_INIT = WHERE_SQL + "  1=1 ";  
  34.     private final String LEFT_BRACKET = "(";  
  35.     private final String RIGHT_BRACKET = ")";  
  36.     /** 
  37.      * 百分号% 
  38.      */  
  39.     private final String PERCENT_SIGN = "%";  
  40.     /** 
  41.      * 单引号 ' 
  42.      */  
  43.     private final String SINGLE_QUOTE = "'";  
  44.   
  45.     private StringBuffer baseSql  = new StringBuffer("");  
  46.     private StringBuffer whereSql  = new StringBuffer("");  
  47.     private StringBuffer orderBySql  = new StringBuffer("");  
  48.     private StringBuffer limitSql  = new StringBuffer("");  
  49.       
  50.     public Map<String, Object> paramsMap  = new HashMap<String, Object>();  
  51.       
  52.     private int whereIndex = -1;  
  53.     private int orderByIndex = -1;  
  54.     private int limitIndex = -1;  
  55.       
  56.     private int offSize;  
  57.     private int pageSize;  
  58.       
  59.       
  60.     private boolean isNewStart = false;  
  61.     private boolean isEndStart = false;  
  62.       
  63.     public SqlBuffer(){}  
  64.       
  65.     public SqlBuffer(String sql){  
  66.         init(sql);  
  67.     }  
  68.       
  69.     private int getWhereIndex(String sql){  
  70.         if(sql != null){  
  71.             return sql.indexOf(WHERE_SQL);  
  72.         }  
  73.         return -1;  
  74.     }  
  75.       
  76.     private int getOrderByIndex(String sql){  
  77.         if(sql != null){  
  78.             return sql.indexOf(ORDER_BY_SQL);  
  79.         }  
  80.         return -1;  
  81.     }  
  82.       
  83.     private int getLimitIndex(String sql){  
  84.         if(sql != null){  
  85.             return sql.indexOf(LIMIT_SQL);  
  86.         }  
  87.         return -1;  
  88.     }  
  89.       
  90.     /** 
  91.      * 判断有没有where查询语句,有则返回true 
  92.      * @return 
  93.      */  
  94.     private boolean hadWhereSql(){  
  95.         if(this.whereIndex > -1){  
  96.             return true;  
  97.         }  
  98.         return false;  
  99.     }  
  100.       
  101.     /** 
  102.      * 判断有没有order by查询语句,有则返回true 
  103.      * @return 
  104.      */  
  105.     private boolean hadOrderBySql(){  
  106.         if(this.orderByIndex > -1){  
  107.             return true;  
  108.         }  
  109.         return false;  
  110.     }  
  111.       
  112.     /** 
  113.      * 判断有没有limit查询语句,有则返回true 
  114.      * @return 
  115.      */  
  116.     private boolean hadLimitSql(){  
  117.         if(this.limitIndex > -1){  
  118.             return true;  
  119.         }  
  120.         return false;  
  121.     }  
  122.       
  123.     /** 
  124.      * 初始化操作 
  125.      * @param sql 
  126.      */  
  127.     private void init(String sql){  
  128.         if(sql != null){  
  129.             String sqlUpper = sql.trim().toUpperCase();  
  130.             this.whereIndex = getWhereIndex(sqlUpper);  
  131.             this.orderByIndex = getOrderByIndex(sqlUpper);  
  132.             this.limitIndex = getLimitIndex(sqlUpper);  
  133.             if(!hadWhereSql() && !hadOrderBySql() && !hadLimitSql()){  
  134.                 baseSql.append(sql);  
  135.             }else{  
  136.                 if(hadWhereSql()){  
  137.                     baseSql.append(sql.substring(0, whereIndex));  
  138.                 }else if(!hadWhereSql() && hadOrderBySql()){  
  139.                     if(hadOrderBySql()){  
  140.                         baseSql.append(sql.substring(0, orderByIndex));  
  141.                     }  
  142.                 }else if(!hadWhereSql() && !hadOrderBySql() && hadLimitSql()){  
  143.                     baseSql.append(sql.substring(0, limitIndex));  
  144.                 }  
  145.             }  
  146.             int whereStartIndex = whereIndex + WHERE_SQL.length();//where条件不需要加where  
  147.             if(hadWhereSql() && hadOrderBySql() && hadLimitSql()){  
  148.                 whereSql.append(sql.substring(whereStartIndex, orderByIndex));  
  149.                 orderBySql.append(sql.substring(orderByIndex, limitIndex));  
  150.                 limitSql.append(sql.substring(limitIndex));  
  151.             }else if(hadWhereSql() && hadOrderBySql() && !hadLimitSql()){  
  152.                 whereSql.append(sql.substring(whereStartIndex, orderByIndex));  
  153.                 orderBySql.append(sql.substring(orderByIndex));  
  154.             }else if(hadWhereSql() && !hadOrderBySql() && hadLimitSql()){  
  155.                 whereSql.append(sql.substring(whereStartIndex, limitIndex));  
  156.                 limitSql.append(sql.substring(limitIndex));  
  157.             }else if(hadWhereSql() && !hadOrderBySql() && !hadLimitSql()){  
  158.                 whereSql.append(sql.substring(whereStartIndex));  
  159.             }else if(!hadWhereSql() && hadOrderBySql() && hadLimitSql()){  
  160.                 orderBySql.append(sql.substring(orderByIndex, limitIndex));  
  161.                 limitSql.append(sql.substring(limitIndex));  
  162.             }else if(!hadWhereSql() && hadOrderBySql() && !hadLimitSql()){  
  163.                 orderBySql.append(sql.substring(orderByIndex));  
  164.             }else if(!hadWhereSql() && !hadOrderBySql() && hadLimitSql()){  
  165.                 limitSql.append(sql.substring(limitIndex));  
  166.             }  
  167.         }  
  168.     }  
  169.       
  170.     /** 
  171.      * and条件连接 
  172.      * @param columnName 表的字段名称 
  173.      * @param columnValue 查询字段名称对应的值 
  174.      * <li>如果whereType为Null或者NotNull,该值可以为空</li> 
  175.      * <li>如果whereType为IN,该值为List类型,如:Arrays.asList("aa", "bb")</li> 
  176.      * @param whereType WhereType枚举,如like,>= 
  177.      * @return SqlBuffer 
  178.      */  
  179.     public SqlBuffer and(String columnName, Object columnValue, WhereType whereType){  
  180.         return add(ConnectType.AND, columnName, columnValue, null, whereType);  
  181.     }  
  182.       
  183.     /** 
  184.      * or条件连接,使用columnName作为命名参数 
  185.      * @param columnName 表的字段名称 
  186.      * @param columnValue 查询字段名称对应的值 
  187.      * <li>如果whereType为Null或者NotNull,该值可以为空</li> 
  188.      * <li>如果whereType为IN,该值为List类型,如:Arrays.asList("aa", "bb")</li> 
  189.      * @param whereType WhereType枚举,如like,>= 
  190.      * @return SqlBuffer 
  191.      */  
  192.     public SqlBuffer or(String columnName, Object columnValue, WhereType whereType){  
  193.         return add(ConnectType.OR, columnName, columnValue, null, whereType);  
  194.     }  
  195.       
  196.     /** 
  197.      * 加上表字段不为空的判断 
  198.      * <li>如columnName is not null</li> 
  199.      * @param connectType ConnectType枚举 
  200.      * @param columnName 表的字段名称 
  201.      * @return SqlBuffer 
  202.      */  
  203.     public SqlBuffer isNotNull(ConnectType connectType, String columnName){  
  204.         return add(connectType, columnName, null, null, WhereType.NOT_NULL);  
  205.     }  
  206.       
  207.     /** 
  208.      * 加上表字段不为空的判断,使用And连接 
  209.      * <li>如columnName is not null</li> 
  210.      * @param columnName 表的字段名称 
  211.      * @return SqlBuffer 
  212.      */  
  213.     public SqlBuffer isNotNull(String columnName){  
  214.         return add(ConnectType.AND, columnName, null, null, WhereType.NOT_NULL);  
  215.     }  
  216.       
  217.     /** 
  218.      * 加上表字段为空的判断 
  219.      * <li>如columnName is null</li> 
  220.      * @param connectType ConnectType枚举 
  221.      * @param columnName 表的字段名称 
  222.      * @return SqlBuffer 
  223.      */  
  224.     public SqlBuffer isNull(ConnectType connectType, String columnName){  
  225.         return add(connectType, columnName, null, null, WhereType.NULL);  
  226.     }  
  227.       
  228.     /** 
  229.      * 加上表字段为空的判断,使用And连接 
  230.      * <li>如columnName is not null</li> 
  231.      * @param columnName 表的字段名称 
  232.      * @return SqlBuffer 
  233.      */  
  234.     public SqlBuffer isNull(String columnName){  
  235.         return add(ConnectType.AND, columnName, null, null, WhereType.NULL);  
  236.     }  
  237.       
  238.     /** 
  239.      * and条件连接,使用columnName作为命名参数 
  240.      * @param columnName 表的字段名称 
  241.      * @param columnValue 查询字段名称对应的值 
  242.      * <li>如果whereType为Null或者NotNull,该值可以为空</li> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值