• 三.Mybatis 多对一与一对多


       Profess.java    一方

    package com.pojo;
    //一对多 一方
    
    import java.util.List;
    
    public class Profess {
        int proid;
        String professName;
        List<Student> stus;
        public int getProid() {
            return proid;
        }
        public void setProid(int proid) {
            this.proid = proid;
        }
        public String getProfessName() {
            return professName;
        }
        public void setProfessName(String professName) {
            this.professName = professName;
        }
        public List<Student> getStus() {
            return stus;
        }
        public void setStus(List<Student> stus) {
            this.stus = stus;
        }
    }

      Student.java   多方

    package com.pojo;
    
    import java.util.Date;
    //学员和专业  多对一 多方
    public class Student {
        int stuid;
        String stuName;
        int stuAge;
        Date stuDate;
        String stuSex;
        String stuProfess;//专业外键字段,可以保留
        String status;
        String professName;//为了方便业务,补充一个专业名字的属性
        Profess profess;//专业对象,做级联查询
        
        public int getStuid() {
            return stuid;
        }
        public void setStuid(int stuid) {
            this.stuid = stuid;
        }
        public String getStuName() {
            return stuName;
        }
        public void setStuName(String stuName) {
            this.stuName = stuName;
        }
        public int getStuAge() {
            return stuAge;
        }
        public void setStuAge(int stuAge) {
            this.stuAge = stuAge;
        }
        public Date getStuDate() {
            return stuDate;
        }
        public void setStuDate(Date stuDate) {
            this.stuDate = stuDate;
        }
        public String getStuSex() {
            return stuSex;
        }
        public void setStuSex(String stuSex) {
            this.stuSex = stuSex;
        }
        public String getStuProfess() {
            return stuProfess;
        }
        public void setStuProfess(String stuProfess) {
            this.stuProfess = stuProfess;
        }
        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }
        public String getProfessName() {
            return professName;
        }
        public void setProfessName(String professName) {
            this.professName = professName;
        }
        public Profess getProfess() {
            return profess;
        }
        public void setProfess(Profess profess) {
            this.profess = profess;
        }
    }

      IStudentDAO.java

    package com.dao;
    
    import java.util.List;
    
    import com.pojo.Student;
    
    public interface IStudentDAO {
        
        List<Student> getStuByProId(int proid);
        
        List<Student> manyToOne();
    }

      Student.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.dao.IStudentDAO">
    <!--     学员和专业的多对一映射 -->
        <resultMap type="com.pojo.Student" id="stu_profess">
            <id property="stuid" column="stuid"/>
            <result property="stuName" column="stuName"/>
            <result property="stuAge" column="stuAge"/>
            <result property="stuDate" column="stuDate"/>
            <result property="stuSex" column="stuSex"/>
            <result property="stuProfess" column="stuProfess"/>
            <!--多对一 -->
            <association property="profess" javaType="com.pojo.Profess">
                <id property="proid" column="proid"></id>
                <result property="professName" column="professName"/>
            </association>
        </resultMap>
        
    <!--     根据专业id来查询学员 -->
        <select id="getStuByProId" parameterType="int" resultType="com.pojo.Student">
            select * from student where stuprofess=#{id}
        </select>    
    <!--     多对一的查询,查询学员同时查出专业信息 -->
        <select id="manyToOne" resultMap="stu_profess">
            select * from student a,profess b where a.stuProfess=b.proid
        </select>
    </mapper>
    IProfessDAO.java
    package com.dao;
    
    import java.util.List;
    
    import com.pojo.Profess;
    
    public interface IProfessDAO {
        List<Profess> ontToMany();
    }

      Profess.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.dao.IProfessDAO">
        <resultMap id="pro_stu" type="com.pojo.Profess">
            <id property="proid" column="proid"></id>
            <result property="professName" column="professName"></result>
            <collection property="stus" ofType="com.pojo.Student" column="proid" select="com.dao.IStudentDAO.getStuByProId"></collection>
        </resultMap>
        
    <!--     一对多的查询 -->
        <select id="ontToMany" resultMap="pro_stu">
            select * from profess
        </select>
    </mapper>

    多对一测试代码:

    Test.java
    package com.pojo;
    
    import java.io.IOException;
    import java.io.Reader;
    import java.util.Date;
    import java.util.List;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import com.dao.IStudentDAO;
    
    public class Test {
        public static void main(String[] args) throws IOException {
            //读取核心的配置文件
            Reader reader = Resources.getResourceAsReader("Configuration.xml");
            //创建会话工厂
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
            //数据库会话
            SqlSession session = factory.openSession();
            //查询一个对象  第一个参数字符串是由Student.xml 中的namespace+id组成
    //        Student stu = session.selectOne("com.dao.StudentMapper.getStudentById",5);
            
            //通过接口的方式
            IStudentDAO stuDAO = session.getMapper(IStudentDAO.class);
            
            //测试多对一的级联查询
            List<Student> list = stuDAO.manyToOne();
            for (Student stu : list) {
                System.out.println(stu.getStuName()+"	"+stu.getProfess().getProfessName());
            }
            
            session.close();
        }
    }

    一对多测试代码:

    package com.pojo;
    
    import java.io.IOException;
    import java.io.Reader;
    import java.util.Date;
    import java.util.List;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import com.dao.IProfessDAO;
    import com.dao.IStudentDAO;
    
    public class Test {
        public static void main(String[] args) throws IOException {
            //读取核心的配置文件
            Reader reader = Resources.getResourceAsReader("Configuration.xml");
            //创建会话工厂
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
            //数据库会话
            SqlSession session = factory.openSession();
            //查询一个对象  第一个参数字符串是由Student.xml 中的namespace+id组成
    //        Student stu = session.selectOne("com.dao.StudentMapper.getStudentById",5);
            
            //通过接口的方式
            IStudentDAO stuDAO = session.getMapper(IStudentDAO.class);
            IProfessDAO proDAO = session.getMapper(IProfessDAO.class);
    
            //测试一对多查询
            List<Profess> list = proDAO.ontToMany();
            for (Profess pro : list) {
                System.out.println(pro.getProfessName());
                for (Student stu : pro.getStus()) {
                    System.out.print(stu.getStuName()+"	");
                }
                System.out.println();
            }
            session.close();
        }
    }

    Configuration中的注册

    <!--     对象的映射文件注册 -->
        <mappers>
            <mapper resource="com/pojo/Student.xml"></mapper>
            <mapper resource="com/pojo/Profess.xml"></mapper>
        </mappers>
  • 相关阅读:
    【插件】博客园markdown编辑器自定义代码黑色背景高亮显示
    【MatrixSynapseChat】安装教程(一)基于centOS7.* 安装Synapse
    【Python3】python安装 matrix-synapse[all] 报错’Error: pg_config executable not found‘
    【python3】在CentOS7上升级SQLite,并让Python3使用新版SQLite
    【linux】CentOS7 升级sqlite3
    【Python3】Centos7 安装Python3.7
    面试官:兄弟,说说Java的static关键字吧
    六一儿童节,程序员写给女儿的一封信
    被缠上了,小王问我怎么在 Spring Boot 中使用 JDBC 连接 MySQL
    女生适合学编程吗?
  • 原文地址:https://www.cnblogs.com/wlxslsb/p/10796600.html
Copyright © 2020-2023  润新知