当前位置: 首页 > 编程日记 > 正文

MyBatis开发入门二:一对多连表查询

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.Driver
url=jdbc:mysql://localhost:3306/mybatis1
username=root
password=lfdy
  • 编写conf.xml,将db.properties加入到conf.xml;
<!--  加载db.properties  这段必须放在上面--><properties resource="db.properties"></properties><!-- 配置连接数据库信息 --><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="${driverClass}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments>
  • 引入别名
<!-- 加入别名 --><typeAliases><package name="com.atguigu.mybatis.domain"/></typeAliases>
  • (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<Student> 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<Student> getStudents() {return students;}public void setStudents(List<Student> students) {this.students = students;}public Classes(int id, String name, Teacher teacher, List<Student> students) {super();this.id = id;this.name = name;this.teacher = teacher;this.students = students;}public Classes() {super();}@Overridepublic 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();}@Overridepublic 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();}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + "]";}}
View Code
  • (4). 编写sql操作对应的***Mapper.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.atguigu.mybatis.mapper.ClassMapper">

  <!-- 方式1:嵌套结果 --><select id="getClass" parameterType="int" resultMap="ClassResultMap3">select * from class c,teacher t,student s where c.teacher_id = t.t_id and c.c_id = s.class_id and c.c_id = #{id}</select><resultMap type="Classes" id="ClassResultMap3"><id property="id" column="c_id"/><result property="name" column="c_name"/><association property="teacher" column="teacher_id" javaType="Teacher"><id property="id" column="t_id"/><result property="name" column="t_name"/></association><collection property="students" ofType="Student"><id property="id" column="s_id"/><result property="name" column="s_name"/></collection></resultMap>

<!-- 嵌套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>

</mapper>
  • (5). 将sql操作对应的***Mapper.xml文件注册到conf.xml文件中
<!-- 注册sql映射的mapper文件 --><mappers><mapper resource="com/atguigu/mybatis/mapper/ClassMapper.xml" /></mappers>
  • (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

相关文章:

ASP.NET里的事务处理

出自&#xff1a; http://blog.csdn.net/ycl111/ 事务是一组组合成逻辑工作单元的数据库操作&#xff0c;虽然系统中可能会出错&#xff0c;但事务将控制和维护每个数据库的一致性和完整性。如果在事务过程中没有遇到错误&#xff0c;事务中的所有修改都将永久成为数据库的一部…

JAVA的正则表达式语法

Java 正则表达式表达式意义&#xff1a;1.字符x 字符 x。例如a表示字符a\\ 反斜线字符。在书写时要写为\\\\。&#xff08;注意&#xff1a;因为java在第一次解析时,把\\\\解析成正则表达式\\&#xff0c;在第二次解析时再解析为\&#xff0c;所以凡是不是1.1列举到的转义…

应届生失业率或继续上升?别怕,这份秋招指南请收好!

受疫情影响&#xff0c;今年的就业形势基本上没跑了&#xff1a;“各行各业&#xff0c;大小企业&#xff0c;全面缩招&#xff01;”据国家统计局7月份的最新数据显示&#xff1a;20-24岁大专及以上人员&#xff08;主要为新毕业大学生&#xff09;失业率比去年同期高 3.3 个百…

微信小程序把玩(三十五)Video API

原文:微信小程序把玩&#xff08;三十五&#xff09;Video API电脑端不能测试拍摄功能只能测试选择视频功能&#xff0c;好像只支持mp4格式&#xff0c;值得注意的是成功之后返回的临时文件路径是个列表tempFilePaths而不是tempFilePath文档写的有点问题。 主要属性&#xff1a…

使用.NET发送邮件

出自&#xff1a; http://blog.csdn.net/ycl111/如果你曾经使用过ASP来发送邮件&#xff0c;你大概会使用CDONTS,但是在.NET里&#xff0c;发送邮件的功能已经封装进 .NET Framework的System.Web.Mail的命名空间里了&#xff0c;使用这个命名空间下类&#xff0c;就可以很容易…

采摘工人月薪十万却无人应聘,英澳农场求助 AI

作者 | 神经小兮来源 | HyperAI超神经金秋时节&#xff0c;本是收获的季节&#xff0c;但是英国、澳大利亚等地的果农却愁容满面。眼看着日渐成熟的瓜果就要烂在地里&#xff0c;却还招不到采摘工人。缺人&#xff0c;成为果农们眼下急需解决的问题。虽然大型联合收割机早已普及…

好记性不如烂笔杆-android学习笔记二 Acitvity lifecycle 生命周期

7&#xff0c;//Acitvity lifecycle 生命周期/***1,一个Activity就是一个类&#xff0c;并且这个类要继承Activity*2&#xff0c;复写onCreate方法*3&#xff0c;每个Activity需要在Androidmanifest.xml文件中配置*4&#xff0c;为Activity添加控件*/ 1 public class Activity …

hdu5740

考验代码能力的题目&#xff0c;感觉网络流一要求输出方案我就写的丑 http://www.cnblogs.com/duoxiao/p/5777632.html 官方题解写的很详细 因为如果一个点染色确定后&#xff0c;整个图的染色也就确定了&#xff1b; 对于两个点u和v, 令它们之间的最短路是dis(u,v), 那么交换它…

xml操作类(转载)

作者&#xff1a;未知 请与本人联系 <%Class XMLDOMDocument Private fNode,fANode Private fErrInfo,fFileName,fOpen Dim XmlDom 返回节点的缩进字串 Private Property Get TabStr(byVal Node) TabStr"" If Node Is Nothing Then Exit Property …

对HDS AMS 2000+巡检案例

1. 使用工具&#xff1a;笔记本&#xff0c;网线一根&#xff0c; 2. 使用软件&#xff1a;vmware虚拟机&#xff08;安装XP P2系统&#xff0c;最好为P3&#xff09;&#xff0c;HSNM2-1152-W-CLI-P01.exe&#xff08;AMS 200管理软件&#xff09;&#xff0c;jre…

用Python实现坦克大战游戏 | 干货贴

作者 | 李秋键出品 | AI科技大本营&#xff08;rgznai100&#xff09;《坦克大战》是1985年日本南梦宫Namco游戏公司在任天堂FC平台上&#xff0c;推出的一款多方位平面射击游戏。游戏以坦克战斗及保卫基地为主题&#xff0c;属于策略型联机类。同时也是FC平台上少有的内建关卡…

SPU、SKU、ARPU是什么,我来记录一下我的理解

在电商系统里经常会提到“商品”、“单品”、“SPU”、“SKU”这几个词&#xff0c;那么这几个词到底是什么意思呢&#xff1f;既然不知道是什么&#xff0c;那么我们就查一下&#xff1a;SPU Standard Product Unit &#xff08;标准化产品单元&#xff09;&#xff0c;SKUst…

用C#操纵IIS(代码)

using System;using System.DirectoryServices;using System.Collections;using System.Text.RegularExpressions;using System.Text;/*** author 吴海燕* email wuhy80-usualyahoo.com* 2004-6-25 第一版*/ namespace Wuhy.ToolBox{/// <summary>/// 这个类是静态类。…

linux 内核参数调整说明

linux 内核参数调整说明 所有的TCP/IP调优参数都位于/proc/sys/net/目录。例如, 下面是最重要的一些调优参数, 后面是它们的含义: 1. /proc/sys/net/core/rmem_max — 最大的TCP数据接收缓冲。2. /proc/sys/net/core/wmem_max — 最大的TCP数据发送缓冲。3. /proc/sys/net/ipv4…

Java 最高均薪 19015 元! 9 月程序员工资出炉,你拖后腿了吗?

在全员争当码农的时代&#xff0c;如果你也想学一门编程语言&#xff0c;那么&#xff0c;我要告诉你&#xff0c;Java 是编程语言中不可撼动的王者。有点难理解&#xff1f;先看个排行榜???? 来自权威开发语言排行榜 TIOBE 的数据&#xff08;截止到 2020 年 4 月&#x…

java 基础知识八 正则表达式

正则表达式 是一种可以用于模式匹配和替换的规范&#xff0c; 一个正则表达式就是由普通的字符&#xff08;例如字符a到z&#xff09;以及特殊字符&#xff08;元字符&#xff09;组成的文字模式&#xff0c; 用以描述在查找文字主体时待匹配的一个或多个字符串。正则表达式作为…

PHP中Session的使用

启用配置//修改php.ini中的session.auto_start 0 为 session.auto_start 1session_start();$_SESSION[username]"HM";

《Two Dozen Short Lessons in Haskell》学习(八)- Function Types, Classes, and Polymorphism

《Two Dozen Short Lessons in Haskell》&#xff08;Copyright © 1995, 1996, 1997 by Rex Page&#xff0c;有人翻译为Haskell二十四学时教程&#xff0c;该书如果不用于赢利&#xff0c;可以任意发布&#xff0c;但需要保留他们的copyright&#xff09;这本书是学习 Ha…

图神经网络快速爆发,最新进展都在这里了

译者 | 刘畅出品 | AI科技大本营&#xff08;rgznai100&#xff09;近年来&#xff0c;图神经网络&#xff08;GNNs&#xff09;发展迅速&#xff0c;最近的会议上发表了大量相关的研究论文。本文作者正在整理一个GNN的简短介绍和最新研究报告的摘要。希望这对任何准备进入该领…

css去掉a标签点击后的虚线框

outline是css3的一个属性&#xff0c;用的很少。 声明&#xff0c;这是个不能兼容的css属性&#xff0c;在ie6、ie7、遨游浏览器都不兼容。 outline控制的到底是什么呢&#xff1f; 当聚焦a标签的时候&#xff0c;在a标签的区域周围会有一个虚线的框&#xff0c;这个框不同于bo…

在SQL Server中保存和输出任意类型的文件

我们可以把任意类型的文件保存到SQL Server中&#xff0c;在进行例子之前&#xff0c;先建立测试用表格&#xff0c;TestFile.sql&#xff1a;if exists (select * from dbo.sysobjects where id object_id(N[dbo].[TestFiles]) and OBJECTPROPERTY(id, NIsUserTable) 1) dro…

工作中InnoDB引擎数据库主从复制同步心得

近期将公司的MySQL架构升级了&#xff0c;由原先的一主多从换成了DRBDHeartbeat双主多从&#xff0c;正好手上有一个电子商务网站新项目也要上线了&#xff0c;用的是DRBDHeartbeat双主一从&#xff0c;由于此过程还是有别于以前的MyISAM引擎的&#xff0c;所以这里也将其心得归…

面试官:因为这个语言,我淘汰了90%的人!

很多人都有这样的经历&#xff1a;大量重复性工作&#xff1b;日报、周报、各种报&#xff0c;无穷无尽&#xff1b;不计其数的数据提取琐碎繁杂的事务让工作的效率极低。如果可以一键完成就好了。对这些问题来说&#xff0c;最高效的解决途径就是 Python。1991 年&#xff0c;…

SQL Server不能启动

SQL Server不能正常启动 I had a similar issue after uninstalling Visual Studio 2010 (which autmatically came with a Visual Studio Express 2013 install). I solved it by going through the follwing steps. Installing Visual Studio 2010 shell from here: https://…

ASP.NET 配置节架构

ASP.NET 配置节架构包含控制 ASP.NET Web 应用程序行为的元素。如果为属性指定了默认值&#xff0c;则该默认值是在 Machine.config 文件中设置的&#xff0c;该文件的路径是 systemroot/Microsoft.NET/Framework/versionNumber/CONFIG/Machine.config。 <configuration>…

IEEE迎来首位华人主席,马里兰大学终身教授刘国瑞当选

10月12日&#xff0c;IEEE宣布马里兰大学终身教授刘国瑞&#xff08;K. J. Ray Liu&#xff09;当选为2021年IEEE主席&#xff0c;他也是首位当选IEEE主席的华人学者&#xff0c;他将在明年1月开始接任现任IEEE主席Susan K. Kathy Land的职务。 在此次IEEE候选主席竞选中&#…

Visual C++ 2010 简介

VC是用来创建基于 Microsoft Windows 和 Microsoft .NET 的应用程序 原文地址&#xff1a;http://msdn.microsoft.com/zh-cn/library/60k1461a%28vvs.100%29.aspx提供了强大而灵活的开发环境&#xff0c;用于创建基于 Microsoft Windows 和 Microsoft .NET 的应用程序。您可以在…

Linux网络编程:基于UDP的程序开发回顾篇

基于无连接的UDP程序设计 同样&#xff0c;在开发基于UDP的应用程序时&#xff0c;其主要流程如下&#xff1a; 对于面向无连接的UDP应用程序在开发过程中服务端和客户端的操作流程基本差不多。对比面向连接的TCP程序&#xff0c;服务端少了listen和accept函数。前面我们也说过…

四款5G版iPhone 12齐发,苹果股价却应声而跌

整理 | 郑丽媛、屠敏题图 | 东方IC来源 | CSDN&#xff08;CSDNnews&#xff09;真快&#xff0c;又见面了。北京时间 10 月 14 日凌晨 1 点&#xff0c;Apple 举办的新品发布会如约而至。今年有关 iPhone 新品的到来有些迟&#xff0c;好在「5G just got real」&#xff0c;万…

Linux编译器GCC的使用

嵌入式Linux编译器GCC的使用 1、GCC概述 作为自由软件的旗舰项目&#xff0c;Richard Stallman在十多年前刚开始写作GCC的时候&#xff0c;还只是仅仅把它当作一个C程序语言的编译器&#xff0c;GCC的意思也只是GNU C Compiler而已。 经过了这么多年的发展&#xff0c;GCC已经不…