POJO包装传递对象

//POJO实体
public class QueryConditionObject {private User user;//user get,set方法
}//Dao 接口
public interface UserMapper{List<User> findByCondition(QueryConditionObject qco);
}//UserMapper.xml中的select标签
<select id="findByCondition" resultType="com.regotto.domain.User"parameterType="com.itheima.domain.QueryVo"><!--此处使用OGNL表达式, 传递select查询的条件-->select * from user where username like #{user.username};
</select>

resultType or resultMap配置结果参数

MyBatis封装resultSet结果集时, 使用反射机制, 因此, 必须保证resultSet结果集中的属性名称与实体的名称保持一致, 因此使用 resultType 遵循以下规则:

  1. 数据库的列名与实体属性名保持一致(在Windows下MySQL不区分大小写, 在Linux下MySQL区分大小写, 一定要注意保持包括大小写也一致)
  2. 若数据库列名与实体属性名不一致, 使用select name as userName…, 来保持resultSet结果集属性的值与实体中的属性值一样, 保证反射属性封装成功.
  3. 使用resultMap定义表列名与实体属性的对应关系.

typeAliases(类型别名)

使用类型别名后, 在parameterType, resultType中就不用写很长的类路径, 直接简写
例如: 以前 => resultType=“com.regotto.domain.User” 后来 => resultType=“user”

<typeAliases><!--对单个实体进行--><typeAlias alias="user" type="com.regotto.domain.User"><!--扫描当前包下的类, 自动别名设定为类名(首字母大小写都行)--><package name="com.regotto.domain">
</typeAliases>

mappers映射器(指定Mapper.xml或者注解修饰的类)

使用Mapper.xml (最常用)
<mapper resource="com/regotto/dao/UserMapper.xml">
使用注解
<mapper class="com.regotto.dao.UserMapper">
mapper接口与Mapper.xml文件在同一个目录中, 且文件名相同
<package name="com.regotto.dao">

MyBatis数据源配置

MyBatis数据源种类: UNPOOLED(不使用连接池), POOLED(使用连接池), JNDI(从JNDI上查找DataSource)
使用连接池, 提高数据库操作性能, 通常MyBatis使用的数据源为POOLED

<dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/>
</dataSource>

MyBatis使用工厂模式创建DataSource, 源码如下:

public interface DataSourceFactory {void setProperties(Properties var1);DataSource getDataSource();
}import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;
//UnpooledDataSourceFactory implements DataSourceFactory
public class PooledDataSourceFactory extends UnpooledDataSourceFactory {public PooledDataSourceFactory() {this.dataSource = new PooledDataSource();}
}

根据源码PooledDataSource中popConnection(String username, String password)方法可得出, 当执行Sql语句的时候, 才会从连接池中获得一个Connection, 使用完毕后, 再归还连接池.

MyBatis事务控制

JDBC事务提交: connection.setAutoCommit(boolean b).
MyBatis中事务提交源码:

 //SqlSessionFactory.class代码如下:public SqlSession openSession() {return this.openSessionFromDataSource(this.configuration.getDefaultExecutorType(), (TransactionIsolationLevel)null, false);}public SqlSession openSession(boolean autoCommit) {return this.openSessionFromDataSource(this.configuration.getDefaultExecutorType(),(TransactionIsolationLevel)null,autoCommit);}private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)

可使用SqlSessionFactory.openSession(true)设置自动提交. 或者使用session.commit进行手动提交.

动态SQL

对paramType中的参数进行判断, 条件成立, 则拼装Sql, 不成立, 不拼装Sql

  • if标签(条件判断, where 1=1用于if条件不成立, 导致sql执行错误)
<select id="findByUser" resultType="user" parameterType="user">select * from user where 1=1<if test="username!=null and username != '' ">and username like #{username}</if> <if test="address != null">and address like #{address}</if>
</select>
  • where标签(简化where 1=1的写法)
<select id="findByUser" resultType="user" parameterType="user"> select * from user<where> <if test="username!=null and username != '' ">and username like #{username}</if> <if test="address != null">and address like #{address}</if>   </where>
</select>
  • foreach标签(等价于for循环, 重复执行Sql)
class Query{private List<Integer> ids;//ids get set方法...
}
<!-- 查询所有用户在 id 的集合之中 -->
<select id="findInIds" resultType="user" parameterType="com.regotto.Query">
<!-- select * from user where id in (1,2,3,4,5); -->select * from user<where> <if test="ids != null and ids.size() > 0"> <foreach collection="ids" open="id in ( " close=")" item="uid" separator=",">#{uid}</foreach></if></where>
</select>SQL 语句:select 字段 from user where id in (?)
<foreach>标签用于遍历集合,它的属性:collection:代表要遍历的集合元素,注意编写时不要写#{}open:代表语句的开始部分close:代表结束部分item:代表遍历集合的每个元素,生成的变量名sperator:代表分隔符

MyBatis多表查询

  • 一对一(多对一)
    User <=> Account
1. 编写sql语句: select * from user, account where user.id = account.id;
2. 定义实体(可以使用int, String等基本属性定义, 定义的实体内容决定了Mapper.xml中select标签的编写方式)class AccountUser{private User user;private String id;private String name;//......//get, set方法}
3. 定义dao接口interface AccountUserMapper{AccountUser findAccountUser();}
4. 定义AccountUserMapper.xml文件resultMap建立查询结果与AccountUser属性的对应关系<resultMap type="account" id="accountMap"> <id column="aid" property="id"/><result column="uid" property="uid"/><result column="money" property="money"/><!-- 它是用于指定从表方的引用实体属性的 --> <association property="user" javaType="user"> <id column="id" property="id"/><result column="username" property="username"/><result column="sex" property="sex"/><result column="birthday" property="birthday"/><result column="address" property="address"/></association></resultMap> <select id="findAll" resultMap="accountMap">select u.*,a.id as aid,a.uid,a.money from account a,user u where a.uid =u.id;</select>
  • 一对多
    User <=> 多个Account
1. 编写sql: select u.*,a.id as aid ,a.uid,a.money from user u left outer join account a on u.id =a.uid
2. 定义实体class User{private String id;private String name;private List<Account> accounts;//get, set方法}
3. 定义dao接口方法: List<Account> findAll();
4. UserMapper.xml
<resultMap type="user" id="userMap"> <id column="id" property="id"></id> <result column="username" property="username"/><result column="address" property="address"/><result column="sex" property="sex"/><result column="birthday" property="birthday"/><!-- collection 是用于建立一对多中集合属性的对应关系ofType 用于指定集合元素的数据类型--> <collection property="accounts" ofType="account"> <id column="aid" property="id"/><result column="uid" property="uid"/><result column="money" property="money"/></collection>
</resultMap>
<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">select u.*,a.id as aid ,a.uid,a.money from user u left outer join account a on u.id =a.uid
</select>

相比于一对一, 若实体属性是集合等结构, 则使用collection标签

  • 多对多
    多个Role <=> 多个User, 中间表USER_ROLE
1.编写sql:SELECT r.*,u.id uid, u.username username FROM ROLE rINNER JOIN USER_ROLE ur  ON ( r.id = ur.rid)    INNER JOIN USER u ON (ur.uid = u.id);
2.编写实体:class Role {private id;private name;List<User> users;//get set方法}
3.dao接口:List<Role> findAll();
4.编写Mapper.xml<!--定义 role 表的 ResultMap--><resultMap id="roleMap" type="role"><id property="roleId" column="rid"></id><result property="roleName" column="role_name"></result><result property="roleDesc" column="role_desc"></result><collection property="users" ofType="user"><id column="id" property="id"></id><result column="username" property="username"></result><result column="address" property="address"></result><result column="sex" property="sex"></result><result column="birthday" property="birthday"></result></collection></resultMap><!--查询所有--><select id="findAll" resultMap="roleMap">select u.*,r.id as rid,r.role_name,r.role_desc from role r

将多对多的关系拆分为两个一对多的关系处理

MyBatis延迟加载

一定程度上提高查询性能, 但是造成的问题是: 当存在大量查询需要立即获得数据, 导致用户体验下降.
在SqlMapperConfig.xml配置文件中开启延迟加载:

<settings> <setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/>
</settings>
  • association实现延迟加载
    account <=> 多个user
AccountUserMapper.xml文件内容
<!-- 建立对应关系 --> <resultMap type="account" id="accountMap"> <id column="aid" property="id"/><result column="uid" property="uid"/><result column="money" property="money"/><!-- 对user进行查询的时候,  指定需要延迟加载的User方法, 对应方法参数名称--> <association property="user" javaType="user" select="com.itheima.dao.IUserDao.findById" column="uid"/></resultMap> <select id="findAll" resultMap="accountMap">select * from account</select>
association属性:
select: 填写我们要调用的 select 映射的 id
column : 填写我们要传递给 select 映射的参数
  • collection实现延迟加载
    user <=> 多个account
AccountUserMapper.xml:
<resultMap type="user" id="userMap"> <id column="id" property="id"></id> <result column="username" property="username"/><result column="address" property="address"/><result column="sex" property="sex"/><result column="birthday" property="birthday"/><!-- collection 是用于建立一对多中集合属性的对应关系ofType 用于指定集合元素的数据类型select 是用于指定查询账户的唯一标识(账户的 dao 全限定类名加上方法名称)column 是用于指定使用哪个字段的值作为条件查询--> <collection property="accounts" ofType="account" select="com.itheima.dao.IAccountDao.findByUid" column="id">
</collection>
</resultMap>
<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">select * from user
</select>
<collection>标签:主要用于加载关联的集合对象select 属性:用于指定查询 account 列表的 sql 语句,所以填写的是该 sql 映射的 idcolumn 属性:用于指定 select 属性的 sql 语句的参数来源,上面的参数来自于 user 的 id 列,所以就写成 id 这一个字段名

**结论: **使用association或者collection进行延迟加载, 需要指定select, column, association与collection的区别: association使用property, javaType; collection使用property, ofType; property都是指定实体中属性的名称, javaType, ofType是指定集合泛型.

MyBatis缓存

一级缓存(SqlSession级别, Session存在缓存就存在)

在映射文件中设定查询结果的缓存有效:

<!-- 根据 id 查询 -->
<select id="findById" resultType="UsEr" parameterType="int" useCache="true">select * from user where id = #{uid}
</select>useCache: 设定缓存是否有效

执行查询操作之后, 结果放入一级缓存中, 下次查询, 数据还是存缓存中获取.

二级缓存(Mapper映射级别, 多个Session共用一个缓存)

1.在SlqMapperConfig.xml中开启二级缓存

MyBatis默认开启二级缓存
<settings><!-- 开启二级缓存的支持 --> <setting name="cacheEnabled" value="true"/>
</settings>

2.在select标签上配置useCache标签值
注: 使用缓存存储对象, 实体必须实现 Serializable接口

MyBatis注解开发不具有灵活性, 不使用

MyBatis动态SQL_多表查询_延迟加载_缓存相关推荐

  1. Mybatis源码分析--关联表查询及延迟加载原理(二)

    在上一篇博客Mybatis源码分析--关联表查询及延迟加载(一)中我们简单介绍了Mybatis的延迟加载的编程,接下来我们通过分析源码来分析一下Mybatis延迟加载的实现原理. 其实简单来说Myba ...

  2. 基于SpringDataJpa的mysql动态分页多表查询

    基于SpringDataJpa的mysql动态分页多表查询 由于这篇文章预计篇幅会很长,关于Spring Data JPA的知识就简短的分享,更多的请自行度娘,JPA 封装了很多查询的接口,但今天要讲 ...

  3. mybatis 多表关联查询_Java修行第041天--MyBatis框架(下)--多表查询

    1 解决列名和属性名不一致问题 如果查询时使用 resultType 属性, 表示采用 MyBatis 的Auto-Mapping(自动映射)机制, 即相同的列名和属性名会自动匹配. 因此, 当数据库 ...

  4. MyBatis如何实现多表查询

    MyBatis如何实现多表查询 先说结论,在MyBatis中我们有两种方式来实现多表查询 一.使用sqlmapper配置文件 二.使用注解 直入主题首先我们来看如何使用配置文件的方式来实现一对一,和一 ...

  5. mybatis中的多表查询

    首先我们先创建两个表和pojo用来演示多表查询,如下: 用户表:t_emp表,其中emp_id为自增主键. t_emp对应的pojo:Employee public class Employee {p ...

  6. mybatis高级(3)_延迟加载_深度延迟_一级缓存_二级缓存

    设置延迟加载需要在mybatis.xml中设置 注: 侵入式延迟加载为真时是延迟加载 侵入式延迟加载为假时是深度延迟加载 <!-- 延迟加载和深度延迟加载 --><settings& ...

  7. asp多表查询并显示_高级子查询

    要点 1.子查询结果只允许返回单行,子查询包括相关子查询:在主查询中,每查询一条记录,需要重新做一次子查询,这种称为相关子查询.非相关子查询:在主查询中,子查询只需要执行一次,子查询结果不再变化,供主 ...

  8. 第七周 Java语法总结之数据库大全_DDL_DML_DQL_约束_备份与还原_表的关系_三大范式_多表查询(内连接_外连接_子查询)_musql事务_隔离级别

    文章目录 数据库 DDL语句:数据库定义语句 1.库的增删查改 1.查询当前mysql下所有的数据库 2.创建数据库 3.查看创建数据库的默认的字符集(了解) 4.修改数据库的字符集(了解) 5.删除 ...

  9. MyBatis如何实现多表查询(一对一、一对多)

    Mybatis实现多表的查询分为以下两种: 方式一:sqlMapper配置文件 一对一:在resultMap标签中使用 association 标签 一对多:在resultMap 标签中使用colle ...

最新文章

  1. qml 自定义消息框_Qt qml 自定义消息提示框
  2. IT人士有哪些保健建议
  3. 64位debian系统下安装inodeClient
  4. python whl_python whl是什么文件
  5. AOS编排语言系列教程(六):创建共享云硬盘EVS
  6. hystrix 页面_《SpringCloud微服务之Hystrix组件》
  7. thymeleaf教程
  8. l455在线清零服务器,爱普生epson l455清零软件官方版
  9. 【facenet人脸识别】利用LFW数据集进行人脸比对测试
  10. php时间格式转换成时间戳,php把时间格式转换为时间戳的案例
  11. 如何一键查询物流信息并筛选出未签收单号
  12. Linux 学习笔记 (一)
  13. 《大数据之路:阿里巴巴大数据实践》-第1篇 数据技术篇 -第7章 数据挖掘
  14. Elasticsearch 创建索引 Java 实现
  15. Mybatis Plus 多租户架构(Multi-tenancy)实现
  16. python 去掉字符串头尾及内部指定字符
  17. 手机隐藏ip地址的方法简单设置
  18. Macbook pro外接显卡实现深度学习
  19. 【华人学者风采】James T. Kwok(郭天佑) 香港科技大学
  20. javascript正则表达式-姜威-专题视频课程

热门文章

  1. mac终端操作文件或文件夹(持续更新)
  2. python 管道队列_关于python:Multiprocessing-管道与队列
  3. Linux中的 硬链接ln和软连接ln -s
  4. sql server 创建唯一性非聚集索引语句_数据库专题—索引原理
  5. 十字连接焊盘_你应该知道的焊盘基础知识
  6. Linux设备驱动模型3——platform平台总线工作原理
  7. 驱动框架6——linux内核的gpiolib学习
  8. linux ssh抓包,如何在SSH连接Linux系统的环境下使用wireshark抓包?
  9. 排队 题解 组合数学+高精度
  10. 随堂小测冲刺.第19天