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的字段名
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章