主题:MyBatis的用法在beetlsql里面怎么实现

uncle_zb 2019年01月03日 148

MyBatis的代码:

 <resultMap id="PermissionMap" type="">    
    <id property="userId" column="userId"/>    
    <result property="account" column="account"/>    
    <result property="nickname" column="nickname"/>    
    <collection property="roleList" ofType="String">        
        <id column="roleName" property="roleName"/>    
    </collection>    
    <collection property="menuList" ofType="String">
        <id column="menuCode" property="menuCode"/>    
    </collection>    
    <collection property="permissionList" ofType="String">
        <id column="permissionCode" property="permissionCode"/>
    </collection>
</resultMap>
<select id="getUserPermission" resultMap="PermissionMap">    
          SELECT user_.id userId,
           user_.account account,
           user_.name nickname,
           role_.name roleName,
           menu_.code menuCode,
           menu_.url permissionCode    FROM o2o_t_user user_         LEFT JOIN o2o_t_role role_ ON role_.id IN 
         <foreach collection="list" index="index" item="i" open="(" separator="," close=")">             #{i}         </foreach>         LEFT JOIN o2o_t_relation rela_ ON role_.id = rela_.roleid
         LEFT JOIN o2o_t_menu menu_ ON rela_.menuid = menu_.id
    WHERE user_.account = #{username} AND user_.status = 1
</select>                                 
public class One2Many extends JSONObject {    
    private Set roleList;    
    private Set menuList;    
    private Set permissionList;    
    private Set permissionIds;    
    private List picList;    
    private List menus;    
    private List users;    
    private List permissions;
}

JSONObject userPermission = permissionMapper.getUserPermission(username,roleIds);

多行记录通过这个查询合并为一条记录

请问在beetlsql里面可以实现这个功能吗?

闲大赋 2019年01月04日

目前版本不具备这种查询结果集后自动组装,未来计划支持。目前可以使用ORM查询来完成

uncle_zb 2019年01月04日

好的! 现在就是采用的ORM来完成的.

User result = resourceDao.getUserPermission(username);
List<AuthRole> roles = ((List<AuthRole>) result.get("authRole"));
Set<String> roleList = new HashSet<>();
Set<String> menuList = new HashSet<>();
Set<String> permissList = new HashSet<>();
roles.forEach(item -> {    
roleList.add(item.getName());    
if (ToolUtil.isNotEmpty(item.get("percode"))) {        
menuList.add(item.get("percode").toString());    
}    
if (ToolUtil.isNotEmpty(item.get("uri"))) {        
permissList.add(item.get("uri").toString());    
}});
userPermission.put("userId", result.getId());
userPermission.put("account", result.getAccount());
userPermission.put("roleList", roleList);
userPermission.put("menuList", menuList);
userPermission.put("permissionList", permissList);

Sql语句:

getUserPermission
===
SELECT * FROM o2o_t_user WHERE account=#account#
@orm.many({"roleid":"ids"},"resource.selectRoles","AuthRole");

selectRoles===
@ var idsarr = strutil.split(ids,",");
SELECT
   role_.name,
   res_.code perCode,
   res_.uri
FROM
   auth_role role_
   LEFT JOIN auth_role_resource rr_ ON rr_.ROLE_ID = role_.ID
   LEFT JOIN auth_resource res_ ON res_.ID = rr_.RESOURCE_ID 
WHERE
   role_.id IN (#join(idsarr)#)