博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MyBatis开发入门二:一对多连表查询
阅读量:6675 次
发布时间:2019-06-25

本文共 5644 字,大约阅读时间需要 18 分钟。

1. 步骤:

  • (1). 加包
  • (2). 编写db.properties;编写conf.xml,将db.properties加入到conf.xml;引入别名
  • (3). 建立实体类
  • (4). 编写sql操作对应的***Mapper.xml文件
  • (5). 将sql操作对应的***Mapper.xml文件注册到conf.xml文件中
  • (6). 编写MyBaitsUtils
  • (7). 测试

2. 详细步骤

  • (1). 加包

  

  • (2). 编写db.properties;
driverClass=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/mybatis1username=rootpassword=lfdy
  • 编写conf.xml,将db.properties加入到conf.xml;
  • 引入别名
  • (3). 建立实体类
package com.atguigu.mybatis.domain;import java.util.List;/** * @author hp * */public class Classes {        private int id;    private String name;    private Teacher teacher;    private List
students; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public List
getStudents() { return students; } public void setStudents(List
students) { this.students = students; } public Classes(int id, String name, Teacher teacher, List
students) { super(); this.id = id; this.name = name; this.teacher = teacher; this.students = students; } public Classes() { super(); } @Override public String toString() { return "Classses [id=" + id + ", name=" + name + ", teacher=" + teacher + ", students=" + students + "]"; } }
View Code
package com.atguigu.mybatis.domain;/** * @author hp * */public class Teacher {    private int id;    private String name;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Teacher(int id, String name) {        super();        this.id = id;        this.name = name;    }    public Teacher() {        super();    }    @Override    public String toString() {        return "Teacher [id=" + id + ", name=" + name + "]";    }    }
View Code
package com.atguigu.mybatis.domain;/** * @author hp * */public class Student {        private int id;    private String name;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Student(int id, String name) {        super();        this.id = id;        this.name = name;    }    public Student() {        super();    }    @Override    public String toString() {        return "Student [id=" + id + ", name=" + name + "]";    }    }
View Code
  • (4). 编写sql操作对应的***Mapper.xml文件
  

        

      <!-- 嵌套chaxun查询 -->

    <select id="getClass2" parameterType="int" resultMap="ClassResultMap2">
        select * from class where c_id = #{id}
    </select>
    <resultMap type="Classes" id="ClassResultMap2">
      <id property="id" column="c_id"/>
    <result property="name" column="c_name"/>
    <association property="teacher" column="teacher_id" javaType="Teacher" select="getTeacher2"/>
    <collection property="students" column="c_id" ofType="Student" select="getStudent2"/>
    </resultMap>
    <select id="getTeacher2" parameterType="int" resultType="Teacher">
      select t_id id,t_name name from teacher where t_id=#{id}
    </select>

    <select id="getStudent2" parameterType="int" resultType="Student">

      select s_id id,s_name name from student where class_id=#{id}
    </select>

 
  • (5). 将sql操作对应的***Mapper.xml文件注册到conf.xml文件中
  • (6). 编写MyBaitsUtils
package com.atguigu.mybatis.test;import java.io.InputStream;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MyBatisUtils {        public static SqlSessionFactory getFactory(){        String resource = "config.xml";                InputStream is = MyBatisUtils.class.getClassLoader().getResourceAsStream(resource);                SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);                    return factory;    }}
  • (7). 测试
package com.atguigu.mybatis.test;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import com.atguigu.mybatis.domain.*;/** * @author hp * */public class MyBatisTest {        public static void main(String[] args) {                SqlSessionFactory factory = MyBatisUtils.getFactory();                SqlSession session = factory.openSession();                String statement = "com.atguigu.mybatis.mapper.ClassMapper.getClass";            Classes c=  (Classes) session.selectOne(statement,1);        System.out.println(c);                session.close();            }}

 

转载于:https://www.cnblogs.com/lfdingye/p/6444189.html

你可能感兴趣的文章
python爬虫 selenium+phantomjs动态解析网页,加载页面成功,返回空数据
查看>>
smtp;550 DY-001 Mail rejected by---游击队?正规军
查看>>
Nginx_handler模块发开(hello模块结构解析)
查看>>
埃式筛法——求n以内素数
查看>>
HDOJ-1051 Wooden sticks(贪心)
查看>>
js实现类选择器和name属性选择器
查看>>
url末尾的斜杠作用探秘
查看>>
k-密码
查看>>
C# - 常用接口
查看>>
随机抽取内容
查看>>
selenium phantomjs java无界面浏览器环境搭建
查看>>
javaWeb开发中entityBean的习惯用法
查看>>
Jmeter不同接口参数上下调用总结
查看>>
mysqldump的使用
查看>>
Redis快速入门
查看>>
[杂谈]时光飞逝
查看>>
【wiggle-subsequence】leetcode-376
查看>>
订餐系统
查看>>
Quartz.NET总结(四)Quartz 远程调度
查看>>
开源虚拟化KVM(二)管理虚拟存储
查看>>