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

介绍thymeleaf-extras-db,一个简化前端select标签数据获取的扩展,使select标签能直接从数据库加载数据,无需额外接口。支持缓存,适用于SpringBoot项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在写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. 标签属性及含义

属性含义是否必填可选值默认值
idid
classclass
namename
stylestyle
order排序方式
allow-empty允许空值true,falsetrue
empty-message空值显示内容&nbsp;
cacheable是否允许缓存true,falsetrue
data-live-searchselect2专有属性true,false
multipleselect2专有属性multiple
data-optionseasyui-combobox专有属性
dict_name(t:dict独有)字典名称,只能填t_dict_type_group的type_group_code字段的值
query(t:select独有)属性规则:表名,显示的字段名[,作为option的value的字段名][,查询条件]

转载于:https://www.cnblogs.com/rainmer/p/10019559.html

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.7</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>takeoutsystem</artifactId> <version>0.0.1-SNAPSHOT</version> <name>takeoutsystem</name> <description>takeoutsystem</description> <properties> <java.version>17</java.version> <!-- MyBatis 版本 --> <mybatis.version>3.5.15</mybatis.version> <mybatis-spring.version>3.0.3</mybatis-spring.version> <!-- 其他依赖版本 --> <jjwt.version>0.11.5</jjwt.version> <springdoc-openapi.version>2.8.5</springdoc-openapi.version> <thymeleaf-extras-springsecurity6.version>3.1.2.RELEASE</thymeleaf-extras-springsecurity6.version> </properties> <dependencyManagement> <dependencies> <!-- 强制所有模块使用此MyBatis版本 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Spring Boot 基础依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 原生 MyBatis Starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> <exclusions> <!-- 排除旧版 MyBatis --> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </exclusion> </exclusions> </dependency> <!-- 显式指定升级后的MyBatis版本 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> <!-- 数据库依赖 --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> </dependency> <!-- 安全与认证依赖 --> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api</artifactId> <version>${jjwt.version}</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>${jjwt.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>${jjwt.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity6</artifactId> <version>${thymeleaf-extras-springsecurity6.version}</version> </dependency> <!-- API文档 --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>${springdoc-openapi.version}</version> </dependency> <!-- 测试依赖 - 修正后 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project> 如何添加对应的mybatisplus依赖,springboot的版本是3.4.7
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值