经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
mybatis框架的xml映射文件常用查询指南
来源:jb51  时间:2021/4/12 15:42:35  对本文有异议

使用mybatis框架时,那必然会有对数据库的查询语句的编写,所以这篇文章希望可以帮助到你。

什么是Mybatis框架?

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

如何使用?

pom文件依赖

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>2.1.3</version>
  5. </dependency>

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

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

在xml文件中绑定持久层接口和实体对象

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.flamelephant.fabricmgt.dao.HostMapper">
  4. <resultMap type="com.flamelephant.fabricmgt.entity.po.Host" id="HostMap">
  5. <result property="id" column="id" jdbcType="INTEGER"/>
  6. <result property="hostName" column="host_name" jdbcType="VARCHAR"/>
  7. <result property="ip" column="ip" jdbcType="VARCHAR"/>
  8. <result property="userName" column="user_name" jdbcType="VARCHAR"/>
  9. <result property="passWord" column="pass_word" jdbcType="VARCHAR"/>
  10. <result property="state" column="state" jdbcType="OTHER"/>
  11. <result property="tag" column="tag" jdbcType="VARCHAR"/>
  12. <result property="gmtCreated" column="gmt_created" jdbcType="TIMESTAMP"/>
  13. <result property="gmtModified" column="gmt_modified" jdbcType="TIMESTAMP"/>
  14. </resultMap>
  15. </mapper>
  • mapper标签中的namespace属性指定的是我们持久层接口的项目路径
  • resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中
  • resultMap标签的type属性是我们要映射的实体对象的项目路径,id为resultMap的唯一标识。
  • resultMap中的result标签是实体和数据库表字段的绑定,其中property属性为实体对象的属性名,column为数据库的字段名,jdbcType是字段的类型。

xml映射文件的sql编写

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

  1. <select id="queryAll" resultMap="HostMap">
  2. select id,
  3. host_name,
  4. ip,
  5. user_name,
  6. pass_word,
  7. state,
  8. tag,
  9. gmt_created,
  10. gmt_modified
  11. from host
  12. <where>
  13. <if test="id != null and id != ''">
  14. and id = #{id}
  15. </if>
  16. <if test="hostName != null and hostName != ''">
  17. and host_name like CONCAT('%', #{hostName}, '%')
  18. </if>
  19. <if test="ip != null and ip != ''">
  20. and ip like CONCAT('%', #{ip}, '%')
  21. </if>
  22. <if test="userName != null and userName != ''">
  23. and user_name = #{userName}
  24. </if>
  25. <if test="passWord != null and passWord != ''">
  26. and pass_word = #{passWord}
  27. </if>
  28. <if test="state != null and state != ''">
  29. and state = #{state}
  30. </if>
  31. <if test="tag != null and tag != ''">
  32. and tag = #{tag}
  33. </if>
  34. <if test="gmtCreated != null">
  35. and gmt_created = #{gmtCreated}
  36. </if>
  37. <if test="gmtModified != null">
  38. and gmt_modified = #{gmtModified}
  39. /if>
  40. </where>
  41. </select>
  • id="queryAll"为持久层接口的抽象方法名
  • resultMap="HostMap" 指定查询结果接收的resultMap的结果集。

持久层接口绑定

  1. /**
  2. * 条件查询
  3. *
  4. * @param host 条件查询
  5. * @return 对象列表
  6. */
  7. List<Host> queryAll(Host host);

通过主键批量删除

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

以上sql语句的原型为

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

foreach标签中的属性理解

  • collection属性为接收的数据源
  • item为集合中的每一个元素
  • index :用于表示在迭代过程中,每次迭代到的位置
  • open :表示该语句以什么开始
  • separator :表示在迭代时数据以什么符号作为分隔符
  • close :表示以什么结束

持久层接口抽象方法

  1. /**
  2. * 批量删除主机
  3. *
  4. * @param hostIds 主机id数组
  5. * @return Integer
  6. */Integer deleteHostByIds(@Param("hostIds") Long[] hostIds);

批量新增

  1. <!--批量增加-->
  2. <insert id="addHostList">
  3. insert into host_and_group(host_group_id, host_id)
  4. values
  5. <foreach collection="hostGroupIdList" item="hostGroupId" index="index" separator=",">
  6. (#{hostGroupId}, #{hostId})
  7. </foreach>
  8. </insert>

持久层接口方法

  1. /**
  2. * 将多个主机添加至一个主机组
  3. *
  4. * @param request
  5. * @return Integer
  6. */Integer addHostList(HostAndGroupRequest request);

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

总结

到此这篇关于mybatis框架的xml映射文件常用查询指南的文章就介绍到这了,更多相关mybatis xml映射文件查询内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号