From 95855a21fc2f8b1cb038b59711fa319f95e20c5d Mon Sep 17 00:00:00 2001 From: mewhz Date: Tue, 21 Dec 2021 22:23:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=BF=9E=E6=8E=A5=E6=B1=A0?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 10 +++++- .../java/com/mewhz/paste/utils/CodeSQL.java | 11 +++--- src/main/resources/db.setting | 22 +++++++++++- src/test/java/com/mewhz/paste/SQLTest.java | 36 +++++++++++++++++++ 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/mewhz/paste/SQLTest.java diff --git a/pom.xml b/pom.xml index 1953d74..76a0747 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ cn.hutool hutool-all - 5.7.15 + 5.7.17 @@ -41,6 +41,14 @@ 5.1.47 + + + com.alibaba + druid + 1.2.8 + + + diff --git a/src/main/java/com/mewhz/paste/utils/CodeSQL.java b/src/main/java/com/mewhz/paste/utils/CodeSQL.java index 27edf6f..ebee8a5 100644 --- a/src/main/java/com/mewhz/paste/utils/CodeSQL.java +++ b/src/main/java/com/mewhz/paste/utils/CodeSQL.java @@ -2,13 +2,16 @@ import cn.hutool.db.Db; import cn.hutool.db.Entity; +import cn.hutool.db.ds.DSFactory; import com.mewhz.paste.model.Code; import com.mewhz.paste.model.IdentifyingCode; +import javax.sql.DataSource; import java.sql.SQLException; import java.util.List; public class CodeSQL { + private DataSource ds = DSFactory.get(); public CodeSQL (){ } @@ -18,7 +21,7 @@ public CodeSQL (){ */ public void insertCode(Code code){ try { - Db.use().insert( + Db.use(ds).insert( Entity.create("code") .set("text", code.getText()) .set("time_id", code.getDate().getTime()+"") @@ -38,7 +41,7 @@ public void insertCode(Code code){ */ public void insertIdentifyingCode(IdentifyingCode identifyingCode){ try{ - Db.use().insert( + Db.use(ds).insert( Entity.create("identifying_code") .set("identifying", identifyingCode.getIdentifying()) .set("time_id", identifyingCode.getTimeId()) @@ -55,7 +58,7 @@ public void insertIdentifyingCode(IdentifyingCode identifyingCode){ public List findCode(String timeId){ List codes = null; try { - codes = Db.use().findAll( + codes = Db.use(ds).findAll( Entity.create("code").set("time_id", "= " + timeId) ); } catch (SQLException e) { @@ -67,7 +70,7 @@ public List findCode(String timeId){ public List findIdentifyingCode(String identifying){ List identifyingCodes = null; try{ - identifyingCodes = Db.use().query("select text, type, date, code.time_id, remark\n" + + identifyingCodes = Db.use(ds).query("select text, type, date, code.time_id, remark\n" + "from code inner join identifying_code\n" + " on code.time_id = identifying_code.time_id\n" + " where identifying = ?", identifying); diff --git a/src/main/resources/db.setting b/src/main/resources/db.setting index 13d218b..ccaac67 100644 --- a/src/main/resources/db.setting +++ b/src/main/resources/db.setting @@ -8,4 +8,24 @@ initialSize = 1 # 最大连接池数量 maxActive = 8 # 最小连接池数量 -minIdle = 1 \ No newline at end of file +minIdle = 1 +# 获取连接时最大等待时间,单位毫秒。配置了maxWait之后, 缺省启用公平锁,并发效率会有所下降, 如果需要可以通过配置useUnfairLock属性为true使用非公平锁。 +maxWait = 600 +# 是否缓存preparedStatement,也就是PSCache。 PSCache对支持游标的数据库性能提升巨大,比如说oracle。 在mysql5.5以下的版本中没有PSCache功能,建议关闭掉。作者在5.5版本中使用PSCache,通过监控界面发现PSCache有缓存命中率记录, 该应该是支持PSCache。 +poolPreparedStatements = false +# 要启用PSCache,必须配置大于0,当大于0时, poolPreparedStatements自动触发修改为true。 在Druid中,不会存在Oracle下PSCache占用内存过多的问题, 可以把这个数值配置大一些,比如说100 +maxOpenPreparedStatements = -1 +# 用来检测连接是否有效的sql,要求是一个查询语句。 如果validationQuery为null,testOnBorrow、testOnReturn、 testWhileIdle都不会其作用。 +validationQuery = SELECT 1 +# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 +testOnBorrow = true +# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 +testOnReturn = true +# 建议配置为true,不影响性能,并且保证安全性。 申请连接的时候检测,如果空闲时间大于 timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 +testWhileIdle = true +# 有两个含义: 1) Destroy线程会检测连接的间隔时间 2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明 +timeBetweenEvictionRunsMillis = 600 +# 物理连接初始化的时候执行的sql +connectionInitSqls = SELECT 1 +# 属性类型是字符串,通过别名的方式配置扩展插件, 常用的插件有: 监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall +filters = stat diff --git a/src/test/java/com/mewhz/paste/SQLTest.java b/src/test/java/com/mewhz/paste/SQLTest.java new file mode 100644 index 0000000..c87b21a --- /dev/null +++ b/src/test/java/com/mewhz/paste/SQLTest.java @@ -0,0 +1,36 @@ +package com.mewhz.paste; + +import cn.hutool.db.Db; +import cn.hutool.db.Entity; +import cn.hutool.db.ds.DSFactory; + +import javax.sql.DataSource; +import java.sql.SQLException; +import java.util.List; +import java.util.Scanner; + +public class SQLTest { + + private static DataSource ds = DSFactory.get(); + + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + while (true){ + String timeId = input.next(); + fun(timeId); + } + } + + public static void fun(String timeId){ + List result = null; + { + try { + result = Db.use(ds).findAll( + Entity.create("student").set("id", "= " + timeId)); + } catch (SQLException e) { + e.printStackTrace(); + } + } + System.out.println(result); + } +}