Mybatis框架-2
1.Mybatis中的接口形式
在Mybatis中使用接口形式将通过代理对象调用方法,从而实现sql的执行
1)定义一个接口
package mapper;
import java.util.List;
import pojo.User;
public interface UserMapper {
public List<User> findAll();
}
2)是通过代理对象调用方法
//测试接口方式
@Test
public void test04(){ SqlSession session = factory.openSession();
//获取接口对象
UserMapper userMapper = session.getMapper(UserMapper.class);
System.out.println(userMapper.getClass()); List<User> userList = userMapper.findAll();
for (User user : userList) {
System.out.println(user);
}
}
3)映射配置文件中,namespace的值是包名.接口名-->mapper.UserMapper
写sql标签的id为方法名 ---> findAll
<select id="findAll" resultType="User">
<include refid="selectUser"/>
</select>
通过接口形式的原理:
当通过接口执行接口方法时,首先Mybatis会为接口创建代理对象.通过代理对象调用方法时,
首先会根据接口的类型mapper.UserMapper匹配
映射文件中的namespace.
如果匹配成功,之后接口方法匹配映射文件中的sql的ID,
如果匹配成功则调用sql语句完成操作.
2.手动封装结果集
需求:
当结果集中的字段的名称和对象属性名称不一致时(或者说pojo中存在自定义引用类型属性),则不能实现自动的映射.
解决方案:
将自动封装改成手动封装即 resultType 改成使用 resultMap 属性(一下是一对一的自关联查询)
<select id="findAll" resultMap="deptRm">
select * from
dept_p d
left join
(select dept_id p_id,dept_name p_name from dept_p) p
on d.parent_id = p.p_id
order by dept_id
</select>
<resultMap type="Dept" id="deptRm" autoMapping="true">
<id column="dept_id" property="deptId"/> <association property="parentDept" javaType="Dept">
<id column="p_id" property="deptId"/>
<result column="p_name" property="deptName"/>
</association>
</resultMap>
bean:

association:一般在实际开发中用于一个bean中存在另一个bean进行数据封装。
autoMapping:若存在引用类型属性时应该添加该属性后就会实现自动映射。
association中的property标识bean中的属性。
JavaType标识bean的全限定名称。
若出现部分不同时主键必须要设置,其他属性如果列名没有发生变化则不需要设置。
mybatis中如果操作的是单表,那么除主键之外,如果结果集中的字段的名称和属性名一致,可以自动完成映射.
3.一对一关联关系:
如果是多表关联查询,结果集中的字段名称和主对象中的属性名一致,可以通过autoMapping="true"实现自动映射.

一对一封装的具体实现过程:
要求:结果集中不允许出现同名字段,否则mybaits不能正确解析。
<!--一对一关联查询 -->
<select id="oneTOne" resultMap="userOneTOne">
select * from
user u
left join
userinfo info
on u.id = info.user_id
</select> <resultMap type="pojo.User" id="userOneTOne" autoMapping="true">
<!--主键封装 -->
<id column="id" property="id"/> <!--一对一封装 -->
<association property="info" javaType="pojo.UserInfo">
<id column="user_id" property="userId"/>
<result column="tel" property="tel"/>
<result column="qq" property="qq"/>
</association>
</resultMap>
一对一封装时 association 与javaType一起联用
4.一对多关联关系:
一对多关联封装时,需要使用集合进行封装。
<!--一对多关联封装-->
<select id="oneTMore" resultMap="deptOneTMore">
select d.dept_id,d.dept_name,u.id,u.name,u.age,u.sex from
dept d
left join
user u
ON d.dept_id = u.dept_id
</select> <resultMap type="pojo.Dept" id="deptOneTMore">
<!--主对象封装完成 -->
<id column="dept_id" property="deptId"/>
<result column="dept_name" property="deptName"/>
<!--一对多关联封装 -->
<collection property="userList" ofType="pojo.User">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
</collection>
</resultMap>
一对多封装时 Collection 和 ofType 一起使用
5.多对多关联关系
多对多其实就是双向一对多。
<!-- 三表联查 -->
<select id="findT_S" resultMap="t_s">
select * from (select t.t_id,t.t_name,t.t_sex,s_t.student_id from
teacher t
left join
s_t
on t.t_id = s_t.teacher_id)t left join
student s
on t.student_id = s.id
</select> <resultMap type="pojo.Teacher" id="t_s">
<id column="t_id" property="tId"/>
<result column="t_name" property="tName"/>
<result column="t_sex" property="tSex"/> <!--多对多封装 -->
<collection property="studentList" ofType="pojo.Student">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
</collection>
</resultMap>
student 、teacher、s_t 关联表 --> 查询出了一个老师的全部学生
Mybatis框架-2的更多相关文章
- Mybatis框架的多对一关联关系(六)
一.一对多的关联映射 一对多关联查询多表数据 1接口 public interface IDeptDAO { //根据部门编号查询该部门单个查询 public Emp getEmpById(Integ ...
- Spring+SpringMvc+Mybatis框架集成搭建教程
一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...
- Mybatis框架中实现双向一对多关系映射
学习过Hibernate框架的伙伴们很容易就能简单的配置各种映射关系(Hibernate框架的映射关系在我的blogs中也有详细的讲解),但是在Mybatis框架中我们又如何去实现 一对多的关系映射呢 ...
- Hibernate框架与Mybatis框架的对比
学习了Hibernate和Mybatis,但是一直不太清楚他们两者的区别的联系,今天在网上翻了翻,就做了一下总结,希望对大家有帮助! 原文:http://blog.csdn.net/firejuly/ ...
- 初识Mybatis框架,实现增删改查等操作(动态拼接和动态修改)
此第一次接触Mybatis框架确实是有点不适应,特别是刚从Hibernate框架转转型过来,那么为什么要使用Mybatis框架,Mybatis框架和Hibernate框架又有什么异同呢? 这个问题在我 ...
- Spring+MyBatis框架中sql语句的书写,数据集的传递以及多表关联查询
在很多Java EE项目中,Spring+MyBatis框架经常被用到,项目搭建在这里不再赘述,现在要将的是如何在项目中书写,增删改查的语句,如何操作数据库,以及后台如何获取数据,如何进行关联查询,以 ...
- SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...
- Spring3.0 与 MyBatis框架 整合小实例
本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...
- 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版) SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式, ...
- 详解Java的MyBatis框架中SQL语句映射部分的编写
这篇文章主要介绍了Java的MyBatis框架中SQL语句映射部分的编写,文中分为resultMap和增删查改实现两个部分来讲解,需要的朋友可以参考下 1.resultMap SQL 映射XML 文件 ...
随机推荐
- 2017 济南综合班 Day 2
木棍(stick) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK有很多木棍,具体的,总共有n根,且每根木棍都有一个长度.为了方便起见,我们可以用一个正 ...
- [USACO13FEB]出租车Taxi
洛谷题目链接:[USACO13FEB]出租车Taxi 题目描述 Bessie is running a taxi service for the other cows on the farm. The ...
- PowerDesigner16 设置导出sql文件的编码
一: 导出数据库结构sql脚本 Database -> Generate Database 在弹窗中选择Format选项卡,修改Encoding,选择自己需要的编码格式. 二:比较数据库差异 ...
- 使用Docker搭建Django,Nginx,R,Python部署环境
转载自https://blog.csdn.net/The_One_is_all/article/details/76063968 基本环境: Ubuntu 16.10 docker 17.06.0-c ...
- 使用shell脚本往文件中加一列
上午大学同学问了我一个脚本的问题,大概需求就是看到所有端口的开启情况,还要知道每个端口的应用程序路径,而且要和之前的数据齐平,就是再加一列数据.我腚眼一看,非常容易嘛,但由于当时忙,所以就说中午给他发 ...
- 【LuoguP3038/[USACO11DEC]牧草种植Grass Planting】树链剖分+树状数组【树状数组的区间修改与区间查询】
模拟题,可以用树链剖分+线段树维护. 但是学了一个厉害的..树状数组的区间修改与区间查询.. 分割线里面的是转载的: ----------------------------------------- ...
- bzoj4695 最假女选手
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4695 [题解] SegmentTree beats!(见jiry_2论文/营员交流) 考虑只 ...
- urllib3使用指南
对比urllib,用urllib3处理http请求十分方便,可以嵌入web服务后端用于访问其它web实例提供的接口 一.安装 pip install urllib3 二.初始化 导入urllib3 i ...
- 自定义ToolBar
一.Toolbar的简介 Toolbar 是 android 5.0 引入的一个新控件,Toolbar出现之前,我们很多时候都是使用ActionBar以及ActionActivity实现顶部导航栏的, ...
- SQL Workbench/J
最近测试segment, 使用了一个新的DB--SQL Workbench/J, 参考文档:http://docs.aws.amazon.com/redshift/latest/mgmt/connec ...