定义实体类

learning-online-system-model工程的com.swx.system.model.po包下创建Dictionary

Dictionary
/**
* 字典实体类
*/
@Data
@TableName("dictionary")
public class Dictionary {

private static final long serialVersionUID = 1L;

/**
* 主键
*/
@TableId(value = "id", type = IdType.AUTO)
public Long id;

/**
* 数据字典名称
*/
@TableField("name")
public String name;

/**
* 数据字典代码
*/
@TableField("code")
public String code;

/**
* 数据字典项--json格式
*/
@TableField("item_values")
public String itemValues;
}

定义Mapper

learning-online-system-service工程下的com.swx.system.mapper包下创建DictionaryMapper

DictionaryMapper
public interface DictionaryMapper extends BaseMapper<Dictionary> {

}

learning-online-system-service工程下的com.swx.system.service包下创建DictionaryService

DictionaryMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.swx.system.mapper.DictionaryMapper">

</mapper>

定义Service

learning-online-system-service工程下的com.swx.system.service包下创建DictionaryService

DictionaryService
public interface DictionaryService extends IService<Dictionary> {
/**
* 根据code获取字典
*
* @param code 字典code
* @return 指定字典
*/
Dictionary getByCode(String code);
}

实现该接口,在impl下创建DictionaryServiceImpl

impl.DictionaryServiceImpl
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

/**
* 根据code获取字典
*
* @param code 字典code
* @return 指定字典
*/
@Override
public Dictionary getByCode(String code) {
return getOne(Wrappers.<Dictionary>lambdaQuery().eq(Dictionary::getCode, code));
}
}

定义Controlller

配置跨域

浏览器访问导致跨域,这里暂时在后端使用拦截器做跨域处理

GlobalCorsConfig
@Configuration
public class GlobalCorsConfig {

@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin("*");
// 允许跨域发送cookie
config.setAllowCredentials(true);
// 放行全部原始头信息
config.addAllowedHeader("*");
// 允许所有方法跨域调用
config.addAllowedMethod("*");

UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
corsConfigurationSource.registerCorsConfiguration("/**", config);
return new CorsFilter(corsConfigurationSource);
}
}

定义Controller

DictionaryController
/**
* 系统字典接口类
*/
@Api("系统字典接口类")
@RestController
@ResponseResult
@RequestMapping("/dictionary")
public class DictionaryController {

private final DictionaryService dictionaryService;
public DictionaryController(DictionaryService dictionaryService) {
this.dictionaryService = dictionaryService;
}

@ApiOperation("查询所有字典")
@GetMapping("/all")
public List<Dictionary> queryAll() {
return dictionaryService.list();
}

@GetMapping("/code/{code}")
public Dictionary getByCode(@PathVariable("code") String code) {
return dictionaryService.getByCode(code);
}
}

系统字典服务已经提供,此时可以打开前端项目验证代码是否正确。