MyBatis06-自定义ResultMap

1:解决数据表字段名和实体类属性名不一致问题

方式1:设置数据表字段别名,别名为实体类属性名
emp_name -> empName

select id,emp_name empName,age,sex,email from t_emp

方式2:通过MyBatis全局配置开启驼峰转换
比如:emp_name->empName

<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

方式3:使用resultMap,自定义结果集

  1. resultMap的id:resultMap唯一标识
  2. type:对应实体类
  3. id:设置主键映射关系
  4. result:设置普通字段映射关系
  5. property:实体类的属性名
  6. column:数据库的字段名
<resultMap id="empResultMap" type="Emp">
    <id property="id" column="id"/>
    <result property="empName" column="emp_name"/>
    <result property="age" column="age"/>
    <result property="sex" column="sex"/>
    <result property="email" column="email"/>
</resultMap>
<select id="selectAll" resultMap="empResultMap">
    select * from t_emp
</select>

2:如何解决多对一关系

场景:比如许多员工属于同一个部门内,如何通过查询员工信息,并且得到部门信息。

员工类:
Dept是部门类

public class Emp {
    private Integer id;
    private String empName;
    private Integer age;
    private String sex;
    private String email;
    private Dept dept;//多对一,设置一的对象
}

方式1:级联属性赋值
a:员工接口的方法:

//查询员工和员工对应的部门
Emp getEmpAndDept(@Param("eid") Integer id);

b:员工接口的映射:连接员工和部门表,员工的部门id=部门id

<select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">
    select * from t_emp left  join  t_dept
             on t_emp.did=t_dept.did where t_emp.id=#{eid}
</select>

c:ResultMap
通过dept.属性 和 字段 进行匹配

<!--级联属性赋值-->
<resultMap id="empAndDeptResultMapOne" type="Emp">
    <id property="id" column="id"/>
    <result property="empName" column="emp_name"/>
    <result property="age" column="age"/>
    <result property="sex" column="sex"/>
    <result property="email" column="email"/>
    <result property="dept.did" column="did"/>
    <result property="dept.deptName" column="dept_name"/>
</resultMap>

方式2:通过association实现
方法和映射同上,需要更改的是ResultMap:

  1. association:处理多对一的映射关系
  2. property:多对一关系的属性
  3. javaType:该属性的类型
<resultMap id="empAndDeptResultMapTwo" type="Emp">
    <id property="id" column="id"/>
    <result property="empName" column="emp_name"/>
    <result property="age" column="age"/>
    <result property="sex" column="sex"/>
    <result property="email" column="email"/>
    <association property="dept" javaType="Dept">
        <id property="did" column="did"/>
        <result property="deptName" column="dept_name"/>
    </association>
</resultMap>

方式3:分布查询

我们可以通过员工id查询到员工的所有信息,这其中包含了部门的did,再通过部门的did查询到部门的信息。

1:分布查询第一步:

a:员工接口的方法:

//分布查询第一步,查询员工信息
Emp getEmpAndDeptByStepOne(@Param("eid") Integer id);

b:员工接口的映射:

<!--分布查询第一步,查询员工信息-->
<select id="getEmpAndDeptByStepOne" resultMap="getEmpAndDeptByStepResultMap">
    select * from t_emp where id=#{eid}
</select>

2:分布查询第二步
a:部门接口的方法:

//分布查询第二步,以did作为条件查询部门信息
Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);

b:部门接口的映射:

<!--分布查询第二步,以did作为条件查询部门信息-->
<resultMap id="deptResultMap" type="Dept">
    <id property="did" column="did"/>
    <result property="deptName" column="dept_name"/>
</resultMap>
<select id="getEmpAndDeptByStepTwo" resultMap="deptResultMap">
    select * from t_dept where did=#{did}
</select>

3:分布查询第三步ResultMap
员工接口映射文件的ResultMap:

  1. association:引用第二个查询
  2. property:需要处理的分布查询的属性
  3. select:使用nameSpace+方法id唯一标识
  4. column:第一个查询的结果字段,作为第二个查询的条件
<resultMap id="getEmpAndDeptByStepResultMap" type="Emp">
    <id property="id" column="id"/>
    <result property="empName" column="emp_name"/>
    <result property="age" column="age"/>
    <result property="sex" column="sex"/>
    <result property="email" column="email"/>
    <association property="dept"
                 select="MapperInterface.DeptMapper.getEmpAndDeptByStepTwo"
                 column="did" fetchType="lazy">
    </association>
</resultMap>

3:如何解决一对多关系

场景:查询部门信息,里面包含许多部门员工的信息
部门类:
Emp是员工类

public class Dept {
    private Integer did;
    private String deptName;
    private List<Emp> emps;//一对多为集合
}

方式1:collection实现
a:部门接口的方法:

//获取部门和部门员工信息
Dept getDeptAndEmpByDid(@Param("did") Integer did);

b:部门接口的映射:通过部门did=员工did查询

<!--获取部门和部门员工信息-->
<select id="getDeptAndEmpByDid" resultMap="deptAndEmpResultMap" >
    select * from t_dept left join t_emp
        on t_dept.did=t_emp.did
    where t_dept.did=#{did}
</select

c:ResultMap:

  1. collection:处理一对多的映射关系
  2. property:需要处理的属性
  3. ofType:该属性对应的集合的数据类型
<resultMap id="deptAndEmpResultMap" type="Dept">
    <id property="did" column="did"/>
    <result property="deptName" column="dept_name"/>
    <collection property="emps" ofType="Emp">
        <id property="id" column="id"/>
        <result property="empName" column="emp_name"/>
        <result property="age" column="age"/>
        <result property="sex" column="sex"/>
        <result property="email" column="email"/>
    </collection>
</resultMap>

方式2:分布查询
先查找部门信息,再根据部门的did查找相应的员工。
1:分布查询第一步:
a:部门接口的方法:

//分布查询第一步:查询部门信息
Dept getDeptAndEmpByStepOne(@Param("did") Integer did);

b:部门接口的映射

<!--分布查询第一步:查询部门信息-->
<select id="getDeptAndEmpByStepOne" resultMap="DeptAndEmpByStepResultMap">
    select * from t_dept where did=#{did}
</select>

2:分布查询第二步:
a:员工接口的方法:

//分布查询第二步:以部门did作为条件查询员工信息
List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);

b:员工接口的映射:

<!--分布查询第二步:以部门did作为条件查询员工信息-->
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
    select id,emp_name empName,age,sex,email  from t_emp where did=#{did}
</select>

3:分布查询第三步ResultMap

<resultMap id="DeptAndEmpByStepResultMap" type="Dept">
    <id property="did" column="did"/>
    <result property="deptName" column="dept_name"/>
    <collection property="emps"
                select="MapperInterface.EmpMapper.getDeptAndEmpByStepTwo"
                column="did"
    >
    </collection>
</resultMap>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-Plus 中,我们可以通过自定义 XML 文件来实现复杂的 SQL 操作,包括分页查询。下面是一个示例,演示如何使用自定义 XML 文件实现 Mybatis-Plus 分页查询: ```xml <!-- Mybatis 自定义 XML 文件 --> <mapper namespace="com.example.mapper.UserMapper"> <select id="selectUserList" resultType="com.example.entity.User"> select * from user <where> <if test="name != null and name != ''"> and name like concat('%', #{name}, '%') </if> </where> </select> <!-- 分页查询 --> <select id="selectUserPage" resultMap="userResultMap"> select * from user <where> <if test="name != null and name != ''"> and name like concat('%', #{name}, '%') </if> </where> <if test="page != null"> limit #{page.current}, #{page.size} </if> </select> </mapper> ``` 其中,`selectUserList` 是一个普通的查询语句,`selectUserPage` 则是一个带有分页参数的查询语句。在分页查询中,我们需要使用 `limit` 关键字来控制返回数据的数量,同时,我们也需要将分页信息传递给 SQL 语句。这里,我们通过 Mybatis 的参数处理机制来实现,在 XML 文件中使用 `${paramName}` 取出参数值。 接下来,我们需要在 Java 代码中调用该查询语句,从而实现分页查询。示例代码如下: ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Override public IPage<User> getUserPage(Page<User> page, String name) { return baseMapper.selectUserPage(page, name); } } ``` 在 Service 实现类中,我们通过 `baseMapper` 来调用自定义 SQL 语句,其中 `selectUserPage` 方法返回一个 `IPage<User>` 对象,表示查询结果。最后,我们将该对象返回给控制器,完成分页查询。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值