场景:一个人有多个手机号,一个手机号对应一个人

CREATE TABLE test.mobile
(
mid INT NOT NULL auto_increment,
tel VARCHAR (50),
pid INT,
PRIMARY KEY (mid)
); CREATE TABLE test.person
(
pid INT NOT NULL auto_increment,
name VARCHAR (50),
PRIMARY KEY (pid)
);
INSERT INTO test.person (name)
VALUES ('wjf');
INSERT INTO test.mobile (tel, pid)
VALUES ('', 1); INSERT INTO test.mobile (tel, pid)
VALUES ('', 1);

先上个配置文件

<?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="wjf.dao.MobileMapper">
<resultMap type="wjf.Obj.Mobile" id="resultMap">
<id property="mid" column="mid" />
<result property="tel" column="tel" />
<association property="person" javaType="wjf.Obj.Person"
column="pid">
<id property="pid" column="pid" />
<result property="name" column="name" />
</association>
</resultMap>
<select id="getMobile" parameterType="int" resultMap="resultMap">
select
p.pid,p.name ,m.mid ,m.tel
from person p ,mobile m where m.pid=p.pid
AND m.mid=#{id}
</select>
<select id="getALLMobile" resultMap="resultMap">
select
p.pid,p.name ,m.mid
,m.tel
from person p ,mobile m where m.pid=p.pid
</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="wjf.dao.PersonMapper">
<resultMap type="wjf.Obj.Person" id="resultMap">
<id property="pid" column="pid" />
<result property="name" column="name" />
<collection property="mobiles" column="pid" ofType="wjf.Obj.Mobile">
<id property="id" column="mid" />
<result property="tel" column="tel" />
</collection>
</resultMap> <select id="getPerson" parameterType="int" resultMap="resultMap">
select
p.pid,p.name ,m.mid ,m.tel
from person p ,mobile m where m.pid=p.pid
AND p.pid=#{id}
</select>
<select id="getALLPerson" resultMap="resultMap">
select
p.pid,p.name ,m.mid
,m.tel
from person p ,mobile m where m.pid=p.pid
</select>
</mapper>

再上个对象

package wjf.Obj;

import java.util.List;

public class Person {
private int pid;
private String name;
private List<Mobile> mobiles;
public List<Mobile> getMobiles() {
return mobiles;
}
public void setMobiles(List<Mobile> mobiles) {
this.mobiles = mobiles;
}
public int getId() {
return pid;
}
public void setId(int id) {
this.pid = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + pid + ", name=" + name + "]";
}
}
package wjf.Obj;

public class Mobile {
private int mid;
private String tel;
private Person person;
/**
* @return the mid
*/
public int getMid() {
return mid;
}
/**
* @param mid the mid to set
*/
public void setMid(int mid) {
this.mid = mid;
}
/**
* @return the person
*/
public Person getPerson() {
return person;
}
/**
* @param person the person to set
*/
public void setPerson(Person person) {
this.person = person;
}
public int getId() {
return mid;
}
public void setId(int id) {
this.mid = id;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
@Override
public String toString() {
return "Mobile [id=" + mid + ", tel=" + tel + "]";
}
}

上个映射类

package wjf.dao;

import java.util.List;

import wjf.Obj.Mobile;

public interface MobileMapper {
public Mobile getMobile(int id); public List<Mobile> getALLMobile();
}
package wjf.dao;

import java.util.List;
import wjf.Obj.Person; public interface PersonMapper {
public Person getPerson(int id); public List<Person> getALLPerson();
}

最后就是mybatis的配置了

<?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>
<typeAliases>
<typeAlias alias="UserInfo" type="wjf.orm.UserInfo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- <mapper resource="wjf/orm/userMapper.xml"/> -->
<!-- <mapper resource="wjf/orm/userMapper2.xml"/> -->
<mapper resource="wjf/config/MobileMapper.xml"/>
<mapper resource="wjf/config/PersonMapper.xml"/>
</mappers>
</configuration>

测试程序如下

    @Test
public void testRelation() {
SqlSessionFactory sqlSessionFactory = MybatisUtil
.getSqlsessionfactory();
SqlSession sqlSession = null;
sqlSession = sqlSessionFactory.openSession();
// PersonMapper pmapper = sqlSession.getMapper(PersonMapper.class);
// List<Person> plist = pmapper.getALLPerson();
// System.out.println(plist.size());
// Person p = pmapper.getPerson(1);
// System.out.println(p.getMobiles().size()); MobileMapper mmapper = sqlSession.getMapper(MobileMapper.class);
List<Mobile> pMobile = mmapper.getALLMobile();
System.out.println(pMobile.size());
Mobile m = mmapper.getMobile(1);
System.out.println(m.getPerson().getName());
}

补上一个mybatis封装类

package wjf.orm;

import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; //工具类
public class MybatisUtil {
public static SqlSessionFactory sqlSessionFactory() { String resource = "config.xml";// 获取mybatis-cofig.xml配置文件的路径
Reader reader = null;// 创建reader
try {
reader = Resources.getResourceAsReader(resource);// 获得reader
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
SqlSessionFactory SqlSessionFactory = new SqlSessionFactoryBuilder()
.build(reader);// 获得sqlsessionfactory
return SqlSessionFactory;
} // 获取getter
public static SqlSessionFactory getSqlsessionfactory() {
return sqlSessionFactory();
} }

mybatis关联查询的更多相关文章

  1. Mybatis关联查询和数据库不一致问题分析与解决

    Mybatis关联查询和数据库不一致问题分析与解决 本文的前提是,确定sql语句没有问题,确定在数据库中使用sql和项目中结果不一致. 在使用SpringMVC+Mybatis做多表关联时候,发现也不 ...

  2. MyBatis基础:MyBatis关联查询(4)

    1. MyBatis关联查询简介 MyBatis中级联分为3中:association.collection及discriminator. ◊ association:一对一关联 ◊ collecti ...

  3. MyBatis关联查询,一对多关联查询

    实体关系图,一个国家对应多个城市 一对多关联查询可用三种方式实现: 单步查询,利用collection标签为级联属性赋值: 分步查询: 利用association标签进行分步查询: 利用collect ...

  4. mybatis 关联查询实现一对多

    场景:最近接到一个项目是查询管理人集合  同时每一个管理人还存在多个出资人   要查询一个管理人列表  每个管理人又包含了出资人列表 采用mybatis关联查询实现返回数据. 实现方式: 1 .在实体 ...

  5. MyBatis关联查询、多条件查询

    MyBatis关联查询.多条件查询 1.一对一查询 任务需求; 根据班级的信息查询出教师的相关信息 1.数据库表的设计 班级表: 教师表: 2.实体类的设计 班级表: public class Cla ...

  6. Mybatis关联查询之二

    Mybatis关联查询之多对多 多对多 一.entity实体类 public class Student { private Integer stuid; private String stuname ...

  7. mybatis关联查询基础----高级映射

    本文链接地址:mybatis关联查询基础----高级映射(一对一,一对多,多对多) 前言: 今日在工作中遇到了一个一对多分页查询的问题,主表一条记录对应关联表四条记录,关联分页查询后每页只显示三条记录 ...

  8. MyBatis关联查询和懒加载错误

    MyBatis关联查询和懒加载错误 今天在写项目时遇到了个BUG.先说一下背景,前端请求更新生产订单状态,后端从前端接收到生产订单ID进行查询,然后就有问题了. 先看控制台报错: org.apache ...

  9. Mybatis关联查询(嵌套查询)

    上一篇文章介绍了基于Mybatis对数据库的增.删.改.查.这一篇介绍下关联查询(join query). 三张表:user article blog 表的存储sql文件: /* Navicat My ...

  10. MyBatis关联查询 (association) 时遇到的某些问题/mybatis映射

    先说下问题产生的背景: 最近在做一个用到MyBatis的项目,其中有个业务涉及到关联查询,我是将两个查询分开来写的,即嵌套查询,个人感觉这样更方便重用: 关联的查询使用到了动态sql,在执行查询时就出 ...

随机推荐

  1. for循环中使用了return

    for循环中使用了return,导致没有循环完毕就结束了整个方法的执行.

  2. Android自定义下拉刷新

    网上的下拉刷新功能很多,不过基本上都是隐藏header的,而项目里面需要只隐藏部分的header,类似QQ好友动态的效果,修改了一些现有的,最后有很多问题,所以就自己自定义了一个,逻辑也很简单,首先就 ...

  3. 在virtual pc中搭建基于ubuntu 的git环境

    1. 在virtual pc 上安装 ubuntu http://www.hanselman.com/blog/InstallingUbuntu104LTSOnWindowsVirtualPCOnWi ...

  4. java的向下转型

    class A{ public void fun1(){ System.out.println("A-->public void fun1()"); } public voi ...

  5. UIPickerView用法(左右比例,整体大小,字体大小)

    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero]; pickerView.autoresizingM ...

  6. 修改Oracle Client的字符集

    修改Oracle Client的字符集 找到注册表 HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_OraDb10g_home1下的 NLS_LANG,修改键值为SIMP ...

  7. kettle參数、变量具体解说

    kettle參数.变量具体解说 kettle 3.2 曾经的版本号里仅仅有 variable 和 argument,kettle 3.2 中.又引入了 parameter 概念.variable 即e ...

  8. QSqlQueryModel的实例操作

    QSqlQueryModel类为SQL的结果集提供了一个只读的数据模型,下面我们先利用这个类进行一个最简单的操作. 我们新建Qt4 Gui Application工程,我这里工程名为queryMode ...

  9. Mysql导出表结构及表数据 mysqldump用法

    几个常用用例: 1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名     mysqldump -u wcnc -p smgp_apps_wcnc >  ...

  10. Middleware

    Middleware The middleware gives a single shot to the views associated into Controllers, before execu ...