定义实体类
在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;
@TableField("item_values") public String itemValues; }
|
定义Mapper
在learning-online-system-service
工程下的com.swx.system.mapper
包下创建DictionaryMapper
DictionaryMapperpublic 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
DictionaryServicepublic interface DictionaryService extends IService<Dictionary> {
Dictionary getByCode(String code); }
|
实现该接口,在impl
下创建DictionaryServiceImpl
impl.DictionaryServiceImpl@Service public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {
@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("*"); 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); } }
|
系统字典服务已经提供,此时可以打开前端项目验证代码是否正确。