mybatis的resultMap使用

工程链接:https://github.com/AbitGo/mybatis_csdn/tree/master/workspace_mybatis_0x01

数据库的sys_user参考前面的

reulttype的写法 

<select id="selectUserAndRoleById" resultType="com.mybatis.chapter3.SysUser">
        select u.id,
        u.user_name userName,
        u.user_password userPassword,
        u.user_email userEmail,
        u.user_info userInfo,
        u.head_img headImg,
        u.create_time create_time,
        <!--内部类的写法-->
        r.id "role.id",
        r.role_name "role.roleName",
        r.enabled "role.enabled",
        r.create_time "role.create_time",
        r.create_by "role.create_by"
        from sys_user u
        inner join sys_user_role ur on u.id = ur.user_id
        inner join sys_role r on ur.role_id = r.id
        where u.id = #{id}
    </select>

reultMap的写法 

<select id="selectUserAndRoleById2" resultMap="userRoleMap">
        select u.id,
        u.user_name,
        u.user_password,
        u.user_email,
        u.user_info,
        u.head_img,
        u.create_time,
        <!--内部类的写法-->
        r.id role_id,
        r.role_name role_Name,
        r.enabled role_enable,
        r.create_time role_create_time,
        r.create_by role_create_by
        from sys_user u
        inner join sys_user_role ur on u.id = ur.user_id
        inner join sys_role r on ur.role_id = r.id
        where u.id = #{id}
    </select>

    <resultMap id="userRoleMap" type="com.mybatis.chapter3.SysUser">
        <id property="id" column="id"/>
        <result property="userName" column = "user_name"/>
        <result property="userPassword" column = "user_password"/>
        <result property="userEmail" column = "user_email"/>
        <result property="userInfo" column = "user_info"/>
        <result property="headImg" column = "head_img" jdbcType="BLOB"/>
        <result property="create_time" column = "create_time" jdbcType="TIMESTAMP"/>
        <!--相关属性-->
        <result property="role.id" column = "role_id"/>
        <result property="role.role_Name" column = "role_Name"/>
        <result property="role.enabled" column = "role_enabled"/>
        <result property="role.create_by" column = "role_create_by"/>
        <result property="role.create_time" column = "role_create_time" jdbcType="TIMESTAMP"/>

    </resultMap>

对应的java

package com.mybatis.chapter3;

import java.util.Date;

public class SysRole {
    private Long id;
    private String role_Name;
    private Long enabled;
    private Long create_by;
    private Date create_time;

    public Long getEnabled() {
        return enabled;
    }

    public Long getId() {
        return id;
    }

    public Date getCreate_time() {
        return create_time;
    }

    public Long getCreate_by() {
        return create_by;
    }

    public String getRole_Name() {
        return role_Name;
    }

    public void setEnabled(Long enabled) {
        this.enabled = enabled;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setCreate_time(Date create_time) {
        this.create_time = create_time;
    }

    public void setCreate_by(Long create_by) {
        this.create_by = create_by;
    }

    public void setRole_Name(String role_Name) {
        this.role_Name = role_Name;
    }
}
package com.mybatis.chapter3;

import java.util.Date;

public class SysUser {
    private Long id;
    private String userName;
    private String userPassword;
    private String userEmail;
    private String userInfo;
    private byte[] headImg;
    private Date create_time;
    private SysRole role;


    public SysRole getRole() {
        return role;
    }

    public Long getId() {
        return id;
    }

    public byte[] getHeadImg() {
        return headImg;
    }

    public Date getCreate_time() {
        return create_time;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public String getUserInfo() {
        return userInfo;
    }

    public String getUserName() {
        return userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setCreate_time(Date create_time) {
        this.create_time = create_time;
    }

    public void setHeadImg(byte[] headImg) {
        this.headImg = headImg;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public void setUserInfo(String userInfo) {
        this.userInfo = userInfo;
    }

    public void setRole(SysRole role) {
        this.role = role;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }
}
package com.mybatis.chapter3;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/selectUserAndRoleById")
    public SysUser selectUserAndRoleById()
    {
        return userService.selectUserAndRoleById(1L);
    }

    @GetMapping("/selectUserAndRoleById2")
    public SysUser selectUserAndRoleById2()
    {
        return userService.selectUserAndRoleById2(1L);
    }
}
package com.mybatis.chapter3;

import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {

    //使用resttype进行高级结果映射<一对一>
    SysUser selectUserAndRoleById(Long id);
    //使用restset进行高级结果映射<一对一>
    SysUser selectUserAndRoleById2(Long id);
}
package com.mybatis.chapter3;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    public SysUser selectUserAndRoleById(Long id)
    {
        return userMapper.selectUserAndRoleById(id);
    }
    public SysUser selectUserAndRoleById2(Long id)
    {
        return userMapper.selectUserAndRoleById2(id);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.chapter3.UserMapper">

    <!--自动映射-->
    <select id="selectUserAndRoleById" resultType="com.mybatis.chapter3.SysUser">
        select u.id,
        u.user_name userName,
        u.user_password userPassword,
        u.user_email userEmail,
        u.user_info userInfo,
        u.head_img headImg,
        u.create_time create_time,
        <!--内部类的写法-->
        r.id "role.id",
        r.role_name "role.roleName",
        r.enabled "role.enabled",
        r.create_time "role.create_time",
        r.create_by "role.create_by"
        from sys_user u
        inner join sys_user_role ur on u.id = ur.user_id
        inner join sys_role r on ur.role_id = r.id
        where u.id = #{id}
    </select>

    <select id="selectUserAndRoleById2" resultMap="userRoleMap">
        select u.id,
        u.user_name,
        u.user_password,
        u.user_email,
        u.user_info,
        u.head_img,
        u.create_time,
        <!--内部类的写法-->
        r.id role_id,
        r.role_name role_Name,
        r.enabled role_enable,
        r.create_time role_create_time,
        r.create_by role_create_by
        from sys_user u
        inner join sys_user_role ur on u.id = ur.user_id
        inner join sys_role r on ur.role_id = r.id
        where u.id = #{id}
    </select>

    <resultMap id="userRoleMap" type="com.mybatis.chapter3.SysUser">
        <id property="id" column="id"/>
        <result property="userName" column = "user_name"/>
        <result property="userPassword" column = "user_password"/>
        <result property="userEmail" column = "user_email"/>
        <result property="userInfo" column = "user_info"/>
        <result property="headImg" column = "head_img" jdbcType="BLOB"/>
        <result property="create_time" column = "create_time" jdbcType="TIMESTAMP"/>
        <!--相关属性-->
        <result property="role.id" column = "role_id"/>
        <result property="role.role_Name" column = "role_Name"/>
        <result property="role.enabled" column = "role_enabled"/>
        <result property="role.create_by" column = "role_create_by"/>
        <result property="role.create_time" column = "role_create_time" jdbcType="TIMESTAMP"/>

    </resultMap>

</mapper>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值