快速开始
About 1391 wordsAbout 5 min
2025-02-06
安装
git clone https://gitee.com/xiandafu/springboot3-beetl-beetlsql-example
mvn clean compile
工程目录
- beetl-sample beetl使用例子
- beetlsql-sample beetlsql 使用例子
1 Beetl模板引擎
进入 beetl-sample 模块
运行
运行 org.example.beetl.BeetlApplication,有如下输出:
Connected to the target VM, address: '127.0.0.1:61322', transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.5)
2025-03-02T10:37:33.219+08:00 INFO 18251 --- [ main] org.example.beetl.BeetlApplication : Starting BeetlApplication using Java 17.0.4.1 with PID 18251 (/Users/lijiazhi/git/springboot3-beetl-beetlsql-example/beetl-sample/target/classes started by lijiazhi in /Users/lijiazhi/git/springboot3-beetl-beetlsql-example)
2025-03-02T10:37:33.225+08:00 INFO 18251 --- [ main] org.example.beetl.BeetlApplication : No active profile set, falling back to 1 default profile: "default"
2025-03-02T10:37:34.192+08:00 INFO 18251 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 18080 (http)
2025-03-02T10:37:34.203+08:00 INFO 18251 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-03-02T10:37:34.203+08:00 INFO 18251 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.15]
2025-03-02T10:37:34.299+08:00 INFO 18251 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-03-02T10:37:34.300+08:00 INFO 18251 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1006 ms
2025-03-02T10:37:34.720+08:00 INFO 18251 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 18080 (http) with context path ''
2025-03-02T10:37:34.730+08:00 INFO 18251 --- [ main] org.example.beetl.BeetlApplication : Started BeetlApplication in 1.947 seconds (process running for 2.994)
使用了Spring Boot3,监听端口为18080.如果需要修改启动端口,需要修改applicaltion.properties
验证
浏览器输入
http://127.0.0.1:18080/
输出
hello beetl
动手修改
修改TestController.indexPage方法,
@GetMapping("/")
public ModelAndView indexPage(){
String name = "beetl";
ModelAndView modelAndView = new ModelAndView("index.html");
modelAndView.addObject("name",name);
return modelAndView;
}
为模板index.html添加一个变量
@GetMapping("/")
public ModelAndView indexPage(){
String name = "beetl";
String friend = "beetlsql";
ModelAndView modelAndView = new ModelAndView("index.html");
modelAndView.addObject("name",name);
modelAndView.addObject("friend",friend);
return modelAndView;
}
修改resources/templates/index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
hello ${name} and ${friend}, today is ${date(),"yyyy-MM-dd"}
</body>
</html>
IDE编译整个工程,浏览器再次访问 http://127.0.0.1:18080/
,输出
hello beetl and beetlsql, today is 2025-03-02
注意,即使单独修改模板,也许编译整个工程,这是因为Beetl默认的模板根目录是classpath的templates,如果不编译整个工程,修改src/resource/tempaltes目录的index.html 无法拷贝到classpath的templates
2 Beetl脚本引擎
运行
同Beetl,进入 beetl-sample 模块,运行 org.example.beetl.BeetlApplication
在浏览器中输入
http://127.0.0.1:18080/script
会调用程序TestController.script
@GetMapping("/script")
@ResponseBody
public Map script(){
String script = "var a = 5;var b = 3+a; return b;";
Map map =BeetlKit.execute(script);
return map;
}
浏览器输出
{"a":5,"b":8,"return":8}
动手修改
允许传入一个变量 c
@GetMapping("/script")
@ResponseBody
public Map script(@RequestParam Integer c){
String script = "var a = 5;var b = 3+a*c; return b;";
Map param = new HashMap();
param.put("c",c);
Map map =BeetlKit.execute(script,param);
return map;
}
在浏览器中输入
http://127.0.0.1:18080/script?c=5
浏览器中输出
{"a":5,"b":28,"return":28}
3 BeetlSQL ORM
进入 beetlsql-sample 模块
运行
运行 org.example.beetlsql.BeetlSQLApplication,有如下输出:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.5)
2025-03-02T14:58:55.887+08:00 INFO 24220 --- [ restartedMain] o.example.beetlsql.BeetlSQLApplication : Starting BeetlSQLApplication using Java 17.0.4.1 with PID 24220 (/Users/lijiazhi/git/springboot3-beetl-beetlsql-example/beetlsql-sample/target/classes started by lijiazhi in /Users/lijiazhi/git/springboot3-beetl-beetlsql-example)
2025-03-02T14:58:55.889+08:00 INFO 24220 --- [ restartedMain] o.example.beetlsql.BeetlSQLApplication : No active profile set, falling back to 1 default profile: "default"
2025-03-02T14:58:55.938+08:00 INFO 24220 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2025-03-02T14:58:55.938+08:00 INFO 24220 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2025-03-02T14:58:56.939+08:00 INFO 24220 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 18080 (http)
2025-03-02T14:58:56.947+08:00 INFO 24220 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-03-02T14:58:56.947+08:00 INFO 24220 --- [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.15]
2025-03-02T14:58:56.994+08:00 INFO 24220 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-03-02T14:58:56.995+08:00 INFO 24220 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1056 ms
2025-03-02T14:58:57.119+08:00 INFO 24220 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2025-03-02T14:58:57.285+08:00 INFO 24220 --- [ restartedMain] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:dbtest user=SA
2025-03-02T14:58:57.287+08:00 INFO 24220 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2025-03-02T14:58:57.656+08:00 INFO 24220 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2025-03-02T14:58:57.678+08:00 INFO 24220 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 18080 (http) with context path ''
2025-03-02T14:58:57.687+08:00 INFO 24220 --- [ restartedMain] o.example.beetlsql.BeetlSQLApplication : Started BeetlSQLApplication in 2.129 seconds (process running for 2.498)
2025-03-02T15:01:03.745+08:00 INFO 24220 --- [io-18080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2025-03-02T15:01:03.746+08:00 INFO 24220 --- [io-18080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2025-03-02T15:01:03.747+08:00 INFO 24220 --- [io-18080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
相比Beetl例子,这里输出多了HikariPool的初始化日志输出,代码在org.example.beetlsql.conf.DataSourceConfig
验证
浏览器输入
http://127.0.0.1:18080/
输出,会从数据库中查询俩条name=lijz
的记录
[{"id":1,"name":"lijz"},{"id":4,"name":"lijz"}]
动手修改
修改org.example.beetlsql.service.BookService,增加一个all方法,查询所有的记录,all方法是BeetlSQL提供的BaseMapper的一个内置方法的
public List<Book> all(){
return bookMapper.all();
}
修改org.example.beetlsql.web.TestController,增加一个all方法
@GetMapping("/all")
public List<Book> all(){
List<Book> list = bookService.all();
return list;
}
IDE编译整个工程,浏览器访问 `http://127.0.0.1:18080/all,输出
[{"id":1,"name":"lijz"},{"id":2,"name":"lucy"},{"id":3,"name":"bear"}]
4 Beetl&BeetlSQL
这是一个综合Beetl和BeetlSQL的例子,Beetl作为模板引擎,BeetlSQL作为ORM工具
进入 beetl-beetlsql-sample 模块
运行
运行 org.example.beetlsql.AllApplication ,输出类似BeetlSQL ORM例子的输出
验证
浏览器输入
http://127.0.0.1:18080/
会调用TestController.indexPage, 输出会渲染输出resources/templates/index.html,输出如下
hello lijz
hello lijz
模板页面内容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!-- for(book in books){ -->
hello ${book.name} <br/>
<!-- } -->
</body>
</html>
模板页面使用了定界符类似HTML的注释符号 <!--
和 -->
,你可以修改resources/beetl.properties,改成其他符号
DELIMITER_PLACEHOLDER_START=${
DELIMITER_PLACEHOLDER_END=}
DELIMITER_STATEMENT_START=<!--
DELIMITER_STATEMENT_END=-->