package com.cheuks.bin.db.manager;
//import 省略
public
class QueryFactory implements QueryType {
?
???private
final Map<String, Template> FORMAT_XQL = new ConcurrentHashMap<String, Template>();
???private
final Map<String, String> UNFORMAT_XQL = new ConcurrentHashMap<String, String>();
???private
final Configuration freemarkerConfiguration = new Configuration(Configuration.VERSION_2_3_0);
???private StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
???private String files;
???public QueryFactory() {
??????super();
??????freemarkerConfiguration.setTemplateLoader(stringTemplateLoader);
???}
???public
synchronized
void put(String name, String XQL, boolean isFormat) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException {
??????if (null == name || null == XQL)
?????????return;
??????if (isFormat) {
?????????stringTemplateLoader.putTemplate(name, XQL);
?????????FORMAT_XQL.put(name, freemarkerConfiguration.getTemplate(name));
??????} else {
?????????UNFORMAT_XQL.put(name, XQL);
??????}
???}
?
???public String getXQL(String name, boolean isFormat, Map<String, Object> params) throws TemplateException, IOException {
??????// if (!isScan)
??????// scan();
??????if (!isFormat)
?????????return UNFORMAT_XQL.get(name);
??????Template tp = FORMAT_XQL.get(name);
??????if (null == tp)
?????????return
null;
??????StringWriter sw = new StringWriter();
??????tp.process(params, sw);
??????return sw.toString();
???}
?
???@SuppressWarnings("restriction")
???@javax.annotation.PostConstruct
???private
void scan() {
??????try {
?????????Set<String> o = null;
?????????o = Scan.doScan(files);//扫描所有 *.queue.xml
?????????xmlExplain(o);
??????} catch (Exception e) {
?????????e.printStackTrace();
??????}
???}
?
???public String getFiles() {return files;}
???public QueryFactory setFiles(String files) {this.files = files; return
this;}
?
???public
void xmlExplain(Set<String> urls) throws ParserConfigurationException, SAXException, IOException {
??????Iterator<String> it = urls.iterator();
??????SAXParserFactory factory = SAXParserFactory.newInstance();
??????SAXParser parser = factory.newSAXParser();
??????xmlHandler handler = new xmlHandler();
??????XMLReader xmlReader = parser.getXMLReader();
??????//读取XML
??????xmlReader.setEntityResolver(new EntityResolver() {
?????????public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
????????????return
new InputSource(this.getClass().getClassLoader().getResourceAsStream("dtd/Query.dtd"));
?????????}
??????});
??????while (it.hasNext()) {
?????????String str = it.next();
?????????InputSource is = new InputSource(Thread.currentThread().getContextClassLoader().getResourceAsStream(str));
?????????is.setEncoding("utf-8");
?????????xmlReader.setContentHandler(handler);
?????????xmlReader.parse(is);
??????}
???}
?
???class xmlHandler extends DefaultHandler {
??????// private boolean isHQL = false;
??????private
boolean format = false;
??????private
boolean alias = false;
??????private String packageName = null;
??????private String name = null;
??????Map<String, String> aliases = new HashMap<String, String>();
??????private String value;
?
??????@Override
??????public
void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
?????????if (qName.equals(QUERY_LIST)) {
????????????packageName = attributes.getValue(PACKAGE);
?????????} else
if (qName.equals(QUERY)) {
????????????// isHQL = attributes.getValue(TYPE).equals("HQL");
????????????name = attributes.getValue(NAME);
????????????format = Boolean.valueOf(attributes.getValue(FREEMARK_FORMAT));
????????????alias = Boolean.valueOf(attributes.getValue(ALIAS));
?????????} else
if (qName.equals(ALIAS)) {
????????????aliases.put(attributes.getValue(ALIAS), attributes.getValue(NAME));
?????????}
?????????super.startElement(uri, localName, qName, attributes);
??????}
?
??????@Override
??????public
void characters(char[] ch, int start, int length) throws SAXException {
?????????value = new String(ch, start, length).replaceAll("(\n|\t)", "");
?????????if (value.length() > 0) {
????????????try {
???????????????put(String.format("%s.%s", packageName, name).toLowerCase(), alias ? alias(value) : value, format);//生成缓存
????????????} catch (Exception e) {
????????????}
?????????}
?
??????}
?
??????private String alias(String str) {
?????????if (alias)
????????????for (Entry<String, String> en : aliases.entrySet())
???????????????str = str.replaceAll(en.getKey(), en.getValue());
?????????return str;
??????}
?
???}
}
package com.cheuks.bin.db.manager;
@SuppressWarnings({ "rawtypes", "unchecked" })
public
abstract
class AbstractHibernateDBAdapter implements DBAdapter {
?
???private QueryFactory queryFactory;
?
???public
abstract Session getSession();
?
???public <T> List<T> getList(Class<?> c) throws Throwable {
??????return getList(c, -1, -1);
???}
?
???public <T> List<T> getList(Class<?> c, int page, int size) throws Throwable {
??????Query query = getSession().createQuery(String.format("FROM %s a", c.getSimpleName()));
??????List list = page > 0 ? page(query, page, size).list() : query.list();
??????return
null == list ? null : list;
???}
?
???public <T> List<T> getListByXqlQueryName(String queryName, boolean isHQL, Object... params) throws Throwable {
??????return getListByXqlQueryName(queryName, isHQL, -1, -1, params);
???}
?
???public <T> List<T> getListByXqlQueryName(String queryName, boolean isHQL, int page, int size, Object... params) throws Throwable {
??????String xql = queryFactory.getXQL(queryName, false, null);
??????Query query = fillParams(isHQL ? getSession().createQuery(xql) : getSession().createSQLQuery(xql), params);
??????List list = page > 0 ? page(query, page, size).list() : query.list();
??????return
null == list ? null : list;
???}
?
???public <T> List<T> getListByXqlQueryName(String queryName, boolean isHQL, boolean isFormat, Map<String, Object> params) throws Throwable {
??????return getListByXqlQueryName(queryName, isHQL, isFormat, params, -1, -1);
???}
?
???public <T> List<T> getListByXqlQueryName(String queryName, boolean isHQL, boolean isFormat, Map<String, Object> params, int page, int size) throws Throwable {
??????String xql = queryFactory.getXQL(queryName, isFormat, params);
??????Query query = fillParams(isHQL ? getSession().createQuery(xql) : getSession().createSQLQuery(xql), params);
??????List list = page > 0 ? page(query, page, size).list() : query.list();
??????return
null == list ? null : list;
???}
?
???protected Query fillParams(Query q, Object... o) {
??????if (null == o || null == q) {
?????????return q;
??????}
??????for (int i = 0, len = o.length; i < len; i++) {
?????????q.setParameter(i, o[i]);
??????}
??????return q;
???}
?
???protected Query fillParams(Query q, Map<String, ?> o) {
??????if (null == o || null == q) {
?????????return q;
??????}
??????for (Entry<String, ?> en : o.entrySet())
?????????try {
????????????q.setParameter(en.getKey(), en.getValue());
?????????} catch (Exception e) {
?????????}
??????return q;
???}
?
???protected Query page(Query q, int pageNum, int size) {
??????if (pageNum >= 0 && size >= 0) {
?????????q.setFirstResult(size * (pageNum - 1));
?????????q.setMaxResults(size);
??????}
??????return q;
???}
?
???public String queryNameFormat(Class<?> entry, String queryName) {
??????return String.format("%s.%s", entry.getName(), queryName).toLowerCase();
???}
?
???public QueryFactory getQueryFactory() {
??????return queryFactory;
???}
?
???public AbstractHibernateDBAdapter setQueryFactory(QueryFactory queryFactory) {
??????this.queryFactory = queryFactory;
??????return
this;
???}
}
package com.cheuks.bin.db.manager.dao;
public
abstract
class AbstractDao<entity, ID extends Serializable> implements BaseDao<entity, ID> {
?
???public
abstract Class<entity> getEntityClass();
?
???public
abstract DBAdapter getDBAdapter();
?
???public List<entity> getList(int page, int size) throws Throwable {
??????return getDBAdapter().getList(getEntityClass(), page, size);
???}
?
???public List<entity> getList(Map<String, Object> params, int page, int size) throws Throwable {
??????return getDBAdapter().getListByXqlQueryName(getDBAdapter().queryNameFormat(getEntityClass(), "list"), true, true, params, page, size);
???}
?
???public <T> List<T> getList(String queryName, Map<String, Object> params, boolean isFromat, int page, int size) throws Throwable {
??????return getDBAdapter().getListByXqlQueryName(getDBAdapter().queryNameFormat(getEntityClass(), queryName), true, isFromat, params, page, size);
???}
?
???public <T> List<T> getListCustomQueryName(String queryName, Map<String, Object> params, boolean isFromat, int page, int size) throws Throwable { return getDBAdapter().getListByXqlQueryName(queryName.toLowerCase(), true, isFromat, params, page, size);
???}
?
???public List<entity> getListEntity(String queryName, Map<String, Object> params, boolean isFromat, int page, int size) throws Throwable {
??????return getDBAdapter().getListByXqlQueryName(getDBAdapter().queryNameFormat(getEntityClass(), queryName), true, isFromat, params, page, size);
???}
?
???public List<entity> getListEntityCustomQueryName(String queryName, Map<String, Object> params, boolean isFromat, int page, int size) throws Throwable {
??????return getDBAdapter().getListByXqlQueryName(queryName.toLowerCase(), true, isFromat, params, page, size);
???}
}