初始MyBatis简介

一、初始MyBatis

一、框架技术

框架(Framework)是一个提供了可重用的公共结构的半成品。

二、MyBatis介绍

1、数据持久化概念

  • 数据持久化是将内存中数据模型转换为存储模型,以及将存储模型转换为内存中的数据模型的统称。
  • MyBatis的前身是iBatis。

2、MyBatis框架及ORM

  1. MyBatis框架简介

    MyBatis是一个开源的数据持久层框架

  2. ROM

    ROM是对象/关系映射,是一种数据持久化技术,是一种半自动化的ROM实现

  3. MyBatis是ORM解决方案

    通过MyBatis建立SQL关系映射

3、MyBatis环境搭建

  1. 下载需要的jar文件

    https://mvnrepository.com/

  2. 部署jar文件

    <dependencies>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>
        
    </dependencies>
    
  3. 创建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>
    
  4. 创建持久化类(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持久化到数据库中的类

  5. 创建测试类:test包

4、MyBatis框架的优缺点

  1. MyBatis框架的优点
    • 与JDBC相比,减少了50%以上的代码量

    • MyBatis是最简单的持久化框架,小巧并且简单易学

    • 提供XML标签,支持编写动态SQL语句

    • 提供映射标签,支持对象与数据库的ORM字段关系映射

  2. MyBatis框架的缺点
    • SQL语句的编写工作量较大
    • SQL语句依赖数据库

三、MyBatis的基本要素——核心对象

1、MyBatis的三个基本要素:

  1. 核心接口和类
  2. MyBatis核心配置文件(mybatis-config.xml)
  3. SQL映射文件(mapper.xml)

2、SQlSessionFactoryBuilder

  1. SQlSessionFactoryBuilder的作用

    负责构建SQLSeeionFactory,并且提供了多个build()方法的重载。

  2. SQLSessionFactoryBuilder的生命周期和作用域

    最大特点是:用过即丢,最佳范围就是存在于方法体内,也就是局部变量而已。

四、MyBatis的基本要素——核心配置文件

MyBatis-config.xml文件

  1. properties元素:properties元素描述的都是外部化,可替代的属性。

  2. settings元素:设置一些非常重要的设置选项,用于设置和改变MyBatis运行中行为。

  3. 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

  1. id:命名空间中唯一的标识符、可以被用来引用这条语句
  2. parameter:查询语句传入参数语句传入参数的类型的完全限定名或别名
  3. resultType:查询语句返回结果类型的完全限定名或别名

二、resultMap

resultMap元素用来描述如何将结果集映射到java对象

  1. resultType

    resultType直接表示放回类型,包括基础数据类型和复杂数据类型

  2. resultMap

    对外部resultMao定义的引用,对应外部resultMap的id

  3. resultType和resultMap

    resultType和resultMap属性不能同时存在,只能有一个

三、MyBatis增删改

  1. insert:id、parameterType
  2. delete:id、parameterType
  3. update:id、parameterType
  4. insert、update、delete元素中没有resultType属性

使用@Param注解

  1. 语法:int update(@Param(“id”)Integer id)
  2. 超过4个以上的参数最好封装成对象

四、resultMap高级结果映射

1、基本配置项

1、属性

id:唯一标识

type映射结果类型

2、子节点

子节点id和result均可实现最基本的结果集映射

2、association

仅处理一对一的关联关系

属性:

  1. javaType:完整java类名或者别名
  2. property:映射数据库列的实体对象的属性
  3. id
  4. result

3、collection

一对多的关联关系的处理

属性:

  1. ofType:完整java类名或者别名
  2. property:映射数据库列的实体对象的属性

五、resultMap自动映射和MyBatis缓存

1、resultMap自动映射级别

自动映射的三个匹配级别:

  1. none:禁止地洞匹配
  2. partial(默认):自动匹配所有属性,有内部嵌套(association、collection)的除外
  3. full:自动匹配所有

2、MyBatis缓存

  1. 一级缓存

    基于PerpetualCache的HashMap本地缓存,作用为seession域内

  2. 二级缓存

    一级缓存缓存的是sql语句,二级缓存缓存的是结果对象

三、动态SQL

一、if+where多条件查询

  1. if

    <if test='判断条件'></if>
    
  2. 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>
 

属性:

  1. prefix:前缀
  2. suffix:后缀
  3. prefixOverrides:对于trim包含内容的首部进行指定内容的忽略
  4. suffixOverrides:对于trim包含内容的首尾进行制定内容的忽略

三、if+set更新

<set>
<if test='判断条件'>
 值,
</if>
</set>

可以配置set关键字,还可删除追加到条件末尾的任何不相关的逗号

四、foreach

  1. foreach元素的属性主要有 item,index,collection,open,separator,close。

  2. item表示集合中每一个元素进行迭代时的别名。

  3. index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置。

  4. open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符。

  5. 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>
    
  6. 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> 
  1. when元素表示当 when 中的条件满足的时候就输出其中的内容
  2. 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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值