Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure datasource and mybatis-plus #29

Merged
merged 7 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/java-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ jobs:
echo "java_files_exist=false" >> $GITHUB_OUTPUT
fi

- name: Set up openjdk-17
uses: actions/setup-java@v4
if: steps.check_java_files.outputs.java_files_exist == 'true'
with:
distribution: 'zulu'
java-version: '17'

- name: Google Java Style Format
if: steps.check_java_files.outputs.java_files_exist == 'true'
uses: axel-op/googlejavaformat-action@v3
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ replay_pid*

# root directory for the file explorer
.root/

# this file should be generated by the helper script
application.properties
2 changes: 2 additions & 0 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@
| `postgresqlDatabaseName` | `string` | `"gcs"` | `Postgres` 数据库名称。 |
| `postgresqlHost` | `string` | `"localhost"` | `Postgres` 主机地址。 |
| `postgresqlPort` | `int` | `5432` | `Postgres` 端口。 |
| `druidLoginUsername` | `string` | `"druid"` | `Druid` 登录用户名。 |
| `druidLoginPassword` | `string` | `"druid"` | `Druid` 登录密码。 |

6 changes: 3 additions & 3 deletions config_debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"profiles": [
"dev"
],
"postgresUserPassword": "test",
"postgresqlUserName": "test",
"postgresqlUserPassword": "test"
"postgresqlUserName": "gcs_debug",
"postgresqlUserPassword": "gcs_debug",
"postgresqlDatabaseName": "gcs_debug"
}
4 changes: 3 additions & 1 deletion config_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@
"postgresqlUserPassword": "postgres",
"postgresqlDatabaseName": "gcs",
"postgresqlHost": "localhost",
"postgresqlPort": 5432
"postgresqlPort": 5432,
"druidLoginUsername": "druid",
"druidLoginPassword": "druid"
}
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
<version>1.2.23</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
</dependencies>

<build>
Expand Down
59 changes: 46 additions & 13 deletions script/deploy_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def deploy_with_sys_v_init(config):
command_checker(res, f"Failed to start {config.serviceName} with boot")


def active_profile(config):
def activate_profile(config):
profile_format = f"spring.profiles.active={parse_iterable_into_str(config.profiles, sep=',')}"
log_debug(f"Profile format: {profile_format}")
try:
Expand All @@ -198,7 +198,34 @@ def active_profile(config):
for line in lines:
if not line.startswith('spring.profiles.active'):
f.write(line)
f.write(profile_format)
f.write(profile_format + '\n')
except Exception as e:
command_checker(1, f"Error: {e}")


def config_datasource(config):
datasource_map_config = {
"username": config.postgresqlUserName,
"password": config.postgresqlUserPassword,
"url": f"jdbc:postgresql://{config.postgresqlHost}:{config.postgresqlPort}/{config.postgresqlDatabaseName}",
"stat-view-servlet.login-username": config.druidLoginUsername,
"stat-view-servlet.login-password": config.druidLoginPassword,
}
datasource_format = "spring.datasource.druid.{0}={1}"
log_debug(f"Datasource format: {datasource_format}")
try:
lines = None
if os.path.exists(application_config_file_path):
with open(application_config_file_path, 'r') as f:
lines = f.readlines()
with open(application_config_file_path, 'w') as f:
if lines:
for line in lines:
if not line.startswith('spring.datasource.druid'):
f.write(line + '\n')
for key, value in datasource_map_config.items():
f.write(datasource_format.format(key, value) + '\n')
log_debug(f"Datasource config: {datasource_format.format(key, value)}")
except Exception as e:
command_checker(1, f"Error: {e}")

Expand All @@ -215,7 +242,7 @@ def init_database(config):
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True, cwd='/tmp')
assert(process.stdin is not None)
process.stdin.write(f'{config.postgresqlUserPassword}')
process.stdin.write(f'{config.postgresUserPassword}')
process.stdin.flush()
out, err = process.communicate()
command_checker(process.returncode, f"Failed to check the user in database: {err}")
Expand All @@ -230,7 +257,7 @@ def init_database(config):
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True, cwd='/tmp')
assert(process.stdin is not None)
process.stdin.write(f'{config.postgresqlUserPassword}')
process.stdin.write(f'{config.postgresUserPassword}')
process.stdin.flush()
out, err = process.communicate()
command_checker(process.returncode, f"Failed to update the password in database: {err}")
Expand All @@ -245,7 +272,7 @@ def init_database(config):
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True, cwd='/tmp')
assert(process.stdin is not None)
process.stdin.write(f'{config.postgresqlUserPassword}')
process.stdin.write(f'{config.postgresUserPassword}')
process.stdin.flush()
out, err = process.communicate()
command_checker(process.returncode, f"Failed to create the user in database: {err}")
Expand All @@ -258,7 +285,7 @@ def init_database(config):
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True, cwd='/tmp')
assert(process.stdin is not None)
process.stdin.write(f'{config.postgresqlUserPassword}')
process.stdin.write(f'{config.postgresUserPassword}')
process.stdin.flush()
out, err = process.communicate()
command_checker(process.returncode, f"Failed to check the database: {err}")
Expand All @@ -272,11 +299,14 @@ def init_database(config):
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True, cwd='/tmp')
assert(process.stdin is not None)
process.stdin.write(f'{config.postgresqlUserPassword}')
process.stdin.write(f'{config.postgresUserPassword}')
process.stdin.flush()
out, err = process.communicate()
command_checker(process.returncode, f"Failed to create the database: {err}")
log_info(f"Database {config.postgresqlDatabaseName} has been created")
run_shell_script = True
else:
run_shell_script = False
# grant the user
log_info(f"Granting the user in database: {config.postgresqlUserName}")
process = subprocess.Popen(['su', '-c',
Expand All @@ -286,15 +316,18 @@ def init_database(config):
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True, cwd='/tmp')
assert(process.stdin is not None)
process.stdin.write(f'{config.postgresqlUserPassword}')
process.stdin.write(f'{config.postgresUserPassword}')
process.stdin.flush()
out, err = process.communicate()
command_checker(process.returncode, f"Failed to grant the user in database: {err}")
log_info(f"User {config.postgresqlUserName} has been granted in database")
res = os.system(f'bash database/database_deploy.sh {config.postgresqlUserName} '
f'{config.postgresqlDatabaseName} {config.postgresqlHost} '
f'{config.postgresqlPort} {config.postgresqlUserPassword}')
command_checker(res, f"Failed to deploy the database")
# Do not run the shell script if the database had been created before
if run_shell_script:
res = os.system(f'bash database/database_deploy.sh {config.postgresqlUserName} '
f'{config.postgresqlDatabaseName} {config.postgresqlHost} '
f'{config.postgresqlPort} {config.postgresqlUserPassword}')
command_checker(res, f"Failed to deploy the database")
config_datasource(config)


def create_or_update_user(username, password):
Expand Down Expand Up @@ -326,7 +359,7 @@ def deploy_on_ubuntu(config):
essential_packages.remove('systemd')
apt_install_package(parse_iterable_into_str(essential_packages))
init_database(config)
active_profile(config)
activate_profile(config)
skip_test = ""
if config.skipTest:
skip_test = "-Dmaven.test.skip=true"
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/edu/cmipt/gcs/GcsApplication.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package edu.cmipt.gcs;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("edu.cmipt.gcs.dao")
public class GcsApplication {

public static void main(String[] args) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/edu/cmipt/gcs/config/DruidConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package edu.cmipt.gcs.config;

import org.springframework.context.annotation.Configuration;

@Configuration
public class DruidConfig {}
7 changes: 7 additions & 0 deletions src/main/java/edu/cmipt/gcs/dao/UserMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package edu.cmipt.gcs.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import edu.cmipt.gcs.pojo.UserPO;

public interface UserMapper extends BaseMapper<UserPO> {}
16 changes: 16 additions & 0 deletions src/main/java/edu/cmipt/gcs/pojo/UserPO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package edu.cmipt.gcs.pojo;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.time.LocalDateTime;

@TableName("t_user")
public record UserPO(
@TableId Long pkUserId,
String username,
String email,
String userPassword,
LocalDateTime gmtCreated,
LocalDateTime gmtUpdated,
LocalDateTime gmtDeleted) {}
6 changes: 5 additions & 1 deletion src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
debug: true
spring:
datasource:
druid:
test-on-borrow: true
test-on-return: true
5 changes: 5 additions & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring:
datasource:
druid:
test-on-borrow: false
test-on-return: false
5 changes: 5 additions & 0 deletions src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring:
datasource:
druid:
test-on-borrow: true
test-on-return: true
Empty file.
38 changes: 38 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
spring:
application:
name: gcs
datasource:
druid:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.postgresql.Driver
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 6000 # unit: ms
time-between-eviction-runs-millis: 60000
min-evication-idle-time-millis: 600000 # min alive time of a connection
max-evication-idle-time-millis: 1200000 # max alive time of a connection
validation-query: SELECT 1
test-while-idle: true
async-init: true
keep-alive: true
filters:
stat:
enable: true
log-slow-sql: true
slow-sql-millis: 1000
wall:
enable: true
log-violation: true
throw-exception: false
config:
drop-table-allow: false
delete-allow: false
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
session-stat-enable: true
session-stat-max-count: 1000
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: false
allow: # empty means allow all

logging:
include-application-name: false
26 changes: 26 additions & 0 deletions src/test/java/edu/cmipt/gcs/controller/dao/UserMapperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package edu.cmipt.gcs.controller.dao;

import com.baomidou.mybatisplus.core.toolkit.Assert;

import edu.cmipt.gcs.dao.UserMapper;
import edu.cmipt.gcs.pojo.UserPO;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class UserMapperTest {

@Autowired private UserMapper userMapper;

@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<UserPO> userList = userMapper.selectList(null);
Assert.isTrue(1 == userList.size(), "");
userList.forEach(System.out::println);
}
}
Loading