MyBatis简介

  • 使用数据库连接池初始化连接资源
  • 将sql语句抽取到xml配置文件中
  • 使用反射、内省等底层技术,自动将实体与表进行属性与字段的自动映射

开发步骤:

  1. 添加mybatis坐标
  2. 创建数据表
  3. 编写实体类
  4. 编写映射文件xxxMapper.xml
  5. 编写核心文件SqlMapConfig.xml
  6. 编写测试类

MyBatis增删改查操作

插入操作
  • 插入语句使用insert标签
  • 在映射文件中使用parameterType属性指定要插入的数据类型
  • Sql语句中使用#{实体属性名}方式引用实体中的属性值
  • 插入操作使用的API是sqlSession.insert(“命名空间.id”,实体对象);
  • 插入操作涉及数据库数据变化。所以要使用sqlSession对象显示的提交事务,即sqlSession.commit()
修改操作
  • 修改语句使用update标签
  • 修改操作使用的API是sqlSession.update(“命名空间.id”,实体对象);
删除操作
  • 删除语句使用delete标签
  • 删除操作使用的API是sqlSession.delete(“命名空间.id”,Integer);

MyBatis核心配置文件解析

environments标签

可以包括多个环境

environments

mapper标签

该标签作用是加载映射

  • 使用相对于类路径的资源引用,例如: <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
  • 使用完全限定资源定位符(URL),例如: <mapper url="file:///var/mappers/AuthorMapper.xml"/>
  • 使用映射器接口实现类的完全限定类名,例如: <mapper class="org.mybatis.builderAuthorMapper'/>
  • 将包内的映射器接口实现全部主册为映射器,例如:<package name="org.mybatis.builder" />
properties标签

加载额外配置的properties文件

typeAliases标签

类型别名为java类型设置一个别名,基本数据类型不需要

<typeAliases>
    <typeAlias type="top.handsomelv.domain.User" alias="user"/>
</typeAliases>

MyBatis代理开发方式

采用Mybatis的代理开发方式实现DAO层的开发

Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。

Mapper接口开发需要遵循以下规范:

  1. Mapper.xml文件中的namespace与mapper接口的全限定名相同
  2. Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
  3. Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同
  4. Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
getMapper(mapper.class);

动态SQL语句

<if

条件判断

<select id="findByCondition" resultType="user" parameterType="user">
        select * from user
        <where>
            <if test="id!=0">
                id=#{id}
            </if>
            <if test="username!=null">
                and username=#{username}
            </if>
            <if test="password!=null">
                and password=#{password}
            </if>
        </where>
    </select>
<foreach

循环执行sql的拼接操作,例如select * from user where id in (1,2,5)

<select id="findByIds" parameterType="list" resultType="user">
        select *
        from user
        <where>
            <foreach collection="list" open="id in (" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </where>
    </select>
SQL片段抽取

将重复的sql提取出来,使用时用include引用

<sql id="select">select * from user</sql>
<include refid="select"></include>

核心配置文件深入

typeHandlers标签

类型处理器将获取的值以合适的方式转换成java类型。

自定义类型处理器处理不支持的非标准类型;实现org.apache.ibatis.type.TypeHandler接口,或继承一个很便利的类org.apache.ibatis.type.BaseTypeHandler,然后可以选择性地将它映射到一个JDBC类型。

  1. 定义转换类继承类BaseTypeHandler
  2. 覆盖4个未实现的方法,其中setNonNullarameter为java程序设置数据到数据库的回调方法,getNullableResult为查询时 mysql的字符串类型转换成java的Type类型的方法
  3. 在MyBatis核心配置文件中进行注册
plugins标签

Mybatis使用第三方插件进行功能拓展,分页助手PageHelper将分页的复杂操作进行封装,使用简单的方式获得分页的相关数据

  1. 导入通用PageHelper坐标
  2. 核心配置文件配置插件
  3. 测试分页数据获取

MyBatis多表操作

一对一查询

案例:查询一个订单同时查询出订单所属的用户

因此Order包含User类

association定义多对一关系

  1. 创建数据库模型
  2. 撰写一对一查询的语句
  3. 创建实体
  4. 创建接口
  5. 配置映射文件xml
<resultMap id="orderMap" type="order">
        <!--手动指定字段与实体属性的映射关系
            column: 数据表的字段名称
            property:实体的属性名称
        -->
        <id column="oid" property="id"></id>
        <result column="ordertime" property="ordertime"></result>
        <result column="total" property="total"></result>
        <!--<result column="uid" property="user.id"></result>
        <result column="username" property="user.username"></result>
        <result column="password" property="user.password"></result>
        <result column="birthday" property="user.birthday"></result>-->

        <!--
            property: 当前实体(order)中的属性名称(private User user)
            javaType: 当前实体(order)中的属性的类型(User)
        -->
        <association property="user" javaType="user">
            <id column="uid" property="id"></id>
            <result column="username" property="username"></result>
            <result column="password" property="password"></result>
            <result column="birthday" property="birthday"></result>
        </association>

    </resultMap>

    <select id="findAll" resultMap="orderMap">
        SELECT *, o.id oid
        FROM orders o,
             USER u
        WHERE o.uid = u.id
    </select>
一对多查询

案例:一个用户的所有订单

collection对应一对多关系

多对多查询

与一对多查询类似

Mybatis注解开发

常用注解
  • @Insert|Delete|Update|Select:实现增删改查
  • @Result|Results:实现结果集或多个结果集封装
  • @One:实现一对一结果集封装
  • @Many:实现一对多结果集封装
<!--加载映射关系-->
    <mappers>
        <!--指定接口所在的包-->
        <package name="com.itheima.mapper"></package>
    </mappers>

@Result:

  • 代替了<id>标签和<result>标签
  • column:数据库列名,property:需要装配的属性名
  • one:需要使用的@One注解(@Result(one=@One)())
  • many:需要使用的@many注解(@Result(many=@many)())

@One:

代替了<assocation>标签,在注解中用来指定子查询返回单一对象

select:指定用来多表查询的sqlmapper

使用格式:@Result(property= "" ,column="" ,@One(select=""))

@Many:

代替了<collection>标签,在注解中用来指定子查询返回对象集合

使用格式:@Result(property= "" ,column="" ,@Many(select=""))