文章目录
1.查询所有一级分类 1.sun-club-application-controller 控制层 1.SubjectCategoryController.java 定义基础的queryPrimaryCategory,调用领域层
2.sun-club-domain 领域层 1.SubjectCategoryDomainService.java 2.SubjectCategoryConverter.java 3.SubjectCategoryDomainServiceImpl.java
3.sun-club-infra 基础设施层 1.service 1.SubjectCategoryService.java 2.SubjectCategoryServiceImpl.java
2.mapper 1.SubjectCategoryDao.java 2.SubjectCategoryDao.xml
4.回到sun-club-application-controller 1.SubjectCategoryDTOConverter.java 2.SubjectCategoryController.java 3.测试 4.问题分析
5.使用动态SQL优化查询 1.sun-club-infra 1.SubjectCategoryDao.xml 2.SubjectCategoryDao.java 3.SubjectCategoryService.java 4.SubjectCategoryServiceImpl.java
2.sun-club-domain 1.SubjectCategoryDomainServiceImpl.java 2.测试
2.根据分类id查二级分类 1.sun-club-application-controller 1.SubjectCategoryController.java
2.修改所有queryPrimaryCategory为queryCategory 3.修改动态sql,使其可以只查询出未逻辑删除的记录
3.分类模块接口开发 1.接口设计 2.sun-club-application-controller 1.SubjectCategoryController.java
3.sun-club-domain 1.SubjectCategoryDomainService.java 2.SubjectCategoryDomainServiceImpl.java
4.sun-club-infra 1.SubjectCategoryService.java 2.SubjectCategoryServiceImpl.java 3.SubjectCategoryDao.java 4.SubjectCategoryDao.xml 5.启动测试
4.删除分类接口开发 1.基本设计 2.sun-club-application-controller 1.SubjectCategoryController.java
3.sun-club-common
4.sun-club-domain 1.SubjectCategoryDomainService.java 2.SubjectCategoryDomainServiceImpl.java 3.测试
5.分类接口总览 1.使用commons-lang3进行开发 1.sun-club-common引入依赖 2.SubjectCategoryController.java使用isBlank进行校验
2.SubjectCategoryDomainServiceImpl.java 插入时设置逻辑删除 3.查询一级分类 1.SubjectCategoryDao.xml,添加逻辑删除字段 2.sun-club-common添加分类的枚举CategoryTypeEnum.java 3.SubjectCategoryDomainServiceImpl.java查询未删除字段 4.SubjectCategoryController.java修改查询一级分类逻辑,使其传入category_type 5.修改接口 6.重启测试
4.查询二级分类
1.查询所有一级分类
1.sun-club-application-controller 控制层
1.SubjectCategoryController.java 定义基础的queryPrimaryCategory,调用领域层
@PostMapping ( "/queryPrimaryCategory" )
public Result < List < SubjectCategoryDTO > > queryPrimaryCategory ( ) {
List < SubjectCategoryBO > subjectCategoryBOList = subjectCategoryDomainService. queryPrimaryCategory ( ) ;
}
2.sun-club-domain 领域层
1.SubjectCategoryDomainService.java
List < SubjectCategoryBO > queryPrimaryCategory ( ) ;
2.SubjectCategoryConverter.java
List < SubjectCategoryBO > convertSubjectCategoryToBo ( List < SubjectCategory > subjectCategoryList) ;
3.SubjectCategoryDomainServiceImpl.java
@Override
public List < SubjectCategoryBO > queryPrimaryCategory ( ) {
List < SubjectCategory > subjectCategoryList = subjectCategoryService. queryPrimaryCategory ( ) ;
List < SubjectCategoryBO > subjectCategoryBOList = SubjectCategoryConverter . INSTANCE . convertSubjectCategoryToBo ( subjectCategoryList) ;
if ( log. isInfoEnabled ( ) ) {
log. info ( "SubjectCategoryDomainServiceImpl queryPrimaryCategory, subjectCategoryBOList:{}" , JSON . toJSONString ( subjectCategoryBOList) ) ;
}
return subjectCategoryBOList;
}
3.sun-club-infra 基础设施层
1.service
1.SubjectCategoryService.java
List < SubjectCategory > queryPrimaryCategory ( ) ;
2.SubjectCategoryServiceImpl.java
@Override
public List < SubjectCategory > queryPrimaryCategory ( ) {
return this . subjectCategoryDao. queryPrimaryCategory ( ) ;
}
2.mapper
1.SubjectCategoryDao.java
List < SubjectCategory > queryPrimaryCategory ( ) ;
2.SubjectCategoryDao.xml
< select id = " queryPrimaryCategory" resultMap = " SubjectCategoryMap" >
select
id, category_name, category_type, image_url, parent_id, created_by, created_time, update_by, update_time
from subject_category
where parent_id = 0
and is_deleted = 0
</ select>
4.回到sun-club-application-controller
1.SubjectCategoryDTOConverter.java
List < SubjectCategoryDTO > convertSubjectCategoryToDto ( List < SubjectCategoryBO > subjectCategoryBOList) ;
2.SubjectCategoryController.java
@PostMapping ( "/queryPrimaryCategory" )
public Result < List < SubjectCategoryDTO > > queryPrimaryCategory ( ) {
List < SubjectCategoryBO > subjectCategoryBOList = subjectCategoryDomainService. queryPrimaryCategory ( ) ;
List < SubjectCategoryDTO > subjectCategoryDTOList = SubjectCategoryDTOConverter . INSTANCE . convertSubjectCategoryToDto ( subjectCategoryBOList) ;
return Result . ok ( subjectCategoryDTOList) ;
}
3.测试
4.问题分析
这样写的查询语句就太死了,所以需要使用动态SQL来优化
5.使用动态SQL优化查询
1.sun-club-infra
1.SubjectCategoryDao.xml
< select id = " queryPrimaryCategory" resultMap = " SubjectCategoryMap" >
select
id, category_name, category_type, image_url, parent_id, created_by, created_time, update_by, update_time
from subject_category
< where>
< if test = " id != null" >
and id = #{id}
</ if>
< if test = " categoryName != null and categoryName != ''" >
and category_name = #{categoryName}
</ if>
< if test = " categoryType != null" >
and category_type = #{categoryType}
</ if>
< if test = " imageUrl != null and imageUrl != ''" >
and image_url = #{imageUrl}
</ if>
< if test = " parentId != null" >
and parent_id = #{parentId}
</ if>
< if test = " createdBy != null and createdBy != ''" >
and created_by = #{createdBy}
</ if>
< if test = " createdTime != null" >
and created_time = #{createdTime}
</ if>
< if test = " updateBy != null and updateBy != ''" >
and update_by = #{updateBy}
</ if>
< if test = " updateTime != null" >
and update_time = #{updateTime}
</ if>
< if test = " isDeleted != null" >
and is_deleted = #{isDeleted}
</ if>
</ where>
</ select>
2.SubjectCategoryDao.java
List < SubjectCategory > queryPrimaryCategory ( @Param ( "subjectCategory" ) SubjectCategory subjectCategory) ;
3.SubjectCategoryService.java
List < SubjectCategory > queryPrimaryCategory ( SubjectCategory subjectCategory) ;
4.SubjectCategoryServiceImpl.java
@Override
public List < SubjectCategory > queryPrimaryCategory ( SubjectCategory subjectCategory) {
return this . subjectCategoryDao. queryPrimaryCategory ( subjectCategory) ;
}
2.sun-club-domain
1.SubjectCategoryDomainServiceImpl.java
2.测试
2.根据分类id查二级分类
1.sun-club-application-controller
1.SubjectCategoryController.java
@PostMapping ( "/queryCategoryByPrimary" )
public Result < List < SubjectCategoryDTO > > queryCategoryByPrimary ( @RequestBody SubjectCategoryDTO subjectCategoryDTO) {
try {
if ( log. isInfoEnabled ( ) ) {
log. info ( "SubjectCategoryController queryCategoryByPrimary SubjectCategoryDTO, subjectCategoryDTO:{}" , JSON . toJSONString ( subjectCategoryDTO) ) ;
}
Preconditions . checkNotNull ( subjectCategoryDTO. getParentId ( ) , "分类id不能为空" ) ;
SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter . INSTANCE . convertDTOToSubjectCategory ( subjectCategoryDTO) ;
List < SubjectCategoryBO > subjectCategoryBOList = subjectCategoryDomainService. queryCategory ( subjectCategoryBO) ;
List < SubjectCategoryDTO > subjectCategoryDTOList = SubjectCategoryDTOConverter . INSTANCE . convertSubjectCategoryToDto ( subjectCategoryBOList) ;
return Result . ok ( subjectCategoryDTOList) ;
} catch ( Exception e) {
log. error ( "SubjectCategoryController queryPrimaryCategory error:{}" , e. getMessage ( ) , e) ;
return Result . fail ( "查询一级分类失败" ) ;
}
}
2.修改所有queryPrimaryCategory为queryCategory
3.修改动态sql,使其可以只查询出未逻辑删除的记录
3.分类模块接口开发
1.接口设计
2.sun-club-application-controller
1.SubjectCategoryController.java
@PostMapping ( "/update" )
public Result < Boolean > update ( @RequestBody SubjectCategoryDTO subjectCategoryDTO) {
try {
if ( log. isInfoEnabled ( ) ) {
log. info ( "SubjectCategoryController update SubjectCategoryDTO, subjectCategoryDTO:{}" , JSON . toJSONString ( subjectCategoryDTO) ) ;
}
Preconditions . checkNotNull ( subjectCategoryDTO. getId ( ) , "分类id不能为空" ) ;
SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter . INSTANCE . convertDTOToSubjectCategory ( subjectCategoryDTO) ;
Boolean result = subjectCategoryDomainService. update ( subjectCategoryBO) ;
return Result . ok ( result) ;
} catch ( Exception e) {
log. error ( "SubjectCategoryController update error:{}" , e. getMessage ( ) , e) ;
return Result . fail ( "更新分类失败" ) ;
}
}
3.sun-club-domain
1.SubjectCategoryDomainService.java
Boolean update ( SubjectCategoryBO subjectCategoryBO) ;
2.SubjectCategoryDomainServiceImpl.java
@Override
public Boolean update ( SubjectCategoryBO subjectCategoryBO) {
if ( log. isInfoEnabled ( ) ) {
log. info ( "SubjectCategoryDomainServiceImpl update SubjectCategoryBO, SubjectCategoryBO:{}" , JSON . toJSONString ( subjectCategoryBO) ) ;
}
SubjectCategory subjectCategory = SubjectCategoryConverter . INSTANCE . convertBoToSubjectCategory ( subjectCategoryBO) ;
int update = subjectCategoryService. update ( subjectCategory) ;
return update > 0 ;
}
4.sun-club-infra
1.SubjectCategoryService.java
int update ( SubjectCategory subjectCategory) ;
2.SubjectCategoryServiceImpl.java
@Override
public int update ( SubjectCategory subjectCategory) {
return this . subjectCategoryDao. update ( subjectCategory) ;
}
3.SubjectCategoryDao.java
int update ( SubjectCategory subjectCategory) ;
4.SubjectCategoryDao.xml
< ! -- 通过主键修改数据-- >
< update id= "update" >
update subject_category
< set>
< if test= "categoryName != null and categoryName != ''" >
category_name = #{ categoryName} ,
< / if >
< if test= "categoryType != null" >
category_type = #{ categoryType} ,
< / if >
< if test= "imageUrl != null and imageUrl != ''" >
image_url = #{ imageUrl} ,
< / if >
< if test= "parentId != null" >
parent_id = #{ parentId} ,
< / if >
< if test= "createdBy != null and createdBy != ''" >
created_by = #{ createdBy} ,
< / if >
< if test= "createdTime != null" >
created_time = #{ createdTime} ,
< / if >
< if test= "updateBy != null and updateBy != ''" >
update_by = #{ updateBy} ,
< / if >
< if test= "updateTime != null" >
update_time = #{ updateTime} ,
< / if >
< if test= "isDeleted != null" >
is_deleted = #{ isDeleted} ,
< / if >
< / set>
where id = #{ id}
< / update>
5.启动测试
4.删除分类接口开发
1.基本设计
2.sun-club-application-controller
1.SubjectCategoryController.java
@PostMapping ( "/delete" )
public Result < Boolean > delete ( @RequestBody SubjectCategoryDTO subjectCategoryDTO) {
try {
if ( log. isInfoEnabled ( ) ) {
log. info ( "SubjectCategoryController delete SubjectCategoryDTO, subjectCategoryDTO:{}" , JSON . toJSONString ( subjectCategoryDTO) ) ;
}
Preconditions . checkNotNull ( subjectCategoryDTO. getId ( ) , "分类id不能为空" ) ;
SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter . INSTANCE . convertDTOToSubjectCategory ( subjectCategoryDTO) ;
Boolean result = subjectCategoryDomainService. delete ( subjectCategoryBO) ;
return Result . ok ( result) ;
} catch ( Exception e) {
log. error ( "SubjectCategoryController delete error:{}" , e. getMessage ( ) , e) ;
return Result . fail ( "删除分类失败" ) ;
}
}
3.sun-club-common
1.IsDeleteFlagEnum.java
package com. sunxiansheng. subject. common. enums ;
import lombok. Getter ;
@Getter
public enum IsDeleteFlagEnum {
DELETED ( 1 , "已删除" ) ,
UN_DELETED ( 0 , "未删除" ) ;
public int code;
public String desc;
IsDeleteFlagEnum ( int code, String desc) {
this . code = code;
this . desc = desc;
}
public static IsDeleteFlagEnum getByCode ( int code) {
for ( IsDeleteFlagEnum value : values ( ) ) {
if ( value. code == code) {
return value;
}
}
return null ;
}
}
4.sun-club-domain
1.SubjectCategoryDomainService.java
Boolean delete ( SubjectCategoryBO subjectCategoryBO) ;
2.SubjectCategoryDomainServiceImpl.java
@Override
public Boolean delete ( SubjectCategoryBO subjectCategoryBO) {
if ( log. isInfoEnabled ( ) ) {
log. info ( "SubjectCategoryDomainServiceImpl delete SubjectCategoryBO, SubjectCategoryBO:{}" , JSON . toJSONString ( subjectCategoryBO) ) ;
}
SubjectCategory subjectCategory = SubjectCategoryConverter . INSTANCE . convertBoToSubjectCategory ( subjectCategoryBO) ;
subjectCategory. setIsDeleted ( 1 ) ;
int update = subjectCategoryService. update ( subjectCategory) ;
return update > 0 ;
}
3.测试
5.分类接口总览
1.使用commons-lang3进行开发
1.sun-club-common引入依赖
< dependency>
< groupId> org.apache.commons</ groupId>
< artifactId> commons-lang3</ artifactId>
< version> 3.11</ version>
</ dependency>
2.SubjectCategoryController.java使用isBlank进行校验
2.SubjectCategoryDomainServiceImpl.java 插入时设置逻辑删除
3.查询一级分类
1.SubjectCategoryDao.xml,添加逻辑删除字段
2.sun-club-common添加分类的枚举CategoryTypeEnum.java
package com. sunxiansheng. subject. common. enums ;
import lombok. Getter ;
@Getter
public enum CategoryTypeEnum {
PRIMARY ( 1 , "岗位大类" ) ,
SECOND ( 2 , "二级分类" ) ;
public int code;
public String desc;
CategoryTypeEnum ( int code, String desc) {
this . code = code;
this . desc = desc;
}
public static CategoryTypeEnum getByCode ( int code) {
for ( CategoryTypeEnum value : values ( ) ) {
if ( value. code == code) {
return value;
}
}
return null ;
}
}
3.SubjectCategoryDomainServiceImpl.java查询未删除字段
4.SubjectCategoryController.java修改查询一级分类逻辑,使其传入category_type
5.修改接口
6.重启测试
4.查询二级分类
1.修改接口