首页 > 其他 > 详细

mybatis--表关系之一对多

时间:2020-03-09 22:42:04      阅读:51      评论:0      收藏:0      [点我收藏+]

mybatis中的多表查询

表关系分类:

  一对一

  多对一(一对多)

  多对多

一对多

  示例:一个用户有多个社会角色,

        我们需要两张表,m_user和m_role表,需要在角色表上面添加用户表的外键

        两个实体类,两个Mapper.xml文件

     当我们查询账户时可以得到,对应的用户

     当我们查询用户时可以得到,得到它账户集合

代码示例:

1.实体类

public class Role {
    private Integer r_id;
    private String r_name;
    private String r_type;
}
public class User {
    private Integer uId;
    private String uName;
    private String uLovel;
}

2.主配置文件,基本不变

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--映入数据库配置资源文件-->
    <properties resource="db.properties"/>
    <!--配置驼峰命名法,且settings属性要在文件environments上面-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>

    <!--别名,仅支持javaBean类的别名-->
    <typeAliases>
        <typeAlias type="com.bean.User" alias="user"></typeAlias>
        <typeAlias type="com.bean.Role" alias="role"></typeAlias>
    </typeAliases>

    <!-- 运行环境配置(整合后归Spring) -->
    <environments default="development">
        <environment id="development">
            <!-- 事物 -->
            <transactionManager type="JDBC" />
            <!-- 连接池 , 可以减少我们获取连接所消耗的时间-->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 指定映射文件
    如果用注解配置:需要偶配置class的全限定类名-->
    <mappers>
        <!--<mapper resource="com/dao/UserMapper.xml" />-->
        <!--<mapper class="com.dao.UserMapper"></mapper>-->
        <package name="com.dao"></package>
    </mappers>

</configuration>

3.映射配置文件UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.UserMapper">
    <select id="findAll"  resultType="user">
        select * from m_user;
    </select>

    <select id="find"  resultType="user" parameterType="java.lang.Integer">
        select * from m_user where u_id=#{uId};
    </select>

</mapper>

4.映射配置文件RoleMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.RoleMapper">
    <select id="findAll"  resultType="role">
        select * from m_role;
    </select>

    <select id="find"  resultType="role" parameterType="java.lang.Integer">
        select * from m_role where r_id=#{rId};
    </select>
</mapper>

5.测试

public class MybatisTest01 {

    @Test
    public void m1(){
        SqlSession session = MybatisUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> all = mapper.findAll();
        for (User user : all) {
            System.out.println(user);
        }
        session.commit();
        session.close();
    }

    @Test
    public void m2(){
        SqlSession session = MybatisUtils.getSession();
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        List<Role> all = mapper.findAll();
        for (Role role : all) {
            System.out.println(role);
        }
        session.commit();
        session.close();
    }
}

输出结果正常

通过编写中间类,获取一对多数据

1.编写中间类

package com.bean;

public class RoleUser extends User {
    private String rName;

    public String getrName() {
        return rName;
    }

    public void setrName(String rName) {
        this.rName = rName;
    }

    @Override
    public String toString() {
        return super.toString()+ "              RoleUser{" +
                "rName=‘" + rName + ‘\‘‘ +
                ‘}‘;
    }
}

2.修改RoleMapper.xml配置文件

   <select id="findAllUR"  resultType="com.bean.RoleUser">
        select u.*,r.r_name  from m_user u,m_role r where u.u_r_id=r.r_id;
    </select>

3.测试

 @Test
    public void m3(){
        SqlSession session = MybatisUtils.getSession();
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        List<RoleUser> allUR = mapper.findAllUR();
        for (RoleUser roleUser : allUR) {
            System.out.println(roleUser);
        }
        session.commit();
        session.close();
    }

输出结果 能取出数据

DEBUG [main] - ==>  Preparing: select u.*,r.r_name from m_user u,m_role r where u.u_r_id=r.r_id; 
DEBUG [main] - ==> Parameters: 
DEBUG [main] - <==      Total: 5
User{uId=1, uName=‘金角‘, uLovel=‘111‘}              RoleUser{rName=‘主公‘}
User{uId=2, uName=‘银角‘, uLovel=‘睡觉‘}              RoleUser{rName=‘将军‘}
User{uId=3, uName=‘铁脚‘, uLovel=‘奥特曼‘}              RoleUser{rName=‘士兵‘}
User{uId=4, uName=‘吕布‘, uLovel=‘孝顺‘}              RoleUser{rName=‘主公‘}
User{uId=7, uName=‘周瑜‘, uLovel=‘谦虚‘}              RoleUser{rName=‘将军‘}

 

通过编写添加对象关系,一的一方,获取一对多数据

1. 修改Role实体类

public class Role {
    private Integer rId;
    private String rName;
    private String rType;
    //一对多,一的一方,一个角色至少对应一个类
    private List<User> user;
}

2.修改UserMapper配置文件,加入resultMap标签

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.UserMapper">
    <resultMap id="userMap" type="user">
        <id column="u_id" property="uId"></id>
        <result column="u_name" property="uName"></result>
        <result column="u_lovel" property="uLovel"></result>
    </resultMap>
    <select id="findAll"  resultType="user">
        select * from m_user;
    </select>

    <select id="find"  resultType="user" parameterType="java.lang.Integer">
        select * from m_user where u_id=#{uId};
    </select>

</mapper>

3.修改RoleMapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.RoleMapper">
    <select id="findAll"  resultMap="roleUser">
        select u.*,r.* from m_user u,m_role r where u.u_r_id=r.r_id;
    </select>

    <select id="findAllUR"  resultType="com.bean.RoleUser">
        select u.*,r.r_name  from m_user u,m_role r where u.u_r_id=r.r_id;
    </select>

    <select id="find"  resultType="role" parameterType="java.lang.Integer">
        select * from m_role where r_id=#{rId};
    </select>

    <!--对Role和User用ResultMap进行封装-->
    <resultMap id="roleUser" type="role">
        <id column="r_id" property="rId"></id>
        <result column="r_name" property="rName"></result>
        <result column="r_type" property="rType"></result>
        <association property="user" resultMap="com.dao.UserMapper.userMap"/>
    </resultMap>

</mapper>

 

3.测试

 @Test
    public void m1(){
        SqlSession session = MybatisUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> all = mapper.findAll();
        for (User user : all) {
            System.out.println(user);
        }
        session.commit();
        session.close();
    }

输出结果

Role{rId=3, rName=‘主公‘, rType=‘A‘, user=[User{uId=1, uName=‘金角‘, uLovel=‘111‘}, User{uId=4, uName=‘吕布‘, uLovel=‘孝顺‘}]}
Role{rId=1, rName=‘将军‘, rType=‘B‘, user=[User{uId=2, uName=‘银角‘, uLovel=‘睡觉‘}, User{uId=7, uName=‘周瑜‘, uLovel=‘谦虚‘}]}
Role{rId=2, rName=‘士兵‘, rType=‘C‘, user=[User{uId=3, uName=‘铁脚‘, uLovel=‘奥特曼‘}]}

 

通过编写添加对象关系,多的一方,获取一对多数据

1.修改User实体类

public class User {
    private Integer uId;
    private String uName;
    private String uLovel;
    //一对多,多的一方,每个用户一个角色
    private Role role;
}

2.修改RoleMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.UserMapper">
    <!--封装 userMap-->
    <resultMap id="userMap" type="user">
        <id column="u_id" property="uId"></id>
        <result column="u_name" property="uName"></result>
        <result column="u_lovel" property="uLovel"></result>
    </resultMap>

    <!--封装 userRoleMap-->
    <resultMap id="userRoleMap" type="user">
        <id column="u_id" property="uId"></id>
        <result column="u_name" property="uName"></result>
        <result column="u_lovel" property="uLovel"></result>
        <association property="role" resultMap="com.dao.RoleMapper.roleMap"/>
    </resultMap>

    <select id="findAllUR"  resultMap="userRoleMap">
        select u.*,r.* from m_user u,m_role r where u.u_r_id=r.r_id;
    </select>

    <select id="findAll"  resultType="user">
        select * from m_user;
    </select>

    <select id="find"  resultType="user" parameterType="java.lang.Integer">
        select * from m_user where u_id=#{uId};
    </select>

</mapper>

3.修改UserMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.UserMapper">
    <!--封装 userMap-->
    <resultMap id="userMap" type="user">
        <id column="u_id" property="uId"></id>
        <result column="u_name" property="uName"></result>
        <result column="u_lovel" property="uLovel"></result>
    </resultMap>

    <!--封装 userRoleMap-->
    <resultMap id="userRoleMap" type="user">
        <id column="u_id" property="uId"></id>
        <result column="u_name" property="uName"></result>
        <result column="u_lovel" property="uLovel"></result>
        <association property="role" resultMap="com.dao.RoleMapper.roleMap"/>
    </resultMap>

    <select id="findAllUR"  resultMap="userRoleMap">
        select u.*,r.* from m_user u,m_role r where u.u_r_id=r.r_id;
    </select>

    <select id="findAll"  resultType="user">
        select * from m_user;
    </select>

    <select id="find"  resultType="user" parameterType="java.lang.Integer">
        select * from m_user where u_id=#{uId};
    </select>

</mapper>

4.测试

  @Test
    public void m1(){
        SqlSession session = MybatisUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> all = mapper.findAllUR();
        for (User user : all) {
            System.out.println(user+":"+user.getRole());
        }
        session.commit();
        session.close();
    }

输出结果

DEBUG [main] - ==>  Preparing: select u.*,r.* from m_user u,m_role r where u.u_r_id=r.r_id; 
DEBUG [main] - ==> Parameters: 
DEBUG [main] - <==      Total: 5
User{uId=1, uName=‘金角‘, uLovel=‘111‘}:Role{rId=3, rName=‘主公‘, rType=‘A‘, user=null}
User{uId=2, uName=‘银角‘, uLovel=‘睡觉‘}:Role{rId=1, rName=‘将军‘, rType=‘B‘, user=null}
User{uId=3, uName=‘铁脚‘, uLovel=‘奥特曼‘}:Role{rId=2, rName=‘士兵‘, rType=‘C‘, user=null}
User{uId=4, uName=‘吕布‘, uLovel=‘孝顺‘}:Role{rId=3, rName=‘主公‘, rType=‘A‘, user=null}
User{uId=7, uName=‘周瑜‘, uLovel=‘谦虚‘}:Role{rId=1, rName=‘将军‘, rType=‘B‘, user=null}

 

 

 

 

 

 

     

  

  

 

mybatis--表关系之一对多

原文:https://www.cnblogs.com/jasonjson/p/12452004.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!