package com.gepower.repair.dao.common; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.apache.log4j.Logger; import org.springframework.jdbc.core.support.JdbcDaoSupport; import org.springframework.util.StringUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * * @author * * An Abstract super class for all DAO's. * Class provide all common method used in the DAO, like loading the sql resources and setting the hibernate template * */ public abstract class AbstaractJDBCTemplate extends JdbcDaoSupport{ protected static final Logger LOG = Logger.getLogger(AbstaractJDBCTemplate.class); private Map<String, String> sqlMap; //@Resource(name = "sessionFactory") // @Autowired // public void setSuperSessionFactory(SessionFactory sessionFactory) { // //super.setSessionFactory(sessionFactory); // } /** Static block, no changes required for any thread. */ private static XPathExpression expr; private static DocumentBuilder docBuilder; static { expr = null; docBuilder = null; try { LOG.info("BaseDAO static initializer"); LOG.info("\tacquiring document builder..."); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // dbf.setValidating(true); docBuilder = dbf.newDocumentBuilder(); LOG.info("\tinstalling XPathExpression..."); XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); expr = xpath.compile("//sql"); } catch (ParserConfigurationException e) { LOG.fatal(e); } catch (XPathExpressionException e) { LOG.fatal(e); } } /** * getting the SQL from the xml file * @return the sql xml file */ protected String[] getSqlXml() { SqlSource sqlXml = getClass().getAnnotation(SqlSource.class); String[] result = null; if (sqlXml != null) { result = sqlXml.value(); } return result; } /** * load the sql content */ private void loadSqls() { if (sqlMap != null && !sqlMap.isEmpty() ) { throw new IllegalStateException( "loadSqls() already be invoked before!"); } LOG.info("sqlMap ---->" + sqlMap); sqlMap = new HashMap<String, String>(); String[] sqlXml = getSqlXml(); if (sqlXml == null || sqlXml.length == 0) { LOG.warn("no sqlXml file specified in this DAO."); return; } for (String location : sqlXml) { loadSqlFrom(location); } dumpLoadedSqls(); } /** * get the sql xml file from the path * * @param location * the file path */ private final void loadSqlFrom(final String location) { if (LOG.isDebugEnabled()) { LOG.debug("try to loading sql from: " + location); } final URL xmlURL = AbstaractJDBCTemplate.class.getResource(location); if (xmlURL == null) { LOG.error(location + " can not be found in the classpath."); return; } InputStream instream = null; NodeList sqls = null; try { instream = xmlURL.openStream(); synchronized(this){ Document doc = docBuilder.parse(instream); sqls = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); if (sqls == null || sqls.getLength() == 0) { LOG.warn("no <sql> elements can be found in the xml - " + location); return; } } Element elm = null; String id = null; String sql = null; for (int i = 0; i < sqls.getLength(); i++) { elm = (Element) sqls.item(i); sql = elm.getTextContent(); id = elm.getAttribute("id"); if (!StringUtils.hasText(id) || !StringUtils.hasText(sql)) { LOG.warn("id or sql content is empty, ignore the entry.\nid:" + id + "\nsql: " + sql); continue; } sqlMap.put(id.trim(), shrink(sql)); } } catch (IOException e) { LOG.fatal("error", e); } catch (SAXException e) { LOG.fatal("error", e); } catch (XPathExpressionException e) { LOG.fatal("error", e); } finally { if (instream != null) { try { instream.close(); } catch (IOException e) { LOG.warn(e); } } } } /** * * @return return the sqls content */ protected String dumpLoadedSqls() { StringBuilder buf = new StringBuilder(); Map.Entry<String, String> entry = null; Iterator<Map.Entry<String, String>> iter = null; for (iter = sqlMap.entrySet().iterator(); iter.hasNext();) { entry = iter.next(); buf.append(entry.getKey()); buf.append("\t => "); buf.append(trimToLine(entry.getValue())); buf.append("\r\n"); } LOG.debug("Loaded Sqls:\n" + buf.toString()); return buf.toString(); } /** * * @param sqlId * the sql id * @return return the related query by the sql id */ protected String getSql(String sqlId) { LOG.info("sqlId -->" + sqlId); if (!StringUtils.hasText(sqlId)) { throw new IllegalArgumentException("invalid sqlId: " + sqlId); } if (sqlMap == null) { loadSqls(); } if(sqlMap.isEmpty()){ LOG.info("===========================----sqlMap is Empty"); loadSqls(); } String sql =sqlMap.get(sqlId); LOG.info("SQL : " + sql); return sql; } /** * * @return return the query */ protected String getSql() { StackTraceElement stackTrace = null; try { boolean flag = true; if (flag) { throw new IllegalArgumentException(); } } catch (IllegalArgumentException e) { stackTrace = e.getStackTrace()[1]; } return stackTrace != null ? getSql(stackTrace.getMethodName()) : null; } /** * * @param str * the query content * @return return the query */ private static String trimToLine(final String str) { if (null == str) { return str; } return str.replace("\n", "").replace("\r", "").trim(); } private static String shrink(String input) { if (input == null || input.equals("")) { return input; } return input.replaceAll("[\\r\\n\\s]+", " ").trim(); } } package com.gepower.repair.dao.common; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Defining an annotation * @author * */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface SqlSource { String[] value() default "/sql.xml"; } Dao @SqlSource("/resources/sql/calculatecatalogcost-sql.xml") sql xml文件 <sqls> <sql id="findCatalogServices"> </sql> </sqls>
相关推荐
config.properties:数据库配置文件 log4j.properties:mybatis日志文件 spring-mvc.xml:spring-MVC配置文件 spring-mybatis.xml:mybatis的配置文件 spring.xml
一个简单的SSM框架整合示例代码,采用XML配置文件方式整合。 适合学习SSM的新手学习使用。 使用的工具说明: IDE: eclipse4.7 SSM框架版本:Srping4.3.9,MyBatis3.4.4 数据库:MySql5.7.19 数据库脚本:/src/sql/...
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
1. 该java工程师基于Maven的Spring + Spring MVC + Mybatis的工程实例 通过一个简单的登录功能了解搭建spring + mybatis的各个部分。 2. 数据库使用的是mysql5.6, 实例涉及一张表 t_user,请查看sqlScript中的脚本...
本项目是基于Spring、Spring MVC、MiniJdbc和Shiro开发的权限管理系统LarvaFrame设计源码,包含436个文件,其中包括145个Java源文件,92个JavaScript文件,30个PNG图片,30个CSS样式文件,27个GIF图片,24个XML文件...
资源介绍 Spring: Spring是一个开源的Java平台,它提供了一种...MyBatis可以使用简单的XML或注解来配置和映射原生信息,将接口和Java的POJOs (Plain Old Java Objects, 普通的Java对象)映射为数据库中的记录。 项目结构
项目描述 辅助业务人员对工程财务状况...2.spring中关于DataSource的配置,需要在tomcat的conf/context.xml中添加数据源 3.PaymentServiceImpl.java中有个import错误 4.FMS.sql数据库文件较大,导入时间较长,请耐心
MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。 Spring MVC 是一个建立在中央前端控制器servlet...
本源码提供了一个基于Spring MVC、CXF和Hibernate的Web服务与数据库操作设计。项目包含53个文件,其中包括15个XML文件、13个Java源文件、10个属性文件、3个YAML文件、3个JSP页面、2个MF文件、1个Gitignore文件、1个...
Spring+mvc+mybatis Mapper xml自动加载修改SQL不用重启tomcat,附带sql脚本,下载直接能运行,启动输入http://localhost:8080/aclome/
- Spring MVC - Spring Data - Spring Security - Spring Cloud 4. Web开发: - HTML、CSS、JavaScript - HTTP协议 - Servlet、JSP - AJAX、JSON、XML 5. 框架和工具: - MyBatis - Hibernate - ...
项目使用日志框架实现sql日志完整输出,并使用springmvc和jxls分别实现excel导出
spring MVC+easyUI+mybatis开发网站后台管理系统(源码+sql文件) urls.properties中需要修改为你本地的静态文件的访问路径。 使用Java平台,采用SpringMVC+Mybatis等主流框架. 数据库:使用免费MYSQL. 前端:...
3. 整合配置: 我们将演示如何将Spring、Spring MVC和MyBatis进行整合配置,以便它们可以无缝协同工作。这包括整合数据库连接池、事务管理、AOP等关键组件 4.项目jar包完善: 提供项目所有jar包。 5.数据库文件SQL...
mybatis未进行二次封装,原滋原味,简单sql采用注解,复杂sql采用Mapper.xml配置 使用了layui的弹出层、菜单、文件上传、富文本编辑、日历、选项卡、数据表格等 表单数据采用bootstrapValidator校验,简单快捷方便...
Java源码 Spring+MVC4+Hibernate4+ExtJS5 系统说明如下: 1、系统导入后发布到应用服务器,例如tomcat上即可运行。 2、导入项目后请将项目编码设置为UTF-8,不是GBK。 3、导入后如果出现感叹号,请修改实际的...
在项目文件夹下的config文件夹中包含数据库常量配置文件,spring配置文件,MyBatis配置文件,log4j配置文件,资源配置文件以及Spring MVC配置文件,其中数据库常量配置文件为db.properties以及MyBatis配置文件...
在项目文件夹下的config文件夹中包含数据库常量配置文件,spring配置文件,MyBatis配置文件,log4j配置文件,资源配置文件以及Spring MVC配置文件,其中数据库常量配置文件为db.properties以及MyBatis配置文件...
2、平台环境:JAVA1.6以上、Apache Tomcat6.0以上、MS SQL2005(可以自己更换数据库,在persistence.xml更改参数就行,具体配置参考第一天给出的链接) 3、调试环境:eclipse(File-import-existing Eclipse projects...
2、平台环境:JAVA1.6以上、Apache Tomcat6.0以上、MS SQL2005(可以自己更换数据库,在persistence.xml更改参数就行,具体配置参考第一天给出的链接) 3、调试环境:eclipse(File-import-existing Eclipse projects...