其他分享
首页 > 其他分享> > mybits 多对多

mybits 多对多

作者:互联网

mybits 多对多

表1

id username
1 feichiwuliu
2 123

表2

id username
1 小明
2 小红
2 小黑

表3 关系表

id userid iserid2
1 1 1
2 1 2
3 2 2
4 2 1
5 2 3

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": "小黑"
                }
            ]
        }
    ]
}

标签:map,userName2,userId2,List,mybits,test,id
来源: https://blog.csdn.net/qq_41575913/article/details/101365190