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,在执行查询时就出 ...
随机推荐
- prestashop二次开发 笔记(支付插件)
//主函数 public function __construct() { $this->name = 'CilPay'; //模块名称 $this->display ...
- android模拟器访问localhost或127.0.0.1报错
在一般的Java Web程序开发中,我们通常使用localhost或者127.0.0.1来访问本机的Web服务,但是如果我们在Android模拟器中也采用同样的地址来访问,Android模拟器将无法正 ...
- 低级错误之Oracle客户端添加数据
本来可以为空的外键,自己非要写一个无意义的值,导致数据保存失败.
- java学习之路---线程(重点)
1.多线程是一种并发机制的有效手段.进程和线程一样,都是实现并发的一个基本单位.线程是比进程更小的一种实现单位,线程是在进程的基础上的进一步划分.所谓的多线就是在一个进程中产生了多个线程,这些线程可 ...
- powershell.exe直接运行命令
powershell.exe -Command "& { ('time={0},user={1}' -f (get-date),(whoami)) | Out-File " ...
- 用JAVA写一个函数,功能例如以下: 随意给定一组数, 找出随意数相加之后的结果为35(随意设定)的情况
用JAVA写一个函数.功能例如以下:随意给定一组数,比如{12,60,-8,99,15,35,17,18},找出随意数相加之后的结果为35(随意设定)的情况. 能够递归算法来解: package te ...
- VMWare9下基于Ubuntu12.10搭建Hadoop-1.2.1集群
VMWare9下基于Ubuntu12.10搭建Hadoop-1.2.1集群 下一篇:VMWare9下基于Ubuntu12.10搭建Hadoop-1.2.1集群-整合Zookeeper和Hbase 近期 ...
- Nginx+Tomcat集群部署
为了获取更好的性能,我们常常需要将tomcat进行集群部署.下文通过nginx转发实现tomcat集群,并通过nginx-upstream-jvm-route插件保证session的粘滞. 应用场景环 ...
- ConvertHelper与泛型集合
在机房重构时.我们常常会用到ConvertHelper. 它把从数据库中查询到的dateTable(也是一个暂时表)转化为泛型,然后再填充到DataGridView控件中. ConvertHelper ...
- java_jdbc_利用结果集元数据将查询结果封装为map_MetaData
package cn.itcast.batch; import java.sql.Connection; import java.sql.ParameterMetaData; import java. ...