高并发架构实战(三) Spring Boot 集成 mybatis-plus

转载请标注原文地址: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
$ tree -I target
.
├── pom.xml
└── src
└── main
├── java
│   └── cn
│   └── lilyssh
│   └── user
│   └── provider
│   ├── UserProviderApplication.java
│   ├── dao
│   │   ├── entity
│   │   │   └── UserEntity.java
│   │   ├── mapper
│   │   │   └── UserMapper.java
│   │   └── repository
│   │   └── UserRepository.java
│   └── service
│   └── UserService.java
└── resources
└── application.yml

2. 添加依赖

user-provider工程中打开pom.xml加入以下依赖。
引入mybatis-plus-boot-starter依赖:

1
2
3
4
5
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.1</version>
</dependency>

3. 编码

(1) 编写Mapper类,要加@Mapper,继承BaseMapper<T>

1
2
3
4
5
6
7
8
9
10
package cn.lilyssh.user.provider.dao.mapper;

import cn.lilyssh.user.provider.dao.entity.UserEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<UserEntity> {

}

(2) 编写repository类,继承ServiceImpl<xxMapper, xxEntity>

1
2
3
4
5
6
7
8
9
10
11
12
13
package cn.lilyssh.user.provider.dao.repository;

import cn.lilyssh.user.provider.dao.entity.UserEntity;
import cn.lilyssh.user.provider.dao.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class UserRepository extends ServiceImpl<UserMapper, UserEntity> {

}

(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
package cn.lilyssh.user.provider.service;

import cn.lilyssh.user.api.model.response.UserQueryResp;
import cn.lilyssh.user.api.service.UserServiceApi;
import cn.lilyssh.user.provider.dao.entity.UserEntity;
import cn.lilyssh.user.provider.dao.repository.UserRepository;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Component
@AllArgsConstructor
public class UserService implements UserServiceApi {

private UserRepository userRepository;

@Override
public List<UserQueryResp> list(){
QueryWrapper<UserEntity> qw = new QueryWrapper<>();
List<UserEntity> userEntityList = userRepository.list(qw);
List<UserQueryResp> resultList=new ArrayList<>();
userEntityList.forEach(userEntity->{
UserQueryResp userQueryResp=new UserQueryResp();
BeanUtils.copyProperties(userEntity,userQueryResp);
resultList.add(userQueryResp);
});
return resultList;
}
}

4. 测试

(1) 使用telnet模拟调用dubbo服务,端口为user-provider.xml里配置的dubbo端口,默认20880。

1
telnet localhost 20880

如果连接成功,会看到

1
2
3
4
5
Trying ::1...
Connected to localhost.
Escape character is '^]'.

dubbo>

(2) 使用ls查看所有服务。

1
2
dubbo>ls
cn.lilyssh.user.api.service.UserServiceApi

(3) 使用cd进入到com.qianxunclub.demo.dubbo.DemoService中,并使用ls查看服务里的方法。

1
2
3
4
5
6
7
8
dubbo>cd cn.lilyssh.user.api.service.UserServiceApi
Used the cn.lilyssh.user.api.service.UserServiceApi as default.
You can cancel default service by command: cd /
dubbo>ls
Use default service cn.lilyssh.user.api.service.UserServiceApi.

list
dubbo>

(4) 使用invoke模拟客户端调用服务。

1
dubbo>invoke list()

会看到:

1
2
3
Use default service cn.lilyssh.user.api.service.UserServiceApi.
[{"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"}]
elapsed: 83 ms.

大功告成!

本文由 lilyssh创作。可自由转载、引用,但需署名作者且注明文章出处。


当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器