mybatis的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
<?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.liuyanzhao.mybatis.mapper.UserMapper">
<!--查询用户-->
<select id="findUserById" parameterType="int" resultType="com.liuyanzhao.mybatis.po.User">
SELECT * FROM user WHERE id=#{value}
</select>
<!--根据用户名称模糊查询,可能返回多条-->
<select id="findUserByName" parameterType="java.lang.String" resultType="com.liuyanzhao.mybatis.po.User">
SELECT * FROM user WHERE username LIKE '%${value}%'
</select>
<!--添加用户-->
<insert id="insertUser" parameterType="com.liuyanzhao.mybatis.po.User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO user(username,birthday,gender,address) VALUE(#{username},#{birthday},#{gender},#{address})
</insert>
<!--删除用户-->
<delete id="deleteUserById" parameterType="java.lang.Integer">
DELETE FROM user WHERE id=#{id}
</delete>
<!--更新用户-->
<update id="updateUserById" parameterType="com.liuyanzhao.mybatis.po.User">
UPDATE user SET username=#{username},birthday=#{birthday},gender=#{gender},address=#{address} WHERE id=#{id}
</update>
</mapper>

需要配置的属性:

  • id=”xxxx” >>> 表示此段sql执行语句的唯一标识,也是接口的方法名称【必须一致才能找到】
  • parameterType=”” >>>表示该sql语句中需要传入的参数, 类型要与对应的接口方法的类型一致【可选】
  • resultMap=“ ”>>> 定义出参,调用已定义的映射管理器的id值
  • resultType=“ ”>>>定义出参,匹配普通Java类型或自定义的pojo【出参类型若不指定,将为语句类型默认类型,如语句返回值为int】

p.s: 至于为何 语句的返回值类型为什么是int,有过JDBC操作经验的朋友可能会有印象,增删改操作实际上返回的是操作的条数。而Mybatis框架本身是基于JDBC的,所以此处也沿袭这种返回值类型。

还有 int会自动转成 Integer,别名

传参和取值:mapper.xml 的灵活性还体现在SQL执行语句可以传参,参数类型通过parameterType= “” 定义

取值方式1:#{value jdbcType = valuetype}:jdbcType 表示该属性的数据类型在数据库中对应的类型,如 #{user jdbcType=varchar} 等价于 String username;

取值方式2:${value } : 这种方式不建议大量使用,可能会发送sql注入而导致安全性问题。一般该取值方式可用在非经常变化的值上,如orderby ${columnName};

封装

1
2
3
<sql id="selectFields">
id, from_id, to_id, conversation_id, content, status, create_time
</sql>

if标签

1
2
3
<if test="name!=null">
where name like concat('%',#{name},'%')
</if>

Foreach标签

1、首先在po类中定义一个集合或者数组 比如 private List ids;

2、在映射文件中

1
<foreach collection="ids" item="ids" item="user_id" open="AND(" close=")" seperator="or"><foreach/>

collection:指定输入对象中集合属性

item:每次遍历生成的对象

open:开始遍历时拼接的串

close:结束遍历时两个对象需要拼接的串

ResultMap

在这里插入图片描述

当数据库方法返回的是复合数据类型(如list等),通常使用resultMap而非resultType

一对一:

使用一对一查询时,在一个实体类中添加另一实体类属性。用resultMap实现映射关系时,使用association连接,javaType为封装的类型。

1
resultType
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
42
public class Person {
private Integer pid;//人的id
private String pname;//人的姓名
private Card card;//人的身份证
public Integer getPid() {
return pid;
}

public void setPid(Integer pid) {
this.pid = pid;
}

public String getPname() {
return pname;
}

public void setPname(String pname) {
this.pname = pname;
}

public Card getCard() {
return card;
}

public void setCard(Card card) {
this.card = card;
}

public Person(Integer pid, String pname, Card card) {
this.pid = pid;
this.pname = pname;
this.card = card;
}

public Person(Integer pid, String pname) {
this.pid = pid;
this.pname = pname;
}

public Person() {
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<select id="getPersonByPid" resultMap="person_card" parameterType="int" >
select * from person p,card c where p.cid=c.cid and p.pid=#{pid}
</select>

<!--自定义返回结果集-->
<!--id是这个结果集的唯一标识符,方便根据Id调用,type是主体返回的实体类-->
<resultMap id="person_card" type="Person" >
<!--主键-->
<id column="pid" property="pid"></id>
<!--普通属性-->
<result column="pname" property="pname"></result>
<!--一对一的映射:人对应他的卡 javaType代表对应的类的类型,column代表是通过哪一列段进行关联-->
<association property="card" javaType="Card" column="cid" >
<id property="cid" column="cid"></id>
<result property="cnumber" column="cnumber"></result>
</association>
</resultMap>

一对多

一对多是在一个类中包含另一个类list的集合,在映射时,使用Collection,ofType为封装类型。注意封装类型与一对一不同

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
 <!--
id:指定主键列的封装规则(主键也可以使用result来定义)
column:指定哪一列
property:指定对应的javaBean属性
-->
<id column="id" property="id"></id>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.how2java.pojo">
<resultMap type="Category" id="categoryBean">
<id column="cid" property="id" />
<result column="cname" property="name" />

<!-- 一对多的关系 -->
<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
<collection property="products" ofType="Product">
<id column="pid" property="id" />
<result column="pname" property="name" />
<result column="price" property="price" />
</collection>
</resultMap>

<!-- 关联查询分类和产品表 -->
<select id="listCategory" resultMap="categoryBean">
select c.*, p.*, c.id 'cid', p.id 'pid', c.name 'cname', p.name 'pname' from category_ c left join product_ p on c.id = p.cid
</select>
</mapper>

二、if 标签

1
2
3
4
5
6
7
8
9
10
11
12
13
<!--统计用户数量-->
<select id="countUserByNameAndGender" parameterType="com.liuyanzhao.mybatis.po.User" resultType="int">
SELECT COUNT(*) FROM user
<!--where标签可以去掉首部的AND符号-->
<where>
<if test="gender!=null and gender!=''">
AND gender = #{gender}
</if>
<if test="username!=null and gender!=''">
AND username LIKE '%${username}%'
</if>
</where>
</select>

三、sql 片段

在没有使用 sql 片段之前

1
2
3
4
<!--查询用户-->
<select id="findUserById" parameterType="int" resultType="com.liuyanzhao.mybatis.po.User">
SELECT id,username,gender,birthday,address FROM user WHERE id=#{value}
</select>

定义和使用 sql 片段

1
2
3
4
5
6
7
8
9
10
11

<!--定义sql片段-->
<sql id="user_table_all_columns">
id,username,gender,birthday,address
</sql>
<!--查询用户-->
<select id="findUserById" parameterType="int" resultType="com.liuyanzhao.mybatis.po.User">
SELECT
<include refid="user_table_all_columns"/>
FROM user WHERE id=#{value}
</select>

其中 sql 标签的 di 表示 sql 片段的唯一标识符