一、快速入门
1. 快速开始
var sql = """
select * from test_data
"""
return db.select(sql)
2. 请求参数获取_get
println("----------1、RequestParam----------");
printf("name = %s,age = %s ", name, age);
println("http://localhost:9999/quick_get_start/request_parameter_acquisition?name=abc&age=49");
println("----------2、RequestHeader----------");
printf("header.token = %s", header.token) println("")
printf("header = %s", header) println("")
println("----------5、Cookie----------");
printf("cookie.JSESSIONID = %s", cookie.JSESSIONID) println("")
printf("cookie = %s", cookie) println("")
println("----------6、Session----------");
printf("session.sid = %s", session.sid) println("")
printf("session = %s", session) println("")
return "请求参数获取_get";
3. 请求参数获取_post
println("----------1、RequestParam----------");
printf("name = %s,age = %s ", name, age) println("")
println("----------3、RequestBody----------");
printf("name2 = %s,age2 = %s ", body.name2, body.age2) println("")
printf("body = %s", body) println("")
print("http://localhost:9999/quick_get_start/request_parameter_acquisition?name=abc&age=49");
print("""
{
"name2": "def",
"age2": 29,
"data": 456
}
""");
return "请求参数获取_post";
4. 请求参数获取_path
println("----------4、Path----------");
printf("name = %s,age = %s ", path.name, path.age);
println("http://localhost:9999/quick_get_start/request_parameter_acquisition/qwe/19");
printf("path = %s", path) println("");
return "请求参数获取_path";
5. 增删改查
println("----------1、#{} 注入参数----------")
var id = 1;
var result = db.select("""
select * from test_data where id = #{id}
""");
printf("result = %s", result) println("")
println("----------2、${} 拼接参数----------")
var id = 2;
var result2 = db.select("""
select * from test_data where id = ${id}
""");
printf("result2 = %s", result2) println("")
println("----------3、动态SQL参数----------")
var id = 1;
var result3 = db.select("select * from test_data ?{id,where id = #{id}}");
printf("result3 = %s", result3) println("")
var id = "2";
var result4 = db.select("select * from test_data ?{id!=null&&id.length() > 3,where id = #{id}}");
printf("result4 = %s", result4) println("")
println("----------4、切换数据源----------")
var id = 1;
var result5 = db.slave.select("""
select * from user where id = ${id}
""")
printf("result5 = %s", result5) println("")
println("----------5、SQL缓存----------")
var id = 3;
var result6 = db.cache("user_cache", 3600 * 1000).select("""
select * from user where id = ${id}
""")
printf("result6 = %s", result6) println("")
println("----------6、自动事务----------")
println("----------7、手动事务----------")
println("----------8、Mybatis语法支持----------")
var id = 2;
var sql = """
select * from test_data
where 1 = 1
and id = #{id}
"""
var result8 = db.select(sql)
printf("result8 = %s", result8) println("")
var sql = """
select * from test_data
where 1 = 1
and id = 0
and id = #{id}
"""
var result9 = db.select(sql)
printf("result9 = %s", result9) println("")
var sql = """
select * from test_data
where
id = 0
id = #{id}
"""
var result10 = db.select(sql)
printf("result10 = %s", result10) println("")
var sql = """
select * from test_data
and id = #{id}
"""
var result11 = db.select(sql)
printf("result11 = %s", result11) println("")
return "增删改查";
6. 单表crud接口
println("----------1、select----------")
var id = 1;
var result = db.table('test_data').column("id").select();
printf("result = %s", result) println("")
println("----------2、page----------")
var result2 = db.table('test_data').page();
printf("result2 = %s", result2) println("")
return "单表crud接口";
7. 分页
println("----------1、自动分页----------")
var page = 1
var size = 2
var result = db.page("""
select * from test_data
""")
printf("result = %s", result) println("")
println("----------2、手动分页----------")
var result2 = db.page("""
select * from test_data
""", 10, 20)
printf("result2 = %s", result2) println("")
return '分页'
二、基础教程
1. 脚本语法
println("----------三元运算符----------")
printf("true = %s", true ? "yes" : "no") println("")
printf("false = %s", false ? "yes" : "no") println("")
printf("null = %s", null ? "yes" : "no") println("")
printf("[] = %s", [] ? "yes" : "no") println("")
printf("{} = %s", {} ? "yes" : "no") println("")
printf("0 = %s", 0 ? "yes" : "no") println("")
printf("'' = %s", '' ? "yes" : "no") println("")
println("----------类型转换----------")
var a = "123"::int;
printf("a instanceof Integer = %s", a instanceof Integer) println("")
var b = "abc"::int(111);
printf("b instanceof Integer = %s", b instanceof Integer) println("")
var c = "2020-01-01"::date('yyyy-MM-dd');
printf("c instanceof Date = %s", c instanceof Date) println("")
println("----------可选链操作符----------")
var a = null;
var b = a?.name;
printf("b = %s", b) println("")
var c = a?.getName();
printf("c = %s", c) println("")
println("----------扩展运算符----------")
var sum = (a, b, c) => a + b + c;
System.out.println(sum(...[1, 2, 3, 4]))
var arr = [3, 4, 5];
System.out.println([1, 2, ...arr, 6, 7])
var arr = [3, 4, 5];
System.out.println({
key1: 1,
...arr
})
println("----------for循环----------")
import 'java.lang.System' as System;
var list = [1, 2, 3];
for (index, item in list) {
System.out.println(index + ":" + item);
}
var sum = 0;
for (value in range(0, 100)) {
sum = sum + value;
}
System.out.printf("sum = %s", sum).println();
println("----------while循环----------")
var count = 100;
var sum = 0;
while (count) {
sum = sum + count;
count = count - 1;
}
System.out.printf("sum = %s", sum).println();
println("----------循环map----------")
import 'java.lang.System' as System;
var map = {
key1: 123,
key2: 456
};
for (key, value in map) {
System.out.println(key + ":" + value);
}
println("----------Import导入----------")
import 'java.lang.System' as System;
import 'javax.sql.DataSource' as ds;
import 'org.apache.commons.lang3.StringUtils' as string;
import 'java.text.*'
System.out.println('调用System打印');
System.out.println(ds);
System.out.println(string.isBlank(''));
System.out.println(new SimpleDateFormat('yyyy-MM-dd').format(new Date()));
import 'java.util.Date' as Date;
var date = new Date();
System.out.printf("date = %s", date).println();
import log;
log.info('Hello {}', 'Magic API!')
log.debug('Hello {}', 'Magic API!')
log.error('Hello {}', 'Magic API!')
println("----------异步调用----------")
var val = async db.select('select * from test_data');
var result = val.get();
System.out.printf("result = %s", result).println();
var list = [];
for (index in range(1, 10)) {
list.add(async (index) => db.selectInt('select #{index}'));
}
var result = list.map(item => item.get());
System.out.printf("result = %s", result).println();
if (name) {
exit 400, '参数填写有误'
}
if (age) {
exit 500, '参数填写有误', 9
}
println("----------类型转换----------")
var a = "1";
var result = {
v1: a::int,
v2: a::int(0),
v3: "2020-01-01"::date('yyyy-MM-dd')
}
System.out.printf("result = %s", result).println();
println("----------嵌入其它脚本语言----------")
var name = "hello";
var test =```javascript
name + ' ~ world'
```;
var result = test();
System.out.printf("result = %s", result).println();
return "脚本语法"
2. 参数校验
var count = db.selectInt("""
select count(*) from test_data where name = #{name}
""")
System.out.printf("count = %s", count).println();
assert count == 0: 400, '名字已存在';
if (count != 0) {
exit 400, '名字已存在'
}
3. 脚本调用 Java
println("----------注入Spring Bean----------")
import 'org.springframework.core.env.Environment' as c_env;
var env = c_env.getProperty('magic-api.web');
System.out.printf("env = %s", env).println();
import com.qs.springbootmagicapi.controller.ApiController;
System.out.printf("ApiController = %s", new ApiController().get()).println();
import "apiController" as java_api;
var result = java_api.get();
System.out.printf("result = %s", result).println();
println("----------调用静态方法----------")
import com.qs.springbootmagicapi.utils.ApiUtil;
var result = ApiUtil.VALUE;
System.out.printf("result = %s", result).println();
var result2 = ApiUtil.isEmpty("wy");
System.out.printf("result2 = %s", result2).println();
println("----------调用普通方法----------")
var list = new ArrayList();
System.out.printf("list = %s", list).println();
import "java.text.SimpleDateFormat";
var dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
System.out.printf("dateStr = %s", dateStr).println();
println("----------调用magic-api的接口----------")
import '@get:/demo/hello' as m_hello;
var message = 'World'
var hello = m_hello();
System.out.printf("hello = %s", hello).println();
println("----------调用magic-api的函数----------")
import '@/m/add' as m_add;
var add = m_add(1, 2);
System.out.printf("add = %s", add).println();
import '@/m/addTest' as m_addTest;
var addTest = m_addTest();
System.out.printf("addTest = %s", addTest).println();
4. 异步调用
println("----------普通方法----------")
var user1 = async db.select("select * from test_data where id = 1");
var user2 = async db.select("select * from test_data where id = 2");
var result = [user1.get(), user2.get()];
System.out.printf("result = %s", result).println();
println("----------lambda----------")
var list = [];
var selectData = (id) => db.select("select * from test_data where id = #{id}");
for (index in range(1, 10)) {
list.add(async selectData(index));
}
for (index in range(1, 10)) {
list.add(async (index) => db.select("select * from test_data where id = #{index}"));
}
var result = list.map(item => item.get());
System.out.printf("result = %s", result).println();
return result;
return '异步调用'
5. Lambda
println("----------映射(map)----------")
var list = [{
sex: 0,
name: '小明',
age: 19
},
{
sex: 1,
name: '小花',
age: 18
}
];
var getAge = (age) => age > 18 ? '成人' : '未成年'
var result = list.map(item => {
age: getAge(item.age),
sex: item.sex == 0 ? '男' : '女',
name: item.name
});
System.out.printf("result = %s", result).println();
var result = select t.name,
t.age > 18 ? '成人' : '未成年' age,
t.sex == 0 ? '男' : '女' sex
from list t;
System.out.printf("result = %s", result).println();
println("----------过滤(filter)----------")
var list = [{
sex: 0,
name: '小明'
},
{
sex: 1,
name: '小花'
}
]
var result = list.filter(item => item.sex == 0);
System.out.printf("result = %s", result).println();
var result = select * from list t where t.sex = 0
System.out.printf("result = %s", result).println();
println("----------过滤+映射(filter + map)----------")
var list = [{
sex: 0,
name: '小明'
},
{
sex: 1,
name: '小花'
}
]
var result = list.filter(item => item.sex == 0).map(item => {
sex: item.sex == 0 ? '男' : '女',
name: item.name
});
System.out.printf("result = %s", result).println();
var result = select t.sex == 0 ? '男' : '女' sex,
t.name from list t where t.sex = 0
System.out.printf("result = %s", result).println();
println("----------分组(group)----------")
var result = [{
xxx: 1,
yyy: 2,
value: 11
},
{
xxx: 1,
yyy: 2,
value: 22
},
{
xxx: 2,
yyy: 2,
value: 33
}
];
var res = result.group(item => item.xxx + '_' + item.yyy)
System.out.printf("result = %s", res).println();
var res = result.group(item => item.xxx + '_' + item.yyy, list => {
count: list.size(),
sum: list.map(v => v.value).sum(),
avg: list.map(v => v.value).avg()
})
System.out.printf("result = %s", res).println();
println("----------关联(join)----------")
var year2019 = [{
"pt": 2019,
"item_code": "code_1",
"sum_price": 2234
},
{
"pt": 2019,
"item_code": "code_2",
"sum_price": 234
},
{
"pt": 2019,
"item_code": "code_3",
"sum_price": 12340
},
{
"pt": 2019,
"item_code": "code_4",
"sum_price": 2344
}
];
var year2018 = [{
"pt": 2018,
"item_code": "code_1",
"sum_price": 1234.0
},
{
"pt": 2018,
"item_code": "code_4",
"sum_price": 1234.0
}
];
var result = year2019.join(year2018, (left, right) => left.item_code == right.item_code, (left, right) => {
'年份': left.pt,
'编号': left.item_code,
'今年': left.sum_price,
'去年': right == null ? 'unknow' : right.sum_price,
'环比去年增长': right == null ? '-' : (((left.sum_price - right.sum_price) / right.sum_price * 100) + "%")
});
System.out.printf("result = %s", result).println();
var result = select
t.pt 年份,
t.item_code 编号,
t.sum_price 今年,
ifnull(t1.sum_price, 'unknow') 去年,
t1.sum_price == null ? '-' : ((t.sum_price - t1.sum_price) / t1.sum_price).asPercent(2) 环比去年增长
from year2019 t
left join year2018 t1 on t.item_code = t1.item_code
System.out.printf("result = %s", result).println();
return 'Lambda'
6. Linq
var result =
select t.name, t.*
from [{
'name': 'Gitee'
}, {
'name': 'Github'
}] t;
System.out.printf("result = %s", result).println();
return 'Linq'