thymeleaf-extras-db 0.0.1发布,select标签加载数据的新姿势

在写thymeleaf页面的时候,我为了偷懒,不想为每个select下拉列表框都写一个接口,于是这个懒人jar诞生了。该jar的核心功能是直接通过thymeleaf页面的自定义标签的属性,直接运行sql并初始化select数据。

项目地址:
github
gitee

简介

thymeleaf-extras-db是针对thymeleaf的扩展,主要是简化前端select标签数据的获取,让select标签直接从数据库加载数据,而不需要单独写接口,支持缓存

导入

<dependency>
    <groupId>com.github.jeesun.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-db</artifactId>
    <version>0.0.1</version>
</dependency>

使用教程

thymeleaf-extras-db目前支持两种自定义标签t:dict和t:select,两个标签仅一个属性不同,其他属性两者都支持。t:dict和t:select都支持普通select标签属性,也支持select2和easyui-combobox属性。需要注意的是,t:dict标签的数据,是从表t_dict_type和t_dict_type_group查询的,需要建表mysql.sql

在html页面上,需要给html标签添加属性xmlns:t="http://www.w3.org/1999/xhtml"。
使用示例:
<t:dict class="form-control select2" id="add_menu_type" name="menuType" dict-name="menu_type" style="width:100%"></t:dict>
<t:select id="add_menu_group_id" name="pid" order="desc" query="t_side_menu,name,id,pid is null" class="form-control select2" data-live-search="true" style="width:100%"></t:select>

easyui中使用方式:
<t:dict class="easyui-combobox" id="search_authority" name="authority" dict-name="role_type"  style="width:160px" allow-empty="true"></t:dict>

1. 新建配置类

在Spring Boot中,使用thymeleaf-extras-db很简单,先新建一个配置类:

@Configuration
public class CustomDialectConfig {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private CacheManager cacheManager;

    @Bean
    public DbDialect dbDialect(){
        //return new DbDialect(jdbcTemplate);
        return new DbDialect(jdbcTemplate, cacheManager);
    }
}

2. 配置缓存

请在application.yml中添加如下配置:

spring:
  cache:
    cache-names: listOptionCache

如果你使用的是ehcache,那么还需要在ehcache.xml中新增如下类似配置:

<cache name="listOptionCache"
    maxElementsInMemory="0"
    eternal="true"
    overflowToDisk="true"
    diskPersistent="true"
    memoryStoreEvictionPolicy="LRU">
</cache>

3. 标签属性及含义

属性 含义 是否必填 可选值 默认值
id id
class class
name name
style style
order 排序方式
allow-empty 允许空值 true,false true
empty-message 空值显示内容 &nbsp;
cacheable 是否允许缓存 true,false true
data-live-search select2专有属性 true,false
multiple select2专有属性 multiple
data-options easyui-combobox专有属性
dict_name (t:dict独有)字典名称,只能填t_dict_type_group的type_group_code字段的值
query (t:select独有)属性规则:表名,显示的字段名[,作为option的value的字段名][,查询条件]
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,123评论 6 13
  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 32,010评论 2 89
  • 今天我当一日店长,单细胞的简单脑子考虑不了复杂的事情,不会带动气氛。体会到店长真的不容易,要考虑太多的事,店...
    梧桐16阅读 97评论 0 0
  • 给初学者的 Jupyter Notebook 教程 Jupyter Notebook 是一个非常强大的工具,常用于...
    Jancd阅读 26,236评论 2 30
  • 星期一|新闻新知|第06篇 如今的商业环境和几十年前比起来大有不同。在商品供不应求的年代,企业几乎不需要花什么力气...
    为时未晚阅读 1,028评论 0 6