Java基础-SSM之mybatis一对一关联
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.准备测试环境(创建数据库表)
1>.创建husbands和wifes表并建立关联关系(外键约束)
use yinzhengjie;create table husbands(id int primary key auto_increment , hname varchar(20)) ; create table wifes(id int primary key , hname varchar(20)) ;alter table wifes add constraint fk_id foreign key (id) references husbands(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>.Husband.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.one2one; 7 8 public class Husband { 9 private Integer id ;10 private String hname ;11 private Wife wife ;12 13 public Integer getId() {14 return id;15 }16 17 public void setId(Integer id) {18 this.id = id;19 }20 21 public String getHname() {22 return hname;23 }24 25 public void setHname(String hname) {26 this.hname = hname;27 }28 29 public Wife getWife() {30 return wife;31 }32 33 public void setWife(Wife wife) {34 this.wife = wife;35 }36 }
2>.Wife.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.one2one; 7 8 public class Wife { 9 private String wname ;10 private Husband husband ;11 12 public String getWname() {13 return wname;14 }15 16 public void setWname(String wname) {17 this.wname = wname;18 }19 20 public Husband getHusband() {21 return husband;22 }23 24 public void setHusband(Husband husband) {25 this.husband = husband;26 }27 }
三.编写配置文件
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
1 2 4 56 7 insert into husbands(hname) values(#{hname}) ; 8 9 10
1 2 4 56 7 insert into wifes(id , hname) values(#{husband.id} , #{wname}) ;8 9
四.编写测试代码
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.one2one.Husband; 9 import cn.org.yinzhengjie.mybatis.domain.one2one.Wife;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 TestOne2One {22 @Test23 public void testInsert() throws Exception {24 InputStream in = Resources.getResourceAsStream("mybatis-config.xml");25 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in);26 SqlSession sess = sf.openSession();27 28 Husband h1 = new Husband();29 h1.setHname("zhangjie");30 Wife w1 = new Wife();31 w1.setWname("xiena");32 h1.setWife(w1);33 w1.setHusband(h1);34 sess.insert("husbands.insert" , h1) ;35 sess.insert("wifes.insert" , w1) ;36 sess.commit();37 sess.close();38 System.out.println("插入成功");39 }40 41 }
运行以上代码查看数据库内容如下: