zoukankan      html  css  js  c++  java
  • Mybatis学习(二)关联映射

    1.一对一映射

    (1)修改UserMapper.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.xx.dao.UserDao">
        <resultMap id="UserResultMap" type="com.xx.entity.User">
            <id column="ID" property="id" jdbcType="DECIMAL"/>
            <result column="NAME" property="name" jdbcType="VARCHAR"/>
            <result column="SEX" property="sex" jdbcType="CHAR"/>
            <result column="AGE" property="age" jdbcType="DECIMAL"/>
          <!--property指的是User类中定义的变量名称 column表示传入的参数 select指向select标签的id --> <association property="info" column="id" select="getTeacher"/> </resultMap> <insert id ="addUser" parameterType="com.xx.entity.User"> INSERT INTO TB_USER(ID,NAME,SEX,AGE) VALUES (#{id},#{name},#{sex},#{age}) </insert> <select id="getTeacher" parameterType="int" resultType="com.xx.entity.StudentInfo"> select * from STUDENTINFO t WHERE t.AGE = #{id} </select> <select id="getUser" resultMap="UserResultMap"> SELECT * FROM TB_USER </select> </mapper>

     (2)添加StudentInfo.java

     (3)在User.java中实例化一个StudentInfo变量

    2.一对多映射

    (1)修改UserMapper.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.xx.dao.UserDao">
        <resultMap id="UserResultMap" type="com.xx.entity.User">
            <id column="ID" property="id" jdbcType="DECIMAL"/>
            <result column="NAME" property="name" jdbcType="VARCHAR"/>
            <result column="SEX" property="sex" jdbcType="CHAR"/>
            <result column="AGE" property="age" jdbcType="DECIMAL"/>
        <!-- fetchType表示懒加载 --> <collection property="info" column="id" select="getTeacher" javaType="ArrayList" ofType="com.xx.entity.StudentInfo" fetchType="lazy"/> </resultMap> <insert id ="addUser" parameterType="com.xx.entity.User"> INSERT INTO TB_USER(ID,NAME,SEX,AGE) VALUES (#{id},#{name},#{sex},#{age}) </insert> <select id="getTeacher" parameterType="int" resultType="com.xx.entity.StudentInfo"> select * from STUDENTINFO t WHERE t.AGE = #{id} </select>
    <select id="getUser" resultMap="UserResultMap"> SELECT * FROM TB_USER </select> </mapper>

     (2)将一对一映射中实例化变量改为变量List,其余同上 

     

  • 相关阅读:
    supervisor安装(sentos7)
    linux网络管理----远程登录工具
    asp.net mvc 文件压缩下载
    JavaScript 逗号表达式
    SQL面试题——查询课程
    js中== ===的区别
    网易笔试题目:三列布局,中间自适应宽度,双飞翼布局,及问题
    搜狐面试题:有12个球,外形都一样,其中有一个质量和其他的不一样,给你一架天平,请问最少称几次可以把那个不同的球找出来。
    行内元素对齐:display:inline-block;
    respond.js第六行 SCRIPT5: 拒绝访问。跨域问题
  • 原文地址:https://www.cnblogs.com/soup227/p/6689810.html
Copyright © 2011-2022 走看看