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


                  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 <?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>.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 <?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&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;allowMultiQueries=true” 表示开启批处理模式-->
    10         <property name="url" value="jdbc:mysql://localhost:5200/yinzhengjie?characterEncoding=utf-8&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;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.Husband"     alias="_Husband" />
    18         <typeAlias type="cn.org.yinzhengjie.mybatis.domain.one2one.Wife"        alias="_Wife" />
    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="HusbandMapper.xml"/>
    35         <mapper resource="WifeMapper.xml"/>
    36     </mappers>
    37 </configuration>
    mybatis-config.xml 文件内容
     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="husbands">
     6     <insert id="insert" useGeneratedKeys="true" keyProperty="id">
     7         insert into husbands(hname) values(#{hname}) ;
     8     </insert>
     9 
    10 </mapper>
    HusbandMapper.xml 文件内容
    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="wifes">
    6     <insert id="insert">
    7         insert into wifes(id , hname) values(#{husband.id} , #{wname}) ;
    8     </insert>
    9 </mapper>
    WifeMapper.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 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     @Test
    23     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 }

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

  • 相关阅读:
    赫夫曼树JAVA实现及分析
    JAVA多线程之wait/notify
    JAVA多线程之volatile 与 synchronized 的比较
    JAVA多线程之线程间的通信方式
    并查集与贪心算法的应用之求解无向图的最小生成树
    Android自定义图形,图形的拼接、叠加、相容
    CSDN问答频道“华章杯”7月排行榜活动开始,丰厚奖品等你拿
    修改MyEclipse内存-------OutOfMemoryError错误
    MySQL中MySQL X.X Command Line Client一闪而过的问题
    C++组合问题
  • 原文地址:https://www.cnblogs.com/yinzhengjie/p/9287322.html
Copyright © 2020-2023  润新知