研究多个数据库连接,使用不同的形式进行数据库操作和编写测试例子进行测试
This commit is contained in:
parent
1946f33be3
commit
5650664fc2
|
@ -12,6 +12,22 @@
|
|||
</entry>
|
||||
</map>
|
||||
</option>
|
||||
<option name="globalConfig">
|
||||
<GlobalConfig>
|
||||
<option name="comment" value="true" />
|
||||
<option name="domainPackage" value="" />
|
||||
<option name="examplePackage" value="" />
|
||||
<option name="examplePostfix" value="Example" />
|
||||
<option name="lombokAnnotation" value="true" />
|
||||
<option name="mapperPackage" value="" />
|
||||
<option name="mapperPostfix" value="Mapper" />
|
||||
<option name="moduleRootPath" value="$PROJECT_DIR$" />
|
||||
<option name="resourcePath" value="src/main/resources" />
|
||||
<option name="sourcePath" value="src/main/java" />
|
||||
<option name="tablePrefix" value="" />
|
||||
<option name="xmlPackage" value="mapper" />
|
||||
</GlobalConfig>
|
||||
</option>
|
||||
<option name="tableConfigs">
|
||||
<map>
|
||||
<entry key="sys_user">
|
||||
|
|
|
@ -5,15 +5,22 @@
|
|||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="050b8051-b1ec-42aa-bac3-c1c189a4697d" name="更改" comment="修改线程继承自定义线程相关类错误问题">
|
||||
<change afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/Global.java" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/config/DataSourceConfig.java" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/util/JdbcDMUtils.java" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/util/JsonUtil.java" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/util/MybatisUtils.java" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/resources/jdbc.properties" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/resources/mybatis-config.xml" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/test/java/com/simulationservice/Application.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/mybatis-generator-config.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/mybatis-generator-config.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/doc/业务流程.vsdx" beforeDir="false" afterPath="$PROJECT_DIR$/doc/业务流程.vsdx" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/DemandThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/DemandThread.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TaskAssignThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TaskAssignThread.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TaskThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TaskThread.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TimeSyncThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TimeSyncThread.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/WasterThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/WasterThread.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/WebSocketServer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/WebSocketServer.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/util/CustomThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/util/CustomThread.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/SimulationServiceApplication.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/com/simulationservice/SimulationServiceApplication.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/controller/InferenceController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/controller/InferenceController.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/InferenceTaskService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/InferenceTaskService.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/resources/application.yaml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/application.yaml" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
|
@ -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 @@
|
|||
<component name="ReactorSettings">
|
||||
<option name="notificationShown" value="true" />
|
||||
</component>
|
||||
<component name="RunManager">
|
||||
<component name="RecentsManager">
|
||||
<key name="MoveClassesOrPackagesDialog.RECENTS_KEY">
|
||||
<recent name="com.simulationservice" />
|
||||
</key>
|
||||
</component>
|
||||
<component name="RunManager" selected="Spring Boot.Application">
|
||||
<configuration name="Application" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" temporary="true" nameIsGenerated="true">
|
||||
<option name="FRAME_DEACTIVATION_UPDATE_POLICY" value="UpdateClassesAndResources" />
|
||||
<module name="SimulationService" />
|
||||
<option name="SPRING_BOOT_MAIN_CLASS" value="com.simulationservice.Application" />
|
||||
<extension name="coverage">
|
||||
<pattern>
|
||||
<option name="PATTERN" value="com.simulationservice.*" />
|
||||
<option name="ENABLED" value="true" />
|
||||
</pattern>
|
||||
</extension>
|
||||
<method v="2">
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
<configuration name="SimulationService" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot">
|
||||
<option name="ALTERNATIVE_JRE_PATH" value="1.8" />
|
||||
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
|
||||
|
@ -104,6 +132,11 @@
|
|||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Spring Boot.Application" />
|
||||
</list>
|
||||
</recent_temporary>
|
||||
</component>
|
||||
<component name="SharedIndexes">
|
||||
<attachedChunks>
|
||||
|
@ -113,6 +146,11 @@
|
|||
</set>
|
||||
</attachedChunks>
|
||||
</component>
|
||||
<component name="SshConsoleOptionsProvider">
|
||||
<option name="myEncoding" value="UTF-8" />
|
||||
<option name="myConnectionType" value="NONE" />
|
||||
<option name="myConnectionId" value="" />
|
||||
</component>
|
||||
<component name="TaskManager">
|
||||
<task active="true" id="Default" summary="默认任务">
|
||||
<changelist id="050b8051-b1ec-42aa-bac3-c1c189a4697d" name="更改" comment="" />
|
||||
|
@ -130,7 +168,7 @@
|
|||
<workItem from="1757664586037" duration="3070000" />
|
||||
<workItem from="1757820561153" duration="249000" />
|
||||
<workItem from="1757829080725" duration="24604000" />
|
||||
<workItem from="1757922989495" duration="29564000" />
|
||||
<workItem from="1757922989495" duration="68720000" />
|
||||
</task>
|
||||
<task id="LOCAL-00001" summary="修改配置文件和去掉打包时test模块">
|
||||
<option name="closed" value="true" />
|
||||
|
|
BIN
doc/业务流程.vsdx
BIN
doc/业务流程.vsdx
Binary file not shown.
6
pom.xml
6
pom.xml
|
@ -94,6 +94,12 @@
|
|||
<version>1.2.58</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com</groupId>
|
||||
<artifactId>dmjdbc</artifactId>
|
||||
<version>8</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 项目文档生成接口 -->
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
|
|
12
src/main/java/com/simulationservice/Global.java
Normal file
12
src/main/java/com/simulationservice/Global.java
Normal file
|
@ -0,0 +1,12 @@
|
|||
package com.simulationservice;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
public class Global {
|
||||
/**
|
||||
* 线程安全队列, 需求数据队列
|
||||
*/
|
||||
|
||||
public static BlockingQueue<String> demandInfoQueue = new LinkedBlockingQueue<>();
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
/**
|
||||
* 想定控制,与前端进行交互的接口
|
||||
|
|
|
@ -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<String, String> 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); // 更新游戏状态,实际应用中可能需要更复杂的逻辑来处理多玩家状态同步等。
|
||||
}
|
||||
}
|
||||
|
|
55
src/main/java/com/simulationservice/util/JdbcDMUtils.java
Normal file
55
src/main/java/com/simulationservice/util/JdbcDMUtils.java
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
30
src/main/java/com/simulationservice/util/JsonUtil.java
Normal file
30
src/main/java/com/simulationservice/util/JsonUtil.java
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
28
src/main/java/com/simulationservice/util/MybatisUtils.java
Normal file
28
src/main/java/com/simulationservice/util/MybatisUtils.java
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
4
src/main/resources/jdbc.properties
Normal file
4
src/main/resources/jdbc.properties
Normal file
|
@ -0,0 +1,4 @@
|
|||
Driver = dm.jdbc.driver.DmDriver
|
||||
URL = jdbc:dm://192.168.0.53:5236/SIMULATION
|
||||
username = simulation
|
||||
password = Simulation001
|
28
src/main/resources/mybatis-config.xml
Normal file
28
src/main/resources/mybatis-config.xml
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE configuration
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||
<configuration>
|
||||
<properties resource="jdbc.properties" />
|
||||
<!--<settings>
|
||||
<setting name="mapUnderscoreToCamelCase" value="true" />
|
||||
<setting name="logImpl" value="sys_user" />
|
||||
</settings>
|
||||
<typeAliases>
|
||||
<package name="com.simulationservice.mapper"/>
|
||||
</typeAliases>-->
|
||||
<environments default="development">
|
||||
<environment id="development">
|
||||
<transactionManager type="JDBC" />
|
||||
<dataSource type="POOLED">
|
||||
<property name="driver" value="${Driver}"/>
|
||||
<property name="url" value="${URL}"/>
|
||||
<property name="username" value="${username}"/>
|
||||
<property name="password" value="${password}"/>
|
||||
</dataSource>
|
||||
</environment>
|
||||
</environments>
|
||||
<mappers>
|
||||
<mapper resource="mapper/SysUserMapper.xml" />
|
||||
</mappers>
|
||||
</configuration>
|
168
src/test/java/com/simulationservice/Application.java
Normal file
168
src/test/java/com/simulationservice/Application.java
Normal file
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -9,5 +9,4 @@ public class SimulationServiceApplication {
|
|||
public static void main(String[] args) {
|
||||
SpringApplication.run(SimulationServiceApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user