MyBatis与MycatDao如何实现外键查询

这篇文章给大家分享的是有关MyBatis与MycatDao如何实现外键查询的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。


 

一丶 MyBatis 查询

首先来看下表结构:

MyBatis与MycatDao如何实现外键查询  

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如何实现外键查询”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!