第一节:一对一关系实现

需要实现一对一的关系,首先我们有两张表,t-addree和t_student。

 CREATE TABLE `t_address` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sheng` varchar(20) DEFAULT NULL,
`shi` varchar(20) DEFAULT NULL,
`qu` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
 insert  into `t_address`(`id`,`sheng`,`shi`,`qu`) values (1,'江苏省','苏州市','姑苏区'),(2,'江苏省','南京市','鼓楼区');
 CREATE TABLE `t_student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`addressId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
 insert  into `t_student`(`id`,`name`,`age`,`addressId`) values (1,'张三',10,1),(2,'李四',11,2),(3,'李四',11,2),(4,'李四',11,2),(5,'李四',11,2),(6,'李四',11,2),(7,'李四',11,2);

然后写model层

Address.java

 package com.javaxk.model;

 public class Address {

     private Integer id;
private String sheng;
private String shi;
private String qu; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSheng() {
return sheng;
}
public void setSheng(String sheng) {
this.sheng = sheng;
}
public String getShi() {
return shi;
}
public void setShi(String shi) {
this.shi = shi;
}
public String getQu() {
return qu;
}
public void setQu(String qu) {
this.qu = qu;
}
@Override
public String toString() {
return "Address [id=" + id + ", sheng=" + sheng + ", shi=" + shi
+ ", qu=" + qu + "]";
} }

Student.java

 package com.javaxk.model;

 public class Student {

     private Integer id;
private String name;
private Integer age;
private Address address; public Student() {
super();
// TODO Auto-generated constructor stub
} public Student(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
} public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
} public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Address getAddress() {
return address;
} public void setAddress(Address address) {
this.address = address;
} @Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", address=" + address + "]";
} }

mappers映射类

AddressMapper.java

 package com.javaxk.mappers;

 import com.javaxk.model.Address;

 public interface AddressMapper {

     public Address findById(Integer id);

 }

StudentMapper.java

 package com.javaxk.mappers;

 import java.util.List;

 import com.javaxk.model.Student;

 public interface StudentMapper {

     public Student findStudentWithAddress(Integer id);
}

主程序运行类

StudentTest3.java

 package com.javaxk.service;

 import java.util.List;

 import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.javaxk.mappers.StudentMapper;
import com.javaxk.model.Student;
import com.javaxk.util.SqlSessionFactoryUtil; public class StudentTest3 { private static Logger logger=Logger.getLogger(StudentTest.class);
private SqlSession sqlSession=null;
private StudentMapper studentMapper=null; /**
* 测试方法前调用
* @throws Exception
*/
@Before
public void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
studentMapper=sqlSession.getMapper(StudentMapper.class);
} /**
* 测试方法后调用
* @throws Exception
*/
@After
public void tearDown() throws Exception {
sqlSession.close();
} @Test
public void testFindStudentWithAddress() {
logger.info("查询学生(带地址)");
Student student=studentMapper.findStudentWithAddress(1);
System.out.println(student);
} }

Mapper映射文件的不同写法。。。

AddressMapper.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="com.javaxk.mappers.AddressMapper"> <resultMap type="Address" id="AddressResult">
<result property="id" column="id"/>
<result property="sheng" column="sheng"/>
<result property="shi" column="shi"/>
<result property="qu" column="qu"/>
</resultMap> <select id="findById" parameterType="Integer" resultType="Address">
select * from t_address where id=#{id}
</select> </mapper>

第一种方式:

 <?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="com.javaxk.mappers.StudentMapper"> <resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/> <result property="address.id" column="addressId"/>
<result property="address.sheng" column="sheng"/>
<result property="address.shi" column="shi"/>
<result property="address.qu" column="qu"/>
</resultMap>
<select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
</select>
</mapper>

第二种方式:

 <?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="com.javaxk.mappers.StudentMapper">
<resultMap type="Address" id="AddressResult">
<result property="id" column="id"/>
<result property="sheng" column="sheng"/>
<result property="shi" column="shi"/>
<result property="qu" column="qu"/>
</resultMap> <resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" resultMap="AddressResult"/>
</resultMap> <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
</select>
</mapper>

第三种方式:

 <?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="com.javaxk.mappers.StudentMapper">
<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" javaType="Address">
<result property="id" column="id"/>
<result property="sheng" column="sheng"/>
<result property="shi" column="shi"/>
<result property="qu" column="qu"/>
</association>
</resultMap> <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
</select> </mapper>

第四种方式:

 <?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="com.javaxk.mappers.StudentMapper">
<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" column="id" select="com.javaxk.mappers.AddressMapper.findById"></association>
</resultMap> <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
</select> </mapper>

第二节:一对多关系实现

StudentMapper.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="com.javaxk.mappers.StudentMapper"> <resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" column="addressId" select="com.javaxk.mappers.AddressMapper.findById"></association>
<association property="grade" column="gradeId" select="com.javaxk.mappers.GradeMapper.findById"></association>
</resultMap> <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
</select> </mapper>

AddressMapper.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="com.javaxk.mappers.AddressMapper"> <resultMap type="Address" id="AddressResult">
<result property="id" column="id"/>
<result property="sheng" column="sheng"/>
<result property="shi" column="shi"/>
<result property="qu" column="qu"/>
</resultMap> <select id="findById" parameterType="Integer" resultType="Address">
select * from t_address where id=#{id}
</select> </mapper>

GradeMapper.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="com.javaxk.mappers.GradeMapper"> <resultMap type="Grade" id="GradeResult">
<result property="id" column="id"/>
<result property="gradeName" column="gradeName"/> </resultMap> <select id="findById" parameterType="Integer" resultType="Grade">
select * from t_grade where id=#{id}
</select> </mapper>

测试主类:

     @Test
public void testFindStudentWithGrade(){
logger.info("查询学生(带年级)");
Student student=studentMapper.findStudentWithAddress(1);
System.out.println(student);
}

(四)MyBatis关系映射的更多相关文章

  1. MyBatis 关系映射XML配置

    关系映射 在我看来这些实体类就没啥太大关联关系,不就是一个sql语句解决的问题,直接多表查询就完事,程序将它设置关联就好 xml里面配置也是配置了sql语句,下面给出几个关系的小毛驴(xml) 一对多 ...

  2. hibernate学习四(关系映射一对一与组件映射)

    一.关系映射简介 在数据库中,表与表的关系,仅有外键.但使用hibernate后,为面向对象的编程,对象与对象的关系多样化:如 一对一,一对多,多对多,并具有单向和双向之分. 开始练习前,复制上一次项 ...

  3. Hibernate学习笔记(四)关系映射之一对一关联映射

    一. 一对一关联映射 ²        两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ²        有两种策略可以实现一对一的关联映射 Ø        主键关联:即让 ...

  4. Mybatis关系映射

    一.一对一关系映射 使用resultType+包装类实现 1.假设问题背景是要求在某一个购物平台的后台程序中添加一个这样的功能:查询某个订单的信息和下该订单的用户信息.首先我们可以知道,一般这样的平台 ...

  5. Hibernate基础学习(四)—对象-关系映射(上)

    一.映射对象标识符      Java语言按内存地址来识别或区分同一个类的不同对象,而关系数据库按主键值来识别或区分同一个表的不同记录.Hibernate使用对象标识符(OID)来建立内存中的对象和数 ...

  6. mybatis 关系映射

    一:订单商品数据模型 1.数据库执行脚本 创建数据库表代码: 1 CREATE TABLE items ( 2 id INT NOT NULL AUTO_INCREMENT, 3 itemsname ...

  7. Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6878529.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(三)——My ...

  8. mybatis关系映射之一对多和多对一

    本实例使用用户(User)和博客(Post)的例子做说明: 一个用户可以有多个博客, 一个博客只对应一个用户 一. 例子(本实体采用maven构建): 1. 代码结构图: 2. 数据库: t_user ...

  9. Java基础-SSM之mybatis一对多和多对一关系映射

    Java基础-SSM之mybatis一对多和多对一关系映射 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备测试环境(创建数据库表)  1>.创建customers表: ...

随机推荐

  1. laravel 数据库迁移转 sql 语句

    可以使用下面的命令 php artisan migrate --pretend --no-ansi 当然,你需要有可以 migrate 的东西. 数据库迁移导出到文件(使用命令) <?php n ...

  2. R画图

    画图函数中的参数: 1.图形元素参数: pch:用于显示点的坐标,可以是一个字符,也可以是0到25的一个整数.如:pch=“+”,pch=1 lty:线条类型.如:lty=2,lty=1 lwd:线条 ...

  3. Chapter 6(树)

    1.树的储存方式 //****************双亲表示法************************ #define Max_TREE_SIZE 100 typedef int TElem ...

  4. pycharm 取消自动保存

    pycharm默认是自动保存的,习惯自己按 ctrl + s 的可以进行如下设置: 菜单File -> Settings... -> Appearance & Behavior - ...

  5. Java基础-程序流程控制第一弹(分支结构/选择结构)

    Java基础-程序流程控制第一弹(分支结构/选择结构) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.if语句 1>.if语句的第一种格式 if(条件表达式){ 语句体: ...

  6. 视音频数据处理入门:UDP-RTP协议解析

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  7. Vue.js学习笔记(一)

    其它前端主流框架可以做的事,Vue.js几乎都可以做, nuxt框架:可以实现vue的服务器端渲染, weex:使用vue语法编写原生app. 基础内容 基础语法 MVVM设计模式 前端组件化 vue ...

  8. [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  9. javascript精雕细琢(二):++、--那点事

    目录 引言 ++和--在数学运算中的计算规则 ++和--在变量引用时的计算规则 ++和--的数据转换应用 引言 对于接触JS时间不长的前端来说,刚开始要实现诸如轮播图,选项卡等小模块时,肯定会用到in ...

  10. webApp 页面适配布局

    webApp 页面布局 1. 流式布局 概念: 流式布局是页面元素宽度按照屏幕分辨率进行适配调整,但是整体布局不变. 设计方法: 布局都是通过百分比来定义宽度,但是高度大都是用px固定的. 弊端: 虽 ...