当前位置: 首页 > article >正文

JavaWeb02-MyBatis

目录

一、MyBatis

1.概述

2.JavaEE三层架构简单介绍

(1)表现层

(2)业务层

(3)持久层

3.框架

4.优势

(1)JDBC的劣势

(2)MyBatis优化

5.使用

(1)快速上手

(2)框架使用部分

(3)编码部分

6.Mapper代理开发

(1)Mapper代理开发的好处

(2)Mapper代理使用步骤

7.Mybatis的配置文件说明

8.查询过程出现的问题

9.xml文件中编写SQL语句没有提示

10.查操作

(1)单条件查询

(2)多条件查询

11.动态查询

(1)多条件动态查询

(2)单条件动态查询(多选一)

12.增操作

(1)主键返回

13.删操作

14.改操作

(1)修改字段数据

(2)修改动态字段数据

15.删操作

(1)单个删除

(2)批量删除

16.Mybatis参数传递

(1)单个参数

(2)多个参数

17.注解方式完成增删改查


一、MyBatis

1.概述

  • MyBatis 是一款优秀的持久层框架,用于简化JDBC 开发

  • MyBatis 本是Apache 的一个开源项目iBatis,2010年这个项目由apache softwarefoundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github

  • 网址:mybatis – MyBatis 3 | 简介

辅助插件:MybatisX

2.JavaEE三层架构简单介绍

(1)表现层

页面展示

(2)业务层

逻辑处理

(3)持久层

负责将数据到保存到数据库的那一层代码

3.框架

  • 框架就是一个半成品软件,是一套可重用的、通用的、软件基础代码模型

  • 在框架的基础之上构建软件编写更加高效、规范、通用、可扩展

4.优势

(1)JDBC的劣势
  • 硬编码

    • 注册驱动,获取连接

    • SQL语句

  • 操作较繁琐

    • 手动设置参数

    • 手动封装结果集

(2)MyBatis优化

MyBatis 免除了几乎所有的JDBC代码以及设置参数和获取结果集的工作

5.使用

(1)快速上手

框架使用部分:

  • 创建模块,导入坐标

  • 编写核心配置文件

  • 编写SQL映射文件

编码部分:

  • 定义实体类(POJO类)

  • 加载核心配置文件,获取SqlSessionFactory对象

  • 获取SqlSession对象

  • 释放资源

(2)框架使用部分
  • 导入坐标

     
   <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.9</version>
        </dependency>
<!--        mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
​
        <!-- 添加slf4j日志api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.20</version>
        </dependency>
        <!-- 添加logback-classic依赖 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!-- 添加logback-core依赖 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.3</version>
        </dependency>

  • 核心配置文件,文件名通常:mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
<!--                数据库连接信息-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///test?useSSL=false&amp;useServerPrepStmts=true"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
<!--        加载SQL的映射文件-->
        <mapper resource="ProductMapper.xml"/>
    </mappers>
</configuration>

  • 编写SQL映射文件,名称:要操作的表名+Mapper(ProductMapper.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
    namespace:名称空间
​
-->
<mapper namespace="product">
<!--
    id:sql语句的唯一标识
    resultType:返回结果类型
-->
    <select id="selectAll" resultType="pojo.Product">
        select * from Product;
    </select>
</mapper>

如果映射文件SQL语句中表名爆红,只是警告,并不影响实际操作

产生原因:ldea和数据库没有建立连接,不识别表信息

解决方法:用IDEA与数据库建立连接即可

(3)编码部分
  • 定义实体类

要与数据库中的数据类型对应

  • 使用

public class MybatisDemo {
    public static void main(String[] args) throws IOException {
//        1.加载核心配置文件,直接从官网粘过来就行了,获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
​
//        2.获取sqlSession对象
        final SqlSession sqlSession = sqlSessionFactory.openSession();
​
//        3.执行SQL
        final List<Product> list = sqlSession.selectList("product.selectAll");
​
        System.out.println(list);
//        4.释放资源
        sqlSession.close();
​
    }
}

6.Mapper代理开发

(1)Mapper代理开发的好处
  • 解决原生方式中的硬编码(更安全)

  • 简化后期执行SQL

(2)Mapper代理使用步骤
  • 定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下

检查方式:编译项目

在文件夹中打开

如下图即为成功

  • 设置SQL映射文件的namespace属性为Mapper接口全限定名

  • 在Mapper 接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致

如果Mapper接口名称和SQL映射文件名称相同,并且在同一目录下,则可以使用包扫描的方式简化SQL映射文件的加载,如下图

  • 编码

    • 通过 SqlSession的 getMapper方法获取 Mapper接口的代理对象

    • 调用对应方法完成sql的执行

//        3.执行SQL
//        3-1.获取接口的代理对象
        final ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
        final List<Product> products = mapper.selectAll();
        products.forEach(product -> System.out.println(product));

7.Mybatis的配置文件说明

可访问网址查看,后续再补

8.查询过程出现的问题

如以下情况

原因:实体类属性名称与数据库字段名称不一致,导致不能自动封装数据

解决方法一:

  • 为数据库字段起别名,让其和实体类属性名称一致(麻烦,不推荐)

方法一的优化方案:

  • 写一个SQL语句块,不灵活,还是不推荐~

解决方法二:

  • 结果映射,resultMap【有两个子标签,一个result,一个id,其中id子标签用来完成主键字段的映射,result用来映射普通字段的映射,使用方法与result一致】

  • type支持别名

再次运行

9.xml文件中编写SQL语句没有提示

在xml文件中按alt+enter

往下拉,根据安装的数据库选择合适的即可

10.查操作

(1)单条件查询
<!--
    参数占位符:
    1.#{}:将其替换为?,为了防止SQL注入。      使用场景:参数传递
    2.${}:拼SQL,存在SQL注入问题。           使用场景:表名或列名不固定的情况,但要注意SQL注入问题
​
    参数类型:parameterType,可以省略
​
    特殊字符处理:
    1.转义字符:适合少量
    2.CDATA区:适合大量,CD回车即可
-->
    <select id="selectById"  resultMap="resultMap_Employee">
        select * from employees where emp_no = #{id}
    </select>
​
    <resultMap id="resultMap_Employee" type="employee">
<!--        将不一样的属性名写一下即可-->
        <result column="emp_no" property="empNo"/>
        <result column="birth_date" property="birthDate"/>
        <result column="first_name" property="firstName"/>
        <result column="last_name" property="lastName"/>
        <result column="hire_date" property="hireDate"/>
​
    </resultMap>

(2)多条件查询

Mybatis提供了三种方式:

mapper.xml:

<select id="selectByCondition01" resultMap="resultMap_Employee">
    select *
    from employees
    where emp_no > #{empNo}
    and gender = #{gender}
</select>
​
<select id="selectByCondition02" resultMap="resultMap_Employee">
    select *
    from employees
    where emp_no > #{empNo}
      and gender = #{gender}
</select>
​
<select id="selectByCondition03" resultMap="resultMap_Employee">
    select *
    from employees
    where emp_no > #{empNo}
      and gender = #{gender}
</select>
​
    <resultMap id="resultMap_Employee" type="employee">
<!--        将不一样的属性名写一下即可-->
        <result column="emp_no" property="empNo"/>
        <result column="birth_date" property="birthDate"/>
        <result column="first_name" property="firstName"/>
        <result column="last_name" property="lastName"/>
        <result column="hire_date" property="hireDate"/>
​
    </resultMap>

pojo实体类:

public class Employee {
    private Integer empNo;
    private Date birthDate;
    private String firstName;
    private String lastName;
    private Character gender;
    private Date hireDate;
​
    public Employee() {
    }
​
    public Employee(Integer empNo, Character gender) {
        this.empNo = empNo;
        this.gender = gender;
    }
​
    public Employee(Integer empNo, Date birthDate, String firstName, String lastName, Character gender, Date hireDate) {
        this.empNo = empNo;
        this.birthDate = birthDate;
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
        this.hireDate = hireDate;
    }
​
    public Integer getEmpNo() {
        return empNo;
    }
​
    public void setEmpNo(Integer empNo) {
        this.empNo = empNo;
    }
​
    public Date getBirthDate() {
        return birthDate;
    }
​
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
​
    public String getFirstName() {
        return firstName;
    }
​
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
​
    public String getLastName() {
        return lastName;
    }
​
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
​
    public Character getGender() {
        return gender;
    }
​
    public void setGender(Character gender) {
        this.gender = gender;
    }
​
    public Date getHireDate() {
        return hireDate;
    }
​
    public void setHireDate(Date hireDate) {
        this.hireDate = hireDate;
    }
​
    @Override
    public String toString() {
        return "Employee{" +
                "empNo=" + empNo +
                ", birthDate=" + birthDate +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", gender=" + gender +
                ", hireDate=" + hireDate +
                '}';
    }
}

mapper.EmployeeMapper

/**
 * 条件查询01
 * 查询员工工号>?且性别为?的所有员工信息
 * *参数接收方式
 * 1.散装参数:如果方法中有多个参数,需要使用@Param("SQL参数占位符名称”)
 * @param empNo 员工工号
 * @param character 性别
 * @return Employee对象集合
 */
List<Employee> selectByCondition01(@Param("empNo")int empNo,@Param("gender")Character character);
​
/**
 * 条件查询02:实体类封装参数
 * @param employee 员工对象,注意对象的属性名称要和SQL参数占位符名称一致
 * @return 员工对象集合
 */
List<Employee> selectByCondition02(Employee employee);
​
/**
 * 条件查询03:map集合
 * @param map map集合,要保证SQL中的参数占位符名称和map集合的键名称一致
 * @return 员工对象集合
 */
List<Employee> selectByCondition03(Map map);

测试:

@Test
public void selectByCondition01() throws IOException {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
​
    final SqlSession sqlSession = sqlSessionFactory.openSession();
​
    final List<Employee> employees = sqlSession.getMapper(EmployeeMapper.class).selectByCondition01(10001, 'F');
    employees.forEach(employee -> System.out.println(employee));
    sqlSession.close();
}
    @Test
    public void selectByCondition022() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
​
        final SqlSession sqlSession = sqlSessionFactory.openSession();
​
//        final List<Employee> employees = sqlSession.getMapper(EmployeeMapper.class).selectByCondition02(new Employee(10001,'F'));
        Employee employee = new Employee();
        employee.setEmpNo(10001);
        employee.setGender('F');
        final List<Employee> employees = sqlSession.getMapper(EmployeeMapper.class).selectByCondition02(employee);
        employees.forEach(e -> System.out.println(e));
        sqlSession.close();
    }
@Test
public void selectByCondition03() throws IOException {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
​
    final SqlSession sqlSession = sqlSessionFactory.openSession();
    
    Map map = new HashMap();
    Integer empNo = 10001;
    Character gender = 'F';
    map.put("empNo",empNo);
    map.put("gender",gender);
​
    final List<Employee> employees = sqlSession.getMapper(EmployeeMapper.class).selectByCondition03(map);
    employees.forEach(e -> System.out.println(e));
    sqlSession.close();
}

11.动态查询

SQL语句会随着用户的输入或外部条件的变化而变化,我们称为 动态SQL

mybatis对动态SQL用很大的支撑

  • if:条件判断

    • test:逻辑判断

  • choose(when,otherwise)

  • trim(where,set)

  • foreach

更具体的可查阅官网,动态SQL

(1)多条件动态查询

修改一下:

<select id="selectByCondition03" resultMap="resultMap_Employee">
    select *
    from employees
    where
        <if test="empNo != null and empNo != ''">
            emp_no > #{empNo}
        </if>
        <if test="gender !=null ">
            and gender = #{gender}
        </if>
​
</select>

测试时会发现如果没有第一个参数后面会报错

解决方法:

  • 第一种方法:统一格式,第一个条件为一个恒等式

            where
#             恒等式
            <if test="empNo != null and empNo != ''">
                and emp_no > #{empNo}
            </if>
            <if test="gender !=null ">
                and gender = #{gender}
            </if>
  • 第二种方法:使用< where >标签替换where关键字

<where>
    <if test="empNo != null and empNo != ''">
        and emp_no > #{empNo}
    </if>
    <if test="gender !=null ">
        and gender = #{gender}
    </if>
</where>

测试:

(2)单条件动态查询(多选一)
<select id="selectByConditionSingle" resultMap="resultMap_Employee">
        select * from employees
    where
        <choose>
            <when test="empNo != null">
                emp_no > #{empNo}
            </when>
            <when test="gender !=null">
                gender = #{gender}
            </when>
            <when test="lastName != null">
                last_name like #{lastName}
            </when>
            <otherwise>
<!--                相当于java switch中的default,写个恒等式即可-->
                
                1 = 1
            </otherwise>
        </choose>
    </select>

或者

<where>
    <choose>
        <when test="empNo != null">
            emp_no > #{empNo}
        </when>
        <when test="gender !=null">
            gender = #{gender}
        </when>
        <when test="lastName != null">
            last_name like #{lastName}
        </when>
    </choose>
</where>

12.增操作

<insert id="add">
    insert into employees
        (first_name,last_name,gender,birth_date,hire_date)
    VALUES
        (#{firstName},#{lastName},#{gender},#{birthDate},#{hireDate})
</insert>

测试的时候会发现代码运行成功但并没有数据!

打开日志,会发现roll back了

观察日志就会发现 -Setting autocommit to false on JDBC Connection

所以在增操作后,需要使用commit()手动提交一次事务!

再次运行,数据添加成功

如果不想手动提交,可在openSession()传递布尔值以开启是否自动提交

  • mybatis默认是开启事务的

(1)主键返回

目的:添加完数据后,获取该数据的id值

<insert id="add" useGeneratedKeys="true" keyProperty="empNo">
    insert into employees
        (first_name,last_name,gender,birth_date,hire_date)
    VALUES
        (#{firstName},#{lastName},#{gender},#{birthDate},#{hireDate})
</insert>

13.删操作

同增

14.改操作

(1)修改字段数据
void update(Employee employee);
<update id="update">
    update employees
    set
        first_name = #{firstName},
        last_name = #{lastName},
        gender = #{gender}
    where emp_no = #{empNo}
</update>

(2)修改动态字段数据
<update id="update02">
    update employees
    <set>
        <if test="firstName != null and firstName != ''">
            first_name = #{firstName},
        </if>
        <if test="lastName != null and lastName != ''">
            last_name = #{lastName}
        </if>
    </set>
    where emp_no = #{empNo}
​
</update>

15.删操作

(1)单个删除
void deleteById(Integer empNo);
<delete id="deleteById">
    delete
    from employees
    where emp_no = #{empNo}
</delete>
(2)批量删除
/**
 * 批量删除
 * @param empNos id数组
 */
void deleteByIds(@Param("empNos") Integer[] empNos);
<!--    mybatis会将数组参数封装为一个map集合
        默认 key = array
            value = 对应数组
        可以使用@Param改变map集合的默认key名称
        foreach属性:separator分隔符
            -->
<delete id="deleteByIds">
    delete from employees
    where emp_no in
    <foreach collection="empNos" item="empNo" separator="," open="(" close=")">
        #{empNo}
    </foreach>
    ;
</delete>

/**
 * 批量删除02
 * @param empNos id数组
 */
void deleteByIds02(Integer[] empNos);
<delete id="deleteByIds02">
    delete from employees
    where emp_no in (
    <foreach collection="array" item="empNo" separator=",">
        #{empNo}
    </foreach>
    );
​
</delete>

测试

Integer[] integers = {10023,10024,10025,10026,10027,10018,10019,10020,10021,10022};
try {
    sqlSession.getMapper(EmployeeMapper.class).deleteByIds(integers);
    sqlSession.commit();
} catch (Exception e) {
    System.out.println("删除失败");
    e.printStackTrace();
}

16.Mybatis参数传递

(1)单个参数
  • pojo实体类:直接使用,属性名和参数占位符名称一致即可

  • Map集合:直接使用,键名和参数占位符名称一致即可

  • Collection:封装为map

  • List:封装为map

  • Array:封装为map

  • 其他:直接使用

(2)多个参数
  • 会将参数列表封装为map集合,由于默认可读性太差,可以使用@Param替换Map集合中默认的arg键名

  • map.put(”arg0“,参数值1)

  • map.put(”param1“ ,参数值1)

  • map.put(”param2“,参数值2)

  • map.put(”arg1“,参数值2)

MyBatis提供了 ParamNameResolver 类来进行参数封装

封装方法为getNamedParams

  1. 在IDEA中,按住ctrl+shift+a打开action

  2. 将 ParamNameResolver粘进搜索框,选择Classes

  3. 搜索出来第一个就是,点进去

  4. 之后ctrl f。在搜索框输入getNamedParams

17.注解方式完成增删改查

使用注解方式会比配置文件开发更加高效

  • 查询:@Select

  • 添加;@Insert

  • 修改:@Update

  • 删除:@Delete

/**
 * 根据id查询
 * @param empNo id
 * @return Employee对象
 */
@Select("select * from employees where emp_no = #{empNo}")
Employee selectById(int empNo);

注意:

  • 注解完成简单功能

  • 配置文件完成复杂功能,(动态SQL)


http://www.kler.cn/news/233388.html

相关文章:

  • QCoro: Qt C++ 20 协程库介绍
  • 基于图像掩膜和深度学习的花生豆分拣(附源码)
  • 【OpenVINO™】在 MacOS 上使用 OpenVINO™ C# API 部署 Yolov5 (上篇)
  • uni-app x,一个纯原生的Android App开发工具
  • 【力扣】复写零,栈 + 双指针法
  • 张楠辞任抖音集团CEO;东方甄选将开服饰号;小红书新增“附近”一级入口;华为分红770亿元
  • Vue3中路由配置Catch all routes (“*“) must .....问题
  • 通过Harbor构建docker私服仓库
  • 前端使用pdf.js进行pdf文件预览的第二种方式:Viewer.html
  • Quartus工程的qsf配置约束文件介绍
  • 【C语言】一道相当有难度的指针某大厂笔试真题(超详解)
  • 106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)
  • RTE2023第九届实时互联网大会:揭秘未来互联网趋势,PPT分享引领行业新思考
  • Python基础篇_修饰符(Decorators)【下】
  • 常用的正则表达式
  • 一条 SQL 查询语句是如何执行的
  • 探索Spring Validation:优雅实现后端数据验证的艺术
  • 数据结构-->线性表-->单链表
  • JAVA面试题12
  • 信号——block+pending+handler表
  • C语言常见面试题:什么是常量?C语言中有哪些类型的常量?
  • Python 小白的 Leetcode Daily Challenge 刷题计划 - 20240209(除夕)
  • 初识文件包含漏洞
  • 【OpenHarmony硬件操作】风扇与温湿度模块
  • 【Spring】Spring 对 Ioc 的实现
  • kaggle实战语义分割-Car segmentation(附源码)
  • 数据库管理phpmyadmin
  • Linux C/C++ 原始套接字:打造链路层ping实现
  • 【ESLint】TypeError:this.libOptions.parse is not a function
  • 【从Python基础到深度学习】4. Linux 常用命令