Mybatis框架下的表连接三种结构及实现
来源:互联网 发布:所以语音软件 编辑:程序博客网 时间:2024/05/16 04:37
一.OneToOne表连接的建立
1.有两种连接方式
1)Customperson findPersonAndCardByPid(int pid); 建立一个中间类CustomPerson,进行连接.
2)Person findPersonAndCardByPid2(int pid); 将Card对象写入Person类中,此方法是常用方法。
2.配置PersonMapper.xml
<mapper namespace="com.dao.PersonMapper">
<resultMap type="com.po.Person" id="personMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<!-- person里关联的card对象 关系是一对一的关系 -->
<association property="card" javaType="com.po.Card">
<result property="no" column="no"/>
</association>
</resultMap>
<select id="findPersonAndCardByPid" parameterType="int"
resultType="customPerson">
SELECT person.id,name,age,no FROM person INNER JOIN
card ON person.id =card.pid WHERE person.id=#{id};
</select>
<select id="findPersonAndCardByPid2" parameterType="int"
resultMap="personMap">
SELECT person.*,card.no FROM person INNER JOIN card ON person.id =card.pid WHERE person.id=#{id};
</select>
</mapper>
3.测试类进行测试
@Test
public void testFind2() {
SqlSession session = sf.openSession();
PersonMapper mapper = session.getMapper(PersonMapper.class);
Person p = mapper.findPersonAndCardByPid2(9);
System.out.println(p.getCard());
session.close();
}
二.OneToMany表连接的建立
1.建立一对多的po类持久化对象 District 类与Street 类
public class District implements Serializable
private Integer id;
private String name;
List<Street> streets = new ArrayList<Street>();
public class Street implements Serializable
private Integer id;
private String name;
private Integer did;
2.以查询的方法为例,编写DistrictMapper.java并配置DistrictMapper.xml
List<District> findAll(); 接口中查询方法
xml配置与查询的sql
<mapper namespace="com.dao.DistrictMapper">
<resultMap type="com.po.District" id="districtMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="streets" javaType="com.po.Street">
<id property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="did" column="did"/>
</collection>
</resultMap>
<select id="findAll" resultMap="districtMap">
SELECT d.*,s.id sid,s.name sname,s.did FROM district d INNER JOIN street s ON d.id=s.did
</select>
</mapper>
三.ManyToMany表的建立
1.建立多对多的持久化对象,Project类和Employee类
public class Project implements Serializable
private int id;
private String name;
public class Employee implements Serializable
private int id;
private String name;
private List<Project> projects = new ArrayList<Project>();
2.实现接口和配置文件
List<Employee> findAll();
<resultMap type="com.po.Employee" id="employeeMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="projects" javaType="com.po.Project">
<id property="id" column="pid"/>
<result property="name" column="pname"/>
</collection>
</resultMap>
多对多由一个中间表emp_prj连接两张表,实现多对多
<select id="findAll" resultMap="employeeMap">
SELECT e.*,p.id pid,p.name pname FROM employee e INNER JOIN emp_prj ep ON e.id=ep.emp_id INNER JOIN project p ON p.id=ep.prj_id;
</select>
- Mybatis框架下的表连接三种结构及实现
- .net框架下实现自动化测试的三种方法
- 框架学习系列 mybatis 第十六篇 mybatis 常用标签三及与hibernate的比较
- spring+strust2+mybatis框架下的导出功能的实现
- S60平台的三种实现的结构(应用程序的用户界面框架)
- S60平台的三种实现的结构(应用程序的用户界面框架)
- SSM框架下实现MyBatis连接查询,一对多和多对一
- mybatis一对一的三种实现方式
- 《深入理解mybatis原理(三)》 MyBatis的一级缓存实现详解 及使用注意事项
- mybatis实现修改表结构
- Mybatis框架的连接池配置
- SSM(Spring+SpringMVC+MyBatis)三大框架整合及遇到的各种问题
- SSH三大框架实现用户登录及用户表增删改查的测试
- mybatis框架下java连接mysql,汉字乱码显示为“???”
- 使用maven,实现ssm(spring+springmvc+mybatis)三大框架的整合DEMO
- mybatis框架下mysql多表更新的操作
- 三构框架,mybatis
- MyBatis框架的开发流程及注意事项
- Android学习笔记:AS 分享代码到Github
- simulink 自定义函数S-function的字符串传递方法
- c++ primer 第五版 StrVec类(p464)
- 人眼识别
- ArcGIS Server 10.2.2安装失败的一种情形和ArcGIS软件相关概念
- Mybatis框架下的表连接三种结构及实现
- 操作系统总结
- poj1860Currency Exchange(最短路)
- Popwindow的介绍和使用
- Linux下的延时函数
- 图像噪声展示,傻傻分不清楚
- 继承和多态
- django 基础
- 注解