本篇博客主要聊聊自定义注解在项目中的应用,自定义注解用来做系统监控日志,这个用法已经烂大街了,无法让面试官眼前一亮的感觉。
项目开发中数据字典使用场景非常多,使用数据字典有一点非常的不方便,那就是数据库中存放的是数字,而实际页面上展示的却是汉字;那么这也就造成了,每次做页面展示的时候都需要给数字进行转义,将其转义成汉字内容展示到页面,而转换的过程过于繁琐;
咱们的自定义注解可以完美的解决这一问题;
浏览器输入:http://localhost:8080/student/listPager 测试
最后浏览器访问的效果图
当然,也可以不走默认规则,xxx就是在类属性上的自定义注解的属性值
相关的pom依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
逆向工程生成实体类、mapper.xml、mapper.java
实体类
package com.zhou.model;
public class DataDict {
private Integer id;
private String datasource;
private String description;
public DataDict(Integer id, String datasource, String description) {
this.id = id;
this.datasource = datasource;
this.description = description;
}
public DataDict() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDatasource() {
return datasource;
}
public void setDatasource(String datasource) {
this.datasource = datasource;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
package com.zhou.model;
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.code = code;
this.val = val;
}
public DataItem() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDatasource() {
return datasource;
}
public void setDatasource(String datasource) {
this.datasource = datasource;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
}
package com.zhou.model;
import com.zhou.annotation.Dict;
public class Student {
private Integer id;
private String name;
@Dict(dicDataSource = "stu_Level")
private Integer stulevel;
@Dict(dicDataSource = "stu_English",dicText = "xxx")
private Integer englishlevel;
@Dict(dicDataSource = "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.englishlevel = englishlevel;
this.stuhobby = stuhobby;
}
public Student() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public