博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java基础-SSM之mybatis多对多关联
阅读量:6974 次
发布时间:2019-06-27

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

            Java基础-SSM之mybatis多对多关联

                                    作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

 

一.准备测试环境(创建数据库表)

 1>.创建teas,stus,links表

use yinzhengjie;create table teas(id int primary key auto_increment , tname varchar(20)) ;create table stus(id int primary key auto_increment , sname varchar(20)) ;        create table links(tid int , sid int) ;alter table links add constraint fk_tid foreign key (tid) references teas(id) ;    alter table links add constraint fk_sid foreign key (sid) references stus(id) ;

2>.添加Maven依赖

1 
2
5
4.0.0
6
cn.org.yinzhengjie
7
Mybatis
8
1.0-SNAPSHOT
9
10
11
junit
12
junit
13
4.11
14
15
16
mysql
17
mysql-connector-java
18
5.1.17
19
20
21
org.mybatis
22
mybatis
23
3.2.1
24
25
26

3>.目录结构如下:

 

二.编写自定义类

1>.Stu.java 文件内容

1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.mybatis.domain.one2many; 7  8 import java.util.ArrayList; 9 import java.util.List;10 11 /**12  *    客户13  */14 public class Customer {15     private Integer id ;16     private String name ;17     private int age ;18 19     //建立从Customer到Order之间一对多关系,因为一个客户可能会有多个订单。我们将多个订单放在一个list中。20     private List
orders = new ArrayList
() ;21 22 public List
getOrders() {23 return orders;24 }25 26 public void setOrders(List
orders) {27 this.orders = orders;28 }29 30 public Integer getId() {31 return id;32 }33 34 public void setId(Integer id) {35 this.id = id;36 }37 38 public String getName() {39 return name;40 }41 42 public void setName(String name) {43 this.name = name;44 }45 46 public int getAge() {47 return age;48 }49 50 public void setAge(int age) {51 this.age = age;52 }53 }

2>.Tea.java 文件内容

1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.mybatis.domain.many2many; 7  8 import java.util.ArrayList; 9 import java.util.List;10 11 public class Tea {12     private Integer id ;13     private String tname ;14 15     public Tea(String tname){16         this.tname = tname ;17     }18     public Tea(){19     }20     private List
stus = new ArrayList
() ;21 22 public Integer getId() {23 return id;24 }25 26 public void setId(Integer id) {27 this.id = id;28 }29 30 public String getTname() {31 return tname;32 }33 34 public void setTname(String tname) {35 this.tname = tname;36 }37 38 public List
getStus() {39 return stus;40 }41 42 public void setStus(List
stus) {43 this.stus = stus;44 }45 46 public void addStus(Stu...stus){47 for(Stu ss : stus){48 this.getStus().add(ss) ;49 ss.getTeas().add(this) ;50 }51 }52 }

 

三.编写配置文件

1>.mybatis-config.xml 文件内容(需要开启批处理模式)

1 
2 3 6
7
8
9
10
11
12
13
14 15
16
17
18
19
20 21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

2>.StuMapper.xml 文件内容

1 
2 4
5
6
7 insert into stus(sname) values(#{sname}) ;8
9

3>.TeaMapper.xml 文件内容

1 
2 4
5
6
7 insert into teas(tname) values(#{tname}) ; 8
9 10
11
12 insert into links(tid,sid) values(#{id}, #{s.id}) ;13
14
15 16
17 delete from links where tid = #{id} ;18 delete from teas where id = #{id}19
20 21 22 23
24
25 delete from links where tid = #{id} ;26
27 update teas set tname = #{tname} where id = #{id} ;28
29
30 insert into links(tid,sid) values(#{id} , #{s.id}) ;31
32
33 34
47 48
49
50
51
52
53
54
55
56

 

四.编写测试代码

1>.测试多对多代码如下:

1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.mybatis.test; 7  8 import cn.org.yinzhengjie.mybatis.domain.many2many.Stu; 9 import cn.org.yinzhengjie.mybatis.domain.many2many.Tea;10 import org.apache.ibatis.io.Resources;11 import org.apache.ibatis.session.SqlSession;12 import org.apache.ibatis.session.SqlSessionFactory;13 import org.apache.ibatis.session.SqlSessionFactoryBuilder;14 import org.junit.Test;15 16 import java.io.InputStream;17 18 /**19  * 测试多对多20  */21 public class TestMany2Many {22     @Test23     public void testInsertCustomer() throws Exception {24         InputStream in = Resources.getResourceAsStream("mybatis-config.xml");25         SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in);26         SqlSession sess = sf.openSession();27         Tea t1 = new Tea("t1");28         Tea t2 = new Tea("t2");29 30         Stu s1 = new Stu("s1");31         Stu s2 = new Stu("s2");32         Stu s3 = new Stu("s3");33         Stu s4 = new Stu("s4");34 35         t1.addStus(s1 , s2 , s3);36         t2.addStus(s2 , s3 , s4);37 38         sess.insert("teas.insert" , t1);39         sess.insert("teas.insert" , t2);40 41         sess.insert("stus.insert" , s1);42         sess.insert("stus.insert" , s2);43         sess.insert("stus.insert" , s3);44         sess.insert("stus.insert" , s4);45 46         sess.update("teas.updateLinks" , t1);47         sess.update("teas.updateLinks" , t2);48 49         sess.commit();50         sess.close();51     }52 53     @Test54     public void testDeleteTea() throws Exception {55         InputStream in = Resources.getResourceAsStream("mybatis-config.xml");56         SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in);57         SqlSession sess = sf.openSession();58         sess.delete("teas.deleteOne" , 7) ;59         sess.commit();60         sess.close();61     }62 63     /**64      * 更新65      */66     @Test67     public void testUpdate() throws Exception {68         InputStream in = Resources.getResourceAsStream("mybatis-config.xml");69         SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in);70         SqlSession sess = sf.openSession();71         Tea t1 = new Tea("tttt") ;72         t1.setId(8);73 74         Stu s1 = new Stu();75         s1.setId(13);76 77         Stu s2 = new Stu();78         s2.setId(14);79 80         t1.addStus(s1,s2);81 82         sess.update("teas.update" , t1) ;83         sess.commit();84         sess.close();85     }86 87     @Test88     public void testSelectOne() throws Exception {89         InputStream in = Resources.getResourceAsStream("mybatis-config.xml");90         SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in);91         SqlSession sess = sf.openSession();92         Tea obj = (Tea)sess.selectOne("teas.selectOne", 7);93 94         System.out.println(obj.getTname());95         sess.commit();96         sess.close();97     }98 }

 2>.统计函数和分页查询

1 
2 3 6
7
8
9
10
11
12
13
14 15
16
17
18
19
20 21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
mybatis-config.xml 文件内容
1 
2 4
5
6
9 10 11
22 23
26
StatMapper.xml 文件内容
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.mybatis.test; 7  8 import org.apache.ibatis.io.Resources; 9 import org.apache.ibatis.session.SqlSession;10 import org.apache.ibatis.session.SqlSessionFactory;11 import org.apache.ibatis.session.SqlSessionFactoryBuilder;12 import org.junit.Test;13 14 import java.io.InputStream;15 import java.util.HashMap;16 import java.util.List;17 import java.util.Map;18 19 /**20  * 测试统计21  */22 public class TestStat {23     @Test24     public void testInsert() throws Exception {25         InputStream in = Resources.getResourceAsStream("mybatis-config.xml");26         SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in);27         SqlSession sess = sf.openSession();28         int count = sess.selectOne("stats.countStus");29         List
> list = sess.selectList("stats.selectAllTeas");30 for(Map
map : list){31 for(Map.Entry
e : map.entrySet()){32 System.out.println(e.getKey() + "\t" + e.getValue());33 }34 System.out.println("========================");35 }36 System.out.println(count);37 sess.commit();38 sess.close();39 }40 41 @Test42 public void testPage() throws Exception {43 InputStream in = Resources.getResourceAsStream("mybatis-config.xml");44 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in);45 SqlSession sess = sf.openSession();46 Map
map = new HashMap
() ;47 map.put("id" , 19) ;48 map.put("offset" , 0) ;49 map.put("limit" , 2) ;50 // map.put("id" , 19) ;51 // map.put("page" , new RowBounds(2, 2)) ;52 List
objects = sess.selectList("stats.selectStusPage", map);53 System.out.println(objects);54 sess.commit();55 sess.close();56 57 }58 }

  测试的目录结构如下:

 

你可能感兴趣的文章
Makefile简介
查看>>
《前端竹节》(2)【正则表达式】
查看>>
ECMAScript 6 学习之路 ( 一 )
查看>>
完全使用 Docker 开发 PHP 项目 (三): 命令容器化
查看>>
android综合资讯App、自定义悬浮框、屏幕助手、空灵音乐源码等
查看>>
Vue全局异常捕获
查看>>
详解CommonsChunkPlugin的配置和用法
查看>>
awk 入坑指北
查看>>
Vert.x入坑须知(3)
查看>>
短网址(short URL)系统的原理及其实现
查看>>
azkaban 安装
查看>>
【290天】我爱刷题系列049(2017.11.22)
查看>>
java重写equals及hashcode方法
查看>>
【253天】我爱刷题系列(12)
查看>>
深入理解 js this 绑定 ( 无需死记硬背,尾部有总结和面试题解析 )
查看>>
ionic3 文件上传下载和预览
查看>>
[js高手之路]js单页hash路由原理与应用实战
查看>>
node之stream(上)——readable
查看>>
翻译webpack3.5.5 - code splitting - 上半部分
查看>>
Vue2+VueRouter2+webpack 构建项目实战(一):准备工作
查看>>