Watch 5 Star 11 Fork 2

threefish / spring-boot-sqltplJavaApache-2.0

Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
类似MyBatais方式的SqlXML模板管理渲染插件,不含ORM spread retract

Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

spring-boot-sqltpl

介绍

一个帮你超轻量的管理sql模版插件,你可以自定义一个渲染引擎来使用模版。

  • 使用 xml 对 sql 片段进行管理,类似 mybatis
  • 可以自定义渲染 sql语句的引擎,默认采用 beetl html 引擎渲染
  • 环境要求JDK1.8+ spring-boot 
  • xml中的 exp 标签 是给未来准备开发的idea插件做的准备(非必须的)。

使用说明三部曲

第一步编辑POM

<!--   sql-tpl 引用  (必须)    -->
 <dependency>
     <groupId>com.github.threefish</groupId>
     <artifactId>sql-tpl</artifactId>
     <version>1.0.2.RELEASE</version>
 </dependency>
<!--  如果你用其他的渲染引擎,那么不需要引入 ibeetl   (非必须 )  -->
<dependency>
    <groupId>com.ibeetl</groupId>
    <artifactId>beetl</artifactId>
    <version>3.0.19.RELEASE</version>
</dependency>
<!--  工具栏只是绑定到beetl语法中使用  (非必须 )  -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

 <build>
    <!--   不要忘了把xml编译出来     -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

第二步写注解


package com.github.threefish.sqltpldemo;

import com.github.threefish.spring.sqltpl.SqlTplResourceLoader;
import com.github.threefish.spring.sqltpl.annotation.EnableSqlTpl;
import com.github.threefish.spring.sqltpl.templte.beetl.BeetlSqlTemplteEngineImpl;
import com.github.threefish.sqltpldemo.dao.SqlDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 黄川 huchuc@vip.qq.com
 * date: 2020/3/14
 */
@SpringBootApplication
@EnableSqlTpl
public class SqlTplDemoApplication {

    public static void main(String[] args) {
        //开启热更新
        SqlTplResourceLoader.DEVELOPER_MODE = true;
        SpringApplication.run(SqlTplDemoApplication.class, args);
    }

    @Bean
    public BeetlSqlTemplteEngineImpl sqlTemplteEngine() {
        BeetlSqlTemplteEngineImpl beetlSqlTemplteEngine = new BeetlSqlTemplteEngineImpl();
        beetlSqlTemplteEngine.getGt().registerFunctionPackage("S", StringUtils.class);
        return beetlSqlTemplteEngine;
    }

    @RestController
    class SqlXmlRest {

        @Autowired
        SqlDao sqlDao;

        @GetMapping("/selectOne")
        public String selectOne() {
            return sqlDao.selectOne();
        }

        @GetMapping("/selectTwo")
        public String selectTwo() {
            return sqlDao.selectTwo();
        }
    }

}

第三步写DAO和xml


package com.github.threefish.sqltpldemo.dao;

import com.github.threefish.spring.sqltpl.SqlTplHolder;
import com.github.threefish.spring.sqltpl.annotation.SqlXml;
import com.github.threefish.spring.sqltpl.service.ISqlTpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 黄川 huchuc@vip.qq.com
 * date: 2020/3/11
 */
@Service
@SqlXml
public class SqlDao implements ISqlTpl {

    private SqlTplHolder tplHolder;

    public String selectOne() {
        return tplHolder.getOriginalTemplate("selectOne");
    }


    public String selectTwo() {
        Map<String, Object> stringObjectMap = new HashMap<>(1);
        stringObjectMap.put("name","测试");
        stringObjectMap.put("age",18);
        return tplHolder.getSql("selectTwo", stringObjectMap);
    }


    @Override
    public SqlTplHolder getSqlTplHolder() {
        return tplHolder;
    }

    @Override
    public void setSqlTpl(SqlTplHolder sqlsTplHolder) {
        this.tplHolder = sqlsTplHolder;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Sqls PUBLIC "sql-tpl.dtd" "https://threefish.gitee.io/spring-boot-sqltpl/sql-tpl.dtd">
<!--本xml中的逻辑语法是因为demo中使用的beetl引擎渲染,所以是beetl的语法,如果你使用其他引擎如freemarker 那么请用freemarker语法-->
<Sqls class="com.github.threefish.sqltpldemo.dao.SqlDao">
    <!--var是当前文件的共享变量,var中不能有表达式-->
    <var name="tableName">tableName_1</var>
    <sql id="selectOne" wrap="true">
        SELECT * from ${tableName}
    </sql>
    <!--wrap=true 表示将会清除换行符\n 默认不清除,xml中的 exp 标签 是给未来准备开发的idea插件做的准备(非必须的)-->
    <sql id="selectTwo" wrap="true">
        SELECT * from ${tableName} where
        <exp>if(S.isNotEmpty(name)){</exp>
        name like CONCAT('%', '${name}', '%');
        <exp>}</exp>
        <exp>if(age>0){</exp>
        and age >= ${age}
        <exp>}</exp>
    </sql>
</Sqls>

Comments ( 7 )

Sign in for post a comment

Java
1
https://gitee.com/threefish/spring-boot-sqltpl.git
git@gitee.com:threefish/spring-boot-sqltpl.git
threefish
spring-boot-sqltpl
spring-boot-sqltpl
master

Search