当前位置: 代码网 > it编程>编程语言>Java > mybatis-plus集成springboot实现XML映射器方式

mybatis-plus集成springboot实现XML映射器方式

2026年03月15日 Java 我要评论
主要功能sql 映射配置1、定义复杂的 sql 查询语句2、配置数据库操作与 java 方法的映射关系3、处理动态 sql 语句补充注解方式的不足1、当 @select、@insert 等注解无法满足

主要功能

sql 映射配置

1、定义复杂的 sql 查询语句

2、配置数据库操作与 java 方法的映射关系

3、处理动态 sql 语句

补充注解方式的不足

1、当 @select、@insert 等注解无法满足复杂 sql 需求时

2、更好地管理长 sql 语句,提高代码可读性

3、支持复杂的条件判断和循环操作

动态 sql 支持

1、使用 <if>、<choose>、<when>、<otherwise> 等标签

2、实现条件查询和动态参数绑定

3、支持集合遍历等复杂操作

使用场景

1、复杂查询: 多表关联查询、嵌套查询等

2、动态条件: 根据不同参数构建不同的 sql 语句

3、批量操作: 批量插入、更新等操作

4、自定义 sql: 当 mybatis-plus 提供的通用方法无法满足需求时

xml 映射器本质上是 mybatis 的特性,在 mybatis-plus 中同样适用,可以与通用 mapper 方法并存使用。

应用

1.依赖

​​​​​​​<dependency>
    <groupid>com.baomidou</groupid>
    <artifactid>mybatis-plus-boot-starter</artifactid>
    <version>3.5.9</version>
</dependency>

2.配置

在application.properties文件中配置:

# 指定mapper接口的xml文件位置

mybatis-plus.mapper-locations=classpath*:mapper/**/*.xml 

# 实体类包名

mybatis-plus.type-aliases-package=com.example.demo.pojo

3.创建xml文件

在mapper文件夹下新建xml包,新建usermapper.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.example.demo.mapper.usermapper" >



<!--这里写了一个方便区分的sql-->

    <delete id="deleteuseraaaaa">

        delete from user where id = #{id}

    </delete>


</mapper>

4.声明方法

在usermapper文件里声明:

//记得加上@mapper注解
@mapper

public interface usermapper extends basemapper<user> {

    int deleteuseraaaaa(@param("id") long id);



}

5.使用

这里在测试类里测试,

@test

void testlogicdelete(){

    int affectedrows = usermapper.deleteuseraaaaa(4l);

    system.out.println("删除了" + affectedrows + "条记录");

}

运行后显示成功。。。。

常见sql操作实现

(以下是官方的文档xml 映射器_mybatis中文网

1.select

<select
  id="selectperson"
  parametertype="int"
  parametermap="deprecated"
  resulttype="hashmap"
  resultmap="personresultmap"
  flushcache="false"
  usecache="true"
  timeout="10"
  fetchsize="256"
  statementtype="prepared"
  resultsettype="forward_only">

属性:

id

在命名空间中唯一的标识符,可以被用来引用这条语句。

parametertype

将会传入这条语句的参数的类全限定名或别名。这个属性是可选的,因为 mybatis 可以通过类型处理器(typehandler)推断出具体传入语句的参数,默认值为未设置(unset)。

parametermap

用于引用外部 parametermap 的属性,目前已被废弃。请使用行内参数映射和 parametertype 属性。

resulttype

期望从这条语句中返回结果的类全限定名或别名。 注意,如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身的类型。 resulttype resultmap 之间只能同时使用一个。

resultmap

对外部 resultmap 的命名引用。结果映射是 mybatis 最强大的特性,如果你对其理解透彻,许多复杂的映射问题都能迎刃而解。 resulttype resultmap 之间只能同时使用一个。

flushcache

将其设置为 true 后,只要语句被调用,都会导致本地缓存和二级缓存被清空,默认值:false

usecache

将其设置为 true 后,将会导致本条语句的结果被二级缓存缓存起来,默认值:对 select 元素为 true

timeout

这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为未设置(unset)(依赖数据库驱动)。

fetchsize

这是一个给驱动的建议值,尝试让驱动程序每次批量返回的结果行数等于这个设置值。 默认值为未设置(unset)(依赖驱动)。

statementtype

可选 statementprepared callable。这会让 mybatis 分别使用 statementpreparedstatement callablestatement,默认值:prepared

resultsettype

forward_onlyscroll_sensitive, scroll_insensitive default(等价于 unset 中的一个,默认值为 unset (依赖数据库驱动)。

databaseid

如果配置了数据库厂商标识(databaseidprovider),mybatis 会加载所有不带 databaseid 或匹配当前 databaseid 的语句;如果带和不带的语句都有,则不带的会被忽略。

resultordered

这个设置仅针对嵌套结果 select 语句:如果为 true,将会假设包含了嵌套结果集或是分组,当返回一个主结果行时,就不会产生对前面结果集的引用。 这就使得在获取嵌套结果集的时候不至于内存不够用。默认值:false

2.insert, update 和 delete

<insert
  id="insertauthor"
  parametertype="domain.blog.author"
  flushcache="true"
  statementtype="prepared"
  keyproperty=""
  keycolumn=""
  usegeneratedkeys=""
  timeout="20">

<update
  id="updateauthor"
  parametertype="domain.blog.author"
  flushcache="true"
  statementtype="prepared"
  timeout="20">

<delete
  id="deleteauthor"
  parametertype="domain.blog.author"
  flushcache="true"
  statementtype="prepared"
  timeout="20">

属性:

id

在命名空间中唯一的标识符,可以被用来引用这条语句。

parametertype

将会传入这条语句的参数的类全限定名或别名。这个属性是可选的,因为 mybatis 可以通过类型处理器(typehandler)推断出具体传入语句的参数,默认值为未设置(unset)。

parametermap

用于引用外部 parametermap 的属性,目前已被废弃。请使用行内参数映射和 parametertype 属性。

flushcache

将其设置为 true 后,只要语句被调用,都会导致本地缓存和二级缓存被清空,默认值:(对 insertupdate delete 语句)true

timeout

这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为未设置(unset)(依赖数据库驱动)。

statementtype

可选 statementprepared callable。这会让 mybatis 分别使用 statementpreparedstatement callablestatement,默认值:prepared

usegeneratedkeys

(仅适用于 insert update)这会令 mybatis 使用 jdbc getgeneratedkeys 方法来取出由数据库内部生成的主键(比如:像 mysql sql server 这样的关系型数据库管理系统的自动递增字段),默认值:false

keyproperty

(仅适用于 insert update)指定能够唯一识别对象的属性,mybatis 会使用 getgeneratedkeys 的返回值或 insert 语句的 selectkey 子元素设置它的值,默认值:未设置(unset)。如果生成列不止一个,可以用逗号分隔多个属性名称。

keycolumn

(仅适用于 insert update)设置生成键值在表中的列名,在某些数据库(像 postgresql)中,当主键列不是表中的第一列的时候,是必须设置的。如果生成列不止一个,可以用逗号分隔多个属性名称。

databaseid

如果配置了数据库厂商标识(databaseidprovider),mybatis 会加载所有不带 databaseid 或匹配当前 databaseid 的语句;如果带和不带的语句都有,则不带的会被忽略。

3.动态sql

<!--    if标签-->

    <select id="selectusers" resulttype="user">

        select * from user

        where 1=1

        <if test="name != null">

            and name = #{name}

        </if>

        <if test="age != null">

            and age = #{age}

        </if>

    </select>



<!--    choose/when/otherwise 标签-->

    <select id="selectusers" resulttype="user">

        select * from user

        where 1=1

        <choose>

            <when test="name != null">

                and name = #{name}

            </when>

            <when test="email != null">

                and email = #{email}

            </when>

            <otherwise>

                and status = 1

            </otherwise>

        </choose>

    </select>



<!--    where 标签 (自动处理 where 和 and/or)-->

    <select id="selectusers" resulttype="user">

        select * from user

        <where>

            <if test="name != null">

                and name = #{name}

            </if>

            <if test="age != null">

                and age = #{age}

            </if>

        </where>

    </select>



<!--    set 标签 (用于 update 语句)-->

    <update id="updateuser">

        update user

        <set>

            <if test="name != null">name=#{name},</if>

            <if test="age != null">age=#{age},</if>

            <if test="email != null">email=#{email},</if>

        </set>

        where id=#{id}

    </update>

4.常见问题解决

参数为 null 的判断

<if test="name != null and name != ''">  <!-- 同时检查null和空字符串 -->

  and name = #{name}

</if>

集合判空

<if test="ids != null and ids.size() > 0">

  and id in

  <foreach collection="ids" item="id" open="(" separator="," close=")">

    #{id}

  </foreach>

</if>

动态排序

<if test="orderby != null">

  order by ${orderby}

  <if test="asc != null and asc">

    asc

  </if>

  <if test="asc != null and !asc">

    desc

  </if>

</if>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com