⾃定义注解+Aop+Redis+SpringBoot应⽤于数据字典
我们在项⽬的开发中,⼀般都会⽤到数据字典,但这有⼀点⽐较⿇烦的是,数据库存的是数字,但在页⾯上进⾏展⽰的是汉字,通常的做法是把数字返给前端,然后前端再把数字进⾏转义;还有就是⽤SQL进⾏联表查询,把汉字查出来⼀起返给前端。其实还有更好的解决⽅案,那就是⽤⾃定义注解+Aop
先来看表设计:
t_annotation_data_dict表
t_annotation_data_item表
t_annotation_student表
现在要做的效果就是不⽤联表查询,将t_annotation_student表⾥⽤数字存放的字段相对应的汉字⼀起返给前端
相关的pom依赖:
古诗送元二使安西<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
相关准备⼯作代码:
rver:
rvlet:
context-path: /annotation
port: 8080
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: sql.jdbc.Driver
url: jdbc:mysql://localhost:3306/demoSite1?uUnicode=true&characterEncoding=utf8 urname: root
password: root
吃螃蟹可以喝酒吗花开花落花满天redis:
databa: 10
host: 192.168.255.101
port: 6379
password: 123456颠簸怎么读
jedis:
pool:
max-active: 100
max-idle: 3
max-wait: -1
min-idle: 0
timeout: 10000
mybatis:
type-alias-package: com.ue.mapper
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-ca: true
logging:
level:
com.ue.mapper: debug
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
实体类代码
package ity;
public class DataItem {
private Integer id;
private String datasource;
private String code;
private String val;
public DataItem(Integer id, String datasource, String code, String val) { this.id = id;
this.datasource = datasource;
this.val = val;
}
public DataItem() {
super();
}
public Integer getId() {
return id;
}
public void tId(Integer id) {
this.id = id;
}
public String getDatasource() {
return datasource;
}
public void tDatasource(String datasource) {
this.datasource = datasource;
}
public String getCode() {
return code;
}
public void tCode(String code) {
}
public String getVal() {
return val;
}
public void tVal(String val) {
this.val = val;
}
}
package ity;
import com.ue.annotation.Dict;
public class Student {
private Integer id;
private String name;
private String name;
@Dict(dictDataSource = "stu_level")
private Integer stuLevel;
@Dict(dictDataSource = "stu_english",dictText = "stuEnglishDictText")
历史思维导图private Integer englishLevel;
@Dict(dictDataSource = "stu_hobby")
private String stuHobby;
简短小故事public Student(Integer id, String name, Integer stuLevel, Integer englishLevel, String stuHobby) { this.id = id;
this.name = name;
this.stuLevel = stuLevel;
this.stuHobby = stuHobby;
}
public Student() {
super();
}
public Integer getId() {
return id;
}
public void tId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void tName(String name) {
this.name = name;
}
绣花枕头
public Integer getStuLevel() {
return stuLevel;
}
public void tStuLevel(Integer stuLevel) {
this.stuLevel = stuLevel;
}
public Integer getEnglishLevel() {
return englishLevel;
}
public void tEnglishLevel(Integer englishLevel) {
}
public String getStuHobby() {
return stuHobby;
}
public void tStuHobby(String stuHobby) {
this.stuHobby = stuHobby;
}
}
Mapper.java代码
package com.ue.mapper;
import ity.DataItem;
import org.springframework.stereotype.Repository;
@Repository
public interface DataItemMapper {
/**
* 根据datasource与code查询字典的⽅法
* @author LiJun
* @Date 2019/11/28
* @Time 10:40
红绿灯英语* @param dataItem
* @return ity.DataItem
*/
DataItem lectByDatasourceCode(DataItem dataItem); }
package com.ue.mapper;
import ity.Student;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface StudentMapper {
List<Student> listPager(Student student);
}
rvice层代码