我尝试使用Thymeleaf作为2-way SQL解析器

突然想到了能否将在Spring的JdbcTemplate(NamedParameterJdbcTemplate)等中指定的SQL定义为2-way SQL~,虽然有很多方法,但这次我尝试使用Thymeleaf(模板引擎)来将SQL转化为2-way SQL。

项目开发

验证版本

    • Spring Boot 2.1.0.RELEASE

 

    Thymeleaf 3.0.11.RELEASE

创建项目

使用SPRING INITIALIZR,在依赖项中选择「JDBC」「H2」和「Thymeleaf」,创建一个Maven项目。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>th-demo</name>
  <description>Demo project for Spring Boot</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

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

    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>


</project>

注意:

使用Spring Boot并非必须,但由于可以简单地使用Thymeleaf创建Spring应用程序,因此我们选择了使用Spring Boot。

准备SQL文件

2-way SQL是以SQL文件的形式记录。

SELECT
   id ,name
FROM
  users
WHERE
  1 = 1
  /*[# th:if="${name} != null"]*/
    AND name = /*[(|:name|)]*/ 'Kazuki'
  /*[/]*/
ORDER BY
  id
SELECT
   id ,name
FROM
  users
WHERE
  1 = 1

    AND name = :name

ORDER BY
  id
SELECT
   id ,name
FROM
  users
WHERE
  1 = 1

ORDER BY
  id

Thymeleaf的配置

很遺憾,Thymeleaf的模板模式中沒有提供SQL的模式,但是有與JavaScript和CSS相同的註釋格式的模式。使用這些模式可以實現「2-way JavaScript」和「2-way CSS」的功能,所以我們選擇了使用JavaScript的模板模式。此外,Spring Boot的默認設定中,模板文件的後綴(擴展名部分)是設定為HTML(.html),我們將其更改為SQL文件的設定(.sql)。

spring.thymeleaf.mode=JAVASCRIPT
spring.thymeleaf.suffix=.sql

设置数据库

为了验证目的,设置数据库(创建表格+添加数据)。

创建表格

DROP TABLE IF EXISTS users;
CREATE TABLE users
(
  id   int,
  name varchar(128)
);

数据注册

INSERT INTO users VALUES(1, 'Kazuki');
INSERT INTO users VALUES(2, 'Shimizu');

验证行动

创建一个JUnit测试用例,验证2-way SQL的运行情况。

package com.example.demo;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.ITemplateEngine;
import org.thymeleaf.context.Context;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ThDemoApplicationTests {

  @Autowired
  NamedParameterJdbcOperations jdbcOperations;

  @Autowired
  ITemplateEngine templateEngine;

  @Test
  public void specifyName() {
    Map<String, Object> params = new HashMap<>();
    params.put("name", "Shimizu");
    Context ctx = new Context(Locale.getDefault(), params);

    String sql = templateEngine.process("users/findByName", ctx);
    System.out.println(sql);

    List<Map<String, Object>> records = jdbcOperations.queryForList(sql, params);
    Assertions.assertThat(records).hasSize(1);
    Assertions.assertThat(records.get(0)).containsEntry("ID", 2);
    Assertions.assertThat(records.get(0)).containsEntry("NAME", "Shimizu");
  }

  @Test
  public void nameNotSpecify() {
    Map<String, Object> params = new HashMap<>();
    Context ctx = new Context(Locale.getDefault(), params);

    String sql = templateEngine.process("users/findByName", ctx);
    System.out.println(sql);

    List<Map<String, Object>> records = jdbcOperations.queryForList(sql, params);
    Assertions.assertThat(records).hasSize(2);
    Assertions.assertThat(records.get(0)).containsEntry("ID", 1);
    Assertions.assertThat(records.get(0)).containsEntry("NAME", "Kazuki");
    Assertions.assertThat(records.get(1)).containsEntry("ID", 2);
    Assertions.assertThat(records.get(1)).containsEntry("NAME", "Shimizu");
  }

}

概述

暫時來說,我們已經確認能夠執行一些類似的操作,但是Thymeleaf並不支援SQL(也就是說,我們使用了JavaScript模板模式作為替代),所以我有一些微妙的感覺。(或許我們可以建議Thymeleaf增加對「SQL」的支援呢!)

请用中文将下列内容改写成本土语言,只需要一个选项:

我使用Thymeleaf创建了一个Issue。

bannerAds