zoukankan      html  css  js  c++  java
  • MayBatis中的 one to many (多的一方维护关系)

    :首先理解什么叫维护关系: 就是维护关系的一方需要在数据库表中建立外键!

          学生的表(外键sgid)                                                             班级的表

                              

    这里是学生(多的一方) 维护着关系  

        %%%%%%%%%%%%%%%%%%一的一方包含多的一方resultMap——list的写法 %%%%%%%%%%%%%%%%%%%%%%%%%%%%

          <collection property="students"  ofType="com.ipcopyright.domain.Student">

                <id property="id" column="s_id"/>

                <result property="name" column="s_name"/>

          </collection>

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    班级Bean

    public class Grade {
    
        private Integer gid;
        private String gname;
        private List<Students> studentsList = new ArrayList<Students>();

    班级的映射
    <?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.cn.entity3.Grade" >
        <resultMap id="grad" type="com.cn.entity3.Grade">
            <id property="gid" column="gid"></id>
            <result property="gname" column="gname"></result>
    
            <collection property="studentsList" ofType="com.cn.entity3.Students">
                <id property="sid" column="sid"></id>
                <result property="sname" column="sname"></result>
            </collection>
        </resultMap>
    
       根据学生姓名查询所在班级
        <select id="ggg" resultMap="grad" parameterType="string">
    
            select g.gname,s.sname
            from grades g inner join students s
            on g.gid = s.sgid and s.sname = #{name}
        </select>
    </mapper>

    学生Bean

    public class Students {
        private Integer sid;
        private String sname;
        private Grade grade;

    学生的映射
    <?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.cn.entity3.Students" >
        <resultMap id="students" type="com.cn.entity3.Students">
            <id property="sid" column="sid"></id>
            <result property="sname" column="sname"></result>
            <!---->
            <association property="grade" resultMap="com.cn.entity3.Grade.grad"></association>
        </resultMap>
    
         
    //根据学生的名字查班级 <select id="find" resultMap="students" parameterType="string"> select s.sid,s.sname,g.gname from students s inner join grades g on s.sgid = g.gid where sname = #{sname} </select> </mapper>
    坚持
  • 相关阅读:
    eclipse中不能找到dubbo.xsd
    CentOS7部署tomcat
    mybatis中的foreach
    mybatis中批量添加orcale
    mybatis中的like使用方式
    mybatis默认参数_parameter和_databaseId
    mybatis中的resultMap
    mybatis操作oracle,插入null值时报错 with jdbctype OTHER
    mybatis 中 #{} 和 ${} 的区别
    mybatis Cause: org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [0, 1, param1, param2]
  • 原文地址:https://www.cnblogs.com/gaoSJ/p/13050012.html
Copyright © 2011-2022 走看看