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,在执行查询时就出 ...
随机推荐
- [二]JQueryMobile常用的组件介绍
1.页头.主要部门.页尾构成一个基本的页面 2.按钮组件(input.a) 3.列表组件(ul) 4.表格组件(table)
- LINUX下的简单线程池
前言 任何一种设计方式的引入都会带来额外的开支,是否使用,取决于能带来多大的好处和能带来多大的坏处,好处与坏处包括程序的性能.代码的可读性.代码的可维护性.程序的开发效率等. 线程池适用场合:任务比较 ...
- Design Pattern Explained 读书笔记二——设计模式序言
设计模式的由来: 20 世纪 90 年代初,一些聪明的开发者偶然接触到 Alexander(Christopher Alexander 的建筑师) 有关模式的工作.他们非常想知道,在建筑学成立的理论, ...
- Java算法实例集合(2)
这是Standford一位计算机老师的私藏,里面包含了不少Java/C++的算法实现代码.有兴趣的朋友可以看看.
- 显示 png 图片
uses pngimage;{显示 png 图片}procedure TForm1.Button2Click(Sender: TObject);var png: TPngImage;begin ...
- UITableViewCell 高度自适应
UITableViewCell 高度自适应一直是我们做动态Cell高度时遇到的最烦躁的问题,Cell动态高度计算可以去看看sunny的这篇文章介绍,今天主要和大家分享下我在使用systemLayout ...
- offsetTop和scrollTop的差别
近期想写个组件,结果被这两个属性搞的有点晕,查了下文档和资料,对这两个属性总结例如以下: 一直以来对offsetLeft,offsetTop,scrollLeft,scrollTop这几个方法非常迷糊 ...
- 插件就是生产力——那些不能错过的XCode插件们
古人云"工欲善其事必先利其器",打造一个强大的开发环境,是立即提升自身战斗力的绝佳途径! 晾一下我的武器库,欢迎大家选用:) 全能搜索家CodePilot 2.0 -------- ...
- check_area
CCTouch* pTouch = ...; CCSprite* pSprite = ...; CCRect rect = pSprite ->boundingBox(); if ((& ...
- app卡顿问题检测--KMCGeigerCounter
介绍: KMCGeigerCounter是一个iOS帧速计算器,像盖革计数器那样,当动画丢失一帧时它就记录一次.掉帧通常是不可见的,但是很难区分55fps和60fps之间的不同,而KMCGeigerC ...