一、初始MyBatis
一、框架技术
框架(Framework)是一个提供了可重用的公共结构的半成品。
二、MyBatis介绍
1、数据持久化概念
- 数据持久化是将内存中数据模型转换为存储模型,以及将存储模型转换为内存中的数据模型的统称。
- MyBatis的前身是iBatis。
2、MyBatis框架及ORM
-
MyBatis框架简介
MyBatis是一个开源的数据持久层框架
-
ROM
ROM是对象/关系映射,是一种数据持久化技术,是一种半自动化的ROM实现
-
MyBatis是ORM解决方案
通过MyBatis建立SQL关系映射
3、MyBatis环境搭建
-
下载需要的jar文件
-
部署jar文件
<dependencies> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> </dependencies> -
创建MyBatis核心配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/><property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> </configuration> -
创建持久化类(POJO)和SQL映射文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.mybatis.example.BlogMapper"> <select id="selectBlog" resultType="Blog">select * from Blog where id = #{id} </select> <insert></insert> </mapper>持久化类是指其实例状态需要被MyBatis持久化到数据库中的类
-
创建测试类:test包
4、MyBatis框架的优缺点
-
MyBatis框架的优点
-
与JDBC相比,减少了50%以上的代码量
-
MyBatis是最简单的持久化框架,小巧并且简单易学
-
提供XML标签,支持编写动态SQL语句
-
提供映射标签,支持对象与数据库的ORM字段关系映射
-
-
MyBatis框架的缺点
- SQL语句的编写工作量较大
- SQL语句依赖数据库
三、MyBatis的基本要素——核心对象
1、MyBatis的三个基本要素:
- 核心接口和类
- MyBatis核心配置文件(mybatis-config.xml)
- SQL映射文件(mapper.xml)
2、SQlSessionFactoryBuilder
-
SQlSessionFactoryBuilder的作用
负责构建SQLSeeionFactory,并且提供了多个build()方法的重载。
-
SQLSessionFactoryBuilder的生命周期和作用域
最大特点是:用过即丢,最佳范围就是存在于方法体内,也就是局部变量而已。
四、MyBatis的基本要素——核心配置文件
MyBatis-config.xml文件
-
properties元素:properties元素描述的都是外部化,可替代的属性。
-
settings元素:设置一些非常重要的设置选项,用于设置和改变MyBatis运行中行为。
-
typeAliases元素:是配置类型别名
<?xml version="1.0" encoding="UTF-8" ?> `<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/><property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> </configuration>
二、SQL映射文件
一、SQL映射文件
元素配置:mapper、cache、resultMap、sql、insert、update、delete、select
- id:命名空间中唯一的标识符、可以被用来引用这条语句
- parameter:查询语句传入参数语句传入参数的类型的完全限定名或别名
- resultType:查询语句返回结果类型的完全限定名或别名
二、resultMap
resultMap元素用来描述如何将结果集映射到java对象
-
resultType
resultType直接表示放回类型,包括基础数据类型和复杂数据类型
-
resultMap
对外部resultMao定义的引用,对应外部resultMap的id
-
resultType和resultMap
resultType和resultMap属性不能同时存在,只能有一个
三、MyBatis增删改
- insert:id、parameterType
- delete:id、parameterType
- update:id、parameterType
- insert、update、delete元素中没有resultType属性
使用@Param注解
- 语法:int update(@Param(“id”)Integer id)
- 超过4个以上的参数最好封装成对象
四、resultMap高级结果映射
1、基本配置项
1、属性
id:唯一标识
type映射结果类型
2、子节点
子节点id和result均可实现最基本的结果集映射
2、association
仅处理一对一的关联关系
属性:
- javaType:完整java类名或者别名
- property:映射数据库列的实体对象的属性
- id
- result
3、collection
一对多的关联关系的处理
属性:
- ofType:完整java类名或者别名
- property:映射数据库列的实体对象的属性
五、resultMap自动映射和MyBatis缓存
1、resultMap自动映射级别
自动映射的三个匹配级别:
- none:禁止地洞匹配
- partial(默认):自动匹配所有属性,有内部嵌套(association、collection)的除外
- full:自动匹配所有
2、MyBatis缓存
-
一级缓存
基于PerpetualCache的HashMap本地缓存,作用为seession域内
-
二级缓存
一级缓存缓存的是sql语句,二级缓存缓存的是结果对象
三、动态SQL
一、if+where多条件查询
-
if
<if test='判断条件'> 值 </if> -
where
<where> <if test='判断条件'> and 值 </if> </where>
二、if+trim多条件查询
<trim prefix="WHERE" prefixOverrides="AND |OR">
<if test="判断条件"> AND name=#{name}</if>
<if test="判断条件"> AND gender=#{gender}</if>
</trim>
属性:
- prefix:前缀
- suffix:后缀
- prefixOverrides:对于trim包含内容的首部进行指定内容的忽略
- suffixOverrides:对于trim包含内容的首尾进行制定内容的忽略
三、if+set更新
<set>
<if test='判断条件'>
值,
</if>
</set>
可以配置set关键字,还可删除追加到条件末尾的任何不相关的逗号
四、foreach
-
foreach元素的属性主要有 item,index,collection,open,separator,close。
-
item表示集合中每一个元素进行迭代时的别名。
-
index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置。
-
open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符。
-
array数组的类型:
<select id="findForeachUser" resultType="User"> select * from t_user where id in <foreach collection="array" index="index" item="id" open="(" separator="," close=")"> #{id} </foreach </select> -
Map的类型
参数为Map:键值不变
<select id="findForeachUser" resultType="User"> select * from t_user where user_name like "%"#{userName}"%" and id in <foreach collection="ids" index="index" item="id" open="(" separator="," close=")"> #{id} </foreach> </select>
五、choose(when、otherwise)
<choose>
<when test="判断条件 ">
值
</when >
<when test="判断条件 ">
值
</when >
<when test="判断条件">
值
</when >
<otherwise>
</otherwise>
</choose>
- when元素表示当 when 中的条件满足的时候就输出其中的内容
- otherwise当when中所有条件都不满足的时候,就会自动输出otherwise元素中的内容
六、MyBatis分页
mybatis.config.xml导入jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<!-- jsqlparser -->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>3.0</version>
</dependency>
配置拦截
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="reasonable" value="true"/>
</plugin>
</plugins>
SQL语句中,要放在要执行的SQL的上面
PageHelper.startPage(起始位置, 页面容量, true);

330

被折叠的 条评论
为什么被折叠?



