1. Mapper配置文件处理特殊字符

用 &gt; 和 &It; 代替 > 和 <

2. 延迟加载

单表查询性能比多表关联查询要高得多,即先查询单表,如果需要关联多表时再进行查询

<!-- 全局配置参数 -->
<settings>
<!-- 延迟加载总开关 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 设置按需加载 -->
<setting name="aggressiveLazyLoading" value="false" />
</settings

3. resultType和resultMap

resultType:当查询字段名和Bean对象属性名一致时,查询结果自动映射

resultMap:当查询字段名与Bean对象属性名不一致时,需要配置resultMap与Bean属性的对应关系,才能映射

association和collection完成一对一和一对多高级映射。

<resultMap id="ResultMap" type="user" >
<!--
字段名:id_,username_,birthday_
id:主键
column:结果集的列名
property:type指定Bean的对应属性
-->
<id column="id_" property="id"/>
<!-- result就是普通列的映射配置 -->
<result column="username_" property="username"/>
<result column="birthday_" property="birthday"/>
</resultMap>

4. 返回主键

<insert id="insertUser" parameterType="User">
<selectKey keyProperty="id" order="AFTER" resultType="int">
select LAST_INSERT_ID()
</selectKey>
INSERT INTO USER(name,sex,age) VALUES(#{name},#{sex},#{age})
</insert>

5. 动态sql

<select id="findByCondition" resultType="User" parameterType="map">
select * from User
<where>
<if test="name!=null">
and name = #{name}
</if>
<if test="id!=null">
and sal = #{id}
</if>
</where>
</select>

6. 缓存

将查询数据放到缓存中,而不用再去数据库从而提高查询效率

  • 一级缓存:每个SqlSession有自己的缓存,只能访问自己的(默认一级缓存)
  • 二级缓存:每个Mapper有自己缓存,Mapper内的SqlSession可以互相访问
  • 二级缓存将数据从内存写入磁盘,序列化和反序列化,所以映射的Bean对象需要实现serializable接口
  • 两级缓存查询都将数据放入内存中,下次查询直接从内存读取,但执行其他语句会清空内从数据,下次查询需要再次从数据库拿出数据
<!-- Mybatis-config.xml全局配置参数 -->
<settings>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings> <!-- 再需要开启二级缓存的Mapper文件的<mapper namespace>下方添加 -->
<cache />

7. Mapper代理(推荐)

只需要写Bean接口,Bean接口的实现对象由mybatis自动生成,Mapper文件与Bean接口需要遵从下面原则

  • mapper文件中namespace为Bean接口的全限定名
  • mapper文件中sql语句id为Bean接口中的方法名
  • mapper文件中parameterType为Bean接口中方法的输入参数类型
  • mapper文件中resultType为Bean接口中方法的返回值类

Bean接口

package com.howl;

public interface userMapper {

	public user findUserById(int id);

}

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="com.howl.userMapper"> <!-- 通过id查询用户 -->
<select id="findUserById" parameterType="Integer" resultType="com.howl.user">
select * from user where id = #{id}
</select> </mapper>

运行

//通过Bean接口获得Mapper代理对象
userMapper userMapper = sqlSession.getMapper(com.howl.userMapper.class);
//调用代理对象里的方法
user user = userMapper.findUserById(1);

最新文章

  1. java良好的编码习惯
  2. js控制只能输入数字和小数点
  3. poj3580 伸展树(区间翻转 区间搬移 删除结点 加入结点 成段更新)
  4. taocode
  5. [原创] zabbix学习之旅二:yum安装
  6. Insist
  7. xcode中如何安装多个版本的模拟器
  8. FZU 1502 Letter Deletion
  9. 响应式布局—设备像素密度测试 (-webkit-min-device-pixel-ratio)
  10. 前端dom元素的操作优化建议
  11. ubuntu18.04搭建hive
  12. 锋利的jQuery初学(4)
  13. ffmpeg-3.2.4-static-win32-for-XP-bin.tar.xz
  14. SQL 资源整理
  15. 『Python』库安装
  16. Gtk-WARNING **: cannot open display: :0.0
  17. C++17尝鲜:结构化绑定声明(Structured Binding Declaration)
  18. mac 下搭建Elasticsearch 5.4.3分布式集群
  19. c++的class声明及相比java的更合理之处
  20. Manta

热门文章

  1. 笨办法学python 习题14 优化过 遇到问题的请看
  2. 《Docker Deep Dive》Note - Docker 引擎
  3. (转)nginx与PHP的关系
  4. [清晰] kubernets权威指南第2版
  5. Java JDK1.8源码学习之路 2 String
  6. 在windows中使用PuTTy上传下载文件和目录
  7. C#_WPF中创建二维码、识别二维码
  8. 5_PHP数组_3_数组处理函数及其应用_3_数组指针函数
  9. Multipath 多路径配置说明
  10. Objective-C学习笔记 利用协议实现回调函数