# 快速开始
我们将通过一个简单的 Demo 来阐述 Hub-Link 功能,在此之前,我们假设您已经:
- 拥有 Java 开发环境以及相应 IDE
- 熟悉 Spring Boot
- 熟悉 Maven
- 熟悉 Mybatis Plus
- 熟悉 Mybatis Link
现有两张表张 t_sys_user
和 t_sys_role
表
t_sys_user
表,其表结构如下:
id | role_id | username | rmks | type | |
---|---|---|---|---|---|
1 | 1 | admin | demo@163.com | admin | 0 |
2 | 2 | anno | anno@163.com | anno | 2 |
3 | 3 | test | test@163.com | test | 1 |
其对应的数据库 Schema 脚本如下:
DROP TABLE IF EXISTS t_sys_user;
CREATE TABLE `t_sys_user` (
`id` bigint(20) NOT NULL COMMENT 'ID',
`role_id` bigint(20) DEFAULT NULL '角色ID',
`username` varchar(32) DEFAULT NULL COMMENT '登录名',
`email` varchar(128) DEFAULT NULL COMMENT '邮件',
`rmks` varchar(128) DEFAULT NULL COMMENT '备注',
`type` tinyint(4) DEFAULT NULL COMMENT '类型(0:管理员,1:非管理员)',
PRIMARY KEY (`id`),
KEY `ind_username` (`username`,`edit_flag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统用户';
其对应的数据库 Data 脚本如下:
DELETE FROM t_sys_user;
INSERT INTO `t_sys_user` VALUES (1, 1, 'admin', 'demo@163.com', 'admin', 0);
INSERT INTO `t_sys_user` VALUES (2, 2, 'anno', 'anno@163.com', 'anno', 2);
INSERT INTO `t_sys_user` VALUES (3, 3, 'test', 'test@163.com', 'test', 1);
t_sys_role
表,其表结构如下:
id | cd | name | rmks |
---|---|---|---|
1 | ROLE_ADMIN | 管理员 | 管理员 |
2 | ROLE_ANON | 匿名 | 匿名 |
3 | ROLE_TEST | 测试角色 | 测试 |
其对应的数据库 Schema 脚本如下:
DROP TABLE IF EXISTS t_sys_role;
CREATE TABLE `t_sys_role` (
`id` bigint(20) NOT NULL COMMENT 'ID',
`cd` varchar(32) DEFAULT NULL COMMENT '编码',
`nm` varchar(64) DEFAULT NULL COMMENT '名称',
`rmks` varchar(128) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统角色';
其对应的数据库 Data 脚本如下:
DELETE FROM t_sys_role;
INSERT INTO `t_sys_role` VALUES (1, 'ROLE_ADMIN', '管理员', '管理员', 0);
INSERT INTO `t_sys_role` VALUES (2, 'ROLE_ANON', '匿名', '匿名', 0);
INSERT INTO `t_sys_role` VALUES (3, 'ROLE_TEST', '测试角色', '测试', 0);
Question
如果从零开始用 hub-Link 来实现该表的数据加功处理我们需要做什么呢?
# 初始化工程
创建一个空的 Spring Boot 工程(工程将以 Mysql 作为默认数据库进行演示)
TIP
可以使用 Spring Initializer (opens new window) 快速初始化一个 Spring Boot 工程
# 添加依赖
引入 Spring Boot Starter 父工程:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>spring-latest-version</version>
<relativePath/>
</parent>
引入 spring-boot-starter
、spring-boot-starter-test
、mybatis-link-boot-starter
、hub-link-annotation
、hub-link-core
、mysql
依赖:
Latest Version: (opens new window)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.gitee.easy4use</groupId>
<artifactId>mybatis-link-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.gitee.easy4use</groupId>
<artifactId>hub-link-annotation</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.gitee.easy4use</groupId>
<artifactId>hub-link-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>
# 配置
在 application.yml
配置文件中添加 Mysql 数据库的相关配置:
# DataSource Config
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/demo?serverTimezone=Hongkong&allowMultiQueries=true
username: root
password: test
# mybatis-link Config,延用mybatis-plus配置,如果需要其他配置请进入mybatis-plus官网查看
mybatis-plus:
typeAliasesPackage: yui.bss.*.vo
mapperLocations: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: ASSIGN_ID
field-strategy: NOT_NULL
column-underline: true
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
banner: false
sql-parser-cache: true
super-mapper-class: yui.comn.mybatisx.core.mapper.BaseDao
定义配置类 MybatisLinkConfig.java
@Configuration
public class MybatisLinkConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationCacheInterceptor(DbType.MYSQL));
return interceptor;
}
@Bean
public ISqlInjector sqlInjector() {
return new SoftSqlInjector(); // 自定义注入类
}
}
在 Spring Boot 启动类中添加 @MapperScan
注解,扫描 Mapper 文件夹:
注:ComponentScan("yui"),扫描中需要加入yui
包扫描
@SpringBootApplication
@ComponentScan("yui")
@MapperScan("yui.bss.*.dao")
public class QuickStartApplication {
public static void main(String[] args) {
SpringApplication.run(QuickStartApplication.class, args);
}
}
# 编码
编写实体类 SysUserVo.java
和 SysRoleVo.java
(此处使用了 Lombok (opens new window) 简化代码)
实体类 SysUserVo.java
@Data
@TableName("t_sys_user")
public class SysUserVo {
@Note("ID")
private Long id;
@Note("角色ID")
private Long roleId;
@Note("登录名")
private String username;
@Note("邮件")
private String email;
@Note("备注")
private String rmks;
@Note("类型(0:管理员,1:非管理员)")
private Integer type;
}
实体类 SysRoleVo.java
@Data
@TableName("t_sys_role")
public class SysRoleVo {
@Note("ID")
private Long id;
@Note("编码")
private String cd;
@Note("名称")
private String nm;
@Note("备注")
private String rmks;
}
编写实体类 SysUserDto.java
@Data
public class SysUserDto {
protected SysUserVo sysUserVo;
protected SysRoleVo sysRoleVo;
}
编写Dao类 SysUserDao.java
@Mapper
public interface SysUserDao extends BaseDao<SysUserVo, SysUserDto> {
@Link( ones = { @OneToOne(leftColumn = "role_id", rightClass = SysRoleVo.class)})
List<SysUserDto> listUserARole(@Param(Constants.WRAPPER) Wrapper<SysUserVo> wrapper);
}
编写Controller.xml类 SysUserController.xml
<controller name="SysUserController">
<grid name="USER_ROLE">
<col type="yui.bss.demo.vo.SysUserVo" />
<col prefix="role" type="yui.bss.demo.vo.SysRoleVo" />
</grid>
</controller>
编写Controller.java类 SysUserController.java
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController {
@Autowired
private SysUserMgr sysUserMgr;
@GetMapping("listUserARole")
public Object listUserARole(String query) {
FindWrapper<SysUserVo> fw = getWrapper(query, SysUserVo.class);
List<SysUserDto> list = sysUserDao.listUserARole(fw);
return buildList(list, "USER_ROLE");
}
}
# 开始使用
通过POSTMAN对接口进行功能测试:
http://localhost:8862/sys/user/listUserARole
TIP
SysUserController 中的 listUserARole()
查询结果通过 buildList(list, "USER_ROLE")
对返回数据进行处理
控制台输出:
{
"code": 0,
"header": {
"id": "ID",
"roleId": "ID",
"username": "登录名",
"email": "邮件",
"rmks": "备注",
"type": "类型(0:管理员,1:非管理员)",
"roleCd": "编码",
"roleNm": "名称",
"roleRmks": "备注"
},
"data": {
"list": [
{
"id": 1,
"roleId": 1,
"username": "admin",
"email": "demo@163.com",
"rmks": "admin",
"type": 0,
"roleCd": "ROLE_ADMIN",
"roleNm": "管理员",
"roleRmks": "管理员"
},
{
"id": 2,
"roleId": 2,
"username": "anno",
"email": "anno@163.com",
"rmks": "anno",
"type": 2,
"roleCd": "ROLE_ANON",
"roleNm": "匿名",
"roleRmks": "匿名"
},
{
"id": 3,
"roleId": 3,
"username": "test",
"email": "test@163.com",
"rmks": "test",
"type": 1,
"roleCd": "ROLE_TEST",
"roleNm": "测试角色",
"roleRmks": "测试"
}
]
}
}
# 小结
通过以上几个简单的步骤,我们就通过 Hub-Link
实现了 t_sys_user 和 t_sys_role 表的联查数据进行加功处理!
从以上步骤中,我们可以看到集成 Hub-Link
和 MyBatis-Link
非常的简单,只需要引入工程,并配置 mapper 扫描路径即可。
但 Hub-Link
的强大远不止这些功能,想要详细了解 Hub-Link 的强大功能?那就继续往下看吧!