>### 1.属性名和数据库中的列名不同时,可以使用别名查询
~~~
例如列名是name 而属性名是e_name
<select id="getAllEmp" resultType="cn.li.pojo.Employee">
select id,name e_name,gender,address from employee
</select>
~~~
>#### 属性名和数据库中的列名不同时或者另一种方法:
~~~
<!-- 自定义封装规则 。resultMap="" column 表示数据库中查询的字段名称 。 -->
<resultMap type="cn.li.pojo.Employee" id="rm2">
<!-- 使用id标签 来指定主键的封装规则 ,mybatis会做优化 。 -->
<id column="id" property="id"></id>
<result column="name" property="e_name" />
<result column="gender" property="gender" />
<result column="address" property="address" />
</resultMap>
<select id="getAllEmp2" resultMap="rm2">
select id,name,gender,address from employee
</select>
~~~
>### 2.级联封装
~~~
<resultMap type="cn.li.pojo.Employee" id="rm3">
<id column="id" property="id" />
<result column="name" property="e_name" />
<result column="gender" property="gender" />
<result column="address" property="address" />
<result column="d_id" property="dept.dId" />
<result column="d_name" property="dept.dName" />
</resultMap>
<select id="sel1" resultMap="rm3">
select e.*,d.* from employee e left
join department d on e.d_id=d.d_id
</select>
~~~
>### 3.使用联合标签配置,添加property,javaType属性
~~~
<resultMap type="cn.li.pojo.Employee" id="rm4">
<id column="id" property="id" />
<result column="name" property="e_name" />
<result column="gender" property="gender" />
<result column="address" property="address" />
<!-- 联合 。 association 指向的是 实体类中的那个属性 需要做联合 配置。 -->
<association property="dept" javaType="cn.li.pojo.Department">
<id column="d_id" property="dId" />
<result column="d_name" property="dName" />
</association>
</resultMap>
<select id="sel2" resultMap="rm4">
select e.*,d.* from employee e left
join department d on e.d_id=d.d_id
</select>
public class Employee{
...
private Department dept;
....
}
~~~
>### 4.分步查询&延迟加载
~~~
<!-- com/igeek/pojo/EmployeeMapper.xml 分布查询 。。。 1.知道员工的id ,根据员工的id能拿到部门id . 2.再根据得到部门id,去查询部门所有信息。 -->
<resultMap type="cn.li.pojo.Employee" id="selStep">
<id column="id" property="id" />
<result column="name" property="e_name" />
<result column="gender" property="gender" />
<result column="address" property="address" />
<!--
association 联合
property 要为实体类中的哪一个属性进行赋值。
select 表示当前的这个属性值 是通过查询拿到的 。(通过那个命名空间下的那一个标签去做查询 。)
column 表示 第一次查询的哪一个列的结果作为参数传递给你第二次要调用的方法。
步骤1 : id name gender address d_id
1 张全蛋 1 暴走大事件 ( 1 )
步骤2 : d_id dId,d_name dName
( 1 ) 开发 。
多个参数的分步查询 。
#{id},#{name},#{age}
column="{name=d_name,id=d_id}";
只有分步查询可以做延迟加载 懒加载 按需加载 。
-->
<!-- select部门映射文件的namespace.标签id -->
<association property="dept"
select="cn.li.dao.DepartmentDao.selDepartmentByID"
column="d_id"><!-- column表示将查询中那一列的结果作为参数传递给另外一个查询 -->
</association>
</resultMap>
<select id="sel3" resultMap="selStep">
select * from employee where id=#{id}
</select>
//DepartmentMapper.xml文件里的内容
<select id="selDepartmentByID" resultType="cn.li.pojo.Department">
select d_id dId,d_name dName from department where d_id=#{id}
</select>
//开启延迟加载 只需要在配置文件中进行全局配置(两个值。)
//延迟加载,又叫懒加载,按需加载 lazyLoadingEnable设置为true ,aggressiveLazyLoading 设置为false
~~~
开启懒加载步骤:
1.导入三个jar包

2.在全局配置文件里配置 也就是config.xml文件里
~~~
<settings>
<setting name="lazyLoadingEnabled" value="true"></setting>
<setting name="aggressiveLazyLoading" value="false"></setting>
</settings>
~~~
>### 5.使用collection查询
~~~
//一对多 查询部门下的所有的员工信息。
<resultMap type="cn.li.pojo.Department" id="selMap2">
<id column="d_id" property="dId"/>
<result column="d_name" property="dName"/>
<!--
collection定义关联集合类型的属性的封装规则
ofType:指定集合里面元素的类型
-->
<collection property="emps" ofType="cn.li.pojo.Employee">
<id column="id" property="id"/>
<result column="name" property="e_name"/>
<result column="gender" property="gender"/>
<result column="address" property="address"/>
</collection>
</resultMap>
<select id="selDep" resultMap="selMap2">
select e.*,d.* from employee e left join department d on e.d_id=d.d_id where d.d_id=#{id}
</select>
public class Department{
...
private List<Employee> emps;
...
}
~~~
~~~
- spring
- 1.spring第一天
- 1.1 安装spring插件(spring tool suite)
- 1.2 spring概述
- 1.3 控制反转&依赖注入
- 1.4 springIOC容器
- 1.5 依赖注入的四种方式
- 1.6 配置bean的细节
- 1.7 bean之间的关系
- 1.8 bean作用域
- 1.9 补充:创建对象的几种方法
- 1源代码位置
- 2.spring第二天
- 2.1 使用外部属性文件
- 2.2 spEL
- 2.3 bean的生命周期
- 2.4 通过工厂方式配置bean
- 2.5 基于注解的方式配置bean
- 2.6 组件装配
- 2.7 静态代理
- 2.8 动态代理
- 2.9 Cglib代理
- 2源代码位置
- 3. spring第三天
- 3.1 springAOP
- 3.1.1 AOP简介
- 3.1.2 为什么使用AOP
- 3.1.3 AOP关键术语
- 3.1.4 AOP图解
- 3.1.5 springAOP实现步骤
- 3.1.6 SpringAOP实现原理:
- 3.1.7 AOP的好处
- 3.1.8 AOP在实际项目中的主要应用
- 3代码地址
- 3.1.9 纯注解版配置aop的方式
- 3.2 maven环境搭建
- 附IDEA激活码
- 4. spring第四天
- 4.1 c3p0事务
- 4.2 命令窗口事务
- 4.3 c3p0连接池设置
- 4.4 事务中的一些基本概念
- 4.5 事务的传播行为
- 4.6 自定义异常
- 4.7 spring整合Junit单元测试
- 4.8 JdbcTemplate(附源代码)
- 事务源代码
- 4.9 纯注解tx
- 4.10 基于xml配置事务
- 0. jsp页面修改编码方式
- 0.1 eclipse配置tomcat
- 0.单例模式-饱汉模式
- 0.单例模式-饥汉模式
- springMVC
- 1. springmvc第一天
- 1.1 springMVC概述
- 1.2 springmvc框架搭建及第一个应用程序
- 1.3 @RequestMapping
- 1.4 RequestMapping修饰类
- 1.5 RequestMapping精准化映射
- 1.6 Ant风格URL
- 1.7 带有占位符的url映射
- 1.8 REST风格
- 1.9 RequerstParam获取请求正文
- 2. springmvc第二天
- 2.1 优化
- 2.2 POJO绑定请求参数
- 2.3 RequestHeader获取请求报头信息
- 2.4 CookieValue获取Cookie信息
- 2.5 获取原生ServletAPI
- 2.6 ModelAndView处理模型数据
- 2.7 Map、Model、ModelMap处理模型数据
- 2.8 @SessionAttributes注解
- 2.9 @ModelAttribute无返回值方法及方法入参
- 2.10 @ModelAttribute修饰有返回值类型的方法
- 代码地址
- 3. springmvc补充
- 3-1 springmvc工作原理
- 3-2 springmvc form表单提交中文乱码
- 3-3 数据的格式化
- 3-4 自定义类型转换器
- 3-5 其他知识点
- 3-6 crud代码
- 3-7 @DateTimeFormat日期格式化
- 3-8 数据验证的概念及JSR303验证
- 3-9 Hibernate-Validator验证框架
- 3-10 Controller捕获错误消息
- 3-11 errors标签在页面中获取错误消息
- 3-12 错误消息的定制及国际化
- 3-13 自定义拦截器
- 3-14 Java代码中获取国际化信息
- 3-15 超级链接设置国际化
- 3-16 AJAX支持之@RequestBody
- mybatis
- 1. mybatis第一天
- 1. 为什么使用mybatis
- 2. 下载地址
- 3. hello
- 4. mybatis三种开发模式
- 5. 全局配属属性内容
- 6. DTD设置
- 7. Mapper中的CRUD
- 8. 8.mybatis使用主键自增
- 9. #{}中的参数处理
- 10. #{}与${}区别
- 11. 集合数据的查询
- 12 动态sql
- 12.1 if
- 12.2 choose, when, otherwise
- 12.3 trim, where, set
- 12.4 foreach
- 代码位置
- 2. mybatis第二天
- 1.封装map类型的数据
- 2. resultMap自定义封装规则
- 0代码位置
- 3. mybatis缓存机制
- ssm整合
- 1.maven
- 2.ssm基础环境搭建
- 2-1 引入项目依赖的jar包
- 2-2 引入bootstrap,jquery
- 2-3 创建项目包结构
- 2-4 编写web.xml配置文件
- 2-5 编写sping,springmvc,mybatis配置文件
- 2-6 逆向工程mbg.xml
- shiro安全框架
- 1.shiro简介
- 易购Buy商城
- 第一天
- 1.课程计划
- 2.电商行业背景
- 3.易购Buy介绍
- 4.易购Buy架构
- 5.工程搭建
- 6.工程启动和测试
- 7.ssm框架整合
- 8.整合测试
- 9.svn
- 9.1 svn服务端
- 9.2 svn客户端
- 第二天
- 1.SOA架构分析
- 2.dubbo使用方法
- 3.注册中心
- 4.工程改造
- 5.easyUI
- maven
- 1.maven介绍
- 2.idea配置maven和服务器
- 3.创建web工程
- 4.分模块构建工程
- 5. 代码位置
- 6. nexus
- Luence搜索
- 1.了解搜索技术
- 2.Lucene的基本使用
- solr
- SolrCloud
