Mybatis框架

目录

一、Mybatis框架概述

 1. Mybatis的含义:

 2. 背景:

二、Mybatis的使用

环境准备

(1)数据库表(tb_brand)及数据准备

(2)实体类 Brand在 com.itheima.pojo 包下创建 Brand 实体类。

(3)编写测试用例测试代码需要在 test/java 目录下创建包及测试用例。

 (4)安装 MyBatisX 插件

1. 配置文件实现实现CRUD

(1)查询所有数据

a. 编写接口方法

b. 编写SQL语句

c. 编写测试方法

d. 数据封装失败及解决方案

    问题:

    原因:

解决方案:  ① 给字段起别名

           ② 使用resultMap定义字段和属性的映射关系

(2)查询详情(根据id查询一条记录)

a. 编写接口方法

b. 编写SQL语句

c. 编写测试方法

 d. 参数占位符

e. parameterType使用

f. SQL语句中特殊字段处理

(3)多条件查询

a. 编写接口方法

b.  编写SQL语句

c. 编写测试方法

(4) 多条件动态SQL

            (a)if 标签:条件判断

            (b)where 标签

(5) 单条件动态SQL

              (c)choose 标签 

a. 编写 接口方法:

b. 编写SQL语句 :

(6)添加数据

a. 编写接口方法

b. 编写SQL语句

c. 编写测试方法

d. 添加数据-主键返回

 (7)修改数据

a. 编写接口方法

b. 编写SQL语句

           (d) set 标签

c. 编写测试方法

(8)删除单条数据

a. 编写接口方法

b. 编写SQL语句

c. 编写测试方法

(9)批量删除

a.  编写接口方法

b. 编写SQL语句

          (e) foreach 标签

c. 编写测试方法 

(10)Mybatis参数传递

a. 多个参数

b. 单个参数

2. 注解实现CRUD

(1) 使用注解开发会比配置文件开发更加方便。

(2) Mybatis 针对 CURD 操作都提供了对应的注解,已经做到见名知意。

(3) 代码实现:

(4) 注解完成简单功能,配置文件完成复杂功能。


一、Mybatis框架概述

 1. Mybatis的含义:

       (1)Mybatis框架是一个持久层框架,几乎解决了jdbc代码在手动设置参数和对结果集的手动获取问题,Mybatis会将参数封装在一个对象中传递给数据库,并将sql语句执行后的结果集封装成对象。

        (2)它提供全局配置文件,建立与数据库的连接;将接口进行分装并提供类和方法实现对数据库的链接和操作;对sql语句执行后的结果进行高级映射并封装成对象;支持动态sql;支持缓存。

        (3) Mybatis中存在自动映射,因为当在数据库中查询的表的属性名和类中的属性名完全相同时才会自动映射并封装,所以需要保证类中的属性和数据库中的属性名相同;如果类中的属性为私有属性,那么类中必须实现get和set方法;还需要保证类中要有无参构造方法,由于mybatis在数据库中查询数据后需要创建对象,调用对应类的无参构造方法,如果找不到对应的无参构造方法,mybatis就会报错;当数据库中的属性名存在驼峰命名,mybatis也是会进行自动映射,但前提是需要开启属性mapUnderscoreToCamelCase,这个属性需要在全局配置文件中设置,表示是否开启驼峰命名的自动映射,true为开启,开启后例如在数据库中的属性名为student_id,那么类中的属性只要设置为studentId这种驼峰命名的方式就可以进行自动映射。

2. 背景:

           Mybatis原本是apache公司的开源项目,最后转给Google公司。

二、Mybatis的使用

环境准备

(1)数据库表(tb_brand)及数据准备

-- 删除tb_brand表
drop table if exists tb_brand;
-- 创建tb_brand表
create table tb_brand
(
-- id 主键
id int primary key auto_increment,
-- 品牌名称
brand_name varchar(20),
-- 企业名称
company_name varchar(20),
-- 排序字段
ordered int,
-- 描述信息
description varchar(100),
-- 状态:0:禁用 1:启用


status int
);
-- 添加数据
insert into tb_brand (brand_name, company_name, ordered, description, status)
values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),
('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联
的智能世界', 1),
('小米', '小米科技有限公司', 50, 'are you ok', 1);

(2)实体类 Brand在 com.itheima.pojo 包下创建 Brand 实体类。

public class Brand {
// id 主键
  private Integer id;
// 品牌名称
  private String brandName;
// 企业名称
  private String companyName;
// 排序字段
  private Integer ordered;
// 描述信息
  private String description;
// 状态:0:禁用 1:启用
  private Integer status;
//省略 setter and getter。自己写时要补全这部分代码
}

(3)编写测试用例测试代码需要在 test/java 目录下创建包及测试用例。

 (4)安装 MyBatisX 插件

        MybatisX 是一款基于 IDEA 的快速开发插件,为效率而生。

主要功能:
              XML映射配置文件 和 接口方法 间相互跳转
              根据接口方法生成 statement

安装方式:
                点击 file ,选择 settings ,就能看到如下图所示界面,注意:安装完毕后需要重启IDEA

 插件效果:

                 红色头绳的表示映射配置文件,蓝色头绳的表示mapper接口。在mapper接口点击红色头绳的小鸟图标会自动跳转到对应的映射配置文件,在映射配置文件中点击蓝色头绳的小鸟图标会自动跳转到对应的mapper接口。也可以在mapper接口中定义方法,自动生成映射配置文件中的 statement ,如图所示

 

1. 配置文件实现实现CRUD

(1)查询所有数据

a. 编写接口方法

     在 com.itheima.mapper 包写创建名为 BrandMapper 的接口。并在该接口中定义 List<Brand> selectAll() 方法。

public interface BrandMapper {
/**
* 查询所有
*/
  List<Brand> selectAll();
}
b. 编写SQL语句

    在 reources 下创建 com/itheima/mapper 目录结构,并在该目录下创建名为 BrandMapper.xml 的映射配置文件

<?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.itheima.mapper.BrandMapper">
     <select id="selectAll" resultType="brand">
         select *
         from tb_brand;
     </select>
</mapper>
c. 编写测试方法

    在 MybatisTest 类中编写测试查询所有的方法

@Test
public void testSelectAll() throws IOException {
    //1. 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new 
SqlSessionFactoryBuilder().build(inputStream);

    //2. 获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();

    //3. 获取Mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    //4. 执行方法
    List<Brand> brands = brandMapper.selectAll();
    System.out.println(brands);

    //5. 释放资源
    sqlSession.close();

}

执行测试方法结果如下:

d. 数据封装失败及解决方案
    问题:从上面结果我们看到了问题,有些数据封装成功了,而有些数据并没有封装成功。
    原因:实体类属性名 和 数据库表列名 不一致,不能自动封装数据

解决方案
  ① 给字段起别名

       只需要保持这两部分的名称一致这个问题就迎刃而解,可以在写sql语句时给这两个字段起别名,将别名定义成和属性名一致即可。

<select id="selectAll" resultType="brand">
    select
    id, brand_name as brandName, company_name as companyName, ordered, description, status
    from tb_brand;
</select>

       而上面的SQL语句中的字段列表书写麻烦,如果表中还有更多的字段,同时其他的功能也需要查询这些字段时就显得我们的代码不够精炼。Mybatis提供了 sql 片段可以提高sql的复用性。

       将需要复用的SQL片段抽取到 sql 标签中   

<sql id="brand_column">
	id, brand_name as brandName, company_name as companyName, ordered, description, status
</sql>

      id属性值是唯一标识,引用时也是通过该值进行引用。


      ② 使用resultMap定义字段和属性的映射关系

      起别名 + sql片段的方式可以解决上述问题,但是它也存在问题。如果还有功能只需要查询部分字段,而不是查询所有字段,那么我们就需要再定义一个 SQL 片段,这就显得不是那么灵活。  

      可以使用resultMap来定义字段和属性的映射关系的方式解决上述问题。

       在映射配置文件中使用resultMap定义 字段 和 属性 的映射关系     

<resultMap id="brandResultMap" type="brand">
    <!--
            id:完成主键字段的映射
                column:表的列名
                property:实体类的属性名
            result:完成一般字段的映射
                column:表的列名
                property:实体类的属性名
        -->
    <result column="brand_name" property="brandName"/>
    <result column="company_name" property="companyName"/>
</resultMap>

注意:在上面只需要定义 字段名 和 属性名 不一样的映射,而一样的则不需要专门定义出来。

       SQL语句正常编写

<select id="selectAll" resultMap="brandResultMap">
    select *
    from tb_brand;
</select>

(2)查询详情(根据id查询一条记录)

a. 编写接口方法

BrandMapper 接口中定义根据id查询数据的方法

/**
  * 查看详情:根据Id查询
  */
Brand selectById(int id);
b. 编写SQL语句
<select id="selectById"  resultMap="brandResultMap">
    select *
    from tb_brand where id = #{id};
</select>
c. 编写测试方法

test/java 下的 com.itheima.mapper 包下的 MybatisTest类中 定义测试方法

 @Test
public void testSelectById() throws IOException {
    //接收参数,该id以后需要传递过来
    int id = 1;

    //1. 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    //2. 获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();

    //3. 获取Mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    //4. 执行方法
    Brand brand = brandMapper.selectById(id);
    System.out.println(brand);

    //5. 释放资源
    sqlSession.close();
}

执行测试方法结果如下:

 d. 参数占位符

    控制台显示的SQL语句,能看到使用 ?进行占位。说明在映射配置文件中的写的 #{id} 最终会被 ?进行占位。

   mybatis提供了两种参数占位符:
  • #{} :执行SQL时,会将 #{} 占位符替换为 ?,将来自动设置参数值。从上述例子可以看出使用#{} 底层使用的是 PreparedStatement

  • ${} :拼接SQL。底层使用的是 Statement,会存在SQL注入问题。如下图将映射配置文件中的 #{} 替换成 ${} 来看效果

    <select id="selectById"  resultMap="brandResultMap">
        select *
        from tb_brand where id = ${id};
    </select>

    重新运行查看结果如下:

e. parameterType使用

       对于有参数的mapper接口方法,在映射配置文件中应该配置 ParameterType 来指定参数类型。只不过该属性可以省略。

<select id="selectById" parameterType="int" resultMap="brandResultMap">
    select *
    from tb_brand where id = ${id};
</select>
f. SQL语句中特殊字段处理

       在SQL语句中写特殊字符,比如某一个字段大于某个值,如下图

       可以看出报错了,因为映射配置文件是xml类型的问题,而 > < 等这些字符在xml中有特殊含义,所以此时我们需要将这些符号进行转义,可以使用以下两种方式进行转义

(a) 转义字符

下图的 &lt; 就是 < 的转义字符。

 (b) CDATA标签

(3)多条件查询

a. 编写接口方法

BrandMapper 接口中定义多条件查询的方法。

Mybatis针对多参数有多种实现:

  • 使用 @Param("参数名称") 标记每一个参数,在映射配置文件中就需要使用 #{参数名称} 进行占位

    List<Brand> selectByCondition(@Param("status") int status, 
    @Param("companyName") String companyName,@Param("brandName") String brandName);
  • 将多个参数封装成一个 实体对象 ,将该实体对象作为接口的方法参数。该方式要求在映射配置文件的SQL中使用 #{内容} 时,里面的内容必须和实体类属性名保持一致。

    List<Brand> selectByCondition(Brand brand);
  • 将多个参数封装到map集合中,将map集合作为接口的方法参数。该方式要求在映射配置文件的SQL中使用 #{内容} 时,里面的内容必须和map集合中键的名称一致。

    List<Brand> selectByCondition(Map map);
b.  编写SQL语句

BrandMapper.xml 映射配置文件中编写 statement,使用 resultMap 而不是使用 resultType

<select id="selectByCondition" resultMap="brandResultMap">
    select *
    from tb_brand
    where status = #{status}
    and company_name like #{companyName}
    and brand_name like #{brandName}
</select>
c. 编写测试方法

test/java 下的 com.itheima.mapper 包下的 MybatisTest类中 定义测试方法

@Test
public void testSelectByCondition() throws IOException {
    //接收参数
    int status = 1;
    String companyName = "华为";
    String brandName = "华为";

    // 处理参数
    companyName = "%" + companyName + "%";
    brandName = "%" + brandName + "%";

    //1. 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //2. 获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //3. 获取Mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

    //4. 执行方法
	//方式一 :接口方法参数使用 @Param 方式调用的方法
    //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
    //方式二 :接口方法参数是 实体类对象 方式调用的方法
     //封装对象
    /* Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);*/
    
    //List<Brand> brands = brandMapper.selectByCondition(brand);
    
    //方式三 :接口方法参数是 map集合对象 方式调用的方法
    Map map = new HashMap();
    map.put("status" , status);
    map.put("companyName", companyName);
    map.put("brandName" , brandName);
    List<Brand> brands = brandMapper.selectByCondition(map);
    System.out.println(brands);

    //5. 释放资源
    sqlSession.close();
}

(4) 多条件动态SQL

上述功能实现存在很大的问题。用户在输入条件时,肯定不会所有的条件都填写,这个时候我们的SQL语句就不能那样写的

例如用户只输入 当前状态 时,SQL语句就是

select * from tb_brand where status = #{status}

而用户如果只输入企业名称时,SQL语句就是

select * from tb_brand where company_name like #{companName}

而用户如果输入了 当前状态企业名称 时,SQL语句又不一样

select * from tb_brand where status = #{status} and company_name like #{companName}

针对上述的需要,Mybatis对动态SQL有很强大的支撑:

- if
- choose (when, otherwise)
- trim (where, set)
- foreach
(a)if 标签:条件判断
  • test 属性:逻辑表达式

    <select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        where
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="companyName != null and companyName != '' ">
                and company_name like #{companyName}
            </if>
            <if test="brandName != null and brandName != '' ">
                and brand_name like #{brandName}
            </if>
    </select>

如上的这种SQL语句就会根据传递的参数值进行动态的拼接。如果此时status和companyName有值那么就会值拼接这两个条件。

执行结果如下:

但是它也存在问题,如果此时给的参数值是

Map map = new HashMap();
// map.put("status" , status);
map.put("companyName", companyName);
map.put("brandName" , brandName);

拼接的SQL语句就变成了

select * from tb_brand where and company_name like ? and brand_name like ?

而上面的语句中 where 关键后直接跟 and 关键字,这就是一条错误的SQL语句。这个就可以使用 where 标签解决

(b)where 标签
  • 作用:

    • 如果所有的参数没有值则不加where关键字

      <select id="selectByCondition" resultMap="brandResultMap">
          select *
          from tb_brand
          <where>
              <if test="status != null">
                  and status = #{status}
              </if>
              <if test="companyName != null and companyName != '' ">
                  and company_name like #{companyName}
              </if>
              <if test="brandName != null and brandName != '' ">
                  and brand_name like #{brandName}
              </if>
          </where>
      </select>

      注意:需要给每个条件前都加上 and 关键字。

    • 会动态的去掉第一个条件前的 and

    • 替换where关键字

(5) 单条件动态SQL

       查询时只能选择三个条件中的一个,但是用户到底选择哪儿一个,我们并不能确定。这种就属于单个条件的动态SQL语句。

      这种需求需要使用到 choose(when,otherwise)标签 实现,

   (c)choose 标签 

                                   类似于Java 中的switch语句

a. 编写 接口方法:

BrandMapper 接口中定义单条件查询的方法。

/**
  * 单条件动态查询
  * @param brand
  * @return
  */
List<Brand> selectByConditionSingle(Brand brand);
b. 编写SQL语句 :

BrandMapper.xml 映射配置文件中编写 statement,使用 resultMap 而不是使用 resultType

<select id="selectByConditionSingle" resultMap="brandResultMap">
    select *
    from tb_brand
    <where>
        <choose><!--相当于switch-->
            <when test="status != null"><!--相当于case-->
                status = #{status}
            </when>
            <when test="companyName != null and companyName != '' "><!--相当于case-->
                company_name like #{companyName}
            </when>
            <when test="brandName != null and brandName != ''"><!--相当于case-->
                brand_name like #{brandName}
            </when>
        </choose>
    </where>
</select>

c.编写测试方法:

BrandMapper.xml 映射配置文件中编写 statement,使用 resultMap 而不是使用 resultType

@Test
public void testSelectByConditionSingle() throws IOException {
    //接收参数
    int status = 1;
    String companyName = "华为";
    String brandName = "华为";

    // 处理参数
    companyName = "%" + companyName + "%";
    brandName = "%" + brandName + "%";

    //封装对象
    Brand brand = new Brand();
    //brand.setStatus(status);
    brand.setCompanyName(companyName);
    //brand.setBrandName(brandName);

    //1. 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //2. 获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //3. 获取Mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    //4. 执行方法
    List<Brand> brands = brandMapper.selectByConditionSingle(brand);
    System.out.println(brands);

    //5. 释放资源
    sqlSession.close();
}

执行测试方法结果如下:

(6)添加数据

a. 编写接口方法

BrandMapper 接口中定义添加方法。

 /**
   * 添加
   */
void add(Brand brand);
b. 编写SQL语句

BrandMapper.xml 映射配置文件中编写添加数据的 statement

<insert id="add">
    insert into tb_brand (brand_name, company_name, ordered, description, status)
    values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
</insert>
c. 编写测试方法

test/java 下的 com.itheima.mapper 包下的 MybatisTest类中 定义测试方法

@Test
public void testAdd() throws IOException {
    //接收参数
    int status = 1;
    String companyName = "波导手机";
    String brandName = "波导";
    String description = "手机中的战斗机";
    int ordered = 100;

    //封装对象
    Brand brand = new Brand();
    brand.setStatus(status);
    brand.setCompanyName(companyName);
    brand.setBrandName(brandName);
    brand.setDescription(description);
    brand.setOrdered(ordered);

    //1. 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //2. 获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //SqlSession sqlSession = sqlSessionFactory.openSession(true); //设置自动提交事务,这种情况不需要手动提交事务了
    //3. 获取Mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    //4. 执行方法
    brandMapper.add(brand);
    //提交事务
    sqlSession.commit();
    //5. 释放资源
    sqlSession.close();
}

执行结果如下:

d. 添加数据-主键返回

在数据添加成功后,有时候需要获取插入数据库数据的主键(主键是自增长)。

在 insert 标签上添加如下属性:

- useGeneratedKeys:能够获取自动增长的主键值。true表示获取
- keyProperty  :指定将获取到的主键值封装到哪儿个属性里

将映射配置文件里 statement 进行修改,如下

<insert id="add" useGeneratedKeys="true" keyProperty="id">
    insert into tb_brand (brand_name, company_name, ordered, description, status)
    values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
</insert>

 (7)修改数据

a. 编写接口方法

BrandMapper 接口中定义修改方法。

/**
   * 修改
   */
void update(Brand brand);

上述方法参数 Brand 就是封装了需要修改的数据,而id肯定是有数据的,这也是和添加方法的区别。

b. 编写SQL语句

BrandMapper.xml 映射配置文件中编写修改数据的 statement

<update id="update">
    update tb_brand
    <set>
        <if test="brandName != null and brandName != ''">
            brand_name = #{brandName},
        </if>
        <if test="companyName != null and companyName != ''">
            company_name = #{companyName},
        </if>
        <if test="ordered != null">
            ordered = #{ordered},
        </if>
        <if test="description != null and description != ''">
            description = #{description},
        </if>
        <if test="status != null">
            status = #{status}
        </if>
    </set>
    where id = #{id};
</update>
(d) set 标签

                   可以用于动态包含需要更新的列,忽略其它不更新的列。

c. 编写测试方法

test/java 下的 com.itheima.mapper 包下的 MybatisTest类中 定义测试方法

@Test
public void testUpdate() throws IOException {
    //接收参数
    int status = 0;
    String companyName = "波导手机";
    String brandName = "波导";
    String description = "波导手机,手机中的战斗机";
    int ordered = 200;
    int id = 6;

    //封装对象
    Brand brand = new Brand();
    brand.setStatus(status);
    //        brand.setCompanyName(companyName);
    //        brand.setBrandName(brandName);
    //        brand.setDescription(description);
    //        brand.setOrdered(ordered);
    brand.setId(id);

    //1. 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //2. 获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //SqlSession sqlSession = sqlSessionFactory.openSession(true);
    //3. 获取Mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    //4. 执行方法
    int count = brandMapper.update(brand);
    System.out.println(count);
    //提交事务
    sqlSession.commit();
    //5. 释放资源
    sqlSession.close();
}

执行测试方法结果如下:

 从结果中SQL语句可以看出,只修改了 status 字段值,因为我们给的数据中只给Brand实体对象的 status 属性设置值了。这就是 set 标签的作用。

(8)删除单条数据

a. 编写接口方法

BrandMapper 接口中定义根据id删除方法。

/**
  * 根据id删除
  */
void deleteById(int id);
b. 编写SQL语句

BrandMapper.xml 映射配置文件中编写删除一行数据的 statement

<delete id="deleteById">
    delete from tb_brand where id = #{id};
</delete>
c. 编写测试方法

test/java 下的 com.itheima.mapper 包下的 MybatisTest类中 定义测试方法

 @Test
public void testDeleteById() throws IOException {
    //接收参数
    int id = 6;

    //1. 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //2. 获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //SqlSession sqlSession = sqlSessionFactory.openSession(true);
    //3. 获取Mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    //4. 执行方法
    brandMapper.deleteById(id);
    //提交事务
    sqlSession.commit();
    //5. 释放资源
    sqlSession.close();
}

(9)批量删除

a.  编写接口方法

BrandMapper 接口中定义删除多行数据的方法。

/**
  * 批量删除
  */
void deleteByIds(int[] ids);
b. 编写SQL语句

BrandMapper.xml 映射配置文件中编写删除多条数据的 statement

编写SQL时需要遍历数组来拼接SQL语句。Mybatis 提供了 foreach 标签供我们使用

(e) foreach 标签

用来迭代任何可迭代的对象(如数组,集合)。

  • collection 属性:

    • mybatis会将数组参数,封装为一个Map集合。

      • 默认:array = 数组

      • 使用@Param注解改变map集合的默认key的名称

  • item 属性:本次迭代获取到的元素。

  • separator 属性:集合项迭代之间的分隔符。foreach 标签不会错误地添加多余的分隔符。也就是最后一次迭代不会加分隔符。

  • open 属性:该属性值是在拼接SQL语句之前拼接的语句,只会拼接一次

  • close 属性:该属性值是在拼接SQL语句拼接后拼接的语句,只会拼接一次

    <delete id="deleteByIds">
        delete from tb_brand where id
        in
        <foreach collection="array" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
        ;
    </delete>
c. 编写测试方法 

test/java 下的 com.itheima.mapper 包下的 MybatisTest类中 定义测试方法

@Test
public void testDeleteByIds() throws IOException {
    //接收参数
    int[] ids = {5,7,8};

    //1. 获取SqlSessionFactory
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //2. 获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //SqlSession sqlSession = sqlSessionFactory.openSession(true);
    //3. 获取Mapper接口的代理对象
    BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    //4. 执行方法
    brandMapper.deleteByIds(ids);
    //提交事务
    sqlSession.commit();
    //5. 释放资源
    sqlSession.close();
}

(10)Mybatis参数传递

Mybatis 接口方法中可以接收各种各样的参数,如下:

  • 多个参数

  • 单个参数:单个参数又可以是如下类型

    • POJO 类型

    • Map 集合类型

    • Collection 集合类型

    • List 集合类型

    • Array 类型

    • 其他类型

a. 多个参数

    接收多个参数需要使用 @Param 注解,那么为什么要加该注解呢?这个问题要弄明白就必须来研究Mybatis 底层对于这些参数是如何处理的。

User select(@Param("username") String username,@Param("password") String password);
<select id="select" resultType="user">
	select *
    from tb_user
    where 
    	username=#{username}
    	and password=#{password}
</select>

      在接口方法中定义多个参数,Mybatis 会将这些参数封装成 Map 集合对象,值就是参数值,而键在没有使用 @Param 注解时有以下命名规则:

  • 以 arg 开头 :第一个参数就叫 arg0,第二个参数就叫 arg1,以此类推。如:

    map.put("arg0",参数值1);

    map.put("arg1",参数值2);

  • 以 param 开头 : 第一个参数就叫 param1,第二个参数就叫 param2,依次类推。如:

    map.put("param1",参数值1);

    map.put("param2",参数值2);

代码验证:

  • UserMapper 接口中定义如下方法

    User select(String username,String password);
  • UserMapper.xml 映射配置文件中定义SQL

    <select id="select" resultType="user">
        select *
        from tb_user
        where 
            username=#{arg0}
            and password=#{arg1}
    </select>

    或者

    <select id="select" resultType="user">
        select *
        from tb_user
        where 
            username=#{param1}
            and password=#{param2}
    </select>
  • 运行代码结果如下

       在映射配合文件的SQL语句中使用用 arg 开头的和 param 书写,代码的可读性会变的特别差,此时可以使用 @Param 注解。

       在接口方法参数上使用 @Param 注解,Mybatis 会将 arg 开头的键名替换为对应注解的属性值。

代码验证:

  • UserMapper 接口中定义如下方法,在 username 参数前加上 @Param 注解

    User select(@Param("username") String username, String password);

    Mybatis 在封装 Map 集合时,键名就会变成如下:

    map.put("username",参数值1);

    map.put("arg1",参数值2);

    map.put("param1",参数值1);

    map.put("param2",参数值2);

  • UserMapper.xml 映射配置文件中定义SQL

    <select id="select" resultType="user">
        select *
        from tb_user
        where 
            username=#{username}
            and password=#{param2}
    </select>
  • 运行程序结果没有报错。而如果将 #{} 中的 username 还是写成 arg0

    <select id="select" resultType="user">
        select *
        from tb_user
        where 
            username=#{arg0}
            and password=#{param2}
    </select>
  • 运行程序则可以看到错误

结论:以后接口参数是多个时,在每个参数上都使用 @Param 注解。这样代码的可读性更高。

b. 单个参数
  • POJO 类型

    直接使用。要求 属性名参数占位符名称 一致

  • Map 集合类型

    直接使用。要求 map集合的键名参数占位符名称 一致

  • Collection 集合类型

    Mybatis 会将集合封装到 map 集合中,如下:

    map.put("arg0",collection集合);

    map.put("collection",collection集合;

    可以使用 @Param 注解替换map集合中默认的 arg 键名。

  • List 集合类型

    Mybatis 会将集合封装到 map 集合中,如下:

    map.put("arg0",list集合);

    map.put("collection",list集合);

    map.put("list",list集合);

    可以使用 @Param 注解替换map集合中默认的 arg 键名。

  • Array 类型

    Mybatis 会将集合封装到 map 集合中,如下:

    map.put("arg0",数组);

    map.put("array",数组);

    可以使用 @Param 注解替换map集合中默认的 arg 键名。

  • 其他类型

    比如int类型,参数占位符名称 叫什么都可以。尽量做到见名知意

2. 注解实现CRUD

(1) 使用注解开发会比配置文件开发更加方便。

@Select(value = "select * from tb_user where id = #{id}")
public User select(int id);

注意:

  • 注解是用来替换映射配置文件方式配置的,所以使用了注解,就不需要再映射配置文件中书写对应的 statement

(2) Mybatis 针对 CURD 操作都提供了对应的注解,已经做到见名知意。

  • 查询 :@Select

  • 添加 :@Insert

  • 修改 :@Update

  • 删除 :@Delete

(3) 代码实现:

  • 将之前案例中 UserMapper.xml 中的 根据id查询数据 的 statement 注释掉

  • UserMapper 接口的 selectById 方法上添加注解

  • 运行测试程序也能正常查询到数据

(4) 注解完成简单功能,配置文件完成复杂功能。

在官方文档中 入门 中有这样的一段话:

         而我们之前写的动态 SQL 就是复杂的功能,如果用注解使用的话,就需要使用到 Mybatis 提供的SQL构建器来完成,而对应的代码如下:

 上述代码将java代码和SQL语句融到了一块,使得代码的可读性大幅度降低。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值