mybatis 关联查询

insert:
          parameterType  参数类型 :你传递的参数类型 ,如果有接口的话 就不用说清楚这个类型
          selectKey:
                          keyProperty  属性名称
                          keyColumn  列名
                          resultType  返回值类型
                          order:
                                   BEFORE 在insert之前执行 用于创建ID使用
                                   AFTER 在insert之后执行 用于得到自动编号ID
        select:
                  association(关联关系映射)  关联数据 或者 关联查询

public class Student {
    private Integer sid;
    private String sname;
    private String address;
    private String email;
    private classroom cls; //表中的cls对象查询 student时将classroom对象中的数据也拿出来
    private String phonenumber;
    private Integer cid;
}
————————————————————————————————————
public class classroom {
    private Integer cid;
    private String curriculum;
}

 关联关系相对应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.sun.mybatis.dao.StudentMappr">
    <insert id="inse" parameterType="com.sun.mybatis.dao.Student">
        INSERT INTO student(sname,address,email,phonenumber,cid)
        VALUES(#{sname},#{address},#{email},#{phonenumber},#{cid})
        <selectKey keyProperty="sid" order="AFTER" resultType="java.lang.Integer" keyColumn="sid">
            -- 这里的返回类型要写resultType="java.lang.Integer"不然无法识别你要返回的类型
            -- 这是查询返回你插入的主键号,因为自动编号是无法识别你掺入的是第几列order="AFTER"这里参数是之后,因为是插入完再拿到值
            SELECT LAST_INSERT_ID()
        </selectKey>
    </insert>
    <resultMap id="SelStuCls" type="com.sun.mybatis.dao.Student">
        <id column="sid" property="sid"/>
        <result column="sname" property="sname"/>
        <result column="address" property="address"/>
        <result column="email" property="email"/>
        <result column="phonenumber" property="phonenumber"/>
        <association property="cls" javaType="com.sun.mybatis.dao.classroom">
            <!--这里classroom是student的关联表-->
            <!--将classroom对象放入Student中的cls对象当中-->
            <result column="cid" property="cid"></result>
            <result column="curriculum" property="curriculum"></result>
        </association>
    </resultMap>
    <select id="SelectStuAndCls" resultMap="SelStuCls">
        SELECT s.*,c.`curriculum`FROM student s INNER JOIN classroom c ON s.cid=c.cid
    </select>
    <select id="SelectStu" resultMap="SelStuCls">
     --    resultMap 映射结果集
     --   resultMap拿到SelStuCls映射文件 对应的是要拿出来的数据
        SELECT s.*,c.`curriculum`FROM student s INNER JOIN classroom c ON s.cid=c.cid where sid=#{param1}
    </select>
</mapper>


main操作类写法

public class Teat {
    public static void main(String[] args) {
        SqlSessionFactory sqlSessionFactory = Mybatisconfig.sqlSessionFactory;
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMappr studentMappr = sqlSession.getMapper(StudentMappr.class);
        Student>students = studentMappr.select();
        
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值