怎么在mybatis框架中查询xml映射文件

发布时间:2021-04-12 15:25:31 作者:Leah
来源:亿速云 阅读:236

本篇文章为大家展示了怎么在mybatis框架中查询xml映射文件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

什么是Mybatis框架?

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。

如何使用?

pom文件依赖

<dependency>
 <groupId>org.mybatis.spring.boot</groupId>
 <artifactId>mybatis-spring-boot-starter</artifactId>
 <version>2.1.3</version>
</dependency>

yml文件配置,这里匹配 resource/mapper/ 路径下的映射文件。

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

在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.flamelephant.fabricmgt.dao.HostMapper">
 <resultMap type="com.flamelephant.fabricmgt.entity.po.Host" id="HostMap">
 <result property="id" column="id" jdbcType="INTEGER"/>
 <result property="hostName" column="host_name" jdbcType="VARCHAR"/>
 <result property="ip" column="ip" jdbcType="VARCHAR"/>
 <result property="userName" column="user_name" jdbcType="VARCHAR"/>
 <result property="passWord" column="pass_word" jdbcType="VARCHAR"/>
 <result property="state" column="state" jdbcType="OTHER"/>
 <result property="tag" column="tag" jdbcType="VARCHAR"/>
 <result property="gmtCreated" column="gmt_created" jdbcType="TIMESTAMP"/>
 <result property="gmtModified" column="gmt_modified" jdbcType="TIMESTAMP"/>
 </resultMap>
</mapper>

xml映射文件的sql编写

通过实体作为筛选条件查询

<select id="queryAll" resultMap="HostMap">
    select id,
           host_name,
           ip,
           user_name,
           pass_word,
           state,
           tag,
           gmt_created,
           gmt_modified
    from host
 <where>
    <if test="id != null and id != ''">
        and id = #{id}
    </if>
    <if test="hostName != null and hostName != ''">
        and host_name like CONCAT('%', #{hostName}, '%')
    </if>
    <if test="ip != null and ip != ''">
        and ip like CONCAT('%', #{ip}, '%')
    </if>
    <if test="userName != null and userName != ''">
        and user_name = #{userName}
    </if>
    <if test="passWord != null and passWord != ''">
        and pass_word = #{passWord}
    </if>
    <if test="state != null and state != ''">
        and state = #{state}
    </if>
    <if test="tag != null and tag != ''">
        and tag = #{tag}
    </if>
    <if test="gmtCreated != null">
        and gmt_created = #{gmtCreated}
    </if>
    <if test="gmtModified != null">
        and gmt_modified = #{gmtModified}
    /if>
 </where>
</select>

持久层接口绑定

/**
 * 条件查询
 *
 * @param host 条件查询
 * @return 对象列表
 */
List<Host> queryAll(Host host);

通过主键批量删除

<!--通过主键批量删除-->
<delete id="deleteHostByIds" parameterType="java.lang.Integer">
 delete
    from host
            where id in
    <if test="hostIds != null and hostIds.length > 0">
        <foreach item="id" collection="hostIds" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
    </if>
 </delete>

以上sql语句的原型为

delete from host where id in(1,2,3)

foreach标签中的属性理解

持久层接口抽象方法

/**
 * 批量删除主机
 *
 * @param hostIds 主机id数组
 * @return Integer
 */Integer deleteHostByIds(@Param("hostIds") Long[] hostIds);

批量新增

<!--批量增加-->
<insert id="addHostList">
 insert into host_and_group(host_group_id, host_id)
       values
    <foreach collection="hostGroupIdList" item="hostGroupId" index="index" separator=",">
            (#{hostGroupId}, #{hostId})
    </foreach>
</insert>

持久层接口方法

/**
 * 将多个主机添加至一个主机组
 *
 * @param request
 * @return Integer
 */Integer addHostList(HostAndGroupRequest request);

上述内容就是怎么在mybatis框架中查询xml映射文件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 使用Spring Boot+MyBatis框架做查询操作
  2. Mybatis框架(一)初识Mybatis框架

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis xml

上一篇:如何在Redis中遍历所有key

下一篇:C++中深拷贝与浅拷贝有什么区别

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》