好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

mybatis框架的xml映射文件常用查询指南

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

什么是mybatis框架?

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

如何使用?

pom文件依赖

?

1

2

3

4

5

<dependency>

  <groupid>org.mybatis.spring.boot</groupid>

  <artifactid>mybatis-spring-boot-starter</artifactid>

  <version> 2.1 . 3 </version>

</dependency>

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

?

1

2

mybatis:

   mapper-locations: classpath:mapper/*.xml

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<?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>

mapper标签中的namespace属性指定的是我们持久层接口的项目路径 resultmap是mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中 resultmap标签的type属性是我们要映射的实体对象的项目路径,id为resultmap的唯一标识。 resultmap中的result标签是实体和数据库表字段的绑定,其中property属性为实体对象的属性名,column为数据库的字段名,jdbctype是字段的类型。

xml映射文件的sql编写

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

<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>

id="queryall"为持久层接口的抽象方法名 resultmap="hostmap" 指定查询结果接收的resultmap的结果集。

持久层接口绑定

?

1

2

3

4

5

6

7

/**

  * 条件查询

  *

  * @param host 条件查询

  * @return 对象列表

  */

list<host> queryall(host host);

通过主键批量删除

?

1

2

3

4

5

6

7

8

9

10

11

<!--通过主键批量删除-->

<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语句的原型为

?

1

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

foreach标签中的属性理解

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

持久层接口抽象方法

?

1

2

3

4

5

6

/**

  * 批量删除主机

  *

  * @param hostids 主机id数组

  * @return integer

  */ integer deletehostbyids( @param ( "hostids" ) long [] hostids);

批量新增

?

1

2

3

4

5

6

7

8

<!--批量增加-->

<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>

持久层接口方法

?

1

2

3

4

5

6

/**

  * 将多个主机添加至一个主机组

  *

  * @param request

  * @return integer

  */ integer addhostlist(hostandgrouprequest request);

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

总结

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

原文链接:https://segmentfault测试数据/a/1190000039798604

查看更多关于mybatis框架的xml映射文件常用查询指南的详细内容...

  阅读:21次