转载请标注原文地址:https://blog.csdn.net/lilyssh/article/details/82753716
Spring Boot 2.0.4 集成 mybatisplus-spring-boot-starter 1.0.5 。项目源码地址
1. 初始化工程
工程 user-provider的结构为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 1$ tree -I target
2.
3├── pom.xml
4└── src
5 └── main
6 ├── java
7 │ └── cn
8 │ └── lilyssh
9 │ └── user
10 │ └── provider
11 │ ├── UserProviderApplication.java
12 │ ├── dao
13 │ │ ├── entity
14 │ │ │ └── UserEntity.java
15 │ │ ├── mapper
16 │ │ │ └── UserMapper.java
17 │ │ └── repository
18 │ │ └── UserRepository.java
19 │ └── service
20 │ └── UserService.java
21 └── resources
22 └── application.yml
23
24
2. 添加依赖
在user-provider工程中打开pom.xml加入以下依赖。
引入mybatis-plus-boot-starter依赖:
1
2
3
4
5
6
7 1<dependency>
2 <groupId>com.baomidou</groupId>
3 <artifactId>mybatis-plus-boot-starter</artifactId>
4 <version>3.0.1</version>
5</dependency>
6
7
3. 编码
(1) 编写Mapper类,要加@Mapper,继承BaseMapper<T>。
1
2
3
4
5
6
7
8
9
10
11
12 1package cn.lilyssh.user.provider.dao.mapper;
2
3import cn.lilyssh.user.provider.dao.entity.UserEntity;
4import com.baomidou.mybatisplus.core.mapper.BaseMapper;
5import org.apache.ibatis.annotations.Mapper;
6
7@Mapper
8public interface UserMapper extends BaseMapper<UserEntity> {
9
10}
11
12
(2) 编写repository类,继承ServiceImpl<xxMapper, xxEntity>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 1package cn.lilyssh.user.provider.dao.repository;
2
3import cn.lilyssh.user.provider.dao.entity.UserEntity;
4import cn.lilyssh.user.provider.dao.mapper.UserMapper;
5import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
6import lombok.extern.slf4j.Slf4j;
7import org.springframework.stereotype.Component;
8
9@Slf4j
10@Component
11public class UserRepository extends ServiceImpl<UserMapper, UserEntity> {
12
13}
14
15
(3) 添加接口实现类,调用mybatis plus提供的方法。
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
32
33
34
35
36
37
38 1package cn.lilyssh.user.provider.service;
2
3import cn.lilyssh.user.api.model.response.UserQueryResp;
4import cn.lilyssh.user.api.service.UserServiceApi;
5import cn.lilyssh.user.provider.dao.entity.UserEntity;
6import cn.lilyssh.user.provider.dao.repository.UserRepository;
7import com.alibaba.dubbo.config.annotation.Service;
8import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
9import lombok.AllArgsConstructor;
10import lombok.extern.slf4j.Slf4j;
11import org.springframework.beans.BeanUtils;
12import org.springframework.stereotype.Component;
13import java.util.ArrayList;
14import java.util.List;
15
16@Slf4j
17@Service
18@Component
19@AllArgsConstructor
20public class UserService implements UserServiceApi {
21
22 private UserRepository userRepository;
23
24 @Override
25 public List<UserQueryResp> list(){
26 QueryWrapper<UserEntity> qw = new QueryWrapper<>();
27 List<UserEntity> userEntityList = userRepository.list(qw);
28 List<UserQueryResp> resultList=new ArrayList<>();
29 userEntityList.forEach(userEntity->{
30 UserQueryResp userQueryResp=new UserQueryResp();
31 BeanUtils.copyProperties(userEntity,userQueryResp);
32 resultList.add(userQueryResp);
33 });
34 return resultList;
35 }
36}
37
38
4. 测试
(1) 使用telnet模拟调用dubbo服务,端口为user-provider.xml里配置的dubbo端口,默认20880。
1
2
3 1telnet localhost 20880
2
3
如果连接成功,会看到
1
2
3
4
5
6
7 1Trying ::1...
2Connected to localhost.
3Escape character is '^]'.
4
5dubbo>
6
7
(2) 使用ls查看所有服务。
1
2
3
4 1dubbo>ls
2cn.lilyssh.user.api.service.UserServiceApi
3
4
(3) 使用cd进入到com.qianxunclub.demo.dubbo.DemoService中,并使用ls查看服务里的方法。
1
2
3
4
5
6
7
8
9
10 1dubbo>cd cn.lilyssh.user.api.service.UserServiceApi
2Used the cn.lilyssh.user.api.service.UserServiceApi as default.
3You can cancel default service by command: cd /
4dubbo>ls
5Use default service cn.lilyssh.user.api.service.UserServiceApi.
6
7list
8dubbo>
9
10
(4) 使用invoke模拟客户端调用服务。
1
2
3 1dubbo>invoke list()
2
3
会看到:
1
2
3
4
5 1Use default service cn.lilyssh.user.api.service.UserServiceApi.
2[{"age":0,"id":1,"idType":0,"phone":0,"sex":0,"status":0,"userName":"Tom","uuid":"3"},{"age":0,"id":2,"idType":0,"phone":0,"sex":0,"status":0,"userName":"Mike","uuid":"4"}]
3elapsed: 83 ms.
4
5
大功告成!