mybits 多对多

mybits 多对多

表1

idusername
1feichiwuliu
2123

表2

idusername
1小明
2小红
2小黑

表3 关系表

iduseridiserid2
111
212
322
421
523

controller

public Success moretomore(HttpServletRequest request){
        Success success=new Success(false);
        String[] must={"list"};
        Map map=getParameterMap(request,must);

        List<Connection> connectionList=testService.selectconnection(map);
        success.setSuccess(true);
        success.setObj(connectionList);
        success.setMsgCode(CommonCode.QuerySucess.getInfo(),CommonCode.QuerySucess.getCode());
        return success;
    }

service impl

List<Connection> selectconnection(Map map);

@Override
    public List<Connection> selectconnection(Map map) {
        List<Connection> connectionList=testMapper.selectconnection(map);
        return connectionList;
    }

mapper

List<Connection> selectconnection(Map map);

xml

<resultMap id="BaseResultMap1" type="hai.guo.novel.Test.entity.Connection">
        <id column="userId" jdbcType="INTEGER" property="userId" />
        <result column="userName" jdbcType="VARCHAR" property="userName" />
        <collection property="connection2List"  fetchType="lazy" ofType="hai.guo.novel.Test.entity.Connection2" javaType="list">
            <id column="userId2" jdbcType="INTEGER" property="userId2" />
            <result column="userName2" jdbcType="VARCHAR" property="userName2" />
        </collection>
    </resultMap>


<select id="selectconnection"   resultMap="BaseResultMap1" >
        select
        test.user.id as userId,
        test.user.username as userName,
        test.user2.id as userId2,
        test.user2.username as userName2
        from  test.user,test.user2,test.connection
        where test.connection.userid=test.user.id and test.connection.userid2=test.user2.id
    </select>

实体类1

public class Connection implements Serializable {

    private Integer userId;

    private String userName;

    private List<Connection2> connection2List ;


}

实体类2

public class Connection2 implements Serializable {

    private Integer userId2;

    private String userName2;

}

执行结果

{
    "msg": "查询成功",
    "code": 100,
    "success": true,
    "obj": [
        {
            "userId": 1,
            "userName": "feichiwuliu",
            "connection2List": [
                {
                    "userId2": 1,
                    "userName2": "小明"
                },
                {
                    "userId2": 2,
                    "userName2": "小红"
                }
            ]
        },
        {
            "userId": 2,
            "userName": "123",
            "connection2List": [
                {
                    "userId2": 2,
                    "userName2": "小红"
                },
                {
                    "userId2": 1,
                    "userName2": "小明"
                },
                {
                    "userId2": 3,
                    "userName2": "小黑"
                }
            ]
        }
    ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值