DBSessionFactory.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package com.yihu.ehr.util.db;
  2. import com.fasterxml.jackson.databind.node.ObjectNode;
  3. import org.apache.commons.dbutils.BasicRowProcessor;
  4. import org.apache.commons.dbutils.BeanProcessor;
  5. import org.apache.commons.dbutils.QueryRunner;
  6. import org.apache.commons.dbutils.ResultSetHandler;
  7. import org.apache.commons.dbutils.handlers.*;
  8. import org.dom4j.Document;
  9. import javax.sql.DataSource;
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.SQLException;
  13. import java.util.*;
  14. /**
  15. * ���ݿ�������,�򻯲���
  16. *
  17. * @created Air on 2015/6/2
  18. */
  19. public class DBSessionFactory {
  20. public static final String HOS = "hos-mysql";
  21. private final static HashMap<String, DataSourcePool> dataSourcePool = new HashMap<>();
  22. private DataSource dataSource;
  23. private QueryRunner queryRunner;
  24. private Connection connection;
  25. private String driver;
  26. public DBSessionFactory() {
  27. DataSourcePool dataSourcePool = DBSessionFactory.dataSourcePool.get(HOS);
  28. if (dataSourcePool != null) {
  29. dataSource = dataSourcePool.getDataSource();
  30. driver = dataSourcePool.getDriver();
  31. }
  32. queryRunner = new QueryRunner();
  33. }
  34. /**
  35. * @param dsName ���ݿ����ñ���,���ڱ�ʶ��������ݿ�����
  36. */
  37. public DBSessionFactory(String dsName) throws SQLException {
  38. DataSourcePool dataSourcePool = DBSessionFactory.dataSourcePool.get(dsName);
  39. if (dataSourcePool != null) {
  40. dataSource = dataSourcePool.getDataSource();
  41. connection = dataSource.getConnection();
  42. driver = dataSourcePool.getDriver();
  43. }
  44. queryRunner = new QueryRunner();
  45. }
  46. /**
  47. * ������ݿ�����
  48. *
  49. * @param dsName ���ݿ����ñ���,���ڱ�ʶ��������ݿ�����
  50. * @param uri ���ݿ����Ӵ�
  51. */
  52. public static synchronized void addDataSource(String dsName, String uri) {
  53. DataSourcePool dataSourcePool = DBSessionFactory.dataSourcePool.get(dsName);
  54. if (dataSourcePool == null) {
  55. DBSessionFactory.dataSourcePool.put(dsName, new DataSourcePool(uri));
  56. }
  57. }
  58. /**
  59. * �ر�Connection
  60. *
  61. * @throws SQLException
  62. */
  63. public void close() throws SQLException {
  64. if (connection != null) {
  65. connection.close();
  66. }
  67. }
  68. /**
  69. * ʹ��Json��ʽ
  70. *
  71. * @param jsonRoot
  72. * @param arrayList
  73. * @param sql SQL�﷨
  74. * @param params �����б�
  75. * @return JSONObject
  76. * @throws SQLException
  77. */
  78. public ObjectNode queryForJson(String jsonRoot, ArrayList<String> arrayList, String sql, Object... params) throws SQLException {
  79. JsonHandler jsonHandler = new JsonHandler(jsonRoot);
  80. jsonHandler.setTagList(arrayList);
  81. return this.query(sql, jsonHandler, params);
  82. }
  83. public ObjectNode queryForJson(String jsonRoot, String sql, Object... params) throws SQLException {
  84. JsonHandler jsonHandler = new JsonHandler(jsonRoot);
  85. return this.query(sql, jsonHandler, params);
  86. }
  87. /**
  88. * ʹ��XML��ʽ
  89. *
  90. * @param sql SQL�﷨
  91. * @param params �����б�
  92. * @return Document XML
  93. * @throws SQLException
  94. */
  95. public Document queryForXml(String sql, Object... params) throws SQLException {
  96. return null;
  97. }
  98. /**
  99. * �����鷽ʽ���ؽ��
  100. *
  101. * @param sql SQL�﷨
  102. * @param params �����б�
  103. * @return Object[]����
  104. * @throws SQLException
  105. */
  106. public Object[] queryForArray(String sql, Object... params) throws SQLException {
  107. ArrayHandler arrayHandler = new ArrayHandler();
  108. return this.query(sql, arrayHandler, params);
  109. }
  110. /**
  111. * �����鷽ʽ�б��ؽ��
  112. *
  113. * @param sql SQL�﷨
  114. * @param params �����б�
  115. * @return Object[]�б�
  116. * @throws SQLException
  117. */
  118. public List<Object[]> queryForArrayList(String sql, Object... params) throws SQLException {
  119. ArrayListHandler arrayListHandler = new ArrayListHandler();
  120. return this.query(sql, arrayListHandler, params);
  121. }
  122. /**
  123. * ��Bean��ʽ�б��ؽ��
  124. *
  125. * @param cls ���ص�������
  126. * @param sql SQL�﷨
  127. * @param params �����б�
  128. * @param <T> ���ص���
  129. * @return T����
  130. * @throws SQLException
  131. */
  132. public <T> T queryForBean(Class<T> cls, String sql, Object... params) throws SQLException {
  133. BeanHandler<T> beanHandler = new BeanHandler<T>(cls);
  134. return this.query(sql, beanHandler, params);
  135. }
  136. /**
  137. * ��Bean�б�ʽ�б��ؽ��
  138. *
  139. * @param <T> ���ص���
  140. * @param cls ���ص�������
  141. * @param beanProcessor
  142. * @param sql SQL�﷨
  143. * @param params �����б� @return T�����б� @throws SQLException
  144. */
  145. public <T> List<T> queryForBeanList(Class<T> cls, BeanProcessor beanProcessor, String sql, Object... params) throws SQLException {
  146. BeanListHandler<T> beanListHandler = null;
  147. if (beanProcessor == null) {
  148. beanListHandler = new BeanListHandler<T>(cls);
  149. } else {
  150. BasicRowProcessor rowProcessor = new BasicRowProcessor(beanProcessor);
  151. beanListHandler = new BeanListHandler<T>(cls, rowProcessor);
  152. }
  153. return this.query(sql, beanListHandler, params);
  154. }
  155. /**
  156. * �����б�ʽ�б��ؽ��
  157. *
  158. * @param cls ���ص�������
  159. * @param sql SQL�﷨
  160. * @param params �����б�
  161. * @param <T> ���ص���
  162. * @return T�����б�
  163. * @throws SQLException
  164. */
  165. public <T> List<T> queryForColumnList(Class<T> cls, String sql, Object... params) throws SQLException {
  166. ColumnListHandler<T> columnListHandler = new ColumnListHandler<T>(String.valueOf(cls));
  167. return this.query(sql, columnListHandler, params);
  168. }
  169. /**
  170. * ��Map��ʽ���ضԽ��
  171. *
  172. * @param sql SQL�﷨
  173. * @param params �����б�
  174. * @return Map
  175. * @throws SQLException
  176. */
  177. public Map<String, Object> queryForMap(String sql, Object... params) throws SQLException {
  178. MapHandler mapHandler = new MapHandler();
  179. return this.query(sql, mapHandler, params);
  180. }
  181. /**
  182. * ��Map�б�ʽ���ضԽ��
  183. *
  184. * @param sql SQL�﷨
  185. * @param params �����б�
  186. * @return Map
  187. * @throws SQLException
  188. */
  189. public List<Map<String, Object>> queryForMapList(String sql, Object... params) throws SQLException {
  190. MapListHandler mapListHandler = new MapListHandler();
  191. return this.query(sql, mapListHandler, params);
  192. }
  193. /**
  194. * ���ݽ������ʽ��ѯ����
  195. *
  196. * @param sql SQL�﷨
  197. * @param rsh ���������
  198. * @param params �����б�
  199. * @param <T> ���ص���
  200. * @return T����
  201. * @throws SQLException
  202. */
  203. public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
  204. checkConnection();
  205. return queryRunner.query(connection, sql, rsh, params);
  206. }
  207. /**
  208. * ���ݽ������ʽ��ѯ����
  209. *
  210. * @param sql SQL�﷨
  211. * @param rsh ���������
  212. * @param <T> ���ص���
  213. * @return T����
  214. * @throws SQLException
  215. */
  216. public <T> T query(String sql, ResultSetHandler<T> rsh) throws SQLException {
  217. checkConnection();
  218. setTransactionReadCommitted();
  219. return queryRunner.query(connection, sql, rsh, (Object[]) null);
  220. }
  221. public int update(String sql) throws SQLException {
  222. checkConnection();
  223. setTransactionReadCommitted();
  224. return queryRunner.update(connection, sql, (Object[]) null);
  225. }
  226. public int update(String sql, Object param) throws SQLException {
  227. checkConnection();
  228. setTransactionReadCommitted();
  229. return queryRunner.update(connection, sql, new Object[]{param});
  230. }
  231. public int update(String sql, Object... params) throws SQLException {
  232. checkConnection();
  233. setTransactionReadCommitted();
  234. return queryRunner.update(connection, sql, params);
  235. }
  236. public int insert(String sql, Object... params) throws SQLException {
  237. checkConnection();
  238. setTransactionReadCommitted();
  239. PreparedStatement stmt = null;
  240. try {
  241. stmt = connection.prepareStatement(sql, 1);
  242. queryRunner.fillStatement(stmt, params);
  243. int result = stmt.executeUpdate();
  244. return result;
  245. } catch (SQLException e) {
  246. throw e;
  247. } finally {
  248. if (stmt != null) {
  249. stmt.close();
  250. }
  251. }
  252. }
  253. public <T> T insert(String sql, ResultSetHandler<T> rsh) throws SQLException {
  254. checkConnection();
  255. setTransactionReadCommitted();
  256. return queryRunner.insert(connection, sql, rsh, (Object[]) null);
  257. }
  258. public <T> T insert(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
  259. checkConnection();
  260. setTransactionReadCommitted();
  261. return queryRunner.insert(connection, sql, rsh, params);
  262. }
  263. /**
  264. * @param sql SQL�﷨
  265. * @param rsh ���������
  266. * @param params �����б�
  267. * @param <T> ���ص���
  268. * @return T����
  269. * @throws SQLException
  270. */
  271. public <T> T insertBatch(String sql, ResultSetHandler<T> rsh, Object[][] params) throws SQLException {
  272. checkConnection();
  273. setTransactionReadCommitted();
  274. return queryRunner.insertBatch(connection, sql, rsh, params);
  275. }
  276. /**
  277. * ����ִ��,insert,update,delete
  278. *
  279. * @param sql SQL�﷨
  280. * @param params �����б�
  281. * @return ��Ӱ������
  282. * @throws SQLException
  283. */
  284. public int[] batch(String sql, Object[][] params) throws SQLException {
  285. checkConnection();
  286. setTransactionReadCommitted();
  287. return queryRunner.batch(connection, sql, params);
  288. }
  289. /**
  290. * 时间获取,不使用BeanProcessorEx方法时,只能获取到可能无法获取到时间部分。
  291. *
  292. * @return
  293. * @throws SQLException
  294. */
  295. public Date getSystemDateTime() throws SQLException {
  296. String sql = null;
  297. if (driver.equals(DataSourcePool.ORACLE_JDBC_DRIVER_ORACLE_DRIVER)) {
  298. sql = "SELECT sysdate DATE_TIME FROM dual";
  299. } else if (driver.equals(DataSourcePool.COM_MICROSOFT_JDBC_SQLSERVER_DRIVER)) {
  300. sql = "SELECT getdate() AS DATE_TIME";
  301. } else if (driver.equals(DataSourcePool.COM_MYSQL_JDBC_DRIVER)) {
  302. sql = "SELECT now() AS DATE_TIME";
  303. }
  304. Map<String, String> propertyMap = new HashMap<>();
  305. propertyMap.put("DATE_TIME", "date");
  306. BeanProcessorEx beanProcessorEx = new BeanProcessorEx(propertyMap);
  307. List<SystemDataTime> systemDataTimes = this.queryForBeanList(SystemDataTime.class, beanProcessorEx, sql);
  308. if (systemDataTimes != null && systemDataTimes.size() != 0) {
  309. return systemDataTimes.get(0).getDate();
  310. }
  311. throw new SQLException("获取数据库系统时间错误.");
  312. }
  313. /**
  314. * �����������
  315. *
  316. * @throws SQLException
  317. */
  318. private void checkConnection() throws SQLException {
  319. if (dataSource == null) {
  320. throw new SQLException("invalid DataSource.");
  321. }
  322. if (connection == null || connection.isClosed()) {
  323. connection = dataSource.getConnection();
  324. }
  325. if (connection == null) {
  326. throw new SQLException("invalid Connection.");
  327. }
  328. }
  329. /**
  330. * �����������ȼ�Ϊ���ύ�Ķ�ȡ
  331. *
  332. * @throws SQLException
  333. */
  334. private void setTransactionReadCommitted() throws SQLException {
  335. connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
  336. }
  337. }