MyBatis注解——多对多

1、数据库建表语句

CREATE TABLE `user` (
  `id` int(11) DEFAULT NULL,
  `username` varchar(50) DEFAULT NULL
);

CREATE TABLE `role` (
  `id` int(11) DEFAULT NULL,
  `rolename` varchar(50) DEFAULT NULL,
  `roledesc` varchar(50) DEFAULT NULL
);


CREATE TABLE `sys_user_role` (
  `id` int(11) DEFAULT NULL,
  `userid` int(11) DEFAULT NULL,
  `roleid` int(11) DEFAULT NULL
);

2、实体

public class UserInfo {

    private Integer id;

    private String username;

    private List<Role> roleList;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public List<Role> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<Role> roleList) {
        this.roleList = roleList;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", roleList=" + roleList +
                '}';
    }
}

public class Role {

    private Integer id;

    private String roleName;

    private String roleDesc;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getRoleDesc() {
        return roleDesc;
    }

    public void setRoleDesc(String roleDesc) {
        this.roleDesc = roleDesc;
    }

    @Override
    public String toString() {
        return "Role{" +
                "id=" + id +
                ", roleName='" + roleName + '\'' +
                ", roleDesc='" + roleDesc + '\'' +
                '}';
    }
}

3、Mapper 接口 java 代码

public interface UserMapper {

    @Select("select * from user")
    @Results(value = {
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "roleList",column = "id",javaType = List.class,
            many = @Many(select = "com.deppon.mapper.IRoleMapper.findRolesByUid"))
    })
    List<UserInfo> findAllUserAndRole();
}

public interface IRoleMapper {

    @Select("select * from role r,sys_user_role ur where r.id = ur.roleid and ur.userid = #{id}")
    List<Role> findRolesByUid(Integer id);
}

4、测试类

public class TestOrderMapperUtil {

    private OrderMapper orderMapper;

    private UserMapper userMapper;

    @Before
    public void before() throws IOException {
        String path = "SqlConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(path);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession(true);
        orderMapper = session.getMapper(OrderMapper.class);
        userMapper = session.getMapper(UserMapper.class);
    }


    @Test
    public void test(){
        List<UserInfo> allUserAndRole = userMapper.findAllUserAndRole();
        for(UserInfo userInfo:allUserAndRole){
            System.out.println(userInfo);
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值