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 25 4.0.0 6cn.org.yinzhengjie 7Mybatis 81.0-SNAPSHOT 910 2611 15junit 12junit 134.11 1416 20mysql 17mysql-connector-java 185.1.17 1921 25org.mybatis 22mybatis 233.2.1 24
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 Listorders = 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 Liststus = 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 67 8 14 15 169 10 11 12 13 17 20 2118 19 22 3223 3124 25 3026 27 28 29 33 34 3735 36
2>.StuMapper.xml 文件内容
1 2 4 56 7 insert into stus(sname) values(#{sname}) ;8 9
3>.TeaMapper.xml 文件内容
1 2 4 56 7 insert into teas(tname) values(#{tname}) ; 8 9 1011 15 1612 insert into links(tid,sid) values(#{id}, #{s.id}) ;13 1417 delete from links where tid = #{id} ;18 delete from teas where id = #{id}19 20 21 22 2324 25 delete from links where tid = #{id} ;26 27 update teas set tname = #{tname} where id = #{id} ;28 29 33 34 47 4830 insert into links(tid,sid) values(#{id} , #{s.id}) ;31 3249 5650 51 52 5553 54
四.编写测试代码
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 67 8 14 15 169 10 11 12 13 17 20 2118 19 22 3223 3124 25 3026 27 28 29 33 34 3635
1 2 4 56 9 10 11 22 23 26
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
测试的目录结构如下: