ballerina 数据操作也是比较方便的,官方也我们提供了数据操作的抽象,但是我们还是依赖数据库驱动的。
数据库驱动还是jdbc模式的

项目准备

  • 项目结构
├── mysql_demo
│ ├── Dockerfile
│ ├── docker-compose.yaml
│ ├── inid.sql
│ ├── mysql-connector-java-8.0.13.jar
│ └── mysql_service.bal
  • 代码说明
dockerfile:
mysql 数据库的dockerfile,包含数据的初始化
FROM mysql/mysql-server:5.7
COPY inid.sql /docker-entrypoint-initdb.d
docker-compose.yaml: docker-compose 运行包含ballerina api 以及数据库
version: "3"
services:
api:
image: dalongrong/mysql_demo:v1.0
ports:
- "9090:9090"
mysql:
build: ./
ports:
- 3306:3306
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: EMPLOYEE_RECORDS
MYSQL_USER: gogs
MYSQL_PASSWORD: dalongrong
TZ: Asia/Shanghai inid.sql: 操作依赖的数据库模型
/*
* MySQL Script - initializeDataBase.sql.
* Create EMPLOYEE_RECORDS database and EMPLOYEES table.
*/ -- Create EMPLOYEE_RECORDS database
CREATE DATABASE IF NOT EXISTS EMPLOYEE_RECORDS; -- Switch to EMPLOYEE_RECORDS database
USE EMPLOYEE_RECORDS; -- create EMPLOYEES table in the database
CREATE TABLE IF NOT EXISTS EMPLOYEES (EmployeeID INT, Name VARCHAR(50), Age INT, SSN INT, PRIMARY KEY (EmployeeID)) mysql_service.bal: 数据库操作代码,集成了docker import ballerina/config;
import ballerina/http;
import ballerina/log;
import ballerina/mysql;
import ballerina/sql;
import ballerinax/docker; type Employee record {
string name;
int age;
int ssn;
int employeeId;
}; // Create SQL endpoint to MySQL database
endpoint mysql:Client employeeDB {
host: "mysql",
port: config:getAsInt("DATABASE_PORT", default = 3306),
name: config:getAsString("DATABASE_NAME", default = "EMPLOYEE_RECORDS"),
username: config:getAsString("DATABASE_USERNAME", default = "gogs"),
password: config:getAsString("DATABASE_PASSWORD", default = "dalongrong"),
dbOptions: { useSSL: false }
};
@docker:Config {
registry: "dalongrong",
name: "mysql_demo",
tag: "v1.0"
} @docker:CopyFiles {
// 注意此处mysql jdbc 驱动地址,为了使用简单,我写的是绝对路径,实际上构建之后会copy 到target 目录,方便镜像构建
files: [{ source: "/Users/dalong/mylearning/ballerina-project/learning/guide/mysql_demo/mysql-connector-java-8.0.13.jar",
target: "/ballerina/runtime/bre/lib" }]
} @docker:Expose {}
endpoint http:Listener listener {,
port: 9090
}; // Service for the employee data service
@http:ServiceConfig {
basePath: "/records"
}
service<http:Service> EmployeeData bind listener { @http:ResourceConfig {
methods: ["POST"],
path: "/employee/"
}
addEmployeeResource(endpoint httpConnection, http:Request request) {
// Initialize an empty http response message
http:Response response;
Employee employeeData;
// Extract the data from the request payload
var payloadJson = check request.getJsonPayload();
employeeData = check <Employee>payloadJson; // Check for errors with JSON payload using
if (employeeData.name == "" || employeeData.age == 0 || employeeData.ssn == 0 ||
employeeData.employeeId == 0) {
response.setTextPayload("Error : json payload should contain
{name:<string>, age:<int>, ssn:<123456>,employeeId:<int>} ");
response.statusCode = 400;
_ = httpConnection->respond(response);
done;
} // Invoke insertData function to save data in the Mymysql database
json ret = insertData(employeeData.name, employeeData.age, employeeData.ssn,
employeeData.employeeId);
// Send the response back to the client with the employee data
response.setJsonPayload(ret);
_ = httpConnection->respond(response);
} @http:ResourceConfig {
methods: ["GET"],
path: "/employee/{employeeId}"
}
retrieveEmployeeResource(endpoint httpConnection, http:Request request, string
employeeId) {
// Initialize an empty http response message
http:Response response;
// Convert the employeeId string to integer
int empID = check <int>employeeId;
// Invoke retrieveById function to retrieve data from Mymysql database
var employeeData = retrieveById(empID);
// Send the response back to the client with the employee data
response.setJsonPayload(untaint employeeData);
_ = httpConnection->respond(response);
} @http:ResourceConfig {
methods: ["PUT"],
path: "/employee/"
}
updateEmployeeResource(endpoint httpConnection, http:Request request) {
// Initialize an empty http response message
http:Response response;
Employee employeeData;
// Extract the data from the request payload
var payloadJson = check request.getJsonPayload();
employeeData = check <Employee>payloadJson; if (employeeData.name == "" || employeeData.age == 0 || employeeData.ssn == 0 ||
employeeData.employeeId == 0) {
response.setTextPayload("Error : json payload should contain
{name:<string>, age:<int>, ssn:<123456>,employeeId:<int>} ");
response.statusCode = 400;
_ = httpConnection->respond(response);
done;
} // Invoke updateData function to update data in mysql database
json ret = updateData(employeeData.name, employeeData.age, employeeData.ssn,
employeeData.employeeId);
// Send the response back to the client with the employee data
response.setJsonPayload(ret);
_ = httpConnection->respond(response);
} @http:ResourceConfig {
methods: ["DELETE"],
path: "/employee/{employeeId}"
}
deleteEmployeeResource(endpoint httpConnection, http:Request request, string
employeeId) {
// Initialize an empty http response message
http:Response response;
// Convert the employeeId string to integer
var empID = check <int>employeeId;
var deleteStatus = deleteData(empID);
// Send the response back to the client with the employee data
response.setJsonPayload(deleteStatus);
_ = httpConnection->respond(response);
}
} public function insertData(string name, int age, int ssn, int employeeId) returns (json){
json updateStatus;
string sqlString =
"INSERT INTO EMPLOYEES (Name, Age, SSN, EmployeeID) VALUES (?,?,?,?)";
// Insert data to SQL database by invoking update action
var ret = employeeDB->update(sqlString, name, age, ssn, employeeId);
// Use match operator to check the validity of the result from database
match ret {
int updateRowCount => {
updateStatus = { "Status": "Data Inserted Successfully" };
}
error err => {
updateStatus = { "Status": "Data Not Inserted", "Error": err.message };
}
}
return updateStatus;
} public function retrieveById(int employeeID) returns (json) {
json jsonReturnValue;
string sqlString = "SELECT * FROM EMPLOYEES WHERE EmployeeID = ?";
// Retrieve employee data by invoking select action defined in ballerina sql client
var ret = employeeDB->select(sqlString, (), employeeID);
match ret {
table dataTable => {
// Convert the sql data table into JSON using type conversion
jsonReturnValue = check <json>dataTable;
}
error err => {
jsonReturnValue = { "Status": "Data Not Found", "Error": err.message };
}
}
return jsonReturnValue;
} public function updateData(string name, int age, int ssn, int employeeId) returns (json){
json updateStatus = {};
string sqlString =
"UPDATE EMPLOYEES SET Name = ?, Age = ?, SSN = ? WHERE EmployeeID = ?";
// Update existing data by invoking update action defined in ballerina sql client
var ret = employeeDB->update(sqlString, name, age, ssn, employeeId);
match ret {
int updateRowCount => {
if (updateRowCount > 0) {
updateStatus = { "Status": "Data Updated Successfully" };
}
else {
updateStatus = { "Status": "Data Not Updated" };
}
}
error err => {
updateStatus = { "Status": "Data Not Updated", "Error": err.message };
}
}
return updateStatus;
} public function deleteData(int employeeID) returns (json) {
json updateStatus = {}; string sqlString = "DELETE FROM EMPLOYEES WHERE EmployeeID = ?";
// Delete existing data by invoking update action defined in ballerina sql client
var ret = employeeDB->update(sqlString, employeeID);
match ret {
int updateRowCount => {
updateStatus = { "Status": "Data Deleted Successfully" };
}
error err => {
updateStatus = { "Status": "Data Not Deleted", "Error": err.message };
}
}
return updateStatus;
}

构建&&运行

  • 构建容器镜像(ballerina mysql 操作)
ballerina build mysql_demo

  • docker-compose 启动(数据库以及服务的启动)
docker-compose up -d

数据操作

  • post 数据
curl -v -X POST -d '{"name":"Alice", "age":20,"ssn":123456789,"employeeId":1}' \
"http://localhost:9090/records/employee" -H "Content-Type:application/json"
  • get 查询数据
curl -v "http://localhost:9090/records/employee/1"
  • 效果

说明

使用起来还是比较简单的,同时集合match的模式匹配操作,开发起来很方便

参考资料

https://ballerina.io/learn/by-guide/data-backed-service/

 
 
 
 

ballerina 学习二十九 数据库操作的更多相关文章

  1. Java从零开始学二十九(大数操作(BigIntger、BigDecimal)

    一.BigInteger 如果在操作的时候一个整型数据已经超过了整数的最大类型长度long的话,则此数据就无法装入,所以,此时要使用BigInteger类进行操作. 不可变的任意精度的整数.所有操作中 ...

  2. ballerina 学习二十五 项目docker 部署&& 运行

    ballerina 官方提供了docker 的runtime,还是比较方便的 基本项目创建 使用cli创建项目 按照提示操作就行 ballerina init -i 项目结构 添加了dockerfil ...

  3. 前端学习(二十九)nodejs(笔记)

    后台语言    java     php     .Net     python    Node.js------------------------------------------------- ...

  4. ballerina 学习二十八 快速grpc 服务开发

    ballerina 的grpc 开发模型,对于开发者来说简单了好多,不是schema first 的方式,而是我们 只要编写简单的ballerina service 就可以了,proto 文件是自动帮 ...

  5. ballerina 学习二十六 项目docker 部署&& 运行(二)

    ballerina 从发布,到现在官方文档的更新也是很给力的,同时也有好多改进,越来越好用了 可以参考官方文档 https://ballerina.io/learn/by-guide/restful- ...

  6. ballerina 学习二十四 监控ballerina

    ballerina 服务的监控还是比较方便的,以及集成了Prometheus Grafana Jaeger Elastic Stack 监控服务监控的集成 主要包含以下几个步骤 a. 安装docker ...

  7. ballerina 学习二十二 弹性服务

    主要包含断路器模式,负载均衡模式,故障转移,重试 Circuit Breaker 参考代码 import ballerina/http; import ballerina/log; import ba ...

  8. ballerina 学习二十 http/https

    提供http && https server && client 访问功能 client endpoint 说白了就是http client 参考代码 import b ...

  9. python学习笔记(十)完善数据库操作

    1.cur = coon.cursor(cursor=pymysql.cursors.DictCursor)的用法 建立游标,指定cursor类型返回的是字典,如果不指定类型,返回的是元组类型数据 i ...

随机推荐

  1. 52 53django

    一.需要弄清楚的几个关键点: KEY: 1 Django项目无论多大,只是一个应用程序 2 地址栏发请求默认是GET请求 form表单可以发送get请求,也可以发送post请求 3 浏览器接受的响应体 ...

  2. H5表单基础知识(二)

    表单新增属性 <!--<input type="text" class="name" />--> <!-- placeholder ...

  3. Greedy Subsequences CodeForces - 1132G

    我们从右往左滑动区间, 假设dp[i]表示i为左端点时的最大长度, 通过观察可以发现, 每添加一个点, 该点$dp$值=它右侧第一个比它大位置处$dp$值+1, 但是每删除一个点会将所有以它为根的$d ...

  4. Dynamic Web Module 3.1 requires Java 1.7 or newer. 错误解决方案

    在写代码的时候工程出现了这样奇怪的bug很是蛋疼啊,经过查询解决方法,终于解决了这些个问题. 下面是解决问题的方法,和大家分享一下 (1)确定你的java工程配置使用了java 7 右键单击你的工程p ...

  5. nyoj-310-河南省第四届省赛题目-二分+dinic

    SECRET 时间限制:3000 ms  |  内存限制:65535 KB 难度:6   描述 Dr.Kong is constructing a new machine and wishes to ...

  6. 第5章——使用 Razor(MVC框架视图引擎)

    Razor 是MVC框架视图引擎的名称. 本章提供 Razor 语法的快速教程,以使你能够识别 Razor 表达式. 本章不打算提供 Razor 的完整参考,而将其视为一个语法速成教程.在本书的后续内 ...

  7. 在Eclipse中Attach Source

    在Eclipse中,有时需要查看类.方法和变量的声明和定义的源代码. 但是在F3查看一些在JDK库中声明/定义的类.方法和 变量的源代码时,Eclipse给你打开的却是相应的.class文件(byte ...

  8. 尚硅谷JavaSEday18 String类练习题

    1.重新编写strim方法,去掉字符串两端的空格 package www.shangguigu.java.exer; import org.junit.Test; /* 练习一:重写Strim方法,输 ...

  9. 1.2 C++命名空间(namespace)

    参考:http://www.weixueyuan.net/view/6326.html 总结: C++语言引入命名空间(Namespace)这一概念主要是为了避免命名冲突,其关键字为 namespac ...

  10. hibernate单向关联与双向关联的区别(原)

    今天看着hibernate的关联关系,有点迷糊.这里通过多对一表述一下双向与单向的区别. 多对一: 就是A表中的一个字段是B表的主键.多个A表中的数据可以对应一个B表中的数据,同理,一个B表中的数据可 ...