Java学习-MyBatis学习(四)

代码下载

解决字段名与属性名不一致
  • ①使用别名emp_name empName解决字段名和属性名不一致
xml 复制代码
<select id="getAllEmpOld" resultType="Emp">
        <!--①使用别名emp_name empName解决字段名和属性名不一致-->
        select eid,emp_name empName,age,sex,email from t_emp;
    </select>
  • ②在全局配置文件中添加全局配置
xml 复制代码
<settings>
        <!-- 将_自动映射为驼峰,emp_name:empName-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
  • ③使用resultMap
xml 复制代码
<!--
         resultMap:设置自定义映射关系
         id:唯一标识
         type:设置映射关系中的实体类类型
         id:设置主键
         属性:
         property:设置映射关系中的属性名,必须是type属性所设置的实体类类型中的属性名
         column:设置映射关系中的字段名,必须是SQL语句查询出的字段名
     -->
    <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>
    <!--③使用resultMap解决字段名和属性名不一致-->
    <select id="getAllEmp" resultMap="empResultMap">
        select * from t_emp;
    </select>
多对一映射关系
  • ①级联属性赋值
xml 复制代码
 <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>
    <!--Emp getEmpAndDept(@Param("eid") Integer eid);-->
    <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
xml 复制代码
<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:处理多对一的映射关系
        javaType:该属性的类型
        -->
        <association property="dept" javaType="Dept">
            <id property="did" column="did"/>
            <id property="deptName" column="dept_name"/>
        </association>
    </resultMap>
    <!--Emp getEmpAndDept(@Param("eid") Integer eid);-->
    <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">
    <!--Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);-->
    <select id="getEmpAndDeptByStepTwo" resultType="Dept">
        select * from t_dept where did=#{did}
    </select>
</mapper>
  • ③分步查询
xml 复制代码
<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"/>
        <!--
        select:设置分步查询的SQL唯一标识(namespace,SQLID或mapper接口的全类名.方法名)
        column:设置分步查询的条件
        fetchType(eager|lazy):当开启全局延迟加载后,通过此属性手动控制延迟加载的效果,eager表示立即加载
        -->
        <association property="dept" select="com.lotus.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did" fetchType="eager"></association>
    </resultMap>
    <!-- Emp getEmpAndDeptByStepOne(); -->
    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select * from t_emp where eid=#{eid}
    </select>
java 复制代码
public interface EmpMapper {
/**
     * 通过分步查询查询员工以及员工所对应部门信息
     * 第一步,查询员工信息
     */
  Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);
  }
  public interface DeptMapper {
    /**
     * 通过分步查询查询员工以及员工所对应部门信息
     * 第一步,通过did查询员工对应的部门信息
     */
    Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
}
一对多映射关系
  • ①使用collection标签
xml 复制代码
 <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>
    <!--Dept getDeptAndEmp(@Param("did") Integer did);-->
    <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>
java 复制代码
/**
     * 获取部门及部门中所有员工信息
     */
    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);
    }
  • ②使用分步查询
xml 复制代码
<!---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>
    <!--Dept getDeptAndEmpByStepOne(@Param("did") Integer did);-->
    <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
        select * from t_dept where did=#{did}
    </select>
<!-- EmpMapper.xml -->
<!--List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);-->
    <select id="getDeptAndEmpByStepTwo" resultType="Emp">
        select * from t_emp where did=#{did}
    </select>
java 复制代码
//----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);
    }
相关推荐
西瓜本瓜@3 小时前
在Android中如何使用Protobuf上传协议
android·java·开发语言·git·学习·android-studio
言之。3 小时前
别学了,打会王者吧
java·python·mysql·容器·spark·php·html5
机智的人猿泰山3 小时前
java kafka
java·开发语言·kafka
Algorithm15763 小时前
谈谈接口和抽象类有什么区别?
java·开发语言
细心的莽夫4 小时前
SpringCloud 微服务复习笔记
java·spring boot·笔记·后端·spring·spring cloud·微服务
264玫瑰资源库5 小时前
问道数码兽 怀旧剧情回合手游源码搭建教程(反查重优化版)
java·开发语言·前端·游戏
pwzs6 小时前
Java 中 String 转 Integer 的方法与底层原理详解
java·后端·基础
东阳马生架构6 小时前
Nacos简介—2.Nacos的原理简介
java
普if加的帕6 小时前
java Springboot使用扣子Coze实现实时音频对话智能客服
java·开发语言·人工智能·spring boot·实时音视频·智能客服
爱喝一杯白开水6 小时前
SpringMVC从入门到上手-全面讲解SpringMVC的使用.
java·spring·springmvc