网站推荐、资源下载等 | 个人网站

mybatis的一对多

1、配置文件

db.properties

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
db.username=root
db.password=123456

SqlMapConfig.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6 
 7     <!-- 加载java的配置文件 -->
 8     <properties resource="config/db.properties"/>
 9     
10     <!-- 配置mybatis的环境信息,与spring整合,该信息由spring来管理 -->
11     <environments default="development">
12         <environment id="development">
13             <!-- 配置JDBC事务控制,由mybatis进行管理 -->
14             <transactionManager type="JDBC"></transactionManager>
15             <!-- 配置数据源,采用mybatis连接池 -->
16             <dataSource type="POOLED">
17                 <property name="driver" value="${db.driver}" />
18                 <property name="url" value="${db.url}" />
19                 <property name="username" value="${db.username}" />
20                 <property name="password" value="${db.password}" />
21             </dataSource>
22         </environment>
23     </environments>
24 
25     <!-- 加载映射文件 -->
26     <mappers>
27         <mapper resource="config/mapper.xml" />
28     </mappers>
29     
30 </configuration>

mapper.xml

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.xiaostudy.domain.Mapper">
 6 
 7     <!-- ========================================================================================== -->
 8     <!-- 一对多之resultMap1 -->
 9     <select id="findOfTeacher" resultMap="OfTeacher">
10         select * from  teacher t, student s where t.tid=s.tid and t.tid=#{id}
11     </select>
12     <resultMap type="com.xiaostudy.domain.Teacher" id="OfTeacher">
13         <id column="tid" property="tid"/>
14         <result column="tname" property="tname"/>
15         <collection  property="student" ofType="com.xiaostudy.domain.Student" ><!-- javaType="HashSet" -->
16             <id column="sid" property="sid"></id>
17             <result column="tid" property="tid"/>
18             <result column="sname" property="sname"/>
19         </collection>
20     </resultMap>
21    <!-- ========================================================================================== -->
22 
23     <!-- ========================================================================================== -->
24     <!-- 一对多之resultMap2 -->
25     <select id="findOfTeachers" resultMap="OfTeachers">
26         select * from teacher t, student s  where t.tid=s.tid and t.tid=#{id}
27     </select>
28     <resultMap type="com.xiaostudy.domain.Teacher" id="OfTeachers">
29         <id column="tid" property="tid"/>
30         <result column="tname" property="tname"/>
31         <collection property="student" ofType="com.xiaostudy.domain.Student" select="getStudent" column="tid"/>
32     </resultMap>
33     
34     <select id="getStudent" parameterType="int" resultType="com.xiaostudy.domain.Student">
35         select * from student s where s.tid = #{id}
36     </select>
37    <!-- ========================================================================================== -->
38 
39 </mapper>

 

2、domain类

Student.java

 

 1 package com.xiaostudy.domain;
 2 
 3 public class Student {
 4     private int sid;
 5     private int tid;
 6     private String sname;
 7     private Teacher teacher;
 8 
 9     public int getSid() {
10         return sid;
11     }
12 
13     public void setSid(int sid) {
14         this.sid = sid;
15     }
16 
17     public int getTid() {
18         return tid;
19     }
20 
21     public void setTid(int tid) {
22         this.tid = tid;
23     }
24 
25     public String getSname() {
26         return sname;
27     }
28 
29     public void setSname(String sname) {
30         this.sname = sname;
31     }
32 
33     public Teacher getTeacher() {
34         return teacher;
35     }
36 
37     public void setTeacher(Teacher teacher) {
38         this.teacher = teacher;
39     }
40 
41     @Override
42     public String toString() {
43         return "Student [sid=" + sid + ", tid=" + tid + ", sname=" + sname
44                 + ", teacher=" + teacher + "]";
45     }
46 
47 }

 

Teacher.java

 

 1 package com.xiaostudy.domain;
 2 
 3 import java.util.Set;
 4 
 5 public class Teacher {
 6     private int tid;
 7     private String tname;
 8     private Set<Student> student;
 9 
10     public Set<Student> getStudent() {
11         return student;
12     }
13 
14     public void setStudent(Set<Student> student) {
15         this.student = student;
16     }
17 
18     public int getTid() {
19         return tid;
20     }
21 
22     public void setTid(int tid) {
23         this.tid = tid;
24     }
25 
26     public String getTname() {
27         return tname;
28     }
29 
30     public void setTname(String tname) {
31         this.tname = tname;
32     }
33 
34     @Override
35     public String toString() {
36         return "Teacher [tid=" + tid + ", tname=" + tname + ", student=" + student + "]";
37     }
38 
39 }

 

3、代理类Mapper.java

 

 1 package com.xiaostudy.domain;
 2 
 3 import java.util.List;
 4 
 5 public interface Mapper {
 6 
 7     // 一对多之resultMap1
 8     public List<Teacher> findOfTeacher(int id);
 9     
10     // 一对多之resultMap2
11     public List<Teacher> findOfTeachers(int id);
12 
13 }

 

4、测试类

 

 1 package com.xiaostudy.test;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.List;
 6 
 7 import org.apache.ibatis.io.Resources;
 8 import org.apache.ibatis.session.SqlSession;
 9 import org.apache.ibatis.session.SqlSessionFactory;
10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
11 
12 import com.xiaostudy.domain.Mapper;
13 import com.xiaostudy.domain.Teacher;
14 
15 /**
16  * @desc 测试类
17  * @author xiaostudy
18  *
19  */
20 public class MybatisTest {
21 
22     public static void main(String[] args) throws IOException {
23         String resource = "config/SqlMapConfig.xml";
24         InputStream inputStream = Resources.getResourceAsStream(resource);
25 
26         // 创建SqlSessionFactory
27         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
28 
29         // 创建SqlSession
30         SqlSession sqlSession = sqlSessionFactory.openSession();
31 
32         // 获取一个代理dao实现
33         Mapper mapper = sqlSession.getMapper(Mapper.class);
34         
35         //一对多之resultMap1
36         /*List<Teacher> list = mapper.findOfTeacher(1);
37         for(Teacher t : list) {
38             System.out.println(t);
39         }*/
40         
41         //一对多之resultMap2
42         List<Teacher> list = mapper.findOfTeachers(1);
43         for(Teacher t : list) {
44             System.out.println(t);
45         }
46         
47         sqlSession.close();
48 
49     }
50     
51 }

 

5、数据库

teacher表

student表

 

 


 

posted @ 2018-09-04 22:36  xiaostudy  阅读(159)  评论(0编辑  收藏  举报
网站推荐
[理工最爱]小时百科 |  GitHub |  Gitee |  开源中国社区 |  牛客网 |  不学网论坛 |  r2coding |  冷熊简历 |  爱盘 |  零散坑 |  bootstrap中文网 |  vue.js官网教程 |  源码分享站 |  maven仓库 |  楼教主网站 |  廖雪峰网站 |  w3cschool |  在线API |  代码在线运行 |  [不学网]代码在线运行 |  JS在线运行 |  PHP中文网 |  深度开源eclipse插件 |  文字在线加密解密 |  菜鸟教程 |  慕课网 |  千图网 |  手册网 |  素材兔 |  盘多多 |  悦书PDF |  sumatra PDF |  calibre PDF |  Snipaste截图 |  shareX截图 |  vlc-media-player播放器 |  MCMusic player |  IDM下载器 |  格式工厂 |  插件网 |  谷歌浏览器插件 |  Crx搜搜 |  懒人在线计算器 |  leetcode算法题库 |  layer官网 |  layui官网 |  formSelects官网 |  Fly社区 |  程序员客栈 |  融云 |  华为云 |  阿里云 |  ztree官网API |  teamviewer官网 |  sonarlint官网 |  editormd |  pcmark10官网 |  crx4chrome官网 |  apipost官网 |  花生壳官网 |  serv-u官网 |  杀毒eset官网 |  分流抢票bypass官网 |  懒猴子CG代码生成器官网 |  IT猿网 |  natapp[内网穿透] |  ngrok[内网穿透] |  深蓝穿透[内网穿透] |  WakeMeOnLan[查看ip] |  iis7 |  [漏洞扫描]Dependency_Check官网 |  [图标UI]fontawesome官网 |  idea插件官网 |  路过图床官网 |  sha256在线解密 |  在线正则表达式测试 |  在线文件扫毒 |  KuangStudy | 
资源下载
电脑相关: Windows原装下载msdn我告诉你 |  U盘制作微PE工具官网下载 |  Linux_CentOS官网下载 |  Linux_Ubuntu官网下载 |  Linux_OpenSUSE官网下载 |  IE浏览器官网下载 |  firefox浏览器官网下载 |  百分浏览器官网下载 |  谷歌google浏览器历史版本下载 |  深度deepin系统官网下载 |  中兴新支点操作系统官网下载 |  文件对比工具Beyond Compare官网下载 |  开机启动程序startup-delayer官网下载 |  openoffice官网下载 |  utorrent官网下载 |  qbittorrent官网下载 |  cpu-z官网下载 |  蜘蛛校色仪displaycal官网下载 |  单文件制作greenone下载 |  win清理工具Advanced SystemCare官网下载 |  解压bandizip官网下载 |  内存检测工具memtest官网下载 |  磁盘坏道检测与修复DiskGenius官网下载 |  磁盘占用可视化SpaceSniffer官网下载 |  [磁盘可视化]WizTree官网下载 |  win快速定位文件Everything官网下载 |  文件定位listary官网下载 |  动图gifcam官网下载 |  7-Zip官网下载 |  磁盘分区工具diskgenius官网下载 |  CEB文件查看工具Apabi Reader官网下载 |  罗技鼠标options官网下载 |  [去除重复文件]doublekiller官网下载 | 
编程相关: ApacheServer官网下载 |  Apache官网下载 |  Git官网下载 |  Git高速下载 |  Jboss官网下载 |  Mysql官网下载 |  Mysql官网历史版本下载 |  NetBeans IDE官网下载 |  Spring官网下载 |  Nginx官网下载 |  Resin官网下载 |  Tomcat官网下载 |  jQuery历史版本下载 |  nosql官网下载 |  mongodb官网下载 |  mongodb_linux历史版本下载 |  mongodb客户端下载 |  VScode官网下载 |  cxf官网下载 |  maven官网下载 |  QT官网下载 |  SVN官网下载 |  SVN历史版本下载 |  nodeJS官网下载 |  oracle官网下载 |  jdk官网下载 |  STS官网下载 |  STS历史版本官网下载 |  vue官网下载 |  virtualbox官网下载 |  docker desktop官网下载 |  github desktop官网下载 |  EditPlus官网下载 |  zTree下载 |  layui官网下载 |  jqgrid官网下载 |  jqueryui官网下载 |  solr历史版本下载 |  solr分词器ik-analyzer-solr历史版本下载 |  zookeeper历史版本官网下载 |  nssm官网下载 |  elasticsearch官网下载 |  elasticsearch历史版本官网下载 |  redis官网下载 |  redis历史版本官网下载 |  redis的win版本下载 |  putty官网下载 |  查看svn密码TSvnPD官网下载 |  MongoDB连接工具Robo官网下载 |  dll查看exescope官网下载 |  dll2c官网下载 |  接口测试apipost官网下载 |  接口测试postman官网下载 |  原型设计工具AxureRP官网下载 |  canal官网下载 |  idea主题样式下载 |  vue的GitHub下载 |  finalShell官网下载 |  ETL工具kafka官网下载 |  cavaj[java反编译]官网下载 |  jd-gui[java反编译]官网下载 |  radmin[远程连接]官网下载 |  tcping[win ping端口]下载 |  jQueryUploadFile官网下载 |  RedisPlus下载 |  aiXcoder智能编程助手官网下载 |  [表单效验]validform官网下载 |  idea官网下载 |  RedisStudio下载 |  MD转word含公式pandoc官网下载 |  logviewer官网下载 |  Kafka官网下载 |  hbase高速下载 |  hadoop官网下载 |  hadooponwindows的GitHub下载 |  hive官网下载 |  soapui官网下载 |  flink官网下载 |  kafkatool官网下载 |  MinIO官网下载 |  MinIO中国镜像下载 | 
办公相关工具
免费在线拆分PDF【不超过30M】 |  免费在线PDF转Word【不超过10M】 |  在线文字识别转换【不超过1M】 |  PDF转换成Word【不超过50M】 |  在线OCR识别 |  Smallpdf |  文件转换器Convertio |  迅捷PDF转换器 |  字母大小写转换工具 |  档铺 |  快传airportal[可文字] |  快传-文叔叔 |  P2P-小鹿快传 |  [图床]ImgURL | 
网站入口
腾讯文档 |  有道云笔记网页版 |  为知笔记网页版 |  印象笔记网页版 |  蓝奏云 |  QQ邮箱 |  MindMaster在线思维导图 |  bilibili |  PDM文件在线打开 |  MPP文件在线打开 |  在线PS软件 |  在线WPS |  阿里云企业邮箱登陆入口 | 
其他
PDF转换 |  悦书PDF转换 |  手机号注册查询 |  Reg007 |  akmsg |  ip8_ip查询 |  ipip_ip查询 |  天体运行testtubegames |  测试帧率 |  在线网速测试 |