首页 > 其他分享 >Mybatis 使用汇总(介绍,功能,连接池,日志,注解,XML映射文件)

Mybatis 使用汇总(介绍,功能,连接池,日志,注解,XML映射文件)

时间:2023-06-26 19:22:55浏览次数:40  
标签:XML 映射 mybatis SQL Mybatis 连接池

Mybatis 介绍
Mybatis 功能
Mybatis 连接池
mybatis日志
Mybatis 注解
Mybatis XML 映射文件

01.Mybatis 是一款优秀的持久层框架(DAO),它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
官网:https://mybatis.org/mybatis-3/zh/index.html

02.Mybatis 功能

1. 数据库连接四要素(驱动、链接、用户名、密码),都配置在springboot默认的配置文件
application.properties中
2. 查询结果的解析及封装,由mybatis自动完成映射封装,我们无需关注
3.在mybatis中使用了数据库连接池技术(Hikari(springboot默认)),从而避免了频繁的创建连接、销毁连接而带来的资源浪费

4.使用了mybatis 开发持久层程序操作数据库,只需关注:1.连接字符串,mapper接口
4.使用了mybatis 开发持久层程序操作数据库,只需关注:1.连接字符串,mapper接口

5.数据库连接池技术:
5.1 Druid(德鲁伊)Druid连接池是阿里巴巴开源的数据库连接池项目功能强大,性能优秀,是Java语言最好的数据库连接池之一
5.2 Hikari(springboot默认



6.开起mybatis日志:
   application.yml文件开启mybatis日志,可以在运行时在控制台查看日志:

configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

  1. 动态SQL-if,where,set,foreach, :定义可重用的SQL片段, :通过属性refid,指定包含的SQL片段
    动态SQL-if,where,set,foreach, :定义可重用的SQL片段, :通过属性refid,指定包含的SQL片段
<select id="list" resultType="com.it.pojo.Emp">
 select * from emp
<where>
<!-- if做为where标签的子元素 -->
<if test="name != null">
 and name like concat('%',#{name},'%')
</if>
<if test="gender != null">
 and gender = #{gender}
</if>
<if test="begin != null and end != null">
 and entrydate between #{begin} and #{end}
 </if>
</where>
 order by update_time desc
</select>


<?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">
<mapper namespace="com.itheima.mapper.EmpMapper">
<!--更新操作-->
<update id="update">
 update emp
<!-- 使用set标签,代替update语句中的set关键字 -->
<set>
<if test="username != null">
 username=#{username},
</if>
<if test="name != null">
 name=#{name},
</if>
<if test="gender != null">
 gender=#{gender},
</if>
<if test="image != null">
 image=#{image},
</if>
<if test="job != null">
 job=#{job},
</if>
<if test="entrydate != null">
 entrydate=#{entrydate},
</if>
<if test="deptId != null">
 dept_id=#{deptId},
</if>
<if test="updateTime != null">
 update_time=#{updateTime}
</if>
</set>
 where id=#{id}
</update>
</mapper>


<!--删除操作-->
<delete id="deleteByIds">
 delete from emp where id in
<foreach collection="ids" item="id" separator="," open="("
close=")">
 #{id}
</foreach>
</delete>

03.在Mybatis中使用XML映射文件方式开发,需要符合一定的规范:

1. XML映射文件的名称与Mapper接口名称一致,并且将XML映射文件和Mapper接口放置在相同包下
(同包同名)
2. XML映射文件的namespace属性为Mapper接口全限定名一致
3. XML映射文件中sql语句的id与Mapper接口中的方法名一致,并保持返回类型一致。

结论:
使用Mybatis的注解,主要是来完成一些简单的增删改查功能。如果需要实现复杂的SQL功能,
建议使用XML来配置映射语句

标签:XML,映射,mybatis,SQL,Mybatis,连接池
From: https://www.cnblogs.com/chenshaojun2008/p/17506534.html

相关文章

  • mybatis中数据库连接池切换到更优秀的连接池上
    数据库连接池切换mybatis中数据库连接池比较流行,性能优越的有2个:数据库连接池技术:A.Hikari(springboot默认)追光者B.Druid(德鲁伊)1Druid(德鲁伊)Druid连接池是阿里巴巴开源的数据库连接池项目功能强大,性能优秀,是Java语言最好的数据库连接池之一2Hikari(springboot默认3.把默......
  • Mybatis中Example的用法(QBC查询)
    QBC查询QueryByCriteria。Criteria是Criterion的复数形式。意思是:规则、标准、准则。在SQL语句中相当于查询条件。QBC查询是将查询条件通过Java对象进行模块化封装。Example简单介绍其实就是一个工具,自动帮你生成对应的代码1.example是Mybatis数据层框架中的一个工具,......
  • Mybatis通用Mapper接口扩展
    这里的扩展指的是通用Mapper没有提供的功能,如批量update。例子:类似于生成下面这样的SQL语句:UPDATEtabple_empSETemp_name=?,emp_age=?,emp_salary=?whereemp_id=?;UPDATEtabple_empSETemp_name=?,emp_age=?,emp_salary=?whereemp_id=?;UPDATEtabple_empSETem......
  • mybatis中如何获取自增主键
    方法1:在insert标签中使用userGeneratedKeys和keyProperty标签:<insertid=”insertname”usegeneratedkeys=”true”keyproperty=”id”>insertintonames(name)values(#{name})</insert>useGeneratedKeys=true表示开启自动生成主键的功能,keyProperty="id"指......
  • MyBatis 缓存
     MyBatis一级缓存1一级缓存原理在一次SqlSession中(数据库会话),程序执行多次查询,且查询条件完全相同,多次查询之间程序没有其他增删改操作,则第二次及后面的查询可以从缓存中获取数据,避免走数据库。 每个SqlSession中持有了Executor,每个Executor中有一个LocalCache。当用......
  • C# - XMLHelper :一个操作XML的简单类库
    下午写了一个操作XML文件的类库,后来不用了,水篇文章存个档......
  • mybatis-执行器
    MyBatis中的Executor接口有三个实现类:SimpleExecutor:SimpleExecutor是最简单的Executor实现。它每次执行SQL语句时都会创建一个新的Statement对象。这种实现方式的缺点是效率较低,因为每次执行SQL语句都需要创建一个新的Statement对象。ReuseExecutor:ReuseExec......
  • Could not resolve type alias 'com.github.mybatis.helper.page.PageSqlInterceptor'
    报错信息 Couldnotresolvetypealias'com.github.mybatis.helper.page.PageSqlInterceptor'.Cause:java.lang.ClassNotFoundException:Cannotfindclass:com.github.mybatis.helper.page.PageSqlInterceptor 原因报错的位置是 mybatis-config.xml 文件中......
  • Mybatis Plus逆向工程
    MybatisPlus逆向工程连接mysql数据库选中对应的数据库表,生成逆向工程......
  • day112 - mybatis的查询与特殊sql语句
    mybatis查询与特殊语句查询普通语句/***根据id查询用户信息*@paramid*@return*/UsergetUserById(@Param("id")Integerid);​<!--UsergetUserById(@Param("id")Integerid);--><selectid="getUserById"resultType="com.......