mybatis3下的通用sql的select语句执行类
2012-02-24 22:18
357 查看
项目中为了使项目的数据库操作从dao层转移到service层,只好写一个通用的sql语句拼接类和相应的mapper类和mapper的xml文件,自定义的SelectBuilder.java的代码如下:
SqlMapper.xml的映射文件如下:
望大家指出设计的不足和程序存在的漏洞和错误。
importjava.io.IOException; importjava.util.ArrayList; importjava.util.List; importcom.util.StringHelper; /** *SelectBuilder:SQL条件拼接类,该类只做sql语句的拼接 *@authorLMH修改 *@notice注意:为了用好该类,必须熟悉掌握sql语句完整语法 *@SQLGrammarSELECT语句的完整语法为: *SELECT[ALL|DISTINCT|DISTINCTROW|TOP]<br/> *{*|talbe.*|[table.]field1[ASalias1][,[table.]field2[ASalias2][,…]]}<br/> *FROMtableexpression[,…][INexternaldatabase]<br/> *[WHERE…]<br/> *[GROUPBY…]<br/> *[HAVING…]<br/> *[ORDERBY…]<br/> *[WITHOWNERACCESSOPTION]<br/> *说明:<br/> *用中括号([])括起来的部分表示是可选的,用大括号({})括起来的部分是表示必须从中选择其中的一个。<br/> * */ publicclassSelectBuilder { privatestaticfinalStringAND=")\nAND("; privatestaticfinalStringOR=")\nOR("; List<String>select=newArrayList<String>(); List<String>from=newArrayList<String>(); List<String>join=newArrayList<String>(); List<String>innerJoin=newArrayList<String>(); List<String>outerJoin=newArrayList<String>(); List<String>leftOuterJoin=newArrayList<String>(); List<String>rightOuterJoin=newArrayList<String>(); List<Criterion>where=newArrayList<Criterion>(); List<Criterion>having=newArrayList<Criterion>(); List<String>groupBy=newArrayList<String>(); List<String>orderBy=newArrayList<String>(); List<String>lastList=newArrayList<String>(); booleandistinct; /** *设置SQL语句中从select到from的部分 *@paramconditons查詢的内容,可以写字段,聚合函数等 *@example参数例子:"table1.idasid,table2.mcasmc", *@notice注意:如果要查询的字段名有相同的,必须用as**的方式进行区分,不然会不能正确set进hashmap里 * */ publicvoidSELECT(Stringconditons) { select.add(conditons); } /** *设置SQL语句中从selectdistinct到from的部分,可以写字段,聚合函数等 *@paramcolumns查詢的字段列表 *@example参数例子:"table1.idasid,table2.mcasmc", *@notice注意:如果要查询的字段名有相同的,必须用as**的方式进行区分,不然会不能正确set进hashmap里 * */ publicvoidSELECT_DISTINCT(Stringcolumns) { distinct=true; SELECT(columns); } /** *设置SQL语句中从from到where的部分 *@paramtable:表名列表,可以是要查询的表名列表,或者直接加上join连接 *@example参数例子:"table1"或者"table1,table2" */ publicvoidFROM(Stringtable) { from.add(table); } /** *设置JOIN连接条件<br/> *切记用了join相关的链接,则form里只能有一个表,而且,join函数之后,后面不得再加from函数 *@paramjoin:JOIN连接条件,由3部分组成:表名别名+on+连接条件 *@examplejoin参数例子:"bmd_mssbonb.bmd_h=loc.x_h" */ publicvoidJOIN(Stringjoin) { this.join.add(join); } /** *设置INNER_JOIN连接条件<br/> *切记用了join相关的链接,则form里只能有一个表,而且,join函数之后,后面不得再加from函数 *@paramjoin:INNER_JOIN连接条件,由3部分组成:表名别名+on+连接条件 *@examplejoin参数例子:"bmd_mssbonb.bmd_h=loc.x_h" */ publicvoidINNER_JOIN(Stringjoin) { innerJoin.add(join); } /** *设置LEFT_OUTER_JOIN连接条件 *用了join相关的链接,则form里只能有一个表,而且,join之后,后面不得再加form函数 *@paramjoin:LEFT_OUTER_JOIN连接条件,由3部分组成:表名别名+on+连接条件 *@examplejoin参数例子:"bmd_mssbonb.bmd_h=loc.x_h" */ publicvoidLEFT_OUTER_JOIN(Stringjoin) { leftOuterJoin.add(join); } /** *设置RIGHT_OUTER_JOIN连接条件 *用了join相关的链接,则form里只能有一个表,而且,join之后,后面不得再加form函数 *@paramjoin:RIGHT_OUTER_JOIN连接条件,由3部分组成:表名别名+on+连接条件 *@examplejoin参数例子:"bmd_mssbonb.bmd_h=loc.x_h" */ publicvoidRIGHT_OUTER_JOIN(Stringjoin) { rightOuterJoin.add(join); } /** *设置OUTER_JOIN连接条件 *用了join相关的链接,则form里只能有一个表,而且,join之后,后面不得再加form函数 *@paramjoin:OUTER_JOIN连接条件,由3部分组成:表名别名+on+连接条件 *@examplejoin参数例子:"bmd_mssbonb.bmd_h=loc.x_h" */ publicvoidOUTER_JOIN(Stringjoin) { outerJoin.add(join); } //为了适应动态参数而重写的where方法(没有参数的情况) /** *设置where子语句(一个参数的情况) *@paramconditions:连接条件,没有传动态参数的查询条件部分<br/> *一个静态查询条件必须包含4部分内容:连接规则字段列运算符值<br/> *连接规则:(and或者or)<br/> *字段名:(field1)<br/> *运算符:必须以下运算符中的一个(>或者>=或者<或者<=或者<br/> *=或者<>或者!=或者like或者notlike或者in或者notin<br/> *或者between或者notbetween)<br/> *值:注意:如果元素符是in,则值部分必须写成(value1,value2...)的形式。<br/> *如果元素符是between,则值部分必须为value1andvalue2的形式,value1为min,value2为max。<br/> *如果元素符是like,则值部分可以在值的左右两边加上%或者_的通配符。<br/> *%:表示匹配任意个字符;_:表示匹配一个字符。<br/> *@exampleconditions参数例子:"andtable1.id=table2.idortable1.mc=table2.mcandtable1.idlike'%1%'" */ publicvoidWHERE(Stringconditions) { if(StringHelper.isNotBlank(conditions)) where.add(newCriterion(conditions)); } //为了适应动态参数而重写的where方法,(单个参数或者in的情况) /** *设置where子语句(两个参数的情况)支持单个参数或者in的情况 *@paramconditions:连接条件,没有传动态参数的查询条件部分,必须包含3部分内容:<br/> *连接规则:(and或者or)+字段列:(field1)+运算符<br/> *运算符:必须以下运算符中的一个(>或者>=或者<或者<=或者=或者<br/> *<>或者!=或者like或者notlike或者in或者notin)<br/> *@paramvalue:参数值,Object对象,支持任何类型<br/> *注意:如果conditions的运算符是in,则value部分可以传递一个list的对象,<br/> *list对象至少要包含一个Object类型的元素;如果conditions的运算符是like<br/> *部分,则value部分可以在"value1"值的左右两边加上%或者_的通配符。<br/> *%:表示匹配任意个字符;_:表示匹配一个字符<br/> *@exampleconditions参数例子:"andtable1.id="或者"andtable1.idlike" */ publicvoidWHERE(Stringconditions,Objectvalue) { if(value!=null)//如果非空 { if(valueinstanceofList<?>)//如果为list { if(!((List)value).isEmpty())//如果list非空 where.add(newCriterion(conditions,value)); } else//如果为其他直接添加 where.add(newCriterion(conditions,value)); } } //为了适应动态参数而重写的where方法(单个参数或者in的情况),有close关闭符号的情况 /** *设置where子语句(三个参数的情况)支持单个参数或者in的情况,有close关闭符号的情况 *@paramconditions:连接条件,没有传动态参数的查询条件部分,必须包含3部分内容:<br/> *连接规则:(and或者or)+字段列:(field1)+运算符<br/> *运算符:必须以下运算符中的一个(>或者>=或者<或者<=或者<br/> *=或者<>或者!=或者like或者notlike或者in或者notin)<br/> *@paramvalue:参数值,Object对象,支持任何类型<br/> *注意:如果conditions的运算符是in,则value部分可以传递一个list的对象,list对象至少要包含一个Object类型的元素<br/> *如果conditions的运算符是like部分,则value部分可以在"value1"值的左右两边加上%或者_的通配符<br/> *%:表示匹配任意个字符;_:表示匹配一个字符<br/> *@exampleconditions参数例子:"andtable1.id="或者"andtable1.idlike" *@paramclose:关闭符号,只有在处理or条件的时候才会用到,其他时候设置null或者"" */ publicvoidWHERE(Stringconditions,Objectvalue,Stringclose) { if(value!=null)//如果非空 { if(valueinstanceofList<?>)//如果为list { if(!((List)value).isEmpty())//如果list非空 where.add(newCriterion(conditions,value,close)); } else//如果为其他直接添加 where.add(newCriterion(conditions,value,close)); } } //为了适应动态参数而重写的where方法(between的情况),有close关闭符号的情况 /** *设置where子语句(四个参数的情况)支持between的情况,有close关闭符号的情况 *@paramconditions:连接条件,没有传动态参数的查询条件部分,必须包含3部分内容:<br/> *连接规则:(and或者or)+字段列:(field1)+运算符<br/> *运算符:必须以下运算符中的一个(between或者notbetween)<br/> *@paramvalue:参数值,Object对象,between的参数值左部分,支持任何类型<br/> *@paramsecondValue参数值,Object对象,between的参数值右部分,支持任何类型<br/> *@exampleconditions参数例子:"andtable1.id="或者"andtable1.idlike" *@paramclose:关闭符号,只有在处理or条件的时候才会用到,其他时候设置null或者"" */ publicvoidWHERE(Stringconditions,Objectvalue,ObjectsecondValue,Stringclose) { if(value!=null&&secondValue!=null) where.add(newCriterion(conditions,value,secondValue,close)); } /** *设置分组字段列表 *切记GROUP_BY里面的字段select里面的字段必须有,不然会触发sql语法错误异常 *@paramcolumns:分组字段 *@examplecolumns参数例子:"table1.id,table2.mc" */ publicvoidGROUP_BY(Stringcolumns) { groupBy.add(columns); } //为了适应动态参数而重写的having方法(没有参数的情况) /** * *设置having子语句(一个参数的情况)<br/> *切记having里面的字段groupby里面必须有,不然会触发sql语法错误异常<br/> *@paramconditions:连接条件,没有传动态参数的查询条件部分<br/> *一个静态查询条件必须包含4部分内容:连接规则字段列运算符值<br/> *连接规则:(and或者or)<br/> *字段名:(field1)<br/> *运算符:必须以下运算符中的一个(>或者>=或者<或者<=或者<br/> *=或者<>或者!=或者like或者notlike或者in或者notin<br/> *或者between或者notbetween)<br/> *值:注意:如果元素符是in,则值部分必须写成(value1,value2...)的形式。<br/> *如果元素符是between,则值部分必须为value1andvalue2的形式,value1为min,value2为max。<br/> *如果元素符是like,则值部分可以在值的左右两边加上%或者_的通配符。<br/> *%:表示匹配任意个字符;_:表示匹配一个字符。<br/> *@exampleconditions参数例子:"andtable1.id=table2.idortable1.mc=table2.mcandtable1.idlike'%1%'" */ publicvoidHAVING(Stringconditions) { if(StringHelper.isNotBlank(conditions)) having.add(newCriterion(conditions)); } //为了适应动态参数而重写的having方法(单个参数或者in的情况) /** *设置having子语句(两个参数的情况)支持单个参数或者in的情况<br/> *切记having里面的字段groupby里面必须有,不然会触发sql语法错误异常<br/> *@paramconditions:连接条件,没有传动态参数的查询条件部分,必须包含3部分内容:<br/> *连接规则:(and或者or)+字段列:(field1)+运算符<br/> *运算符:必须以下运算符中的一个(>或者>=或者<或者<=或者=或者<br/> *<>或者!=或者like或者notlike或者in或者notin)<br/> *@paramvalue:参数值,Object对象,支持任何类型<br/> *注意:如果conditions的运算符是in,则value部分可以传递一个list的对象,<br/> *list对象至少要包含一个Object类型的元素;如果conditions的运算符是like<br/> *部分,则value部分可以在"value1"值的左右两边加上%或者_的通配符。<br/> *%:表示匹配任意个字符;_:表示匹配一个字符<br/> *@exampleconditions参数例子:"andtable1.id="或者"andtable1.idlike" */ publicvoidHAVING(Stringconditions,Objectvalue) { if(value!=null)//如果非空 { if(valueinstanceofList<?>)//如果为list { if(!((List)value).isEmpty())//如果list非空 having.add(newCriterion(conditions,value)); } else//如果为其他直接添加 having.add(newCriterion(conditions,value)); } } //为了适应动态参数而重写的having方法(单个参数或者in的情况),有close关闭符号的情况 /** *设置having子语句(三个参数的情况)支持单个参数或者in的情况,有close关闭符号的情况<br/> *切记having里面的字段groupby里面必须有,不然会触发sql语法错误异常<br/> *@paramconditions:连接条件,没有传动态参数的查询条件部分,必须包含3部分内容:<br/> *连接规则:(and或者or)+字段列:(field1)+运算符<br/> *运算符:必须以下运算符中的一个(>或者>=或者<或者<=或者<br/> *=或者<>或者!=或者like或者notlike或者in或者notin)<br/> *@paramvalue:参数值,Object对象,支持任何类型<br/> *注意:如果conditions的运算符是in,则value部分必须传递一个list的对象,<br/> *list对象至少要包含一个Object类型的元素;<br/> *如果conditions的运算符是like部分,则value部分可以在"value1"值的左右两边加上%或者_的通配符<br/> *%:表示匹配任意个字符;_:表示匹配一个字符<br/> *@exampleconditions参数例子:"andtable1.id="或者"andtable1.idlike" *@paramclose:关闭符号,只有在处理or条件的时候才会用到,其他时候设置null或者"" */ publicvoidHAVING(Stringconditions,Objectvalue,Stringclose) { if(value!=null)//如果非空 { if(valueinstanceofList<?>)//如果为list { if(!((List)value).isEmpty())//如果list非空 having.add(newCriterion(conditions,value,close)); } else//如果为其他直接添加 having.add(newCriterion(conditions,value,close)); } } //为了适应动态参数而重写的having方法(between的情况),有close关闭符号的情况 /** *设置having子语句(四个参数的情况)支持between的情况,有close关闭符号的情况<br/> *切记having里面的字段groupby里面必须有,不然会触发sql语法错误异常<br/> *@paramconditions:连接条件,没有传动态参数的查询条件部分,必须包含3部分内容:<br/> *连接规则:(and或者or)+字段列:(field1)+运算符<br/> *运算符:必须以下运算符中的一个(between或者notbetween)<br/> *@paramvalue:参数值,Object对象,between的参数值左部分,支持任何类型<br/> *@paramsecondValue:参数值,Object对象,between的参数值右部分,支持任何类型<br/> *@exampleconditions参数例子:"andtable1.id="或者"andtable1.idlike" *@paramclose:关闭符号,只有在处理or条件的时候才会用到,其他时候设置null或者"" */ publicvoidHAVING(Stringconditions,Objectvalue,ObjectsecondValue,Stringclose) { if(value!=null&&secondValue!=null) having.add(newCriterion(conditions,value,secondValue,close)); } /** *设置排序子语句 *切记ORDER_BY里面的字段select里面的字段必须有,不然会触发sql语法错误异常 *@paramconditions:排序条件 *@exampleconditions参数例子:"table1.iddecs" */ publicvoidORDER_BY(Stringconditions) { orderBy.add(conditions); } privateStringBuilderselectClause(Stringkeyword, List<String>parts,Stringopen,Stringclose,Stringconjunction) { StringBuilderbuilder=newStringBuilder(); if(!parts.isEmpty()) { if(builder.length()>0) builder.append("\n"); builder.append(keyword); builder.append(""); builder.append(open); Stringlast="________"; for(inti=0,n=parts.size();i<n;i++) { Stringpart=parts.get(i); if(i>0&&!part.equals(AND)&&!part.equals(OR)&&!last.equals(AND) &&!last.equals(OR)) { builder.append(conjunction); } builder.append(part); last=part; } builder.append(close); } returnbuilder; } privatevoidwhereClause() { if(where.size()>0) { System.out.print("WHERE1=1"); for(Criterioncri:where) { if(cri.isNoValue()) System.out.print(cri.getCondition()); if(cri.isSingleValue()) System.out.print(cri.getCondition()+"'"+cri.getValue()+"'"); if(cri.isBetweenValue()) System.out.print(cri.getCondition()+"'"+cri.getValue()+"'and'"+cri.getSecondValue()+"'"); if(cri.isListValue()) { StringBuilderstrb=newStringBuilder(); strb.append(cri.getCondition()); strb.append("("); Listlist=(List)cri.getValue(); for(inti=0;i<list.size();i++) { Objecto=list.get(i); strb.append("'"+o+"'"); if(i<list.size()-1) strb.append(","); } strb.append(")"); System.out.print(strb); } //处理关闭符号的代码 if(cri.close==null||cri.close.equals("")) System.out.println(); else System.out.println(cri.close); } } } privatevoidhavingClause() { if(having.size()>0) { System.out.print("HAVING1=1"); for(Criterioncri:having) { if(cri.isNoValue()) System.out.print(cri.getCondition()); if(cri.isSingleValue()) System.out.print(cri.getCondition()+"'"+cri.getValue()+"'"); if(cri.isBetweenValue()) System.out.print(cri.getCondition()+"'"+cri.getValue()+"'and'"+cri.getSecondValue()+"'"); if(cri.isListValue()) { StringBuilderstrb=newStringBuilder(); strb.append(cri.getCondition()); strb.append("("); Listlist=(List)cri.getValue(); for(inti=0;i<list.size();i++) { Objecto=list.get(i); strb.append("'"+o+"'"); if(i<list.size()-1) strb.append(","); } strb.append(")"); System.out.print(strb); } //处理关闭符号的代码 if(cri.close==null||cri.close.equals("")) System.out.println(); else System.out.println(cri.close); } } } /** *打印由各部分组合起来的SQL语句 */ publicvoidprintSQL() { if(!select.isEmpty()) System.out.println(selectClause("SELECT",this.select,"","",",")); if(!from.isEmpty()) System.out.println(selectClause("FROM",this.from,"","",",")); if(!innerJoin.isEmpty()) System.out.println(selectClause("INNERJOIN",this.innerJoin,"","","\nINNERJOIN")); if(!outerJoin.isEmpty()) System.out.println(selectClause("OUTERJOIN",this.outerJoin,"","","\nOUTERJOIN")); if(!leftOuterJoin.isEmpty()) System.out.println(selectClause("LEFTOUTERJOIN",this.leftOuterJoin,"","","\nLEFTOUTERJOIN")); if(!rightOuterJoin.isEmpty()) System.out.println(selectClause("RIGHTOUTERJOIN",this.rightOuterJoin,"","","\nRIGHTOUTERJOIN")); whereClause(); if(!groupBy.isEmpty()) System.out.println(selectClause("GROUPBY",this.groupBy,"","",",")); havingClause(); if(!orderBy.isEmpty()) System.out.println(selectClause("ORDERBY",this.orderBy,"","",",")); } /** *Criterion:where或者having的子语句条件类 *@authorLMH修改 * */ publicclassCriterion{ privateStringcondition; privateObjectvalue; privateObjectsecondValue; privatebooleannoValue; privatebooleansingleValue; privatebooleanbetweenValue; privatebooleanlistValue; privateStringclose;//为了解决or要有括号括起来而添加的属性 privateStringtypeHandler; publicStringgetCondition(){ returncondition; } publicObjectgetValue(){ returnvalue; } publicObjectgetSecondValue(){ returnsecondValue; } publicbooleanisNoValue(){ returnnoValue; } publicbooleanisSingleValue(){ returnsingleValue; } publicbooleanisBetweenValue(){ returnbetweenValue; } publicbooleanisListValue(){ returnlistValue; } publicStringgetTypeHandler(){ returntypeHandler; } publicStringgetClose(){ returnclose; } publicvoidsetClose(Stringclose){ this.close=close; } protectedCriterion(Stringcondition){ super(); this.condition=condition; this.typeHandler=null; this.noValue=true; } protectedCriterion(Stringcondition,Objectvalue){ this.condition=condition; this.value=value; if(valueinstanceofList<?>){ this.listValue=true; }else{ this.singleValue=true; } } protectedCriterion(Stringcondition,Objectvalue,Stringclose){ this.condition=condition; this.value=value; this.close=close; if(valueinstanceofList<?>){ this.listValue=true; }else{ this.singleValue=true; } } protectedCriterion(Stringcondition,Objectvalue,ObjectsecondValue,Stringclose){ super(); this.condition=condition; this.value=value; this.secondValue=secondValue; this.close=close; this.betweenValue=true; } } publicstaticvoidmain(String[]args)throwsIOException{ SelectBuildersql=newSelectBuilder(); sql.SELECT("l.mc,l.id"); System.out.println(sql.selectClause("SELECT",sql.select,"","",",")); sql.FROM("bmdl,useru"); System.out.println(sql.selectClause("FROM",sql.from,"","",",")); sql.INNER_JOIN("DEPARTMENTDonD.ID=P.DEPARTMENT_ID"); System.out.println(sql.selectClause("INNERJOIN",sql.innerJoin,"","","\nINNERJOIN")); sql.WHERE("l.id=u.id"); Listl=newArrayList(); l.add("1"); l.add("2"); sql.WHERE("or(l.idin",l); sql.WHERE("andl.idbetween",1,2,")"); sql.whereClause(); sql.GROUP_BY("l.id,u.id"); System.out.println(sql.selectClause("GROUPBY",sql.groupBy,"","",",")); sql.HAVING("l.id=u.id"); Listl2=newArrayList(); l2.add("1"); l2.add("2"); sql.HAVING("or(l.idin",l2); sql.HAVING("andl.idbetween","1","2",")"); sql.havingClause(); } /**********get方法*******/ publicList<String>getSelect(){ returnselect; } publicList<String>getFrom(){ returnfrom; } publicList<String>getJoin(){ returnjoin; } publicList<String>getInnerJoin(){ returninnerJoin; } publicList<String>getOuterJoin(){ returnouterJoin; } publicList<String>getLeftOuterJoin(){ returnleftOuterJoin; } publicList<String>getRightOuterJoin(){ returnrightOuterJoin; } publicList<Criterion>getWhere(){ returnwhere; } publicList<Criterion>getHaving(){ returnhaving; } publicList<String>getGroupBy(){ returngroupBy; } publicList<String>getOrderBy(){ returnorderBy; } publicbooleanisDistinct(){ returndistinct; } }
SqlMapper.xml的映射文件如下:
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mappernamespace="com.dao.SqlMapper"> <!--方法一:支持單表或者多表的通用查詢方法By梁敏鸿2012年2月24日--> <selectid="selectBySql"resultType="map"parameterType="com.common.SelectBuilder"> <!--1.SQL语句的基本部分--> <iftest="distinct"> <iftest="!select.empty"> SELECTDISTINCT </if> <foreachcollection="select"item="select_content"open=""separator=","close=""> ${select_content} </foreach> </if> <iftest="!distinct"> <iftest="!select.empty"> SELECT </if> <foreachcollection="select"item="select_content"open=""separator=","close=""> ${select_content} </foreach> </if> <iftest="!from.empty"> FROM <foreachcollection="from"item="from_content"open=""separator=","close=""> ${from_content} </foreach> </if> <!--2.join查询部分--> <iftest="!join.empty"> JOIN <foreachcollection="join"item="join_content"open=""separator="\nJOIN"close=""> ${join_content} </foreach> </if> <iftest="!innerJoin.empty"> INNERJOIN <foreachcollection="innerJoin"item="innerJoin_content"open=""separator="\nINNERJOIN"close=""> ${innerJoin_content} </foreach> </if> <iftest="!outerJoin.empty"> OUTERJOIN <foreachcollection="outerJoin"item="outerJoin_content"open=""separator="\nOUTERJOIN"close=""> ${outerJoin_content} </foreach> </if> <iftest="!leftOuterJoin.empty"> LEFTOUTERJOIN <foreachcollection="leftOuterJoin"item="leftOuterJoin_content"open=""separator="\nLEFTOUTERJOIN"close=""> ${leftOuterJoin_content} </foreach> </if> <iftest="!rightOuterJoin.empty"> RIGHTOUTERJOIN <foreachcollection="rightOuterJoin"item="rightOuterJoin_content"open=""separator="\nRIGHTOUTERJOIN"close=""> ${rightOuterJoin_content} </foreach> </if> <!--3.SQL语句WHERE部分--> <iftest="!where.empty"> where1=1 <foreachcollection="where"item="where_Criterion"open=""separator=""close=""> <choose> <whentest="where_Criterion.noValue"> ${where_Criterion.condition} </when> <whentest="where_Criterion.singleValue"> ${where_Criterion.condition}#{where_Criterion.value} </when> <whentest="where_Criterion.betweenValue"> ${where_Criterion.condition}#{where_Criterion.value}and#{where_Criterion.secondValue} </when> <whentest="where_Criterion.listValue"> ${where_Criterion.condition} <foreachcollection="where_Criterion.value"item="listItem"open="("close=")"separator=","> #{listItem} </foreach> </when> </choose> <!--加上close关闭符号,为了处理or的括号包含的情况--> <iftest="where_Criterion.close!=nullandwhere_Criterion.close!=''"> ${where_Criterion.close} </if> </foreach> </if> <!--4.SQL语句GROUPBY部分--> <iftest="!groupBy.empty"> GROUPBY <foreachcollection="groupBy"item="groupBy_content"open=""separator=","close=""> ${groupBy_content} </foreach> </if> <!--5.SQL语句HAVING部分--> <iftest="!having.empty"> HAVING1=1 <foreachcollection="having"item="having_Criterion"open=""separator=""close=""> <choose> <whentest="having_Criterion.noValue"> ${having_Criterion.condition} </when> <whentest="having_Criterion.singleValue"> ${having_Criterion.condition}#{having_Criterion.value} </when> <whentest="having_Criterion.betweenValue"> ${having_Criterion.condition}#{having_Criterion.value}and#{having_Criterion.secondValue} </when> <whentest="having_Criterion.listValue"> ${having_Criterion.condition} <foreachcollection="having_Criterion.value"item="listItem"open="("close=")"separator=","> #{listItem} </foreach> </when> </choose> <!--加上close关闭符号,为了处理or的括号包含的情况--> <iftest="having_Criterion.close!=nullandhaving_Criterion.close!=''"> ${having_Criterion.close} </if> </foreach> </if> <!--6.SQL语句ORDERBY部分--> <iftest="!orderBy.empty"> ORDERBY <foreachcollection="orderBy"item="orderBy_content"open=""separator=","close=""> ${orderBy_content} </foreach> </if> </select> <!--方法二:支持單表或者多表的通用统计方法Bylmh2012年2月24日--> <selectid="countBySql"resultType="int"parameterType="com.common.SelectBuilder"> <iftest="distinct"> <iftest="!select.empty"> SELECTDISTINCT </if> COUNT(*) </if> <iftest="!distinct"> <iftest="!select.empty"> SELECT </if> COUNT(*) </if> <iftest="!from.empty"> FROM <foreachcollection="from"item="from_content"open=""separator=","close=""> ${from_content} </foreach> </if> <iftest="!join.empty"> JOIN <foreachcollection="join"item="join_content"open=""separator="\nJOIN"close=""> ${join_content} </foreach> </if> <iftest="!innerJoin.empty"> INNERJOIN <foreachcollection="innerJoin"item="innerJoin_content"open=""separator="\nINNERJOIN"close=""> ${innerJoin_content} </foreach> </if> <iftest="!outerJoin.empty"> OUTERJOIN <foreachcollection="outerJoin"item="outerJoin_content"open=""separator="\nOUTERJOIN"close=""> ${outerJoin_content} </foreach> </if> <iftest="!leftOuterJoin.empty"> LEFTOUTERJOIN <foreachcollection="leftOuterJoin"item="leftOuterJoin_content"open=""separator="\nLEFTOUTERJOIN"close=""> ${leftOuterJoin_content} </foreach> </if> <iftest="!rightOuterJoin.empty"> RIGHTOUTERJOIN <foreachcollection="rightOuterJoin"item="rightOuterJoin_content"open=""separator="\nRIGHTOUTERJOIN"close=""> ${rightOuterJoin_content} </foreach> </if> <iftest="!where.empty"> where1=1 <foreachcollection="where"item="where_Criterion"open=""separator=""close=""> <choose> <whentest="where_Criterion.noValue"> ${where_Criterion.condition} </when> <whentest="where_Criterion.singleValue"> ${where_Criterion.condition}#{where_Criterion.value} </when> <whentest="where_Criterion.betweenValue"> ${where_Criterion.condition}#{where_Criterion.value}and#{where_Criterion.secondValue} </when> <whentest="where_Criterion.listValue"> ${where_Criterion.condition} <foreachcollection="where_Criterion.value"item="listItem"open="("close=")"separator=","> #{listItem} </foreach> </when> </choose> <iftest="where_Criterion.close!=nullandwhere_Criterion.close!=''"> ${where_Criterion.close} </if> </foreach> </if> <iftest="!groupBy.empty"> GROUPBY <foreachcollection="groupBy"item="groupBy_content"open=""separator=","close=""> ${groupBy_content} </foreach> </if> <iftest="!having.empty"> HAVING1=1 <foreachcollection="having"item="having_Criterion"open=""separator=""close=""> <choose> <whentest="having_Criterion.noValue"> ${having_Criterion.condition} </when> <whentest="having_Criterion.singleValue"> ${having_Criterion.condition}#{having_Criterion.value} </when> <whentest="having_Criterion.betweenValue"> ${having_Criterion.condition}#{having_Criterion.value}and#{having_Criterion.secondValue} </when> <whentest="having_Criterion.listValue"> ${having_Criterion.condition} <foreachcollection="having_Criterion.value"item="listItem"open="("close=")"separator=","> #{listItem} </foreach> </when> </choose> <iftest="having_Criterion.close!=nullandhaving_Criterion.close!=''"> ${having_Criterion.close} </if> </foreach> </if> <iftest="!orderBy.empty"> ORDERBY <foreachcollection="orderBy"item="orderBy_content"open=""separator=","close=""> ${orderBy_content} </foreach> </if> </select> </mapper>
望大家指出设计的不足和程序存在的漏洞和错误。
相关文章推荐
- mybatis一个select标签执行多个sql语句
- mybatis执行查询语句查询不到结果,控制台输出sql语句复制到数据库中能查到数据
- MyBatis-Spring 执行SQL语句的流程
- MyBatis源码分析-SQL语句执行的完整流程
- mybatis如何在控制台打印执行的sql语句
- ibatis3中执行SelectBuilder/SqlBuilder生成的SQL语句。
- mysql5.7执行sql语句报错:In aggregated query without GROUP BY, expression #1 of SELECT list contains nonagg
- 使用MyBatis一次执行多条SQL语句,需要配置jdbc连接串
- MyBatis中,传String类型参数到xml配置文件,并执行SQL语句后返回String类型的结果写法(jeesite)
- mybatis 打印sql执行语句
- MyBatis源码分析-SQL语句执行的完整流程
- Spring配置Mybatis执行原生SQL语句
- MyBatis执行SQL返回0,SQL语句在数据库正常返回数据,Toal: 0
- mybatis 动态sql 根据条件执行相应的语句
- mysql5.7执行sql语句报错:In aggregated query without GROUP BY, expression #1 of SELECT list contains nonagg
- sql语句中有insert然后有个select@@identity,该怎么执行
- 在mybatis执行SQL语句之前进行拦截处理
- SQL查询语句中select from where group by having order by的执行顺序
- SQL中SELECT语句的执行顺序
- Mybatis中的resultType和resultMap(mybatis执行ddl语句和特殊sql语句)