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

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

                              作者:尹正杰

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

一.准备测试环境(创建数据库表)文章地址https://www.yii666.com/article/758259.html

 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) ;

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

2>.添加Maven依赖

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.org.yinzhengjie</groupId>
<artifactId>Mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
</project>

3>.目录结构如下:文章来源地址:https://www.yii666.com/article/758259.html

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

二.编写自定义类

1>.Husband.java 文件内容

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.mybatis.domain.one2one; public class Husband {
private Integer id ;
private String hname ;
private Wife wife ; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getHname() {
return hname;
} public void setHname(String hname) {
this.hname = hname;
} public Wife getWife() {
return wife;
} public void setWife(Wife wife) {
this.wife = wife;
}
}

2>.Wife.java 文件内容文章来源地址https://www.yii666.com/article/758259.html

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.mybatis.domain.one2one; public class Wife {
private String wname ;
private Husband husband ; public String getWname() {
return wname;
} public void setWname(String wname) {
this.wname = wname;
} public Husband getHusband() {
return husband;
} public void setHusband(Husband husband) {
this.husband = husband;
}
}

三.编写配置文件网址:yii666.com<网址:yii666.com

 <?xml version="1.0" encoding="UTF-8" ?>

 <!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--注意 : “?characterEncoding=utf-8&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;allowMultiQueries=true” 表示开启批处理模式-->
<property name="url" value="jdbc:mysql://localhost:5200/yinzhengjie?characterEncoding=utf-8&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;allowMultiQueries=true"/>
<property name="username" value="root"/>
<property name="password" value="yinzhengjie"/>
</properties> <!-- 我们使用typeAliases标签给我们自定义类起个别名。-->
<typeAliases>
<typeAlias type="cn.org.yinzhengjie.mybatis.domain.one2one.Husband" alias="_Husband" />
<typeAlias type="cn.org.yinzhengjie.mybatis.domain.one2one.Wife" alias="_Wife" />
</typeAliases> <environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 我们使用mapper标签指定映射文件,使用resource指定具体的路径,如果没有写绝对路径,默认的根路径就在resources目录中-->
<mapper resource="HusbandMapper.xml"/>
<mapper resource="WifeMapper.xml"/>
</mappers>
</configuration>

mybatis-config.xml 文件内容

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 定义名字空间 -->
<mapper namespace="husbands">
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into husbands(hname) values(#{hname}) ;
</insert> </mapper>

HusbandMapper.xml 文件内容

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 定义名字空间 -->
<mapper namespace="wifes">
<insert id="insert">
insert into wifes(id , hname) values(#{husband.id} , #{wname}) ;
</insert>
</mapper>

WifeMapper.xml 文件内容

四.编写测试代码

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.mybatis.test; import cn.org.yinzhengjie.mybatis.domain.one2one.Husband;
import cn.org.yinzhengjie.mybatis.domain.one2one.Wife;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import java.io.InputStream; /**
* 测试一对一
*/
public class TestOne2One {
@Test
public void testInsert() throws Exception {
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in);
SqlSession sess = sf.openSession(); Husband h1 = new Husband();
h1.setHname("zhangjie");
Wife w1 = new Wife();
w1.setWname("xiena");
h1.setWife(w1);
w1.setHusband(h1);
sess.insert("husbands.insert" , h1) ;
sess.insert("wifes.insert" , w1) ;
sess.commit();
sess.close();
System.out.println("插入成功");
} }

   运行以上代码查看数据库内容如下:

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

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

Java基础-SSM之mybatis一对一关联-相关文章

  1. Mybatis表关联多对一

  2. Mybatis表关联一对多、多对一、多对多

  3. 04—mybatis的关联映射

  4. mybatis 一对一关联 association 返回空值

  5. Java基础-SSM之mybatis一对一关联

  6. mybatis动态拼接条件的技巧 where 1=1 或者where标签

    /**     * 根据输入的学生信息进行条件检索     * 1. 当只输入用户名时, 使用用户名进行模糊检索;     * 2. 当只输入邮箱时, 使用性别进行完全匹配     * 3. 当用户名和性别都存在时, 用这两个条件进行查询匹配的用     * @param student     * @return 

  7. mybatis collection的使用

    Mybatis collection的使用今天学习了mybatis中的collection使用,作为记录以后使用。首先看一下javabean的结构!public class Article{    private User user;    private String name;    private int id;    private Date time;public class User{    private int id;    private String name;    private ListArticle article;用户

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png