MyBatis 与 MycatDao 之间的是是非非 (第一章 外键查询)

MyBatis 与 MycatDao 之间的是是非非 (第一章 : 外键查询)

本篇文章的意图并不是为了在两者之间整个孰优孰劣,简单来说这两者都有各自的优势,毕竟MyBatis诞生的时间比MycatDao早得多,但是MycatDao作为一个新人,它在它擅长的领域展露了手脚,同时它也吸引了大量优秀的人才来共同维护,相信在未来MycatDao也会在开发人员心中占领一个重要的地位

一丶 MyBatis 查询

首先来看下表结构:

user(用户信息表)表的id是user_role(用户权限中间表)中的user_id的外键

使用MyBatis联结查询我们需要如下配置和代码:
实体对象:

// User

@Getter
@Setter
@TableName("user")
public class User extends BaseEntity {

    private String name;
    
    private String password;

    private  List<Role> roles;
}
//用户权限关系中间表

@Getter
@Setter
@TableName("user_role")
public class RoleRe {
    private String userId;

    private String roleId;

}

web层:

    @GetMapping("/testFind")
    public User testFind() {
        return userService.testFind();
    }

servre层:

    public User testFind() {
        return userMapper.leftJionRole();
    }

mapper层:

    User leftJionRole();

xml配置:

    <resultMap type="com.step.template.main.entity.User" id="UserAndRolesResultMap">
        <!-- 先配置 User 的属性 -->
        <id column="id" property="id" />
        <result column="name"  property="name"/>
        <result column="password"  property="password"/>
        <!-- 再配置 Role 集合 -->
        <collection property="roles" ofType="com.step.template.main.entity.RoleRe">
            <id column="role_id" property="roleId" />
        </collection>
    </resultMap>


    <select id="leftJionRole" resultMap="UserAndRolesResultMap">
        SELECT id,u.name,u.password, ur.role_id FROM user u, user_role ur WHERE u.id = ur.user_id
    </select>

xml是MyBatis的一大优势,xml允许更自由的操作动态sql,但是在带来便利的同时也增加了相应的配置操作,我们如果要使用关联查询,如果返回的对象其中有一个是集合属性就必须配置collection并且要指定类型ofType
查询结果:

{
    "id": 3,
    "name": "test",
    "password": "111111",
    "roles": [
        {
            "userId": null,
            "roleId": "3"
        },
        {
            "userId": null,
            "roleId": "2"
        }
    ]
}

这里的userId为空是显而易见的,因为并没有在result中配置userId的映射字段,所以返回的结果为null,这样的小失误也经常发生在开发中

二丶 MycatDao 查询

实体对象

@Getter
@Setter
//mycatDao 会映射实体的大小写转为下划线
public class UserRole {
    //需要告知mycatDao这个字段为外键
    @ForeginKey(value = User.class)
    private String userId;

    private String roleId;

}

    @GetMapping(value = "/test/user", produces = "application/json")
    public JsonValue getUserInfoList() {
        //分页对象及状态码
        PageResultSet result = new PageResultSet();
        JsonValue jsonRest = null;
        try {

            PagedQuery qry = new PowerDomainQuery()
                    //去除重复字段
                    .withAutoRemoveDupFields(true)
                    //添加第一个属性User,并忽略返回字段id
                    .addDomainFieldsExclude(User.class, new String[] { "id" })
                    //添加第二个属性 用户权限关联表
                    .addDomainFieldsExclude(UserRole.class, null);
            //执行查询
            jsonRest = leaderDao.exePagedQuery(qry);
        } catch (Exception e) {
            result.retCode = -1;
            jsonRest = Json.createValue("error:" + e.toString());
        }
        return jsonRest;
    }

响应结果

[
    {
        "name": {
            "chars": "test",
            "string": "test",
            "valueType": "STRING"
        },
        "password": {
            "chars": "111111",
            "string": "111111",
            "valueType": "STRING"
        },
        "roles": {
            "chars": "user,admin1",
            "string": "user,admin1",
            "valueType": "STRING"
        },
        "userId": {
            "integral": true,
            "valueType": "NUMBER"
        },
        "roleId": {
            "integral": true,
            "valueType": "NUMBER"
        }
    },
    {
        "name": {
            "chars": "test",
            "string": "test",
            "valueType": "STRING"
        },
        "password": {
            "chars": "111111",
            "string": "111111",
            "valueType": "STRING"
        },
        "roles": {
            "chars": "user,admin1",
            "string": "user,admin1",
            "valueType": "STRING"
        },
        "userId": {
            "integral": true,
            "valueType": "NUMBER"
        },
        "roleId": {
            "integral": true,
            "valueType": "NUMBER"
        }
    }
]

在操作上MycatDao简化了很多操作来方便开发者,这一点会在开发效率上得到明显的体现,但是MycatDao在响应结果上体验感逊色于MyBatis,这一点相信在未来的发展中会得到改善

三丶 使用总结

MyBatis这位老大哥除了在编码量和配置项稍稍落后于MycatDao,但是在响应结果上十分优秀,这个结果也就注定了MyBatis需要有那么多约束来规定开发者.
MycatDao更加倾向于让开发者更自由,使用更基础的技术打造最适合的框架,在响应结果上并不出色的MycatDao在未来一定会改变,毕竟它只是一个新生的角色.

下一期精彩内容可关注公众号:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值