一、 建立数据库
二、 建立Dict实体类-->Mapper->DictService->DictServiceImpl
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
//字典表对应的实体
@Data
@TableName("S_DICT")
public class Dict implements Serializable {
private static final long serialVersionUID = 4856481237441669262L;
@TableId(type = IdType.AUTO)
private Long id;
private String type;
private Integer code;
private String label;
private String description;
public static long getSerialVersionUID() {
return serialVersionUID;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
三、 WebConfig里导入组件
@Bean
@ConditionalOnMissingBean
public CustomDialect customDialect(){
return new CustomDialect("Dict Dialect");
}
四、 在util包里建两个类
- CustomDialect.java
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.thymeleaf.context.IExpressionContext;
import org.thymeleaf.dialect.AbstractDialect;
import org.thymeleaf.dialect.IExpressionObjectDialect;
import org.thymeleaf.expression.IExpressionObjectFactory;
import java.util.Collections;
import java.util.Set;
@ConditionalOnMissingBean
public class CustomDialect extends AbstractDialect implements IExpressionObjectDialect {
public CustomDialect(String name) {
super(name);
}
@Override
public IExpressionObjectFactory getExpressionObjectFactory() {
return new IExpressionObjectFactory() {
@Override
public Set<String> getAllExpressionObjectNames() {
return Collections.singleton("dic");
}
@Override
public Object buildObject(IExpressionContext iExpressionContext, String s) {
return new DictUtils();
}
@Override
public boolean isCacheable(String s) {
return true;
}
};
}
}
- DictUtils.java
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.Dict;
import com.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.List;
@Component
public class DictUtils {
@Autowired
private DictService service;
private static DictService sysDictService;
@PostConstruct
public void init() {
sysDictService = service;
}
//获取字段值
public String getDictValue(String type, Integer code) {
Dict dict = sysDictService.getOne(new QueryWrapper<Dict>()
.eq("type", type).eq("code", code));
return dict.getLabel();
}
//获取字典code值
public Integer getCode(String type, String label) {
//缓存中数据,则db
Dict dict = sysDictService.getOne(new QueryWrapper<Dict>()
.eq("type", type).eq("label", label));
return dict.getCode();
}
//获取字典集合
}
五、 实现
在html里
[[${#dic.getDictValue('item_cate',2)}]]
Comments | 1 条评论