Ballerina V 1.0-将数据库连接保存在单独的文件中
在我的ballerina项目中,我在同一个.bal文件中创建了DB连接并实现了服务Ballerina V 1.0-将数据库连接保存在单独的文件中,ballerina,Ballerina,在我的ballerina项目中,我在同一个.bal文件中创建了DB连接并实现了服务 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("
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/config;
导入ballerina/http;
进口芭蕾舞演员/io;
进口芭蕾舞演员/芭蕾舞演员;
导入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”),
用户名:config:getAsString(“student.jdbc.username”),
密码:config:getAsString(“student.jdbc.password”),
池选项:{maximumPoolSize:5},
dbOptions:{useSSL:false}
});
输入学生记录{
国际标准标识;
字符串名;
智力年龄;
字符串地址;
};
监听器http:listener studentMgtServiceListener=new(9090);
@http:ServiceConfig{
基本路径:“/学生”
}
在studentMgtServiceListener上服务studentMgtService{
@http:ResourceConfig{
方法:[“获取”],
路径:“/”
}
资源函数getStudent(http:Caller-Caller,http:Request-req){
var selectStudents=studentMgtDB->select(“选择*来自学生”,学生);
http:Response-Response=new;
如果(选择学生是表){
response.statusCode=200;
}否则{
response.statusCode=500;
}
选中紧急呼叫->响应(响应);
}
}
我只想将DB连接部分移动到一个单独的文件中,因为这样更利于维护。那么最好的方法是什么呢?如果你想在多个bal文件中管理你的代码,你需要用Ballerina项目来组织你的代码。有关更多信息,请参阅
// Create a new project with the name 'student_mgt_proj'
$ ballerina new studentmgt_proj
$ cd student_mgt_proj
// Create a new module named 'studentmgt'
$ ballerina add studentmgt
现在将所有bal文件添加到src/studentmgt
目录
// Build the executable service jar
$ ballerina build studentmgt
// You can also use java -jar studentmgt.jar, if you are using Java 8
$ ballerina run studentmgt.jar
谢谢你。我也试过这个。现在我在studentmgt模块中有2个.bal文件,分别为dbConn.bal和student.bal。在dbConn.bal中,我创建了一个返回jdbc:Client的函数。当我在student.bal中调用该方法时,dbConn.bal中出现了一个编译错误,即“undocumented return parameter”。我在这里犯了什么错误?在中提出了另一个问题:。