diff --git a/pom.xml b/pom.xml index 55173e8..78344aa 100644 --- a/pom.xml +++ b/pom.xml @@ -1,32 +1,62 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - org.wzy - sqltemplate - 0.0.1-SNAPSHOT - jar + org.wzy + sqltemplate + 0.0.1-SNAPSHOT + jar - sqltemplate - http://maven.apache.org + sqltemplate + http://maven.apache.org - - UTF-8 - + + UTF-8 + + + + + junit + junit + 4.11 + test + + + ognl + ognl + 3.0.8 + + + cn.hutool + hutool-all + 5.0.7 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + utf-8 + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + + + + + + - - - junit - junit - 4.11 - test - - - ognl - ognl - 3.0.8 - - - - diff --git a/src/main/java/org/wzy/sqltemplate/Context.java b/src/main/java/org/wzy/sqltemplate/Context.java index 3dab02a..e10af0b 100644 --- a/src/main/java/org/wzy/sqltemplate/Context.java +++ b/src/main/java/org/wzy/sqltemplate/Context.java @@ -9,110 +9,109 @@ import ognl.OgnlException; import ognl.OgnlRuntime; import ognl.PropertyAccessor; +import org.wzy.sqltemplate.utils.CommonUtil; /** - * * @author Wen - * */ public class Context { - - static { - OgnlRuntime.setPropertyAccessor(HashMap.class, new ContextAccessor()); - } + static { + OgnlRuntime.setPropertyAccessor(HashMap.class, new ContextAccessor()); + } - public static final String BINDING_DATA = "_data"; + public static final String BINDING_DATA = "_data"; - private Configuration cfg; + private Configuration cfg; - private Map binding; + private Map binding; - private StringBuilder sql = new StringBuilder(); + private StringBuilder sql = new StringBuilder(); - private List parameter; + private List parameter; - private int uniqueNumber = 0; + private int uniqueNumber = 0; - public Context(Configuration cfg, Object data) { - this.cfg = cfg; - binding = new HashMap(); - parameter = new ArrayList(); - binding.put(BINDING_DATA, data); - } + public Context(Configuration cfg, Object data) { + this.cfg = cfg; + binding = new HashMap(); + binding.put("util", new CommonUtil()); + parameter = new ArrayList(); + binding.put(BINDING_DATA, data); + } - public void bind(String key, Object value) { - binding.put(key, value); - } + public void bind(String key, Object value) { + binding.put(key, value); + } - public void appendSql(String sqlFragement) { - sql.append(sqlFragement).append(" "); - } + public void appendSql(String sqlFragement) { + sql.append(sqlFragement).append(" "); + } - public Map getBinding() { - return this.binding; - } + public Map getBinding() { + return this.binding; + } - public List getParameter() { - return this.parameter; - } + public List getParameter() { + return this.parameter; + } - public void addParameter(Object parameter) { - this.parameter.add(parameter); - } + public void addParameter(Object parameter) { + this.parameter.add(parameter); + } - public String getSql() { - return sql.toString(); - } + public String getSql() { + return sql.toString(); + } - public void setSql(String sql) { - this.sql = new StringBuilder(sql); - } + public void setSql(String sql) { + this.sql = new StringBuilder(sql); + } - public int getUniqueNumber() { - return ++uniqueNumber; - } + public int getUniqueNumber() { + return ++uniqueNumber; + } - public Configuration getConfiguration() { - return this.cfg; - } + public Configuration getConfiguration() { + return this.cfg; + } - static class ContextAccessor implements PropertyAccessor { + static class ContextAccessor implements PropertyAccessor { - public Object getProperty(Map context, Object target, Object name) - throws OgnlException { - Map map = (Map) target; + public Object getProperty(Map context, Object target, Object name) + throws OgnlException { + Map map = (Map)target; - Object result = map.get(name); - if (result != null) { - return result; - } + Object result = map.get(name); + if (result != null) { + return result; + } - Object parameterObject = map.get(BINDING_DATA); - if (parameterObject instanceof Map) { - return ((Map) parameterObject).get(name); - } + Object parameterObject = map.get(BINDING_DATA); + if (parameterObject instanceof Map) { + return ((Map)parameterObject).get(name); + } - return null; - } + return null; + } - public void setProperty(Map context, Object target, Object name, - Object value) throws OgnlException { - Map map = (Map) target; - map.put(name, value); - } + public void setProperty(Map context, Object target, Object name, + Object value) throws OgnlException { + Map map = (Map)target; + map.put(name, value); + } - public String getSourceAccessor(OgnlContext arg0, Object arg1, - Object arg2) { - // TODO Auto-generated method stub - return null; - } + public String getSourceAccessor(OgnlContext arg0, Object arg1, + Object arg2) { + // TODO Auto-generated method stub + return null; + } - public String getSourceSetter(OgnlContext arg0, Object arg1, Object arg2) { - // TODO Auto-generated method stub - return null; - } + public String getSourceSetter(OgnlContext arg0, Object arg1, Object arg2) { + // TODO Auto-generated method stub + return null; + } - } + } } diff --git a/src/main/java/org/wzy/sqltemplate/SqlTemplate.java b/src/main/java/org/wzy/sqltemplate/SqlTemplate.java index 16c1390..464d09a 100644 --- a/src/main/java/org/wzy/sqltemplate/SqlTemplate.java +++ b/src/main/java/org/wzy/sqltemplate/SqlTemplate.java @@ -176,7 +176,7 @@ private Document buildXml(String templateContent) public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { - return new InputSource(SqlTemplate.class.getResourceAsStream("script-1.0.dtd")); + return new InputSource(SqlTemplate.class.getResourceAsStream("/script-1.0.dtd")); } }); builder.setErrorHandler(new ErrorHandler() { diff --git a/src/main/java/org/wzy/sqltemplate/utils/CommonUtil.java b/src/main/java/org/wzy/sqltemplate/utils/CommonUtil.java new file mode 100644 index 0000000..c3fa440 --- /dev/null +++ b/src/main/java/org/wzy/sqltemplate/utils/CommonUtil.java @@ -0,0 +1,102 @@ +package org.wzy.sqltemplate.utils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +import cn.hutool.core.date.DateTime; +import cn.hutool.core.date.DateUtil; + +/** + * 便于模板中日期的一些操作 + * + * @author xingchuan.qxc + * @since 2020/03/23 + */ +public class CommonUtil { + + /** + * 通过一个日期的入参,得出N天后的日期 + * + * @param date 格式yyyyMMdd + * @param days 加多少天 + * @return 计算之后的日期,格式yyyyMMdd + */ + public String addDays(String date, int days) { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); + try { + Date d = simpleDateFormat.parse(date); + Calendar calendar = Calendar.getInstance(); + calendar.setTime(d); + calendar.add(Calendar.DATE, days); + return simpleDateFormat.format(calendar.getTime()); + } catch (ParseException e) { + throw new RuntimeException(e); + } + } + + /** + * 给一个指定日期,返回这个日期对应的星期的星期一是哪天 + * + * @param date 给定的日期 + * @return 指定日期所在星期的星期一 + */ + public String getWeekMonday(String date) { + DateTime d = DateUtil.parse(date, "yyyyMMdd"); + Calendar calendar = DateUtil.beginOfWeek(d.toCalendar(), true); + return DateUtil.format(calendar.getTime(), "yyyyMMdd"); + } + + /** + * 给一个指定日期,返回这个日期对应的星期的星期五是哪天 + * + * @param date 给定的日期 + * @return 指定日期所在星期的星期五 + */ + public String getWeekFriday(String date) { + DateTime d = DateUtil.parse(date, "yyyyMMdd"); + Calendar calendar = DateUtil.beginOfWeek(d.toCalendar(), true); + calendar.add(Calendar.DATE, 4); + return DateUtil.format(calendar.getTime(), "yyyyMMdd"); + } + + /** + * 给一个指定日期,返回这个日期对应的上一个星期的星期一是哪天 + * + * @param date 给定的日期 + * @return 指定日期的上一个星期的星期一 + */ + public String getLastWeekMonday(String date) { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); + try { + Date d = simpleDateFormat.parse(date); + DateTime dateTime = DateUtil.offsetWeek(d, -1); + Calendar calendar = DateUtil.beginOfWeek(dateTime.toCalendar(), true); + Date monday = calendar.getTime(); + return simpleDateFormat.format(monday); + } catch (ParseException e) { + throw new RuntimeException(e); + } + } + + /** + * 给一个指定日期,返回这个日期对应的上一个星期的星期五是哪天 + * + * @param date 给定的日期 + * @return 指定日期的上一个星期的星期五 + */ + public String getLastWeekFriday(String date) { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); + try { + Date d = simpleDateFormat.parse(date); + DateTime dateTime = DateUtil.offsetWeek(d, -1); + Calendar calendar = DateUtil.beginOfWeek(dateTime.toCalendar(), true); + calendar.add(Calendar.DATE, 4); + Date friday = calendar.getTime(); + return simpleDateFormat.format(friday); + } catch (ParseException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/org/wzy/sqltemplate/script-1.0.dtd b/src/main/resources/script-1.0.dtd similarity index 96% rename from src/main/java/org/wzy/sqltemplate/script-1.0.dtd rename to src/main/resources/script-1.0.dtd index 83a03e3..59ea76e 100644 --- a/src/main/java/org/wzy/sqltemplate/script-1.0.dtd +++ b/src/main/resources/script-1.0.dtd @@ -1,34 +1,34 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/org/wzy/sqltemplate/TestTemplate.java b/src/test/java/org/wzy/sqltemplate/TestTemplate.java index e423792..10b42ea 100644 --- a/src/test/java/org/wzy/sqltemplate/TestTemplate.java +++ b/src/test/java/org/wzy/sqltemplate/TestTemplate.java @@ -13,20 +13,36 @@ public class TestTemplate { + @Test + public void testTime(){ + Configuration configuration = new Configuration(); + + SqlTemplate template = configuration + .getTemplate("select * from user where id = #{id} T = #{T},T1=#{util.addDays(T,15)}"); + + HashMap map = new HashMap(); + + map.put("id", "11"); + map.put("T","20200322"); + SqlMeta process = template.process(map); + System.out.println(process.getSql()); + System.out.println(process); + } + @Test public void testIf() { Configuration configuration = new Configuration(); SqlTemplate template = configuration - .getTemplate("select * from user where id = #{id} "); + .getTemplate("select * from user where id = #{id} id = #{id-1}"); HashMap map = new HashMap(); map.put("id", "11"); SqlMeta process = template.process(map); - + System.out.println(process.getSql()); System.out.println(process); }