美文网首页
Mybatis - association 一对一级联

Mybatis - association 一对一级联

作者: zbsong | 来源:发表于2020-04-08 21:09 被阅读0次
    1. 实际操作中,我能需要先确定对象的关系,先创建两张表,模拟一个一对一的对象关系。


      image.png
      image.png

    (随便定义的,只为了演示)
    authors:作者表,存储姓名、登录名、密码
    authorInformations:作者信息表,存储年龄、座右铭
    上面两张表是一对一的关系,一个作者只对应一条信息记录

    1. 新建一个Spring Boot工程,命名为mybatis_cascade(随意)
    2. 配置项目,并且通过mybatis-generator生成上面说的两个类的信息,步骤略...
    3. Author类内容如下
    @Data
    public class Author {
        private String id;
    
        private String name;
    
        private String loginName;
    
        private String pwssword;
    
       //添加authorInformation属性,形成级联
       private AuthorInformation authorInformation;
    
    }
    
    1. AuthorInformation类内容如下
    @Data
    public class AuthorInformation {
        private String id;
    
        private String authorId;
    
        private Integer age;
    
        private String motto;
    
    }
    
    1. AuthorMapper内容如下(为了方便查看,将默认生成的方法都删掉了,下面几个文件的内容也是一样都删掉了,新建了一个方法,根据ID 查询作者的信息)
    @Mapper
    public interface AuthorMapper {
    
        Author getAuthorById(String id);
    
    }
    
    1. AuthorInformationMapper类内容如下
    @Mapper
    public interface AuthorInformationMapper {
    
        AuthorInformation getAuthorInformationbyAuthorId(String authorId);
    }
    
    1. AuthorMapper.xml 内容如下
    <?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="com.sy.mybatis.mapper.AuthorMapper" >
      <resultMap id="BaseResultMap" type="com.sy.mybatis.pojo.Author" >
        <id column="id" property="id" jdbcType="VARCHAR" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="login_name" property="loginName" jdbcType="VARCHAR" />
        <result column="pwssword" property="pwssword" jdbcType="VARCHAR" />
        <association property="authorInformation" column="id" select="com.sy.mybatis.mapper.AuthorInformationMapper.getAuthorInformationbyAuthorId"></association>
      </resultMap>
      <sql id="Base_Column_List" >
        id, name, login_name, pwssword
      </sql>
      
      <select id="getAuthorById" resultMap="BaseResultMap" parameterType="java.lang.String" >
        select 'false' as QUERYID,
        <include refid="Base_Column_List" />
        from authors
        where id = #{id,jdbcType=VARCHAR}
      </select>
    
    </mapper>
    

    9.AuthorInformationMapper.xml内容如下

    <?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="com.sy.mybatis.mapper.AuthorInformationMapper" >
      <resultMap id="BaseResultMap" type="com.sy.mybatis.pojo.AuthorInformation" >
        <id column="id" property="id" jdbcType="VARCHAR" />
        <result column="author_id" property="authorId" jdbcType="VARCHAR" />
        <result column="age" property="age" jdbcType="INTEGER" />
        <result column="motto" property="motto" jdbcType="VARCHAR" />
      </resultMap>
      <sql id="Base_Column_List" >
        id, author_id, age, motto
      </sql>
      
      <select id="getAuthorInformationbyAuthorId" resultMap="BaseResultMap" parameterType="java.lang.String" >
        select 'false' as QUERYID,
        <include refid="Base_Column_List" />
        from author_informations
        where author_id = #{authorId,jdbcType=VARCHAR}
      </select>
      
    </mapper>
    

    .xml文件创建完以后,来看一下AuthorMapper.xml文件中resultMap的<association/>。
    <association property="authorInformation" column="id" select="com.sy.mybatis.mapper.AuthorInformationMapper.getAuthorInformationbyAuthorId"></association>
    select 元素来指定指定的sql去查询
    column 传递给 select 语句的参数,多个参数,使用逗号分隔

    1. 创建Service来获取作者信息
    @Service
    public class AuthorService {
    
        @Autowired
        private AuthorMapper authorMapper;
    
        public Author getAuthorById(String id) {
            Author author = authorMapper.getAuthorById(id);
            return author;
        }
    
    }
    
    测试

    调用AuthorService的getAuthorById方法获取作者信息


    image.png

    可以看到在获取authors表信息的时候把authorInformations对应的信息也查询了出来。

    查看控制台打印的日志也可以看到整个sql的执行过程。 image.png

    相关文章

      网友评论

          本文标题:Mybatis - association 一对一级联

          本文链接:https://www.haomeiwen.com/subject/hllzphtx.html