SpringBoot项目与项目之间简单流程

简单总结一下SpringBoot项目与项目之间简单流程,以获取字典为例,不谈论实现代码,只说流程跳转

过程

  • 首先是consume模块(服务消费),在controller层定义
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@RestController
@RequestMapping(value = "/dcDic")
public class DcDicController {

@Autowired
DcOrgService dcOrgService;

// 获取行政区字典
@PostMapping("/getArea")
public ResultMsg<Dictionary> getArea(@RequestBody DcOrgParam dcOrgParam) {

List<DcOrg> list = dcOrgService.getList(dcOrgParam);

// 填充字典
Dictionary dictionary = new Dictionary();
dictionary.fillBy(list);

return ResultMsg.ok(dictionary);

}
}
  • 然后在consume模块的Service层定义(这层主要是将接口暴露给consumecontroller)
1
2
3
4
5
6
@FeignClient(value = "service-groundNet")
public interface DcOrgService {

@PostMapping("/dcOrg/getList")
List<DcOrg>getList(@RequestBody DcOrgParam dcOrgParam);
}
  • provider模块的controller层,需要定义一个和上面一样的URL
1
2
3
4
5
6
7
8
9
10
11
12
@RestController
@RequestMapping(value = {"/dcOrg"})
public class DcOrgController {

@Autowired
DcOrgRepository dcOrgRepository;
// 这儿定义的url要和consume模块的Service层一样
@PostMapping("/getList")
public List<DcOrg> getList(@RequestBody DcOrgParam dcOrgParam) {
return dcOrgRepository.getList(dcOrgParam);
}
}
  • 在仓储层repository中定义方法(类似Service层)
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.nb.xry.common.repository;

//父接口
public interface CrudRepository<E, PK> {

void add(E e) throws Exception;

void remove(PK id) throws Exception;

void edit(E e) throws Exception;

E get(PK id);
}
1
2
3
4
5
// 这里继承的是自定义的一个增删改查接口(子接口自动具有父接口的所有抽象数据和方法)
// 子接口
public interface DcOrgRepository extends CrudRepository<DcOrg,String> {
List<DcOrg> getList(DcOrgParam dcOrgParam);
}
  • 定义仓储层repository的实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Repository
public class DcOrgRepositoryImpl implements DcOrgRepository {

@Autowired
DcOrgMapper dcOrgMapper;

@Override
public void add(DcOrg dcOrg) throws Exception {

}

@Override
public void remove(String id) throws Exception {

}

@Override
public void edit(DcOrg dcOrg) throws Exception {

}

@Override
public DcOrg get(String id) {

}

@Override
public List<DcOrg> getList(DcOrgParam dcOrgParam) {
return dcOrgMapper.getList(dcOrgParam);
}
}
  • 定义Mapper接口
1
2
3
4
5
@Mapper
public interface DcOrgMapper {

List<DcOrg> getList(DcOrgParam dcOrgParam);
}
  • 最后写mybaits的Mapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<select id="getList" parameterType="com.nb.xry.groundnet.param.DcOrgParam" resultMap="BaseResultMap">
SELECT ID, NAME, ADDRESS, UID, TYPE, SHORTNAME, CSSSTYLE, SEQ, isdelete, createtime, updatetime,
orgcode, importName, districtCode, wgType, dc_datastate, dc_insertTime, isChange,content
FROM dc_grid_org_org
<where>
<if test="level != null and level != 0">
and type = #{level}
</if>
<if test="parentUid != null and parentUid != '' ">
and uid like CONCAT(#{parentUid},'%')
</if>
</where>
</select>
赏个🍗吧
0%