02.Beetl模板的基础用法 【变量、循环、条件】

本期视频做了一个博客的首页列表;

内容简介:springboot 集成 beetlsql;使用for循环,使用if控制语句,使用虚拟属性,定义变量等等

一起学beetl目录:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598

作者:GK


集成BeetlSql,用来查询数据库


引入依赖

1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-jdbc</artifactId> 4 </dependency> 5 6 <dependency> 7 <groupId>com.zaxxer</groupId> 8 <artifactId>HikariCP</artifactId> 9 </dependency> 10 <dependency> 11 <groupId>mysql</groupId> 12 <artifactId>mysql-connector-java</artifactId> 13 <version>6.0.5</version> 14 </dependency>

application.properties中增加数据库配置

1spring.datasource.url=jdbc:mysql://mysql:3306/blog?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false&useInformationSchema=true 2spring.datasource.username=root 3spring.datasource.password=8975789757 4spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

增加数据源配置类

1package com.ibeetl.blog.config; 2 3import com.zaxxer.hikari.HikariDataSource; 4import org.springframework.context.annotation.Bean; 5import org.springframework.context.annotation.Configuration; 6import org.springframework.core.env.Environment; 7 8import javax.sql.DataSource; 9 10/** 11 * @author GavinKing 12 * @ClassName: DBConfig 13 * @Description: 14 * @date 2018/11/18 15 */ 16@Configuration 17public class DBConfig { 18 19 @Bean(name = "datasource") 20 public DataSource datasource(Environment env) { 21 HikariDataSource ds = new HikariDataSource(); 22 ds.setJdbcUrl(env.getProperty("spring.datasource.url")); 23 ds.setUsername(env.getProperty("spring.datasource.username")); 24 ds.setPassword(env.getProperty("spring.datasource.password")); 25 ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name")); 26 return ds; 27 } 28} 29

修改模板文件为html结尾


配置模板文件为html 结尾,在application.properties中增加配置

beetl.suffix=html

变量


临时变量:使用var 定义;类似js语法; 全局变量:整个模板都能访问的变量;通过 template.bind("key",object)去定义 共享变量:所有的模板都能访问的变量;通过

1GroupTemplate gt = new GroupTemplate(resourceLoader, cfg); 2Map<String,Object> shared = new HashMap<String,Object>(); 3shared.put("name", "beetl"); 4gt.setSharedVars(shared);

去定义。 模板变量:相当于用一个变量表示一段模板或者代码;

1<% 2var content = { 3 var c = "1234"; 4 print(c); 5%> 6模板其他内容: 7 8<% }; %>

循环语句


for循环,支持 for-in和for(exp,exp,exp)

1for(blog in page.list){ 2 .... 3} 4for(var i=0;i<page.list.~size){ 5 page.list[i].... 6} 7

其他循环语句

1var i = 0; 2while(i<5){ 3 print(i); 4 i++; 5} 6//--------elsefor用法,如果for没有进行循环时执行---------- 7 8var list = []; 9for(item in list){ 10 11}elsefor{ 12 print("未有记录"); 13}

条件语句

1var a =true; 2var b = 1; 3if(a&&b==1){ 4 5}else if(a){ 6 7}else{ 8 9} 10 11//---------switch------- 12var b = 1; 13switch(b){ 14 case 0: 15 print("it's 0"); 16 break; 17 case 1: 18 print("it's 1"); 19 break; 20 default: 21 print("error"); 22} 23//---------select,更加强大的switch------- 24 25var b = 1; 26select(b){ 27 case 0,1: 28 print("it's small int"); 29 case 2,3: 30 print("it's big int"); 31 default: 32 print("error"); 33} 34 35//----------- 36 37var b = 1; 38select{ 39 case b<1,b>10: 40 print("it's out of range"); 41 break; 42 case b==1: 43 print("it's 1"); 44 break; 45 default: 46 print("error"); 47}

时间格式化

${date,"yyyy-MM-dd"}

这样就能格式化时间了,嗯,超简单

项目git地址:https://gitee.com/gavink/beetl-blog

视频地址:下载下来会更清晰,说话比较慢,建议 1.2x倍速播放

百度网盘下载: https://pan.baidu.com/s/1LyxAxlKpVXgVjwSXIbzBuA 提取码: 68im

bilibili (可以调节清晰度): https://www.bilibili.com/video/av36278644/?p=2

博客目录:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )