直接正文,方便处理 sql 参数
import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import com.jfinal.plugin.activerecord.SqlPara; import com.jfinal.template.Engine; /** * object.toPara(_SQL_PARA_) */ public class ParaExt { public static class ExtensionMethod { public static void init() { Map<Class<?>, Class<?>> map = new HashMap<Class<?>, Class<?>>(8); map.put(String.class, StringExt.class); map.put(Character.class, CharacterExt.class); map.put(Integer.class, IntegerExt.class); map.put(Long.class, LongExt.class); map.put(Float.class, FloatExt.class); map.put(Double.class, DoubleExt.class); map.put(Short.class, ShortExt.class); map.put(Byte.class, ByteExt.class); for (Entry<Class<?>, Class<?>> e : map.entrySet()) { Engine.addExtensionMethod(e.getKey(), e.getValue()); } } } public static class StringExt { public String toPara(String self, SqlPara sqlPara) { sqlPara.addPara(self); return "?"; } } public static class CharacterExt { public String toPara(Character self, SqlPara sqlPara) { sqlPara.addPara(self); return "?"; } } public static class IntegerExt { public String toPara(Integer self, SqlPara sqlPara) { sqlPara.addPara(self); return "?"; } } public static class LongExt { public String toPara(Long self, SqlPara sqlPara) { sqlPara.addPara(self); return "?"; } } public static class FloatExt { public String toPara(Float self, SqlPara sqlPara) { sqlPara.addPara(self); return "?"; } } public static class DoubleExt { public String toPara(Double self, SqlPara sqlPara) { sqlPara.addPara(self); return "?"; } } public static class ShortExt { public String Short(Short self, SqlPara sqlPara) { sqlPara.addPara(self); return "?"; } } public static class ByteExt { public String toPara(Byte self, SqlPara sqlPara) { sqlPara.addPara(self); return "?"; } } }
1、初始化:ParaExt.ExtensionMethod.init()
2、案例:#(title.toPara(_SQL_PARA_))
select * from blog where title like #(('%' + title + '%').toPara(_SQL_PARA_))
SqlPara 如下:
Sql: select * from blog where title like ?
Para: [%aaa%]
补充:新增其它用法
import java.lang.reflect.Array; import java.util.Enumeration; import java.util.Iterator; import com.jfinal.plugin.activerecord.SqlPara; public class SqlObject { public static String PARA(SqlPara sqlPara, Object obj) { sqlPara.addPara(obj); return "?"; } public static String RLIKE(SqlPara sqlPara, Object obj) { return PARA(sqlPara, obj + "%"); } public static String LLIKE(SqlPara sqlPara, Object obj) { return PARA(sqlPara, "%" + obj); } public static String LIKE(SqlPara sqlPara, Object obj) { return PARA(sqlPara, "%" + obj + "%"); } public static String IN(SqlPara sqlPara, Object obj) { Class<?> clazz = obj.getClass(); StringBuilder sb = new StringBuilder(); boolean isFrist = true; if (clazz.isArray()) { for (int i = 0, length = Array.getLength(obj); i < length; i++) { isFrist = append(sqlPara, sb, Array.get(obj, i), isFrist); } } else if (obj instanceof Iterable<?>) { Iterator<?> it = ((Iterable<?>) obj).iterator(); while (it.hasNext()) { isFrist = append(sqlPara, sb, it.next(), isFrist); } } else if (obj instanceof Enumeration<?>) { Enumeration<?> enu = (Enumeration<?>) obj; while (enu.hasMoreElements()) { isFrist = append(sqlPara, sb, enu.nextElement(), isFrist); } } else { throw new RuntimeException("当前类型不是数组类型:" + clazz.getName()); } return sb.toString(); } private static boolean append(SqlPara sqlPara, StringBuilder sb, Object obj, boolean isFrist) { if (!isFrist) { sb.append(","); } isFrist = false; sqlPara.addPara(obj); sb.append("?"); return isFrist; } }
1、初始化:engine.addSharedObject("so", new SqlObject())
2、案例:name="张三",hobby=["唱歌","跳舞"]
select * from person p where name < #(so.LIKE(_SQL_PARA_,name)) and hobby in (#(so.IN(_SQL_PARA_,hobbys)))
SqlPara 如下:
Sql: select * from person p where name < ?) and hobby in (?,?)
Para: [张三, 唱歌, 跳舞]