这篇文章给大家分享的是有关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文件中绑定持久层接口和实体对象

<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.flamelephant.fabricmgt.dao.HostMapper"><resultMaptype="com.flamelephant.fabricmgt.entity.po.Host"id="HostMap"><resultproperty="id"column="id"jdbcType="INTEGER"/><resultproperty="hostName"column="host_name"jdbcType="VARCHAR"/><resultproperty="ip"column="ip"jdbcType="VARCHAR"/><resultproperty="userName"column="user_name"jdbcType="VARCHAR"/><resultproperty="passWord"column="pass_word"jdbcType="VARCHAR"/><resultproperty="state"column="state"jdbcType="OTHER"/><resultproperty="tag"column="tag"jdbcType="VARCHAR"/><resultproperty="gmtCreated"column="gmt_created"jdbcType="TIMESTAMP"/><resultproperty="gmtModified"column="gmt_modified"jdbcType="TIMESTAMP"/></resultMap></mapper>

mapper标签中的namespace属性指定的是我们持久层接口的项目路径

resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中

resultMap标签的type属性是我们要映射的实体对象的项目路径,id为resultMap的唯一标识。

resultMap中的result标签是实体和数据库表字段的绑定,其中property属性为实体对象的属性名,column为数据库的字段名,jdbcType是字段的类型。

xml映射文件的sql编写

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

<selectid="queryAll"resultMap="HostMap">selectid,host_name,ip,user_name,pass_word,state,tag,gmt_created,gmt_modifiedfromhost<where><iftest="id!=nullandid!=''">andid=#{id}</if><iftest="hostName!=nullandhostName!=''">andhost_namelikeCONCAT('%',#{hostName},'%')</if><iftest="ip!=nullandip!=''">andiplikeCONCAT('%',#{ip},'%')</if><iftest="userName!=nullanduserName!=''">anduser_name=#{userName}</if><iftest="passWord!=nullandpassWord!=''">andpass_word=#{passWord}</if><iftest="state!=nullandstate!=''">andstate=#{state}</if><iftest="tag!=nullandtag!=''">andtag=#{tag}</if><iftest="gmtCreated!=null">andgmt_created=#{gmtCreated}</if><iftest="gmtModified!=null">andgmt_modified=#{gmtModified}/if></where></select>

id="queryAll"为持久层接口的抽象方法名

resultMap="HostMap" 指定查询结果接收的resultMap的结果集。

持久层接口绑定

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

通过主键批量删除

<!--通过主键批量删除--><deleteid="deleteHostByIds"parameterType="java.lang.Integer">deletefromhostwhereidin<iftest="hostIds!=nullandhostIds.length>0"><foreachitem="id"collection="hostIds"index="index"open="("separator=","close=")">#{id}</foreach></if></delete>

以上sql语句的原型为

deletefromhostwhereidin(1,2,3)

foreach标签中的属性理解

collection属性为接收的数据源

item为集合中的每一个元素

index :用于表示在迭代过程中,每次迭代到的位置

open :表示该语句以什么开始

separator :表示在迭代时数据以什么符号作为分隔符

close :表示以什么结束

持久层接口抽象方法

/***批量删除主机**@paramhostIds主机id数组*@returnInteger*/IntegerdeleteHostByIds(@Param("hostIds")Long[]hostIds);

批量新增

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

持久层接口方法

/***将多个主机添加至一个主机组**@paramrequest*@returnInteger*/IntegeraddHostList(HostAndGroupRequestrequest);

我是元素封装在一个对象中,所以这个对象里有批量增加的元素,则直接可以传一个对象。

感谢各位的阅读!关于“MyBatis框架怎么通过xml映射文件实现查询语句编写”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!