Basic usage of Beetl template [variables, cycles, conditions] - Beetl video course

Keywords: Programming Spring MySQL JDBC Database

This video makes a list of the blog's home page;

Content introduction: springboot integrates beetlsql; uses for loop, if control statement, virtual property, variable definition, etc

Learn beetl directory together: https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598

Author: GK

Integrate BeetlSql to query database

Introducing dependency

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

Add database configuration in 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

Add data source configuration class

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;
        }
}

Modify the template file to the end of html

The configuration template file ends in html. Add configuration in application.properties

beetl.suffix=html

variable

Temporary variables: defined by var; similar to js syntax; Global variables: variables that can be accessed by the entire template; defined by template.bind("key",object) Shared variables: variables that can be accessed by all templates; through

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

To define. Template variable: equivalent to using a variable to represent a template or code;

<%
var content = {
        var c = "1234";
        print(c);
%>
Other contents of the template:

<% }; %>

Loop statement

For loop, support for in and for(exp,exp,exp)

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

Other loop statements

var i = 0;
while(i<5){
        print(i);
        i++;
}
//--------Else for usage, if for does not loop----------

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

}elsefor{
        print("No record");
}

Conditional statement

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, a more powerful 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");
}

Project git address: https://gitee.com/gavink/beetl-blog

Video address: it will be clearer and speak slowly after downloading. It is recommended to play the video at 1.2x speed

Links: https://pan.baidu.com/s/1LyxAxlKpVXgVjwSXIbzBuA Extraction code: 68im

Catalog: https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598

Posted by aniket_dj on Mon, 09 Dec 2019 17:27:36 -0800