美文网首页
9.平凡之路-高级映射一对多

9.平凡之路-高级映射一对多

作者: 胖先森 | 来源:发表于2017-08-26 16:04 被阅读0次

整体还是在上个项目的基础上进行测试,单独学习这个知识点,所以大家需要重新来过

1.准备阶段

User.java持久化类

public class User {

    private Integer user_id;
    private String account;
    private String password;
    private String user_name;
    private Integer status;
    private Date login_time;
    private String ip;
    private Integer fk_role_id;
}

这里没有设置关联对象了

Role.java持久化类

public class Role {

    private Integer role_id;
    private String role_name;
    private String role_key;
    private Integer status;

    //关联对象
    private List<User> userList;
}

一个角色对应多个用户,我们使用集合进行处理

2.collection标签之select方式

  • 映射文件说明
    RoleMapper.xml代码如下:
<mapper namespace="com.shxt.model.Role">
    <!-- 基础的映射 -->
    <resultMap type="Role" id="BaseResultMapper">
        <id column="role_id" property="role_id"/>
        <result column="role_name" property="role_name"/>
        <result column="role_key" property="role_key"/>
        <result column="status" property="status"/>
    </resultMap>
    <!-- 公共代码字段提取  -->
    <sql id="sys_role_columns">
        role_id,role_name,role_key,status
    </sql>
    <sql id="sys_role_columns_alias">
        ${alias}.role_id,${alias}.role_name,${alias}.role_key,${alias}.status
    </sql>
    
    <resultMap type="Role" id="SimpleResultMapper" 
        extends="BaseResultMapper">
        
        <!-- 配置集合 List<User>-->
        <collection property="userList" 
        
        javaType="java.util.List" 
        
        ofType="com.shxt.model.User"
        
        column="role_id"
        
        select="com.shxt.model.User.listUser"
        />
    </resultMap>
    
    <select id="load" parameterType="int" resultMap="SimpleResultMapper">
        SELECT
            <include refid="sys_role_columns"/>
        FROM
            sys_role
        WHERE
            role_id=#{role_id}
    </select>
</mapper>

这里使用了<collection> 标签

<collection property="类中的属性" 
        
        javaType="类中属性对应的类型,可以省略" 
        
        ofType="结果集处理的类型,也可以理解为结合中泛型的类型,必须填写"
        
        column="通过查询的那个字段的值,在进行查询,该字段必须有查询,不然无法查找数据"
        
        select="com.shxt.model.User.listUser  命名空间.ID 使用column的值去执行对应的SQL语句"
/>

UserMapp.xml代码如下:

基本上不会对其进行改变

<mapper namespace="com.shxt.model.User">
   
     <resultMap type="com.shxt.model.User" id="BaseResultMapper">
        <id column="user_id" property="user_id"/>
        <result column="account" property="account"/>
        <result column="password" property="password"/>
        <result column="user_name" property="user_name"/>
        <result column="status" property="status"/>
        <result column="login_time" property="login_time"/>
        <result column="ip" property="ip"/>
        <result column="fk_role_id" property="fk_role_id"/>
    </resultMap>
    
    
    <sql id="sys_user_columns">
        user_id,account,password,user_name,status,login_time,ip,fk_role_id
    </sql>
    <sql id="sys_user_columns_alias">
        ${alias}.user_id,${alias}.account,${alias}.password,
        ${alias}.user_name,${alias}.status,${alias}.login_time,
        ${alias}.ip,${alias}.fk_role_id
    </sql>
    
    <select id="listUser" parameterType="int" resultMap="BaseResultMapper">
        SELECT
            <include refid="sys_user_columns"/>
        FROM
            sys_user
        WHERE
            fk_role_id=#{role_id}
    
    </select>
</mapper>
  • RoleDao接口和RoleDaoImpl实现类
    RoleDao.java代码如下:
public interface RoleDao {
    Role getRoleByPK(int role_id);
}

RoleDaoImpl.java代码如下:

public class RoleDaoImpl implements RoleDao {

    @Override
    public Role getRoleByPK( int role_id ) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSqlSession();
            return sqlSession.selectOne(Role.class.getName()+".load", role_id);
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
    }
}
  • Java测试代码
    @Test
    public void test01(){
        RoleDao roleDao = new RoleDaoImpl();

        System.out.println(roleDao.getRoleByPK(-100));
    }
  • 图解说明
select方式图解说明

3.collection标签之resultMap属性说明

因为select方式总是执行的SQL语句比较多,我们可以通过连接查询解决问题

  • 映射文件
    RoleMapper.xml代码如下:
<mapper namespace="com.shxt.model.Role">
    <resultMap type="Role" id="BaseResultMapper">
        <id column="role_id" property="role_id"/>
        <result column="role_name" property="role_name"/>
        <result column="role_key" property="role_key"/>
        <result column="status" property="status"/>
    </resultMap>
    <sql id="sys_role_columns">
        role_id,role_name,role_key,status
    </sql>
    <sql id="sys_role_columns_alias">
        ${alias}.role_id,${alias}.role_name,${alias}.role_key,${alias}.status
    </sql>
    <resultMap type="Role" id="JoinResultMapper" extends="BaseResultMapper">
        <collection property="userList" javaType="list" ofType="com.shxt.model.User"
        resultMap="com.shxt.model.User.BaseResultMapper"
        />
   
    </resultMap>
    <select id="getJoin" parameterType="int" resultMap="JoinResultMapper">
        SELECT
            <include refid="sys_role_columns_alias">
                <property name="alias" value="sr"/>
            </include>
            , 
           <include refid="com.shxt.model.User.sys_user_columns_alias">
                <property name="alias" value="su"/>
            </include>
        FROM
            sys_role sr
        LEFT JOIN sys_user su ON sr.role_id = su.fk_role_id
        WHERE
            sr.role_id =#{role_id}
    </select>
</mapper>

说明

 <collection property="userList" javaType="list" ofType="com.shxt.model.User"
        resultMap="com.shxt.model.User.BaseResultMapper 命名空间.ID找到结果集映射的位置"
  • RoleDao接口和RoleDaoImpl实现类
    RoleDao.java代码
public interface RoleDao {
    Role getJoinRolrByPK(int role_id);
}

RoleDaoImpl.java代码

public class RoleDaoImpl implements RoleDao {
    @Override
    public Role getJoinRolrByPK( int role_id ) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtils.getSqlSession();
            return sqlSession.selectOne(Role.class.getName()+".getJoin", role_id);
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
    }
}
  • Java测试代码
    @Test
    public void test02(){
        RoleDao roleDao = new RoleDaoImpl();

        System.out.println(roleDao.getJoinRolrByPK(-100222));
    }

后话:Hibernate的多对多和MyBatis的多对多处理方案不太一样,但是个人还是比较喜欢MyBatis的方式,其实Hibernate也可以一样优秀的处理

相关文章

网友评论

      本文标题:9.平凡之路-高级映射一对多

      本文链接:https://www.haomeiwen.com/subject/gjtvdxtx.html