【发布时间】:2019-10-08 12:07:19
【问题描述】:
在我的芭蕾舞演员项目中,我在同一个 .bal 文件中完成了数据库连接创建和服务实现。
import ballerina/config;
import ballerina/http;
import ballerina/io;
import ballerina/jsonutils;
import ballerinax/java.jdbc;
jdbc:Client studentMgtDB = new ({
url: "jdbc:mysql://" + config:getAsString("student.jdbc.dbHost") + ":" + config:getAsString("student.jdbc.dbPort") + "/" + config:getAsString("student.jdbc.db"),
username: config:getAsString("student.jdbc.username"),
password: config:getAsString("student.jdbc.password"),
poolOptions: {maximumPoolSize: 5},
dbOptions: {useSSL: false}
});
type Student record {
int std_id;
string name;
int age;
string address;
};
listener http:Listener studentMgtServiceListener = new (9090);
@http:ServiceConfig {
basePath: "/students"
}
service studentMgtService on studentMgtServiceListener {
@http:ResourceConfig {
methods: ["GET"],
path: "/"
}
resource function getStudent(http:Caller caller, http:Request req) {
var selectStudents = studentMgtDB->select("SELECT * FROM student", Student);
http:Response response = new;
if (selectStudents is table<Student>) {
response.statusCode = 200;
} else {
response.statusCode = 500;
}
checkpanic caller->respond(response);
}
}
我只想将数据库连接部分移动到单独的文件中,因为这样更便于维护。那么最好的方法是什么?
【问题讨论】:
标签: ballerina