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

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

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>.目录结构如下:

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

 

二.编写自定义类

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 文件内容

相关文章:

  • 2022-01-17
  • 2021-10-13
  • 2022-01-08
  • 2021-08-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-01
  • 2021-07-26
  • 2021-09-09
  • 2021-11-06
  • 2022-02-18
  • 2022-12-23
  • 2022-02-07
相关资源
相似解决方案