02.Beetl模板的基礎用法 【變量、循環、條件】---《Beetl視頻課程》

本期視頻做了一個博客的首頁列表;

內容簡介:springboot 集成 beetlsql;使用for循環,使用if控制語句,使用虛擬屬性,定義變量等等

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

作者:GK


集成BeetlSql,用來查詢數據庫


引入依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.5</version>
        </dependency>

application.properties中增加數據庫配置

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

增加數據源配置類

package com.ibeetl.blog.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;

/**
 * @author GavinKing
 * @ClassName: DBConfig
 * @Description:
 * @date 2018/11/18
 */
@Configuration
public class DBConfig {

        @Bean(name = "datasource")
        public DataSource datasource(Environment env) {
            HikariDataSource ds = new HikariDataSource();
            ds.setJdbcUrl(env.getProperty("spring.datasource.url"));
            ds.setUsername(env.getProperty("spring.datasource.username"));
            ds.setPassword(env.getProperty("spring.datasource.password"));
            ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
            return ds;
        }
}

修改模板文件爲html結尾


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

beetl.suffix=html

變量


臨時變量:使用var 定義;類似js語法;
全局變量:整個模板都能訪問的變量;通過 template.bind("key",object)去定義
共享變量:所有的模板都能訪問的變量;通過

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

去定義。
模板變量:相當於用一個變量表示一段模板或者代碼;

<%
var content = {
        var c = "1234";
        print(c);
%>
模板其他內容:

<% }; %>

循環語句


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

for(blog in page.list){
    ....
}
for(var i=0;i<page.list.~size){
    page.list[i]....
}

其他循環語句

var i = 0;
while(i<5){
        print(i);
        i++;
}
//--------elsefor用法,如果for沒有進行循環時執行----------

var list = [];
for(item in list){

}elsefor{
        print("未有記錄");
}

條件語句

var a =true;
var b = 1;
if(a&&b==1){

}else if(a){

}else{

}

//---------switch-------
var b = 1;
switch(b){
        case 0:
                print("it's 0");
                break;
        case 1:
                print("it's 1");
                break;
        default:
                print("error");
}
//---------select,更加強大的switch-------

var b = 1;
select(b){
        case 0,1:
                print("it's small int");
        case 2,3:
                print("it's big int");
        default:
                print("error");
}

//-----------

var b = 1;
select{
        case b<1,b>10:
                print("it's out of range");
                break;
        case b==1:
                print("it's 1");
                break;
        default:
                print("error");
}

時間格式化

${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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章