@Test public void testDynamicSql() throws Exception { final Map<String, Object> params = Maps.newHashMap(); params.put("ab", 1); final Pair<String, List<Object>> sqlPair = Dao.dynamicSql( "test.test1", params); System.out.println("sqlPair = " + sqlPair); }
public static Pair<String, List<Object>> dynamicSql(String sqlId, Map<String, Object> param) { String sqlTemplete = SqlKit.sql(sqlId); if (null == sqlTemplete || sqlTemplete.isEmpty()) { logger.error("sql语句不存在：sql id是" + sqlId); return null; } if (param == null) { param = Maps.newHashMap(); } String sql = Freemarkers.render(sqlTemplete, param); List<Object> params = Lists.newArrayList(); Set<String> keySet = param.keySet(); for (String key : keySet) { if (param.get(key) == null) { break; } Object paramValue = param.get(key); final String placeChar = StringPool.HASH + key + StringPool.HASH; if (StringUtils.containsIgnoreCase(sql, placeChar)) { sql = StringUtils.replace(sql, placeChar, StringPool.QUESTION_MARK); params.add(paramValue); } } return Pair.of(sql.replaceAll("[\\s]{2,}", " "), params); }
Dao { public static Pair<String, List<Object>> dynamicSql(String sqlId, Map<String, Object> param) { String sqlTemplete = SqlKit.sql(sqlId); if (null == sqlTemplete || sqlTemplete.isEmpty()) { logger.error("sql语句不存在：sql id是" + sqlId); return null; } if (param == null) { param = Maps.newHashMap(); } String sql = Freemarkers.render(sqlTemplete, param); List<Object> params = Lists.newArrayList(); Set<String> keySet = param.keySet(); for (String key : keySet) { if (param.get(key) == null) { break; } Object paramValue = param.get(key); final String placeChar = StringPool.HASH + key + StringPool.HASH; if (StringUtils.containsIgnoreCase(sql, placeChar)) { sql = StringUtils.replace(sql, placeChar, StringPool.QUESTION_MARK); params.add(paramValue); } } return Pair.of(sql.replaceAll("[\\s]{2,}", " "), params); } }
Dao { public static Pair<String, List<Object>> dynamicSql(String sqlId, Map<String, Object> param) { String sqlTemplete = SqlKit.sql(sqlId); if (null == sqlTemplete || sqlTemplete.isEmpty()) { logger.error("sql语句不存在：sql id是" + sqlId); return null; } if (param == null) { param = Maps.newHashMap(); } String sql = Freemarkers.render(sqlTemplete, param); List<Object> params = Lists.newArrayList(); Set<String> keySet = param.keySet(); for (String key : keySet) { if (param.get(key) == null) { break; } Object paramValue = param.get(key); final String placeChar = StringPool.HASH + key + StringPool.HASH; if (StringUtils.containsIgnoreCase(sql, placeChar)) { sql = StringUtils.replace(sql, placeChar, StringPool.QUESTION_MARK); params.add(paramValue); } } return Pair.of(sql.replaceAll("[\\s]{2,}", " "), params); }  }
Dao { public static Pair<String, List<Object>> dynamicSql(String sqlId, Map<String, Object> param) { String sqlTemplete = SqlKit.sql(sqlId); if (null == sqlTemplete || sqlTemplete.isEmpty()) { logger.error("sql语句不存在：sql id是" + sqlId); return null; } if (param == null) { param = Maps.newHashMap(); } String sql = Freemarkers.render(sqlTemplete, param); List<Object> params = Lists.newArrayList(); Set<String> keySet = param.keySet(); for (String key : keySet) { if (param.get(key) == null) { break; } Object paramValue = param.get(key); final String placeChar = StringPool.HASH + key + StringPool.HASH; if (StringUtils.containsIgnoreCase(sql, placeChar)) { sql = StringUtils.replace(sql, placeChar, StringPool.QUESTION_MARK); params.add(paramValue); } } return Pair.of(sql.replaceAll("[\\s]{2,}", " "), params); }  static Pair<String, List<Object>> dynamicSql(String sqlId, Map<String, Object> param); static boolean isNew(M m); static List<Record> findBy(SqlSelect sqlSelect); static Record findOne(SqlSelect sqlSelect); static boolean deleteByIds(Serializable[] ids, Class<M> modelClass); static boolean isNew(M m, String pk_column); static Page<Record> paginate(String sqlPaginatePrefix,
                                        PageDto pageDto); static String oraclePaginateSql(String sql); }
Dao { public static Pair<String, List<Object>> dynamicSql(String sqlId, Map<String, Object> param) { String sqlTemplete = SqlKit.sql(sqlId); if (null == sqlTemplete || sqlTemplete.isEmpty()) { logger.error("sql语句不存在：sql id是" + sqlId); return null; } if (param == null) { param = Maps.newHashMap(); } String sql = Freemarkers.render(sqlTemplete, param); List<Object> params = Lists.newArrayList(); Set<String> keySet = param.keySet(); for (String key : keySet) { if (param.get(key) == null) { break; } Object paramValue = param.get(key); final String placeChar = StringPool.HASH + key + StringPool.HASH; if (StringUtils.containsIgnoreCase(sql, placeChar)) { sql = StringUtils.replace(sql, placeChar, StringPool.QUESTION_MARK); params.add(paramValue); } } return Pair.of(sql.replaceAll("[\\s]{2,}", " "), params); }  static Pair<String, List<Object>> dynamicSql(String sqlId, Map<String, Object> param); static boolean isNew(M m); static List<Record> findBy(SqlSelect sqlSelect); static Record findOne(SqlSelect sqlSelect); static boolean deleteByIds(Serializable[] ids, Class<M> modelClass); static boolean isNew(M m, String pk_column); static Page<Record> paginate(String sqlPaginatePrefix,
                                        PageDto pageDto); static String oraclePaginateSql(String sql);  }