Java基础-SSM之mybatis一对一外键关联
Java基础-SSM之mybatis一对一外键关联
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.准备测试环境(创建数据库表)
1>.创建husbandsfk和wifesfk表
1 use yinzhengjie; 2 3 create table husbandsfk(id int primary key auto_increment , hname varchar(20)) ; 4 5 create table wifesfk(id int primary key auto_increment , wname varchar(20) , hid int , CONSTRAINT fk_hid FOREIGN KEY (hid) references husbandsfk(id)) ; 6 7 alter table wifesfk add constraint unq_hid unique (hid) ;
2>.添加Maven依赖
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <groupId>cn.org.yinzhengjie</groupId> 7 <artifactId>Mybatis</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 <dependencies> 10 <dependency> 11 <groupId>junit</groupId> 12 <artifactId>junit</artifactId> 13 <version>4.11</version> 14 </dependency> 15 <dependency> 16 <groupId>mysql</groupId> 17 <artifactId>mysql-connector-java</artifactId> 18 <version>5.1.17</version> 19 </dependency> 20 <dependency> 21 <groupId>org.mybatis</groupId> 22 <artifactId>mybatis</artifactId> 23 <version>3.2.1</version> 24 </dependency> 25 </dependencies> 26 </project>
3>.目录结构如下:
二.编写自定义类
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.fk; 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 }
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.fk; 7 8 public class Wife { 9 private Integer id ; 10 private String wname ; 11 private Husband husband ; 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 getWname() { 22 return wname; 23 } 24 25 public void setWname(String wname) { 26 this.wname = wname; 27 } 28 29 public Husband getHusband() { 30 return husband; 31 } 32 33 public void setHusband(Husband husband) { 34 this.husband = husband; 35 } 36 }
三.编写配置文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <!DOCTYPE configuration 4 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 5 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 6 <configuration> 7 <properties> 8 <property name="driver" value="com.mysql.jdbc.Driver"/> 9 <!--注意 : “?characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true” 表示开启批处理模式--> 10 <property name="url" value="jdbc:mysql://localhost:5200/yinzhengjie?characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true"/> 11 <property name="username" value="root"/> 12 <property name="password" value="yinzhengjie"/> 13 </properties> 14 15 <!-- 我们使用typeAliases标签给我们自定义类起个别名。--> 16 <typeAliases> 17 <typeAlias type="cn.org.yinzhengjie.mybatis.domain.one2one.fk.Husband" alias="_HusbandFK" /> 18 <typeAlias type="cn.org.yinzhengjie.mybatis.domain.one2one.fk.Wife" alias="_WifeFK" /> 19 </typeAliases> 20 21 <environments default="development"> 22 <environment id="development"> 23 <transactionManager type="JDBC"/> 24 <dataSource type="POOLED"> 25 <property name="driver" value="${driver}"/> 26 <property name="url" value="${url}"/> 27 <property name="username" value="${username}"/> 28 <property name="password" value="${password}"/> 29 </dataSource> 30 </environment> 31 </environments> 32 <mappers> 33 <!-- 我们使用mapper标签指定映射文件,使用resource指定具体的路径,如果没有写绝对路径,默认的根路径就在resources目录中--> 34 <mapper resource="HusbandFKMapper.xml"/> 35 <mapper resource="WifeFKMapper.xml"/> 36 </mappers> 37 </configuration>
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <!-- 定义名字空间 --> 5 <mapper namespace="husbandsfk"> 6 <insert id="insert" useGeneratedKeys="true" keyProperty="id"> 7 insert into husbandsfk(hname) values(#{hname}) ; 8 </insert> 9 10 <select id="selectOne" resultMap="rm_Husband"> 11 select 12 h.id hid, 13 h.hname hhname , 14 w.id wid , 15 w.wname wwname , 16 w.hid whid 17 from 18 husbandsfk h left outer join wifesfk w on w.hid = h.id 19 where 20 h.id = #{id} 21 </select> 22 23 <resultMap id="rm_Husband" type="_HusbandFK"> 24 <id column="hid" property="id"/> 25 <result column="hhname" property="hname" /> 26 <association property="wife" javaType="_WifeFK" column="whid"> 27 <id column="wid" property="id"/> 28 <result column="wwname" property="wname"/> 29 <association property="husband" javaType="_HusbandFk" column="whid"> 30 <id column="hid" property="id"/> 31 <result column="hhname" property="hname"/> 32 </association> 33 </association> 34 </resultMap> 35 </mapper>
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <!-- 定义名字空间 --> 5 <mapper namespace="wifesfk"> 6 <insert id="insert" useGeneratedKeys="true" keyProperty="id"> 7 insert into wifesfk(wname , hid) values(#{wname} , #{husband.id}) ; 8 </insert> 9 10 </mapper>
四.编写测试代码
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.fk.Husband; 9 import cn.org.yinzhengjie.mybatis.domain.one2one.fk.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 TestOne2OneFK { 22 /** 23 * 测试插入 24 */ 25 @Test 26 public void testInsert() throws Exception { 27 InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); 28 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in); 29 SqlSession sess = sf.openSession(); 30 Husband h1 = new Husband(); 31 h1.setHname("liyapeng"); 32 Wife w1 = new Wife(); 33 w1.setWname("wangfei"); 34 h1.setWife(w1); 35 w1.setHusband(h1); 36 37 sess.insert("husbandsfk.insert" , h1) ; 38 sess.insert("wifesfk.insert" , w1) ; 39 40 sess.commit(); 41 sess.close(); 42 System.out.println("插入成功!"); 43 } 44 45 46 /** 47 * 测试查询 48 */ 49 @Test 50 public void testSelectOne() throws Exception { 51 InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); 52 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in); 53 SqlSession sess = sf.openSession(); 54 Husband husband = (Husband) sess.selectOne("husbandsfk.selectOne", 1); 55 System.out.println(husband.getHname()); 56 sess.commit(); 57 sess.close(); 58 } 59 }
运行以上代码查看数据库内容如下:
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/9287302.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。