MapperAsso.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="bean.User">
<!--一对一关联-->
<select id="selectOneAssoObj" parameterType="int" resultMap="OneAssoObjRM">
select * from user where id = #{id}
</select>
<resultMap type="association.AssoObj" id="OneAssoObjRM">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="salary" column="name" select="selectOneSalary"/>
<!--一对多关联配置备注-->
<!--<collection property="salarys" ofType="bean.Salary" column="name" select="selectFewSalarys">-->
</resultMap> <select id="selectOneSalary" parameterType="String" resultType="bean.Salary">
select * from salary where name = #{name}
</select> <!--id:名称; parameterType传入参数类型; resultType:返回数据类型-->
<select id="selectOneUser" parameterType="int" resultType="bean.User">
select * from user where id = #{id}
</select> <!--批量查询-->
<select id="selectAllUser" resultMap="userList">
select * from user
</select>
<resultMap type="first.UserBean" id="userList">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap> </mapper>

AssoObj.java

 package association;

 import bean.Salary;

 /**
* 数据封装类
* user class with Salary object
* 一对一关联Javabean
*/
public class AssoObj {
private int id;
private String name;
private String age;
private Salary salary; public AssoObj() { } public AssoObj(String name, String age) {
this.name = name;
this.age = age;
} public AssoObj(int id, String name, String age, Salary salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public Salary getSalary() {
return salary;
} public void setSalary(Salary salary) {
this.salary = salary;
} @Override
public String toString() {
return "AssoObj{id:" + id + "; 姓名:" + name +
"; 年龄:" + age + "; Salary对象:" + salary + "}";
}
}

TestAsso.java

 package association;

 import bean.Salary;
import bean.User;
import bean.UserSalary;
import first.UserBean;
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.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import zhujie.UserMapperInterface; import java.io.IOException;
import java.io.InputStream;
import java.util.List; /*
* 一对一关联举例
*
* */
public class TestAsso {
String resource = "mybatis-config-assosciation.xml";
SqlSessionFactory sqlSessionFactory = null;
SqlSession session = null; @Before
public void before() {
// System.out.println("Before");
try {
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 创建session对象
session = sqlSessionFactory.openSession(); } catch (IOException e) {
e.printStackTrace();
}
} @After
public void close() {
session.close();
// System.out.println("After");
} // 一对一关联测试用例
@Test
public void testSelectOneAssoObj() {
AssoObj assoObj = session.selectOne("bean.User.selectOneAssoObj", 1);
System.out.println("查询单个AssoObj:" + assoObj);
} @Test
public void testSelectOneUser() {
User user = session.selectOne("bean.User.selectOneUser", 1);
System.out.println("查询单个User:" + user);
} @Test
public void testSelectAllUser() {
List<User> listUser = session.selectList("bean.User.selectAllUser");
System.out.println("记录个数:" + listUser.size());
System.out.println(listUser);
} @Test
public void testSelectOneSalary() {
Salary salary = session.selectOne("bean.User.selectOneSalary", "Tom");
System.out.println("查询单个Salary:" + salary);
} }

mybatis-config-assosciation.xml

 <?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>
<!--从外部配置文件导入jdbc信息-->
<properties resource="jdbc.properties"></properties> <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="association/MapperAsso.xml"/> </mappers>
</configuration>

mybatis之一对一关联的更多相关文章

  1. MyBatis:一对一关联查询

    MyBatis从入门到放弃三:一对一关联查询 前言 简单来说在mybatis.xml中实现关联查询实在是有些麻烦,正是因为起框架本质是实现orm的半自动化. 那么mybatis实现一对一的关联查询则是 ...

  2. Mybatis学习——一对一关联表查询

    1.SQL语句建表 CREATE TABLE teacher( t_id ) ); CREATE TABLE class( c_id ), teacher_id INT ); ALTER TABLE ...

  3. mybatis 的一对一关联查询association

    现在项目的列表查询数据需要查一个总数count, 如果直接写在同一个sql里面,会导致查询速度很慢, 因此,想到使用关联查询,例子如下: 附上代码: 其中遇到的坑哟: 1.association中的s ...

  4. mybatis 一对一关联映射实例

    在实际项目开发中,经常存在一对一的关系,如一个人对应一张身份证信息,这就是一对一的关系.下面是一个简单的实例: 1.建表过程我就省略了,主要是一张Person表,一张IDCard表,其相关属性见步骤2 ...

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

    mybatis 一对一关联 association 返回空值 最近学习spring mvc + mybatis开发,看的书是<Spring MVC+Mybatis开发 从入门到精通>,在学 ...

  6. MyBatis从入门到放弃三:一对一关联查询

    前言 简单来说在mybatis.xml中实现关联查询实在是有些麻烦,正是因为起框架本质是实现orm的半自动化. 那么mybatis实现一对一的关联查询则是使用association属性和resultM ...

  7. Mybatis学习4——一对一关联查询方法2------实体作为属性

    实体order和user采用resultMap order package pojo; import java.util.Date; public class Order { private Inte ...

  8. Mybatis学习4——一对一关联查询方法1--创建实体

    创建一个实体继承两个实体之一,另一个实体作为属性 实体1. order package pojo; import java.util.Date; public class Order { privat ...

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

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

随机推荐

  1. Python基础(9)——类

    类的语法 类的定义 class Dog(object): print("hello,I am a dog!") d = Dog() #实例化这个类, #此时的d就是类Dog的实例化 ...

  2. Jmeter—开篇

    Jmeter以开源.轻便著称,做接口测试.性能测试都可以借助Jmeter,从这篇开始记录我使用到的Jmeter功能. 安装 Jmeter官网:http://jmeter.apache.org/ 去官网 ...

  3. jqgrid 加入右键菜单按钮管理

    除了在表格底部添加自定义按钮外,还可以通过设置右键菜单按钮来添加自定义事件.看下图: 如何实现以上功能? 1)引入ContextMenu插件 2)创建一个函数用于初始化右键菜单(本示例取名为 init ...

  4. Ubuntu学习总结-01 安装Ubuntu

    Ubuntu(友帮拓.优般图.乌班图)是一个以桌面应用为主的开源GNU/Linux操作系统,Ubuntu 是基于Debian GNU/Linux,支持x86.amd64(即x64)和ppc架构,由全球 ...

  5. WPF EventTrigger,BeginStoryboard

    <Window x:Class="WpfApplication2.LoginWind" xmlns="http://schemas.microsoft.com/wi ...

  6. 2017-2018-2 20155231《网络对抗技术》实验八: WEB基础实验

    2017-2018-2 20155231<网络对抗技术>实验八:Web基础 实验要求: Web前端HTML(0.5分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与P ...

  7. 汇编 REPNE/REPNZ 指令,SCASB 指令

    知识点: REPNE/REPNZ 指令 SCASB 指令 一.SCASB 指令 cmp byte ptr [edi],al //对标志位的影响相当于sub指令 //同时还会修改寄存器EDI的值:如 ...

  8. 洛谷 P3302 [SDOI2013]森林

    ->题目链接 题解: #include<queue> #include<cstdio> #include<cstring> #include<iostr ...

  9. EAS_BI(扩展报表)

    case when 的使用 1. 扩展报表,一张收费单据中,下面分为分录 问题描述: 收费单中有一个分录用于记录检测的项目名称以及标准费用.收费单有自己的主键,分录中的外键即是收费单的主键,然后分录表 ...

  10. JS跨浏览器的事件处理

    1. 事件流 事件:用户或浏览器自身执行的某种动作.如click(点击事件).mouse***(鼠标事件). 事件流:页面中接收事件的顺序,也可理解为事件在页面中传播的顺序. DOM事件流包括三个阶段 ...