• Java学习-MyBatis学习(四)


    代码下载

    解决字段名与属性名不一致
    • ①使用别名emp_name empName解决字段名和属性名不一致
    <select id="getAllEmpOld" resultType="Emp">
            
            select eid,emp_name empName,age,sex,email from t_emp;
        select>
    
    • ②在全局配置文件中添加全局配置
    <settings>
            
            <setting name="mapUnderscoreToCamelCase" value="true"/>
        settings>
    
    • ③使用resultMap
    
        <resultMap id="empResultMap" type="Emp">
            <id property="eid" column="eid"/>
            <id property="empName" column="emp_name"/>
            <id property="age" column="age"/>
            <id property="sex" column="sex"/>
            <id property="email" column="email"/>
        resultMap>
        
        <select id="getAllEmp" resultMap="empResultMap">
            select * from t_emp;
        select>
    
    多对一映射关系
    • ①级联属性赋值
     <resultMap id="empAndDeptResultMapOne" type="Emp">
            <id property="eid" column="eid"/>
            <id property="empName" column="emp_name"/>
            <id property="age" column="age"/>
            <id property="sex" column="sex"/>
            <id property="email" column="email"/>
            <id property="dept.did" column="did"/>
            <id property="dept.deptName" column="dept_name"/>
        resultMap>
        
        <select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">
            select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid=#{eid}
        select>
    
    • ②使用association
    <resultMap id="empAndDeptResultMapTwo" type="Emp">
            <id property="eid" column="eid"/>
            <id property="empName" column="emp_name"/>
            <id property="age" column="age"/>
            <id property="sex" column="sex"/>
            <id property="email" column="email"/>
            
            <association property="dept" javaType="Dept">
                <id property="did" column="did"/>
                <id property="deptName" column="dept_name"/>
            association>
        resultMap>
        
        <select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">
            select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid=#{eid}
        select>
    <mapper namespace="com.lotus.mybatis.mapper.DeptMapper">
        
        <select id="getEmpAndDeptByStepTwo" resultType="Dept">
            select * from t_dept where did=#{did}
        select>
    mapper>
    
    • ③分步查询
    <resultMap id="empAndDeptByStepResultMap" type="Emp">
            <id property="eid" column="eid"/>
            <id property="empName" column="emp_name"/>
            <id property="age" column="age"/>
            <id property="sex" column="sex"/>
            <id property="email" column="email"/>
            
            <association property="dept" select="com.lotus.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                         column="did" fetchType="eager">association>
        resultMap>
        
        <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
            select * from t_emp where eid=#{eid}
        select>
    
    public interface EmpMapper {
    /**
         * 通过分步查询查询员工以及员工所对应部门信息
         * 第一步,查询员工信息
         */
      Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);
      }
      public interface DeptMapper {
        /**
         * 通过分步查询查询员工以及员工所对应部门信息
         * 第一步,通过did查询员工对应的部门信息
         */
        Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
    }
    
    一对多映射关系
    • ①使用collection标签
     <resultMap id="deptAndEmpResultMap" type="Dept">
            <id property="did" column="did"/>
            <id property="deptName" column="dept_name"/>
    
            <collection property="emps" ofType="Emp">
                <id property="eid" column="eid"/>
                <id property="empName" column="emp_name"/>
                <id property="age" column="age"/>
                <id property="sex" column="sex"/>
                <id property="email" column="email"/>
            collection>
        resultMap>
        
        <select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
            select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did=#{did}
        select>
    
    /**
         * 获取部门及部门中所有员工信息
         */
        Dept getDeptAndEmp(@Param("did") Integer did);
    //测试代码
     @Test
        public void testGetDeptAndEmp() {
            SqlSession sqlSession = SqlSessionUtils.getSqlSession();
            DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
            Dept dept = mapper.getDeptAndEmp(1);
            System.out.println(dept);
        }
    
    • ②使用分步查询
    <!---DeptMapper.xml>
    <resultMap id="deptAndEmpByStepResultMap" type="Dept">
            <id property="did" column="did"/>
            <result property="deptName" column="dept_name"/>
            <collection property="emps"
                        select="com.lotus.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                        column="did">collection>
        resultMap>
        
        <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
            select * from t_dept where did=#{did}
        select>
    
    
        <select id="getDeptAndEmpByStepTwo" resultType="Emp">
            select * from t_emp where did=#{did}
        select>
    
    //----DeptMapper
    /**
         * 分步查询①查询部门信息
         */
        Dept getDeptAndEmpByStepOne(@Param("did") Integer did);
    //----EmpMapper
    /**
         * 分步查询②根据did查询员工信息
         */
    List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);
    
    //测试代码
     @Test
        public void testGetDeptAndEmpStep() {
            SqlSession sqlSession = SqlSessionUtils.getSqlSession();
            DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
            Dept dept = mapper.getDeptAndEmpByStepOne(1);
            System.out.println(dept);
        }
    
  • 相关阅读:
    java毕业设计基于的电商平台的设计与实现Mybatis+系统+数据库+调试部署
    数学基础(四)极大似然估计、误差的高斯分布与最小二乘估计的等价性
    亚马逊关键词搜索API接口(item_search-按关键字搜索亚马逊商品接口),亚马逊API接口
    [MRCTF2020]PixelShooter
    人大金仓分析型数据库使用之创建和管理表空间
    jvm调优【减少GC频率和Full GC次数】中Gc是什么
    17-JavaSE基础巩固练习:Math类API两道数学算法水题
    SprignCloud Alibaba集成Nacos实现服务注册、服务发现、服务调用详细代码案例
    微信小程序封装的请求事件
    Java函数式编程(2):流式计算
  • 原文地址:https://blog.csdn.net/xuwenpeng/article/details/139637952