MyBatis高级查询 一对一映射

drop database if exists simple;
create database simple;

use simple;
drop table if exists sys_user;
create table sys_user
(
    id bigint not null auto_increment comment ‘用户ID‘,
    user_name varchar(50) comment ‘用户名‘,
    user_password varchar(50) comment ‘密码‘,
    user_email varchar(50) comment ‘邮箱‘,
    user_info text comment ‘简介‘,
    head_img blob comment ‘头像‘,
    create_time datetime comment ‘创建时间‘,
    primary key (id)
);
alter table sys_user comment ‘用户表‘;

drop table if exists sys_role;
create table sys_role(
    id bigint not null auto_increment comment ‘角色ID‘,
    role_name varchar(50) comment ‘角色名‘,
    enabled int comment ‘有效标志‘,
    create_by bigint comment ‘创建人‘,
    create_time datetime comment ‘创建时间‘,
    primary key (id)
);
alter table sys_role comment ‘角色表‘;

drop table if exists sys_privilege;
create table sys_privilege
(
    id bigint not null auto_increment comment ‘权限ID‘,
    privilege_name varchar(50) comment ‘权限名称‘,
    privilege_url varchar(50) comment ‘权限URL‘,
    primary key (id)
);
alter table sys_privilege comment ‘权限表‘;

drop table if exists sys_user_role;
create table sys_user_role
(
    user_id bigint comment ‘用户ID‘,
    role_id bigint comment ‘角色ID‘
);
alter table sys_user_role comment ‘用户角色关联表‘;

drop table if exists sys_role_privilege;
create table sys_role_privilege
(
    role_id bigint comment ‘用户ID‘,
    privilege_id bigint comment ‘角色ID‘
);
alter table sys_role_privilege comment ‘角色权限关联表‘;

假如1个用户只能有1种角色sys_user和sys_role是通过sys_user_role一对一关联;

1.使用自动映射处理一对一关系;优点:当一定会使用到嵌套结果时使用。

  1.在SysUser.class model中增加SysRole的对象。

private SysRole role;

  2.在查询的xml Mapper文件中对查询出的role属性写出对应的属性名称。

 <select id="selectUserAndById" resultType="test.model.SysUser">
          select
              u.id,
              u.user_name userName,
              u.user_password userPassword,
              u.user_email userEmail,
              u.create_time createTime,
              u.user_info userInfo,
              u.head_img  headImg,
              r.id "role.id",
              r.role_name "role.roleName",
              r.enabled "role.enabled",
              r.create_by "role.createBy",
              r.create_time "role.createTime"
              from sys_user u inner join sys_user_role ur on u.id=ur.user_id
              inner join sys_role r on ur.user_id=r.id where u.id=#{id}
  </select>

2.使用resultMap配置一对一映射

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test.dao.SysUserMapper">
  <resultMap id="BaseResultMap" type="test.model.SysUser">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="user_password" jdbcType="VARCHAR" property="userPassword" />
    <result column="user_email" jdbcType="VARCHAR" property="userEmail" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="user_info" jdbcType="LONGVARCHAR" property="userInfo" />
    <result column="head_img" jdbcType="LONGVARBINARY" property="headImg" />

  </resultMap>

  <resultMap type="test.model.SysUser" id="userRoleResultMap" extends="BaseResultMap">
      <!-- role相关属性 -->
    <result column="role_id" jdbcType="BIGINT" property="role.id" />
    <result column="role_name" jdbcType="VARCHAR" property="role.roleName" />
    <result column="enabled" jdbcType="INTEGER" property="role.enabled" />
    <result column="create_by" jdbcType="BIGINT" property="role.createBy" />
    <result column="role_create_time" jdbcType="TIMESTAMP" property="role.createTime" />
  </resultMap>
  <!-- 注意返回 resultMap="userRoleResultMap"-->
  <select id="selectUserAndById" resultMap="userRoleResultMap">
          select
              u.id,
              u.user_name userName,
              u.user_password userPassword,
              u.user_email userEmail,
              u.create_time createTime,
              u.user_info userInfo,
              u.head_img  headImg,
              r.id role_id,
              r.role_name role_name,
              r.enabled enabled,
              r.create_by create_by,
              r.create_time role_create_time
              from sys_user u inner join sys_user_role ur on u.id=ur.user_id
              inner join sys_role r on ur.user_id=r.id where u.id=#{id}
  </select>
</mapper>

3.使用resultMap的associaction标签配置一对一映射

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test.dao.SysUserMapper">
  <resultMap id="BaseResultMap" type="test.model.SysUser">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="user_password" jdbcType="VARCHAR" property="userPassword" />
    <result column="user_email" jdbcType="VARCHAR" property="userEmail" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="user_info" jdbcType="LONGVARCHAR" property="userInfo" />
    <result column="head_img" jdbcType="LONGVARBINARY" property="headImg" />

  </resultMap>

  <!-- 继承上面的UserResultMap -->
  <resultMap type="test.model.SysUser" id="userRoleResultMap" extends="BaseResultMap">
      <!-- role相关属性 -->
      <!-- 对应的ResultMap在test.dao.SysRoleMapper这个命名控件中-->
    <association property="role" columnPrefix="role_" resultMap="test.dao.SysRoleMapper.BaseResultMap"></association>
  </resultMap>

  <select id="selectUserAndById" resultMap="userRoleResultMap">
          select
              u.id,
              u.user_name userName,
              u.user_password userPassword,
              u.user_email userEmail,
              u.create_time createTime,
              u.user_info userInfo,
              u.head_img  headImg,
              r.id role_id,
              r.role_name role_role_name,
              r.enabled role_enabled,
              r.create_by role_create_by,
              r.create_time role_create_time
              from sys_user u inner join sys_user_role ur on u.id=ur.user_id
              inner join sys_role r on ur.user_id=r.id where u.id=#{id}
  </select>
<mapper/>
<mapper namespace="test.dao.SysRoleMapper">
  <resultMap id="BaseResultMap" type="test.model.SysRole">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="role_name" jdbcType="VARCHAR" property="roleName" />
    <result column="enabled" jdbcType="INTEGER" property="enabled" />
    <result column="create_by" jdbcType="BIGINT" property="createBy" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
  </resultMap>
</mapper>

4.association标签的嵌套查询 (子查询)

时间: 2024-07-30 19:20:56

MyBatis高级查询 一对一映射的相关文章

MyBatis 级联查询一对一与一对多

mybatis是通过映射sql语句把关系模型(数据库中的表)与领域模型(java中的实体类)关联起的  简单分析级联查询:       关系模型中:表与表只有主外键关联       领域模型中:实体类与实体类这间关联,只有一和多的关系.                            一是指别一个实体类以对象属性存在当前实体类中.                            多是指别一个实体类以集合对象属性存在当前实体类中 下面我以例子的形式给大家说明:数据库脚本如下:drop

【Mybatis】Mybatis关联查询一对一和一对多的实现

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文主要讲了使用Mybatis实现关联查询,分为一对一和一对多两种情况,最后并对ResultMap进行一个简要说明. 一.创建表.分析 下面是两表,一个是顾客表,一个是车票表.一个顾客可以对应多张车票,但是一张车票只能对应一个顾客 t_customer:顾客表,一个顾客可以对应多张车票 t_ticket:车票表,一张车票只能对应一个顾客 1.创建数据表及插入初始数据 创建数据表 use te

Mybatis 高级查询的小整理

高级查询的整理 // resutlType无法帮助我们自动的去完成映射,所以只有使用resultMap手动的进行映射 resultMap: type 结果集对应的数据类型 id 唯一标识,被引用的时候,进行指定 autoMapping 开启自动映射 extends 继承 子标签: association:配置对一的映射 property 定义对象的属性名 javaType 属性的类型 autoMapping 开启自动映射 collection:配置对多的映射 property 定义对象的属性名

MyBatis高级查询 一对多映射

数据库表在一对一映射中. 在数据库sys_user_role中新增一条记录 一个用户可以有多个角色.查询出所有用户和所对应的角色. 1.collection集合的嵌套结果映射 <!-- SysUserMapper.xml --> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN&q

Mybatis关联查询一对一和一对多的实现

Mybatis一对一关联查询 有两张表,老师表teacher和班级表class,一个class班级对应一个teacher,一个teacher对应一个class 需求是根据班级id查询班级信息(带老师的信息) 创建teacher和class表: CREATE TABLE teacher ( t_id INT PRIMARY KEY AUTO_INCREMENT, t_name VARCHAR(20) ); CREATE TABLE class ( c_id INT PRIMARY KEY AUTO

Mybatis高级查询之关联查询

3 关联查询 做查询之前,先修改几个配置.mapper.xml是在mybatis-config.xml中指定,那么我们每增加一个mapper都要增加一个配置,很麻烦.为了简化配置.需要将mapper接口和mapper.xml放到同一个文件下,并且接口和xml文件命名一致.使用mybatis的自动扫描:.这样,当我们新增接口的时候,直接创建接口和对应xml文件就可以了: <mappers> <!--<mapper resource="com.test.mapper.dao/

MyBatis高级查询 存储过程

1.第一个存储过程  根据用户id查询用户其他信息 #第一个存储过程 #根据用户id查询用户其他信息 DROP PROCEDURE IF EXISTS `select_user_by_id`; DELIMITER ;; CREATE PROCEDURE `select_user_by_id` ( IN userId BIGINT, OUT userName VARCHAR (50), OUT userPassword VARCHAR (50), OUT userEmail VARCHAR (50

MyBatis高级查询

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace表示命名空间 保证它是唯一 cn.itsource.mybatis.dao.

MyBatis高级映射查询(3)

一.数据库数据和项目搭建过程 1.主要要四张表,分别为user用户信息表.items商品表.orderdetail订单明细表.orders订单表.表的结构和数据如下: 表结构 CREATE DATABASE mybatis DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `items` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(32) NOT NUL