代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
自动生成sql 的where 语句
作者:
12叔
/ 发布于
2014/4/3
/
378
package com.jayqqaa12.model.easyui; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.apache.commons.lang.StringUtils; import com.jayqqaa12.jbase.jfinal.ext.Model; import com.jayqqaa12.jbase.util.Txt; import com.jayqqaa12.jbase.util.Validate; import com.jfinal.core.Controller; import com.jfinal.plugin.activerecord.TableInfo; import com.jfinal.plugin.activerecord.TableInfoMapping; /*** * * 基于约定 其他人不要使用 * * 可用来生成 where * * @author 12 * */ public class Form { // public Map<String, String> pramMap = new HashMap<String, String>(); public Map<String, String> fromMap = new HashMap<String, String>(); public Set<String> fuzzySerach = new HashSet<String>(); public Set<String> integerValue = new HashSet<String>(); public Set<String> inValue = new HashSet<String>(); public Set<String> ninValue = new HashSet<String>(); private String tableName = ""; String where = " where 1=1 "; public Form() { fuzzySerach.add("name"); fuzzySerach.add("title"); integerValue.add("status"); integerValue.add("type"); integerValue.add("operation"); } public Form(String tableName) { this(); if (!Validate.isEmpty(tableName)) this.tableName = tableName; } /*** * 生成 many to many sql * 表默认名称 改为 Class。getSimpleName。toLowerCase * 根据 主键 关联 * * @param manyClazz1 * @param manyClazz2 */ public String manyToMany(Class<? extends Model> manyClazz1 ,Class<? extends Model> manyClazz2,String midTableName){ String sql = ""; TableInfo table1 = TableInfoMapping.me().getTableInfo(manyClazz1); TableInfo table2 = TableInfoMapping.me().getTableInfo(manyClazz2); String name1 = manyClazz1.getSimpleName().toLowerCase(); String name2 =manyClazz2.getSimpleName().toLowerCase(); sql+=" select `"+name1+"`.* "+",`"+name2+"`.* " + " from "+table1.getTableName() +" as `"+name1 +"` join "+midTableName+ " on "+midTableName+"."+name1+"_"+table1.getPrimaryKey()+ "=`"+name1+"`."+table1.getPrimaryKey()+ " join "+table2.getTableName() +" as `"+name2+ "` on "+midTableName+"."+name2+"_"+table2.getPrimaryKey()+ "=`"+name2+"`."+table2.getPrimaryKey(); return sql; } /*** * 生成 one to many sql * 表默认名称 改为 Class。getSimpleName。toLowerCase * 根据 主键 关联 * * @param oneClazz * @param manyclazz */ public String oneToMany(Class<? extends Model> oneClazz ,Class<? extends Model> manyclazz){ String sql = ""; TableInfo onetable = TableInfoMapping.me().getTableInfo(oneClazz); TableInfo manytable = TableInfoMapping.me().getTableInfo(manyclazz); String oneName = oneClazz.getSimpleName().toLowerCase(); String manyName =manyclazz.getSimpleName().toLowerCase(); sql+=" select `"+oneName+"`.* "+",`"+manyName+"`.* " + " from "+onetable.getTableName() +" as `"+oneName + "` join "+manytable.getTableName()+" as `"+manyName+ "` on `"+oneName+"`."+onetable.getPrimaryKey()+ "=`"+manyName+"`."+manytable.getPrimaryKey(); return sql; } public static Form getForm(String tableName, Controller c, String... params) { Form form = new Form(tableName); for (String key : params) { form.fromMap.put(key, c.getPara(key, null)); } Enumeration<String> names = c.getParaNames(); while (names.hasMoreElements()) { String name = names.nextElement(); if (name.contains(".")) form.addFrom(form, name, c.getPara(name, null)); else if(name.contains("-")) form.addFrom(form, name, c.getPara(name,null)); } return form; } public void addFrom(Form form, String name, String value) { if (name.endsWith("-*")) { String newName = Txt.split(name, "-*")[0]; form.fuzzySerach.add(newName); form.fromMap.put(newName, value); } else if (name.endsWith("-i")) { String newName = Txt.split(name, "-i")[0]; form.integerValue.add(newName); form.fromMap.put(newName, value); } else if (name.endsWith("-in")) { String newName = Txt.split(name, "-in")[0]; form.inValue.add(newName); form.fromMap.put(newName, value); } else if (name.endsWith("-nin")) { String newName = Txt.split(name, "-nin")[0]; form.ninValue.add(newName); form.fromMap.put(newName, value); } else if (name.endsWith("-s")) { String newName = Txt.split(name, "-s")[0]; form.ninValue.add(newName); form.fromMap.put(newName, value); } } /** * 手动添加where * * @param where * @return */ public String addWhere(String where) { return this.where += " " + where; } // // /** // * 手动添加where 到getwhere的基础上 // * // * @param where // * @return // */ // public String getWhere(String where) // { // where += getWhere()+where; // return where; // } public String getFromParm(String key) { return fromMap.get(key); } public String setFromParm(String key, String value) { return fromMap.put(key, value); } public String getWhere(DataGrid dg) { if (getWhere().contains("date")) dg.sortOrder = "asc"; return getWhere() + sort(dg.sortName, dg.sortOrder); } /*** * '%Y-%m-%d' * * @return */ public String groupDate() { return " GROUP BY DATE_FORMAT(date,'%Y-%m-%d')"; } public String getWhereGroupDate() { return getWhere() + groupDate(); } public String getCountSql(String string) { return null; } /*** * 设置自己的where * * @param dg * @param where * @return */ public String getWhereAndLimit(DataGrid dg, String where) { int page = dg.page; if (page > 0) page -= 1; return where + limit(page, dg.total); } public String getWhereAndLimit(DataGrid dg) { int page = dg.page; if (page > 0) page -= 1; return getWhere(dg) + limit(page, dg.total); } public String getWhere() { if (!Validate.isEmpty(tableName) && !tableName.contains(".")) tableName += "."; where = " where 1=1 "; for (String key : fromMap.keySet()) { String value = fromMap.get(key); String namespace =tableName; if(key.contains(".")) namespace=""; if (StringUtils.isNotBlank(value)) { if ("dateStart".equals(key)) gteq(namespace + "date", quotation(value)); else if ("dateEnd".equals(key)) lteq(namespace + "date", quotation(value)); else if ("createdateStart".equals(key)) gteq(namespace + "createdate", quotation(value)); else if ("createdateEnd".equals(key)) lteq(namespace + "createdate", quotation(value)); else if ("modifydateStart".equals(key)) gteq(namespace + "modifydate", quotation(value)); else if ("modifydateEnd".equals(key)) lteq(namespace + "modifydate", quotation(value)); else if (fuzzySerach.contains(key)) like(namespace + key, value); else if (integerValue.contains(key)) where += " and " + namespace + key + "=" + value + " "; else if (inValue.contains(key)) where += " and " + namespace + key + " in (" + value + ")"; else if (ninValue.contains(key)) where += " and " + namespace + key + " not in (" + value + ")"; else where += " and " + namespace + key + "=" + quotation(value) + " "; } } return where; } public String sort(String sortName, String sortOrder) { if (Validate.isEmpty(sortName)) return ""; else return " order by " + sortName + " " + sortOrder; } public String limit(int page, int size) { return " limit " + size * page + "," + size; } public String limit(int size) { return " limit " + size; } /*** * >= * * @param key * @param value * @return */ public String gteq(String key, String value) { return where += " and " + key + ">=" + value; } /*** * <= * * @param key * @param value * @return */ public String lteq(String key, String value) { return where += " and " + key + "<=" + value; } /*** * > * * @param key * @param value * @return */ public String gt(String key, String value) { return where += " and " + key + ">" + value; } /*** * < * * @param key * @param value * @return */ public String lt(String key, String value) { return where += " and " + key + "<" + value; } public String quotation(String value) { return "'" + value + "'"; } public String like(String key, String value) { return where += " and " + key + " like '%" + value + "%'"; } }
试试其它关键字
自动生成sql
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
12叔
贡献的其它代码
(
1
)
.
自动生成sql 的where 语句
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3