From 969337f209a1f1d86c4b63ed2bd3ac1a035ed9d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com>
Date: Thu, 1 Jun 2023 15:43:10 +0800
Subject: [PATCH] 'commit'
---
pom.xml | 7 +++
.../Exam/Controller/ExamController.java | 2 +-
.../FengHuang/Exam/Model/ExamModel.java | 13 ++--
.../FengHuang/Plugin/SlaveDrudPlugin.java | 62 +++++++++++++++++++
.../java/com/dsideal/FengHuang/Start.java | 57 ++++++++++++-----
src/main/resource/Sql/Exam.sql | 2 +-
src/main/resource/Sql/dm.sql | 2 +-
src/main/resource/application.properties | 13 ++--
target/classes/Sql/Exam.sql | 2 +-
target/classes/Sql/dm.sql | 2 +-
target/classes/application.properties | 13 ++--
11 files changed, 141 insertions(+), 34 deletions(-)
create mode 100644 src/main/java/com/dsideal/FengHuang/Plugin/SlaveDrudPlugin.java
diff --git a/pom.xml b/pom.xml
index 7f5230b..bc2fd49 100644
--- a/pom.xml
+++ b/pom.xml
@@ -262,6 +262,13 @@
kafka-clients
3.4.0
+
+
+
+ org.apache.shardingsphere
+ sharding-jdbc-core
+ 4.1.1
+
diff --git a/src/main/java/com/dsideal/FengHuang/Exam/Controller/ExamController.java b/src/main/java/com/dsideal/FengHuang/Exam/Controller/ExamController.java
index 421e4d0..845104b 100644
--- a/src/main/java/com/dsideal/FengHuang/Exam/Controller/ExamController.java
+++ b/src/main/java/com/dsideal/FengHuang/Exam/Controller/ExamController.java
@@ -62,7 +62,7 @@ public class ExamController extends Controller {
kv.set("list", list);
int allCount = 0;
for (Record record : list) {
- allCount += record.getInt("count");
+ allCount += record.getInt("c");
}
kv.set("allCount", allCount);
renderJson(kv);
diff --git a/src/main/java/com/dsideal/FengHuang/Exam/Model/ExamModel.java b/src/main/java/com/dsideal/FengHuang/Exam/Model/ExamModel.java
index 9a6d2ae..9a0644e 100644
--- a/src/main/java/com/dsideal/FengHuang/Exam/Model/ExamModel.java
+++ b/src/main/java/com/dsideal/FengHuang/Exam/Model/ExamModel.java
@@ -28,8 +28,8 @@ public class ExamModel {
public Kv addPerson(String person_name, String ks, String tel) {
Kv kv = Kv.create();
//1、检查此人员是不是已经存在过
- String sql = "select count(1) as count from t_exam_person where person_name=? and tel=? and b_use=1 and end_time is not null";
- int cnt = Db.findFirst(sql, person_name, tel).getInt("count");
+ String sql = "select count(1) as c from t_exam_person where person_name=? and tel=? and b_use=1 and end_time is not null";
+ int cnt = Db.findFirst(sql, person_name, tel).getInt("c");
if (cnt > 0) {
kv.set("success", false);
kv.set("message", "当前人员已经进行过测试并且已交卷,不能重复进行!");
@@ -71,8 +71,8 @@ public class ExamModel {
Collections.shuffle(tmpList);
//此题型需要抽取的数量
- sql = "select count from t_exam_question_type where type_id=?";
- int count = Db.findFirst(sql, type_id).getInt("count");
+ sql = "select \"count\" as c from t_exam_question_type where type_id=?";
+ int count = Db.findFirst(sql, type_id).getInt("c");
//批量存
List writeList = new ArrayList<>();
@@ -153,7 +153,7 @@ public class ExamModel {
* 功能:获取整体信息,比如有几种题型,都是啥,每种题型有几道题
*/
public List getExamInfo() {
- String sql = "select t1.type_id,t2.type_name,t2.count from t_exam_question as t1 inner join t_exam_question_type as t2 on t1.type_id=t2.type_id group by t1.type_id,t2.type_name,t2.count";
+ String sql = "select t1.type_id,t2.type_name,t2.\"count\" as c from t_exam_question as t1 inner join t_exam_question_type as t2 on t1.type_id=t2.type_id group by t1.type_id,t2.type_name,t2.\"count\"";
List list = Db.find(sql);
return list;
}
@@ -255,11 +255,10 @@ public class ExamModel {
* 功能:查看自己答卷的整体情况,哪个题目答了,哪个题目没有答
*/
public int getPersonUnFinishCount(String person_id) {
- String sql = "select count(1) as count from t_exam_record where person_id=? and reply is null";
+ String sql = "select count(1) as \"count\" from t_exam_record where person_id=? and reply is null";
return Db.findFirst(sql, person_id).getInt("count");
}
-
/**
* 功能:查看自己答卷的整体情况,哪个题目答对,哪个题目没有答,哪些答错(交卷后可见)
*/
diff --git a/src/main/java/com/dsideal/FengHuang/Plugin/SlaveDrudPlugin.java b/src/main/java/com/dsideal/FengHuang/Plugin/SlaveDrudPlugin.java
new file mode 100644
index 0000000..c617073
--- /dev/null
+++ b/src/main/java/com/dsideal/FengHuang/Plugin/SlaveDrudPlugin.java
@@ -0,0 +1,62 @@
+package com.dsideal.FengHuang.Plugin;
+
+import com.jfinal.plugin.IPlugin;
+import com.jfinal.plugin.activerecord.IDataSourceProvider;
+import com.jfinal.plugin.druid.DruidPlugin;
+import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
+import org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * 读写分离扩展类
+ */
+public class SlaveDrudPlugin implements IPlugin, IDataSourceProvider {
+ //读写分离的rule
+ MasterSlaveRuleConfiguration masterSlaveRuleConfiguration;
+ //数据源map
+ Map druidPlugins;
+ //原数据库连接源map
+ Map dataSourceMap;
+ //最终sharding-jdbc封装后的数据库连接源
+ DataSource dataSource;
+
+ public SlaveDrudPlugin(MasterSlaveRuleConfiguration masterSlaveRuleConfiguration, Map druidPlugins) {
+ this.masterSlaveRuleConfiguration = masterSlaveRuleConfiguration;
+ this.druidPlugins = druidPlugins;
+ dataSourceMap = new HashMap<>();
+ }
+
+ public boolean start() {
+ //遍历数据源 ,将数据源加入sharding jdbc
+ for (Map.Entry entry : druidPlugins.entrySet()) {
+ entry.getValue().start();
+ dataSourceMap.put(entry.getKey(), entry.getValue().getDataSource());
+ }
+ try {
+ dataSource =
+ MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, masterSlaveRuleConfiguration,
+ new Properties());
+ System.out.println(dataSource);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return true;
+ }
+
+ public boolean stop() {
+ for (Map.Entry entry : druidPlugins.entrySet()) {
+ entry.getValue().stop();
+ dataSourceMap.put(entry.getKey(), entry.getValue().getDataSource());
+ }
+ return true;
+ }
+
+ public DataSource getDataSource() {
+ return dataSource;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/dsideal/FengHuang/Start.java b/src/main/java/com/dsideal/FengHuang/Start.java
index afac3f3..625511e 100644
--- a/src/main/java/com/dsideal/FengHuang/Start.java
+++ b/src/main/java/com/dsideal/FengHuang/Start.java
@@ -13,6 +13,7 @@ import com.dsideal.FengHuang.Interceptor.*;
import com.dsideal.FengHuang.LoginPerson.Controller.LoginPersonController;
import com.dsideal.FengHuang.Menu.Controller.MenuController;
import com.dsideal.FengHuang.Organization.Controller.OrganizationController;
+import com.dsideal.FengHuang.Plugin.SlaveDrudPlugin;
import com.dsideal.FengHuang.Student.Controller.StudentController;
import com.dsideal.FengHuang.StudentYd.Controller.StudentYdController;
import com.dsideal.FengHuang.Teacher.Controller.TeacherController;
@@ -20,6 +21,7 @@ import com.dsideal.FengHuang.TeacherYd.Controller.TeacherYdController;
import com.dsideal.FengHuang.Util.FileUtil;
import com.dsideal.FengHuang.Util.LogBackLogFactory;
import com.dsideal.FengHuang.Util.PkUtil;
+import com.google.common.collect.Lists;
import com.jfinal.config.*;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
@@ -31,10 +33,14 @@ import com.jfinal.plugin.druid.IDruidStatViewAuth;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;
+import org.apache.shardingsphere.api.config.masterslave.LoadBalanceStrategyConfiguration;
+import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
public class Start extends JFinalConfig {
@@ -95,14 +101,12 @@ public class Start extends JFinalConfig {
}
- /**
- * 配置插件
- */
- @Override
- public void configPlugin(Plugins me) {
- DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"),
- PropKit.get("password").trim(), PropKit.get("driverClassName"));
- //最大连接池数量,默认为8
+ /*
+ 功能:创建数据库链接
+ */
+ public DruidPlugin createDruidPlugin(String url, String username, String password, String driverClass) {
+ DruidPlugin druidPlugin = new DruidPlugin(url, username, password, driverClass);
+ //最大连接池数量
druidPlugin.setMaxActive(20);
//最小连接池数量
druidPlugin.setMinIdle(1);
@@ -114,7 +118,6 @@ public class Start extends JFinalConfig {
druidPlugin.setTimeBetweenEvictionRunsMillis(60000);
//连接保持空闲而不被驱逐的最小时间
druidPlugin.setMinEvictableIdleTimeMillis(300000);
-
//建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
druidPlugin.setTestWhileIdle(true);
//申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。默认为true
@@ -123,10 +126,38 @@ public class Start extends JFinalConfig {
druidPlugin.setTestOnReturn(false);
//数据监控
druidPlugin.addFilter(new StatFilter());
+ return druidPlugin;
+ }
+
+ /**
+ * 配置插件
+ */
+ @Override
+ public void configPlugin(Plugins me) {
+ DruidPlugin dp1 = createDruidPlugin(PropKit.get("master_jdbcUrl"), PropKit.get("master_user"), PropKit.get("master_password").trim(), PropKit.get("master_driverClassName"));
+ DruidPlugin dp2 = createDruidPlugin(PropKit.get("slave_jdbcUrl"), PropKit.get("slave_user"), PropKit.get("slave_password").trim(), PropKit.get("slave_driverClassName"));
+
+ Map drudMap = new HashMap<>();
+ drudMap.put("ds_master", dp1);
+ drudMap.put("ds_slave", dp2);
+
+ //负载均衡算法
+ LoadBalanceStrategyConfiguration loadBalanceStrategyConfiguration = new LoadBalanceStrategyConfiguration("round_robin");
+ MasterSlaveRuleConfiguration masterSlaveRuleConfig =
+ new MasterSlaveRuleConfiguration(
+ "device_read_write",
+ "ds_master",
+ Lists.newArrayList("ds_slave"),
+ loadBalanceStrategyConfiguration);
+
+ SlaveDrudPlugin drudPlugin = new SlaveDrudPlugin(masterSlaveRuleConfig, drudMap);
+ me.add(drudPlugin);
+
+ ActiveRecordPlugin arp = new ActiveRecordPlugin("aGroup", drudPlugin);
+ arp.setDevMode(PropKit.getBoolean("devMode", false));
+ //配置数据库方言
+ arp.setDialect(new PostgreSqlDialect());
- me.add(druidPlugin);
- // 配置ActiveRecord插件
- ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
//遍历sql目录下所有的sql文件
File sqlDir;
String basePath = Start.class.getClassLoader().getResource(".").getPath();
@@ -142,8 +173,6 @@ public class Start extends JFinalConfig {
arp.setDialect(new PostgreSqlDialect());
//显示SQL语句
//arp.setShowSql(true);
- //设置大小写不敏感工厂
- //arp.setContainerFactory(new CaseInsensitiveContainerFactory(false));
//加载
me.add(arp);
diff --git a/src/main/resource/Sql/Exam.sql b/src/main/resource/Sql/Exam.sql
index 3e44a73..82af744 100644
--- a/src/main/resource/Sql/Exam.sql
+++ b/src/main/resource/Sql/Exam.sql
@@ -5,7 +5,7 @@
inner join t_exam_person as t2 on t1.person_id=t2.person_id
where t1.reply=t1.answer and t2.end_time is not null and t1.b_use=1 and t2.b_use=1
group by t1.person_id,t2.person_name,t2.ks,t2.tel,t2.start_time,t2.end_time
- order by sum(t1.score) desc,EXTRACT(epoch FROM CAST( t2.end_time AS TIMESTAMP))-EXTRACT(epoch FROM CAST( t2.start_time AS TIMESTAMP)) asc
+ order by sum(t1.score) desc
#end
#sql("getPersonAllInfoAfterJiaoJuan")
diff --git a/src/main/resource/Sql/dm.sql b/src/main/resource/Sql/dm.sql
index ec35e6a..27f6db8 100644
--- a/src/main/resource/Sql/dm.sql
+++ b/src/main/resource/Sql/dm.sql
@@ -59,7 +59,7 @@
-- 获取指定父节点的行政区划
#sql("getAreaByParentId")
select t1.id,t1.area_code,t1.area_name,t1.parent_id,
- (case when parent_id='-1' then 1 else 0 end ) as open,
+ if(parent_id='-1', 1 , 0 ) as open,
(select count(1) from t_dm_area as t2 where t2.parent_id=t1.id) as is_leaf
from t_dm_area as t1 where t1.parent_id=? order by t1.area_code
#end
diff --git a/src/main/resource/application.properties b/src/main/resource/application.properties
index 206d16c..326d291 100644
--- a/src/main/resource/application.properties
+++ b/src/main/resource/application.properties
@@ -1,8 +1,13 @@
# 数据库信息
-driverClassName=org.postgresql.Driver
-user=postgres
-password=DsideaL147258369
-jdbcUrl=jdbc:postgresql://10.10.14.209:5432/ccdjzswd_db
+master_driverClassName=org.postgresql.Driver
+master_user=postgres
+master_password=DsideaL147258369
+master_jdbcUrl=jdbc:postgresql://10.10.14.209:5432/ccdjzswd_db
+
+slave_driverClassName=org.postgresql.Driver
+slave_user=postgres
+slave_password=DsideaL147258369
+slave_jdbcUrl=jdbc:postgresql://10.10.14.209:5432/ccdjzswd_db
# redis ip
redis_ip=10.10.14.169
diff --git a/target/classes/Sql/Exam.sql b/target/classes/Sql/Exam.sql
index 3e44a73..82af744 100644
--- a/target/classes/Sql/Exam.sql
+++ b/target/classes/Sql/Exam.sql
@@ -5,7 +5,7 @@
inner join t_exam_person as t2 on t1.person_id=t2.person_id
where t1.reply=t1.answer and t2.end_time is not null and t1.b_use=1 and t2.b_use=1
group by t1.person_id,t2.person_name,t2.ks,t2.tel,t2.start_time,t2.end_time
- order by sum(t1.score) desc,EXTRACT(epoch FROM CAST( t2.end_time AS TIMESTAMP))-EXTRACT(epoch FROM CAST( t2.start_time AS TIMESTAMP)) asc
+ order by sum(t1.score) desc
#end
#sql("getPersonAllInfoAfterJiaoJuan")
diff --git a/target/classes/Sql/dm.sql b/target/classes/Sql/dm.sql
index ec35e6a..27f6db8 100644
--- a/target/classes/Sql/dm.sql
+++ b/target/classes/Sql/dm.sql
@@ -59,7 +59,7 @@
-- 获取指定父节点的行政区划
#sql("getAreaByParentId")
select t1.id,t1.area_code,t1.area_name,t1.parent_id,
- (case when parent_id='-1' then 1 else 0 end ) as open,
+ if(parent_id='-1', 1 , 0 ) as open,
(select count(1) from t_dm_area as t2 where t2.parent_id=t1.id) as is_leaf
from t_dm_area as t1 where t1.parent_id=? order by t1.area_code
#end
diff --git a/target/classes/application.properties b/target/classes/application.properties
index 206d16c..326d291 100644
--- a/target/classes/application.properties
+++ b/target/classes/application.properties
@@ -1,8 +1,13 @@
# 数据库信息
-driverClassName=org.postgresql.Driver
-user=postgres
-password=DsideaL147258369
-jdbcUrl=jdbc:postgresql://10.10.14.209:5432/ccdjzswd_db
+master_driverClassName=org.postgresql.Driver
+master_user=postgres
+master_password=DsideaL147258369
+master_jdbcUrl=jdbc:postgresql://10.10.14.209:5432/ccdjzswd_db
+
+slave_driverClassName=org.postgresql.Driver
+slave_user=postgres
+slave_password=DsideaL147258369
+slave_jdbcUrl=jdbc:postgresql://10.10.14.209:5432/ccdjzswd_db
# redis ip
redis_ip=10.10.14.169