美文网首页计算机微刊Java 杂谈程序员
8.平凡之路 - 高级映射一对一和多对一

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

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

    一 resultMap标签之一对一映射

    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;
        //关联对象
        private Role role;
            
            //省略getter和setter方法
    }
    

    Role.java 文件

    public class Role {
    
        private Integer role_id;
        private String role_name;
        private String role_key;
        private Integer status;
    
            //省略getter和setter方法
    }
    

    2.传统方式

    • UserMapper.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>    
        <select id="load" parameterType="_int" resultMap="BaseResultMapper">
            SELECT
                <include refid="sys_user_columns"/>
            FROM
                sys_user
            WHERE
                user_id=#{user_id}
        </select>
    </mapper>
    
    • UserDao接口和UserDaoImpl实现类

    UserDao.java接口代码

    public interface UserDao {
        User getUserByPK(int user_id);
    }
    

    UserDaoImpl.java实现类

    public class UserDaoImpl implements UserDao {
        @Override
        public User getUserByPK( int user_id ) {
            SqlSession sqlSession = null;
            try {
                sqlSession = MyBatisUtils.getSqlSession();
                return sqlSession.selectOne(User.class.getName()+".load", user_id);
            } finally {
                MyBatisUtils.closeSqlSession(sqlSession);
            }
        }
    }
    
    • RoleMapper.xml映射文件
    <mapper namespace="com.shxt.model.Role">
        <resultMap type="com.shxt.model.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>
        <select id="get" parameterType="_int" resultMap="BaseResultMapper">
            SELECT
                <include refid="sys_role_columns"/>
            FROM
                sys_role
            WHERE role_id=#{role_id}
        </select>
    </mapper>
    
    • RoleDao.java文件和RoleDaoImpl.java实现类

    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()+".get", role_id);
            } finally {
                MyBatisUtils.closeSqlSession(sqlSession);
            }
        }
    }
    
    • Java代码测试
        @Test
        public void 获取用户信息(){
            UserDao userDao = new UserDaoImpl();//接口回调
            User user = userDao.getUserByPK(-999);
            //获取对应外键信息
            if(user.getFk_role_id()!=null){
                RoleDao roleDao = new RoleDaoImpl();
                //查询角色对应的信息
                Role role = roleDao.getRoleByPK(user.getFk_role_id());
                //建立关系
                user.setRole(role);
            }
            System.out.println(user);
        }
    
    • 图解说明


      一对一传统方式

    3.resultMap标签之select方式

    其实就是替换了

    //获取对应外键信息
    if(user.getFk_role_id()!=null){
      RoleDao roleDao = new RoleDaoImpl();
       //查询角色对应的信息
       Role role = roleDao.getRoleByPK(user.getFk_role_id());
       //建立关系
       user.setRole(role);
    }
    

    这段代码

    • 映射文件
         <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>
    
     <resultMap type="com.shxt.model.User" id="SimpleResultMapper"  extends="BaseResultMapper">
            <!-- association 用来映射关联对象  -->
            <association property="role" javaType="com.shxt.model.Role"
            column="fk_role_id"
            select="com.shxt.model.Role.get"
            />
        </resultMap>
    
        <sql id="sys_user_columns">
            user_id,account,password,user_name,status,login_time,ip,fk_role_id
        </sql>
        <!-- 使用了另一个ID resultMap="SimpleResultMapper"  -->
        <select id="load" parameterType="_int" resultMap="SimpleResultMapper">
            SELECT
                <include refid="sys_user_columns"/>
            FROM
                sys_user
            WHERE
                user_id=#{user_id}
        </select>
    
    
    • Java测试代码
        @Test
        public void SELECT方式(){
            UserDao userDao = new UserDaoImpl();//接口回调
            User user = userDao.getUserByPK(-999);
            System.out.println(user);
        }
    
    • 图解说明


      select方式说明
      结果集继承

    4.resultMap标签之resultMap属性方式

    • UserDao接口和UserDaoImpl实现类
      UserDao.java代码
    public interface UserDao {
        User getUserLeftJoin(int user_id);
    }
    

    UserDaoImpl.java代码

    
    public class UserDaoImpl implements UserDao {
        @Override
        public User getUserLeftJoin( int user_id ) {
            SqlSession sqlSession = null;
            try {
                sqlSession = MyBatisUtils.getSqlSession();
    
                return sqlSession.selectOne(User.class.getName()+".getUserLeftJoin", user_id);
            } finally {
                MyBatisUtils.closeSqlSession(sqlSession);
            }
        }
    }
    
    • 映射文件
    <mapper namespace="com.shxt.model.User">
    
        <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>
        
    
         <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>
        <resultMap type="com.shxt.model.User" 
            id="JoinResultMapper" 
            extends="BaseResultMapper">
             <association property="role" javaType="com.shxt.model.Role"
                resultMap="com.shxt.model.Role.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"/> -->
            </association>
        
        </resultMap>
        <select id="getUserLeftJoin" parameterType="_int" 
            resultMap="JoinResultMapper">
            SELECT
                <include refid="sys_user_columns_alias">
                    <property name="alias" value="u"/>
                </include>
                , 
                <!-- 关键如何找到那些字段 -->
                <include refid="com.shxt.model.Role.sys_role_columns_alias">
                    <property name="alias" value="r"/>
                </include>
            FROM
                sys_user u
            LEFT JOIN sys_role r ON u.fk_role_id = r.role_id
            WHERE
                u.user_id =#{user_id}
        </select>
    
    • Java测试代码
        @Test
        public void 连接查询_结果集处理(){
            UserDao userDao = new UserDaoImpl();//接口回调
            User user = userDao.getUserLeftJoin(-999);
            System.out.println(user);
        }
    
    • 图解说明


      推荐说明

    二 多对一映射测试

    • UserDao接口和UserDaoImpl实现类
      UserDao.java代码
    public interface UserDao {
        List<User> list01();
        List<User> list02();
    
    }
    

    UserDaoImpl.java代码

    public class UserDaoImpl implements UserDao {
        @Override
        public List<User> list01() {
            SqlSession sqlSession = null;
            try {
                sqlSession = MyBatisUtils.getSqlSession();
    
                return sqlSession.selectList(User.class.getName()+".list01");
            } finally {
                MyBatisUtils.closeSqlSession(sqlSession);
            }
        }
    
        @Override
        public List<User> list02() {
            SqlSession sqlSession = null;
            try {
                sqlSession = MyBatisUtils.getSqlSession();
    
                return sqlSession.selectList(User.class.getName()+".list02");
            } finally {
                MyBatisUtils.closeSqlSession(sqlSession);
            }
        }
    
    }
    
    • 映射文件
        <select id="list01" resultMap="SimpleResultMapper">
            SELECT
                <include refid="sys_user_columns"/>
            FROM
                sys_user
        </select>
        <select id="list02"  resultMap="JoinResultMapper">
            SELECT
                <include refid="sys_user_columns_alias">
                    <property name="alias" value="u"/>
                </include>
                , 
                <include refid="com.shxt.model.Role.sys_role_columns_alias">
                    <property name="alias" value="r"/>
                </include>
            FROM
                sys_user u
            LEFT JOIN sys_role r ON u.fk_role_id = r.role_id
        </select>
    
    • 图解说明


      多对一测试

    相关文章

      网友评论

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

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