diff --git a/.idea/mybatis-generator-config.xml b/.idea/mybatis-generator-config.xml index a28eabb..fd28e40 100644 --- a/.idea/mybatis-generator-config.xml +++ b/.idea/mybatis-generator-config.xml @@ -12,6 +12,22 @@ + + + + + + + + + + + + + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index bbfbe09..57aab92 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,15 +5,22 @@ + + + + + + + + + + - - - - - - - + + + + @@ -70,18 +77,20 @@ "RequestMappingsPanelWidth1": "75", "RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.git.unshallow": "true", + "Spring Boot.Application.executor": "Debug", "Spring Boot.SimulationService.executor": "Debug", "git-widget-placeholder": "master", + "last_directory_selection": "D:/work/JavaProject/SimulationService/src/test/java", "last_opened_file_path": "D:/work/JavaProject/SimulationService/pom.xml", "node.js.detected.package.eslint": "true", "node.js.detected.package.tslint": "true", "node.js.selected.package.eslint": "(autodetect)", "node.js.selected.package.tslint": "(autodetect)", "nodejs_package_manager_path": "npm", - "project.structure.last.edited": "问题", + "project.structure.last.edited": "库", "project.structure.proportion": "0.15", "project.structure.side.proportion": "0.37011495", - "settings.editor.selected.configurable": "vcs.Git", + "settings.editor.selected.configurable": "preferences.pluginManager", "vue.rearranger.settings.migration": "true" }, "keyToStringList": { @@ -94,7 +103,26 @@ - + + + + + + + + + + + + + + + + + + + + @@ -104,6 +132,11 @@ + + + + + @@ -113,6 +146,11 @@ + + + + + @@ -130,7 +168,7 @@ - + diff --git a/doc/业务流程.vsdx b/doc/业务流程.vsdx index a2449a2..fad69b1 100644 Binary files a/doc/业务流程.vsdx and b/doc/业务流程.vsdx differ diff --git a/pom.xml b/pom.xml index 929630d..5fb6caa 100644 --- a/pom.xml +++ b/pom.xml @@ -94,6 +94,12 @@ 1.2.58 + + com + dmjdbc + 8 + + org.springdoc diff --git a/src/main/java/com/simulationservice/Global.java b/src/main/java/com/simulationservice/Global.java new file mode 100644 index 0000000..474fee5 --- /dev/null +++ b/src/main/java/com/simulationservice/Global.java @@ -0,0 +1,12 @@ +package com.simulationservice; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +public class Global { + /** + * 线程安全队列, 需求数据队列 + */ + + public static BlockingQueue demandInfoQueue = new LinkedBlockingQueue<>(); +} diff --git a/src/main/java/com/simulationservice/config/DataSourceConfig.java b/src/main/java/com/simulationservice/config/DataSourceConfig.java new file mode 100644 index 0000000..7177966 --- /dev/null +++ b/src/main/java/com/simulationservice/config/DataSourceConfig.java @@ -0,0 +1,68 @@ +package com.simulationservice.config; + +import com.zaxxer.hikari.HikariDataSource; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.util.StringUtils; + +import javax.sql.DataSource; + +@Configuration +public class DataSourceConfig { + + /** + * 创建 orders 数据源的配置对象 + */ + @Primary + @Bean(name = "ordersDataSourceProperties") + @ConfigurationProperties(prefix = "spring.datasource.orders") // 读取 spring.datasource.orders 配置到 DataSourceProperties 对象 + public DataSourceProperties ordersDataSourceProperties() { + return new DataSourceProperties(); + } + + /** + * 创建 orders 数据源 + */ + @Bean(name = "ordersDataSource") + @ConfigurationProperties(prefix = "spring.datasource.orders.hikari") // 读取 spring.datasource.orders 配置到 HikariDataSource 对象 + public DataSource ordersDataSource() { + // <1.1> 获得 DataSourceProperties 对象 + DataSourceProperties properties = this.ordersDataSourceProperties(); + // <1.2> 创建 HikariDataSource 对象 + return createHikariDataSource(properties); + } + + /** + * 创建 users 数据源的配置对象 + */ + @Bean(name = "usersDataSourceProperties") + @ConfigurationProperties(prefix = "spring.datasource.users") // 读取 spring.datasource.users 配置到 DataSourceProperties 对象 + public DataSourceProperties usersDataSourceProperties() { + return new DataSourceProperties(); + } + + /** + * 创建 users 数据源 + */ + @Bean(name = "usersDataSource") + @ConfigurationProperties(prefix = "spring.datasource.users.hikari") + public DataSource usersDataSource() { + // 获得 DataSourceProperties 对象 + DataSourceProperties properties = this.usersDataSourceProperties(); + // 创建 HikariDataSource 对象 + return createHikariDataSource(properties); + } + + private static HikariDataSource createHikariDataSource(DataSourceProperties properties) { + // 创建 HikariDataSource 对象 + HikariDataSource dataSource = properties.initializeDataSourceBuilder().type(HikariDataSource.class).build(); + // 设置线程池名 + if (StringUtils.hasText(properties.getName())) { + dataSource.setPoolName(properties.getName()); + } + return dataSource; + } +} \ No newline at end of file diff --git a/src/main/java/com/simulationservice/controller/InferenceController.java b/src/main/java/com/simulationservice/controller/InferenceController.java index dda1c8f..248bdce 100644 --- a/src/main/java/com/simulationservice/controller/InferenceController.java +++ b/src/main/java/com/simulationservice/controller/InferenceController.java @@ -10,6 +10,8 @@ import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; /** * 想定控制,与前端进行交互的接口 diff --git a/src/main/java/com/simulationservice/service/InferenceTaskService.java b/src/main/java/com/simulationservice/service/InferenceTaskService.java index 78d46f6..305479c 100644 --- a/src/main/java/com/simulationservice/service/InferenceTaskService.java +++ b/src/main/java/com/simulationservice/service/InferenceTaskService.java @@ -3,6 +3,10 @@ package com.simulationservice.service; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + /** * 推演任务服务,用于创建线程和管理 */ @@ -11,6 +15,13 @@ public class InferenceTaskService { private DemandThread demandThread; private TimeSyncThread timeSyncThread; + + private final Map runState = new ConcurrentHashMap<>(); // 简单示例使用内存存储状态 + private static final String uuid = UUID.randomUUID().toString(); // ID,实际应用中可能需要更复杂的逻辑生成唯一ID + static { + //runState.put(GAME_ID, INITIAL_STATE); // 初始化游戏状态 + } + @Async public boolean loadScenario(String roomId, String scenarioId) { @@ -81,4 +92,12 @@ public class InferenceTaskService { demandThread.setTime(time); timeSyncThread.setTime(time); } + + public String getState() { + return "0";//gameState.get(GAME_ID); // 返回当前游戏状态,实际应用中可能需要更复杂的逻辑来处理多玩家状态同步等。 + } + + public void setState(String newState) { + //gameState.put(GAME_ID, newState); // 更新游戏状态,实际应用中可能需要更复杂的逻辑来处理多玩家状态同步等。 + } } diff --git a/src/main/java/com/simulationservice/util/JdbcDMUtils.java b/src/main/java/com/simulationservice/util/JdbcDMUtils.java new file mode 100644 index 0000000..ee1df03 --- /dev/null +++ b/src/main/java/com/simulationservice/util/JdbcDMUtils.java @@ -0,0 +1,55 @@ +package com.simulationservice.util; + +import org.springframework.jdbc.support.JdbcUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.sql.*; +import java.util.Properties; + +public class JdbcDMUtils { + + private static String driver; + private static String url; + private static String username; + private static String password; + + static{ + try { + //加载JDBC配置文件jdbc.properties + InputStream jdbc = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); + Properties properties = new Properties(); + properties.load(jdbc); + //获取配置文件信息 + driver = properties.getProperty("Driver"); + url = properties.getProperty("URL"); + username = properties.getProperty("username"); + password = properties.getProperty("password"); + + //加载驱动 + Class.forName(driver); + } catch (IOException | ClassNotFoundException e) { + e.printStackTrace(); + } + } + + //创建数据库连接对象connection + public static Connection getconnection() throws SQLException { + Connection con = DriverManager.getConnection(url, username, password); + return con; + } + + + //释放资源 + public static void release(Connection con, PreparedStatement pre, ResultSet res) throws SQLException { + if (con != null){ + con.close(); + } + if (pre != null){ + con.close(); + } + if (res != null){ + res.close(); + } + } +} diff --git a/src/main/java/com/simulationservice/util/JsonUtil.java b/src/main/java/com/simulationservice/util/JsonUtil.java new file mode 100644 index 0000000..e0ebb26 --- /dev/null +++ b/src/main/java/com/simulationservice/util/JsonUtil.java @@ -0,0 +1,30 @@ +package com.simulationservice.util; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +import java.text.SimpleDateFormat; + +public class JsonUtil { + + public static String getJson(Object object) { + return getJson(object, "yyyy-MM-dd HH:mm:ss"); + } + + public static String getJson(Object object,String dateFormat) { + ObjectMapper mapper = new ObjectMapper(); + //不使用时间差的方式 + mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + //自定义日期格式对象 + SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); + //指定日期格式 + mapper.setDateFormat(sdf); + try { + return mapper.writeValueAsString(object); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/src/main/java/com/simulationservice/util/MybatisUtils.java b/src/main/java/com/simulationservice/util/MybatisUtils.java new file mode 100644 index 0000000..298e65c --- /dev/null +++ b/src/main/java/com/simulationservice/util/MybatisUtils.java @@ -0,0 +1,28 @@ +package com.simulationservice.util; + +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; + +import java.io.IOException; +import java.io.InputStream; + +public class MybatisUtils { + private static SqlSessionFactory sqlSessionFactory ; + static { + try { + String resource = "mybatis-config.xml"; + //Ressoures调用getResourceAsStream方法读取resource文件并将它加载成流 + InputStream inputStream = Resources.getResourceAsStream(resource); + //构建SqlSessionFactory工厂,以便获取Sqlsession.通过SqlSessionFactoryBuilder()类调用build方法构建SqlSessionFactory工厂。 + sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + } catch (IOException e) { + e.printStackTrace(); + } + } + //获取sqlSession并返回。通过SqlSessionFactory工厂调用openSession()方法获取Sqlsession.注意Sqlsessoin中封装了所有SQL执行命令的方法。 + public static SqlSession getSession() { + return sqlSessionFactory.openSession(); + } +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index c745f2e..c595c4b 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -8,6 +8,27 @@ spring: password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver + # 订单数据源配置 + orders: + url: jdbc:mysql://127.0.0.1:3306/main?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai + driver-class-name: com.mysql.cj.jdbc.Driver + username: root + password: 123456 + # HikariCP 自定义配置,对应 HikariConfig 配置属性类 + hikari: + minimum-idle: 20 # 池中维护的最小空闲连接数,默认为 10 个。 + maximum-pool-size: 20 # 池中最大连接数,包括闲置和使用中的连接,默认为 10 个。 + # 用户数据源配置 + users: + url: jdbc:mysql://127.0.0.1:3306/evaluationsystem?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai + driver-class-name: com.mysql.cj.jdbc.Driver + username: root + password: 123456 + # HikariCP 自定义配置,对应 HikariConfig 配置属性类 + hikari: + minimum-idle: 15 # 池中维护的最小空闲连接数,默认为 10 个。 + maximum-pool-size: 15 # 池中最大连接数,包括闲置和使用中的连接,默认为 10 个。 + redis: host: 127.0.0.1 port: 6379 diff --git a/src/main/resources/jdbc.properties b/src/main/resources/jdbc.properties new file mode 100644 index 0000000..08cdc35 --- /dev/null +++ b/src/main/resources/jdbc.properties @@ -0,0 +1,4 @@ +Driver = dm.jdbc.driver.DmDriver +URL = jdbc:dm://192.168.0.53:5236/SIMULATION +username = simulation +password = Simulation001 \ No newline at end of file diff --git a/src/main/resources/mybatis-config.xml b/src/main/resources/mybatis-config.xml new file mode 100644 index 0000000..2673484 --- /dev/null +++ b/src/main/resources/mybatis-config.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/com/simulationservice/Application.java b/src/test/java/com/simulationservice/Application.java new file mode 100644 index 0000000..222e100 --- /dev/null +++ b/src/test/java/com/simulationservice/Application.java @@ -0,0 +1,168 @@ +package com.simulationservice; + +import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.simulationservice.domain.SysUser; +import com.simulationservice.mapper.SysUserMapper; +import com.simulationservice.util.JdbcDMUtils; +import com.simulationservice.util.MybatisUtils; +import com.sun.org.slf4j.internal.Logger; +import com.sun.org.slf4j.internal.LoggerFactory; +import org.apache.ibatis.session.SqlSession; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import javax.annotation.Resource; +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +@SpringBootApplication +public class Application implements CommandLineRunner { + + private Logger logger = LoggerFactory.getLogger(Application.class); + + @Resource(name = "ordersDataSource") + private DataSource ordersDataSource; + + @Resource(name = "usersDataSource") + private DataSource usersDataSource; + + public static PreparedStatement pre; + public static ResultSet re; + + //数据库查询 + public void TestSelect() throws SQLException { + //创建数据库连接 + Connection con = JdbcDMUtils.getconnection(); + if (con != null){ + System.out.println(("数据库连接成功")); + }else { + System.out.println(("数据库连接失败")); + } + String sql = "select * from sys_user"; + + //执行SQL + pre = con.prepareStatement(sql); + + //返回查询到的结果集 + re = pre.executeQuery(); + //循环输出结果集 + while (true) { + if (!re.next()) break; + System.out.println("id:" + re.getObject("id") + "\tname:" + re.getObject("RealName")); + System.out.println(); + } + //释放资源 + JdbcDMUtils.release(con, pre, re); + } + + //插入数据 + public void testInsert() throws SQLException { + //创建数据库连接 + Connection con = JdbcDMUtils.getconnection(); + String sql = "insert into t1 values(1,'xktk'),(2,'xktk2')"; + PreparedStatement pre = con.prepareStatement(sql); + int i = pre.executeUpdate(); + if (i != 0){ + System.out.println("插入成功"); + }else { + System.out.println("插入失败"); + } + JdbcDMUtils.release(con,pre,re); + } + + //更新数据 + public void testUpdate() throws SQLException { + Connection con = JdbcDMUtils.getconnection(); + String sql = "update t1 set name = 'xswl' where name = 'xktk'"; + pre = con.prepareStatement(sql); + int i = pre.executeUpdate(); + if (i != 0){ + System.out.println("修改成功"); + }else { + System.out.println("修改失败"); + } + JdbcDMUtils.release(con,pre,re); + } + + //删除数据 + public void testDelete() throws SQLException { + Connection con = JdbcDMUtils.getconnection(); + String sql = "delete from t1 where name = 'xktk2'"; + pre = con.prepareStatement(sql); + int i = pre.executeUpdate(); + if (i != 0){ + System.out.println("删除成功"); + }else { + System.out.println("删除失败"); + } + JdbcDMUtils.release(con,pre,re); + } + + //创建表 + public void TestCreate() throws SQLException { + Connection con = JdbcDMUtils.getconnection(); + String sql = "create table t2 (id int,name varchar)"; + pre = con.prepareStatement(sql); + int i = pre.executeUpdate(sql); + //返回值表示你update影响的纪录条数,如果是-1表示无影响,如果是建表等语句,返回0。 + if (i == 0){ + System.out.println("创建成功"); + }else { + System.out.println("创建失败"); + } + JdbcDMUtils.release(con,pre,re); + } + + public void selectUser(){ + ObjectMapper objMapper = new ObjectMapper(); + //调用MybatisUtils中的getSession()方法获取Sqlsession + SqlSession session = MybatisUtils.getSession(); + //session调用getMapper方法获取接口对象 + SysUserMapper mapper = session.getMapper(SysUserMapper.class); + //调用接口方法返回查询到的结果集 + SysUser user = mapper.selectByPrimaryKey("USER-994BEF702B6C9254B4913F388C5546BE"); + System.out.println(user); + + try { + String json = objMapper.writeValueAsString(user); + System.out.println(json); + } catch (Exception e) { + e.printStackTrace(); + } + session.close(); + } + + public static void main(String[] args) { + // 启动 Spring Boot 应用 + SpringApplication.run(Application.class, args); + } + + @Override + public void run(String... args) throws SQLException { + // orders 数据源 + JSONObject jsonObject = new JSONObject(); + jsonObject.putIfAbsent("[Order]获得数据源", ordersDataSource.getClass()); + String jsonString = jsonObject.toJSONString(); + System.out.println(jsonString); + + // users 数据源 + JSONObject jsonObject1 = new JSONObject(); + jsonObject1.putIfAbsent("[user]获得数据源", usersDataSource.getClass()); + String jsonString1 = jsonObject1.toJSONString(); + System.out.println(jsonString1); + + try { + TestSelect(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + selectUser(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/simulationservice/SimulationServiceApplication.java b/src/test/java/com/simulationservice/SimulationServiceApplication.java similarity index 99% rename from src/main/java/com/simulationservice/SimulationServiceApplication.java rename to src/test/java/com/simulationservice/SimulationServiceApplication.java index 4dfea80..ad6a487 100644 --- a/src/main/java/com/simulationservice/SimulationServiceApplication.java +++ b/src/test/java/com/simulationservice/SimulationServiceApplication.java @@ -9,5 +9,4 @@ public class SimulationServiceApplication { public static void main(String[] args) { SpringApplication.run(SimulationServiceApplication.class, args); } - }