mybatis关联查询
场景:一个人有多个手机号,一个手机号对应一个人
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关联查询的更多相关文章
- Mybatis关联查询和数据库不一致问题分析与解决
Mybatis关联查询和数据库不一致问题分析与解决 本文的前提是,确定sql语句没有问题,确定在数据库中使用sql和项目中结果不一致. 在使用SpringMVC+Mybatis做多表关联时候,发现也不 ...
- MyBatis基础:MyBatis关联查询(4)
1. MyBatis关联查询简介 MyBatis中级联分为3中:association.collection及discriminator. ◊ association:一对一关联 ◊ collecti ...
- MyBatis关联查询,一对多关联查询
实体关系图,一个国家对应多个城市 一对多关联查询可用三种方式实现: 单步查询,利用collection标签为级联属性赋值: 分步查询: 利用association标签进行分步查询: 利用collect ...
- mybatis 关联查询实现一对多
场景:最近接到一个项目是查询管理人集合 同时每一个管理人还存在多个出资人 要查询一个管理人列表 每个管理人又包含了出资人列表 采用mybatis关联查询实现返回数据. 实现方式: 1 .在实体 ...
- MyBatis关联查询、多条件查询
MyBatis关联查询.多条件查询 1.一对一查询 任务需求; 根据班级的信息查询出教师的相关信息 1.数据库表的设计 班级表: 教师表: 2.实体类的设计 班级表: public class Cla ...
- Mybatis关联查询之二
Mybatis关联查询之多对多 多对多 一.entity实体类 public class Student { private Integer stuid; private String stuname ...
- mybatis关联查询基础----高级映射
本文链接地址:mybatis关联查询基础----高级映射(一对一,一对多,多对多) 前言: 今日在工作中遇到了一个一对多分页查询的问题,主表一条记录对应关联表四条记录,关联分页查询后每页只显示三条记录 ...
- MyBatis关联查询和懒加载错误
MyBatis关联查询和懒加载错误 今天在写项目时遇到了个BUG.先说一下背景,前端请求更新生产订单状态,后端从前端接收到生产订单ID进行查询,然后就有问题了. 先看控制台报错: org.apache ...
- Mybatis关联查询(嵌套查询)
上一篇文章介绍了基于Mybatis对数据库的增.删.改.查.这一篇介绍下关联查询(join query). 三张表:user article blog 表的存储sql文件: /* Navicat My ...
- MyBatis关联查询 (association) 时遇到的某些问题/mybatis映射
先说下问题产生的背景: 最近在做一个用到MyBatis的项目,其中有个业务涉及到关联查询,我是将两个查询分开来写的,即嵌套查询,个人感觉这样更方便重用: 关联的查询使用到了动态sql,在执行查询时就出 ...
随机推荐
- Keil MDK 5.0发布了
从上图可以看出MDK500.EXE (294,308K)分成了两部分,MDK核和software packs.MDK核跟以往一样分成四个部分,编辑器,编译器,包安装,调试跟踪.此版soft ...
- 射频识别技术漫谈(7)——ID卡【worldsing笔记】
ID(Identification)是识别的意思,ID卡就是识别卡.ID卡包含范围广泛,只要具有识别功能的卡片都可以叫ID卡,例如条码卡,磁卡都可以是ID卡,我们这儿说的当然是射频识别卡. 射频ID卡 ...
- define
define('player',['videoplay'],function(videoplay){ var wrap_player = $('#live_SWF'), obj_player = '' ...
- 山东理工大学ACM平台题答案关于C语言 1543 Egypt
Egypt Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 A long time ago, the Egyptians fi ...
- HDU2177:取(2堆)石子游戏(威佐夫博弈)
Problem Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同 ...
- 创建、显示和删除保存的用户名和密码(cmdkey)
创建,显示和删除保存的用户名和密码: cmdkey.exe /add:targetname /user:username /pass:password
- 删除浏览器浏览器删除cookie方法
上班之余抽点时光出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下删除浏览器 文章目录导航 适用范围及演示工具 什么是cookie? cookie有什么作用? ie6/ie7/ie8 ...
- SAP进销存难点分析及对策
1.基本需求: 业务部门提出如上表格式进销存需求,并且金额要和总账中存货科目保持一致,如果要实现上表格式进校存,可以通过SAP标准程序(MC.9.MB51.MB5B)加工繁琐而成.现分析一下SAP标准 ...
- Supervised Learning-Regression
假设我们有一张房子属性及其价格之间的关系表(如下图所示) ,根据这些数据如何估计其他房子的价格?我们的第一个反应肯定是参考属性相似的房子的价格.在属性较少时这个方法还行得通,属性太复杂时就不那么简单了 ...
- C#_IComparable实例 - 对象ID进行排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Comp ...