三层架构设计:基于Spring Boot开发要使用三层架构:
数据访问层(Dao)、业务逻辑层(Service)、控制层(Control-ler)
(1)数据访问层(Dao):Dao层是最底层的设计,用户操作数据库。通过MyBatis持久化实现接口开发,XML文件。Dao层的设计步骤:1、在数据库中生成数据库表 2通过XML文件实现数据库的映射和操作。
(2)业务逻辑层(Service):Service层是中间层,在Dao层和Controller层之间进行逻辑处理工作。考虑到程序 的耦合度问题,为了降低各个模块的关联,每个接口都封装有各自的业务处理方法。
(3)控制层(Controller):Controller是第三层,通过调用Service层,与前端页面交互。
CRUD案例如下:
Controller层:
@Slf4j
@RestController
@RequestMapping("/xxxList")
public class XxxListController {
@Autowired
private XxxListService xxxListService;
/**
* 查询:配置
* 权限-管理员
*
* @param param
* @return
*/
@PostMapping(value = "/selectXxxConfig")
@AuthInterceptor("mg:getXxx:selectXxxConfig")
public Result selectXxxConfig(@RequestBody String param) {
try {
Map<String, Object> paramMap = JsonUtils.readValue(param, Map.class);
return Result.success().result(xxxListService.selectXxxConfig(paramMap));
} catch (Exception e) {
log.error("XxxListController selectXxxConfig is error===:" + e.getMessage(), e);
return Result.failure("查询失败");
}
}
/**
* 新增/修改:配置
* 权限-管理员
*
* @param param
* @return
*/
@PostMapping(value = "/addOrModifyXxxConfig")
@AuthInterceptor("mg:getXxx:addOrModifyXxxConfig")
public Result addOrModifyXxxConfig(@RequestBody String param) {
try {
Map<String, Object> paramMap = JsonUtils.readValue(param, Map.class);
xxxListService.addOrModifyXxxConfig(paramMap);
} catch (Exception e) {
log.error("XxxListController addOrModifyXxxConfig is error===:" + e.getMessage(), e);
return Result.failure("更新失败");
}
return Result.success().result(null);
}
/**
* 删除:表单数据
* 权限-管理员
*
* @param param
* @return
*/
@PostMapping(value = "/deleteData")
@AuthInterceptor("mg:getXxx:deleteData")
public Result deleteFormData(@RequestBody String param) {
try {
Map<String, Object> paramMap = JsonUtils.readValue(param, Map.class);
xxxListService.deleteData(paramMap);
} catch (Exception e) {
log.error("XxxListController deleteData is error===:" + e.getMessage(), e);
return Result.failure("数据删除失败");
}
return Result.success().result(null);
}
}
Servicer层:
public interface XxxListService {
//查询:配置
PageInfo<Map<String, Object>> selectXxxConfig(Map<String, Object> paramMap);
//新增/修改:配置
Result addOrModifyXxxConfig(Map<String, Object> paramMap);
//删除:表单数据
void deleteData(Map<String, Object> paramMap);
}
ServicerImpl层:
@Slf4j
@Service
public class XxxListServiceImpl implements XxxListService {
@Autowired
private XxxListMapper xxxListMapper;
/*
* 查询:配置
*
*/
@Override
public PageInfo<Map<String, Object>> selectXxxConfig(Map<String, Object> paramMap) {;
CommonUtil.getPageHelper(paramMap);
List<Map<String, Object>> list = xxxListMapper.selectXxxConfig(paramMap);
PageInfo<Map<String, Object>> result = new PageInfo<>(list);
return result;
}
/*
* 新增/修改:配置
*
*/
@Override
public Result addOrModifyXxxConfig(Map<String, Object> paramMap) {
String addOrModify = String.valueOf(paramMap.get("addOrModify"));
if(addOrModify.equals("add")){
paramMap.remove("addOrModify");
List<Map<String,String>> reg = getRegionList(); // 获取地域信息
List<Map<String,String>> list = xxxListMapper.selectXxxRegAll(); // 查询本数据库地域信息
Iterator<Map<String, String>> iterator = reg.iterator();
while (iterator.hasNext()){
Map<String, String> next = iterator.next();
String name = String.valueOf(next.get("name"));
String code = String.valueOf(next.get("code"));
next.put("isEnable","1");
Iterator<Map<String, String>> iterator2 = list.iterator();
while (iterator2.hasNext()){
Map<String, String> next2 = iterator2.next();
String regionName = String.valueOf(next2.get("name"));
String RegionCode = String.valueOf(next2.get("code"));
if(name.equals(regionName) && code.equals(RegionCode)){
iterator.remove();
}
}
}
if(reg.size() > 0){
xxxListMapper.addXxxConfig(reg);
}
}
else if(addOrModify.equals("modify")){ // "isEnableVal",1:"使用",2:"停用"
String sortId = String.valueOf(paramMap.get("sortId"));
String isEnable = String.valueOf(paramMap.get("isEnable"));
if(!sortId.equals("null") && !sortId.equals("") || !isEnable.equals("null") && !isEnable.equals("")){
xxxListMapper.modifyXxxConfig(paramMap);
}
}
return Result.success().result(null);
}
/*
* 删除:表单数据
*
*/
@Override
public void deleteData(Map<String, Object> paramMap) {
xxxListMapper.deleteData(paramMap);
}
}
Mapper层(又称Dao层):文章来源:https://www.toymoban.com/news/detail-612904.html
@Mapper
public interface XxxListMapper {
//查询:所有配置(先查所有配置)
List<Map<String, String>> selectXxxRegAll();
//查询:配置
List<Map<String, Object>> selectXxxConfig(Map<String, Object> paramMap);
//新增:配置
void addXxxConfig(List<Map<String, String>> paramList);
//修改:配置
void modifyXxxConfig(Map<String, Object> paramMap);
//删除:表单数据
void deleteData(Map<String, Object> paramMap);
}
Mapper.xml层(MyBatis层):文章来源地址https://www.toymoban.com/news/detail-612904.html
<?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">
<!--命名空间:找到所在的XxxListMapper接口的位置-->
<mapper namespace="com.itheima.service.mapper.XxxListMapper">
<!--sql语句代码-->
<!-- 查询:所有配置(先查所有配置) -->
<select id="selectXxxRegAll" resultType="map">
SELECT *
FROM xxx_region_list_dic where 1 = 1
</select>
<!-- 查询:配置 -->
<select id="selectXxxConfig" parameterType="map" resultType="map">
SELECT *
FROM xxx_region_list_dic where 1 = 1
<!-- 分页字段 -->
<if test="null != processResult">
and processResult = #{processResult}
</if>
<if test="null != name and '' != name">
and name = #{name}
</if>
<if test="null != code and '' != code">
and code = #{code}
</if>
<if test="null != isEnable and '' != isEnable">
and isEnable = #{isEnable}
</if>
order by sortId
</select>
<!-- 新增:配置 -->
<insert id="addXxxConfig" parameterType="java.util.List">
<foreach collection="list" item="region" index="index" separator=";">
INSERT IGNORE INTO xxx_region_list_dic
<foreach collection="region.entrySet()" index="key" separator="," open="(" close=")">
${key}
</foreach>
VALUES
<foreach collection="region.entrySet()" item="value" separator="," open="(" close=")">
#{value}
</foreach>
</foreach>
</insert>
<!-- 修改:配置 -->
<update id="modifyXxxConfig" parameterType="map">
update xxx_region_list_dic
<set>
<if test = "null != sortId and '' != sortId">
sortId = #{sortId},
</if>
<if test = "null != isEnable and '' != isEnable">
isEnable = #{isEnable}
</if>
</set>
where
<if test = "null != seqId and '' != seqId">
seqId = #{seqId}
</if>
</update>
<!-- 删除:表单数据 -->
<delete id="deleteData">
delete from xxx_form_data_list
where
<if test = "null != No and '' != No">
No = #{No}
</if>
</delete>
</mapper>
到了这里,关于<Spring Boot>开发基于三层架构设计:Dao层、Service层、Controller层及案例一的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!