Mybatis复习之通过xml文件配置sql

557 阅读2分钟

  用通用mapper等框架写注解配置SQL时间长了,有点忘记XML配置SQL基本标签和规则了,复习一下,待补充 ....
1、<resultMap> 标签
该标签的作用是将查询到的结果,存入到指定的实体类中

id 属性是用来唯一标识该 resultMap 的,这个大家都知道,type 属性标识要存入的实体类的绝对路径,也就是其所在包的全路径

<resultMap id="BaseResultMap" type="com.mapper.QuestionMapper">
    <id column="id" property="id" jdbcType="INTEGER"/>
    <result column="question" property="question" jdbcType="VARCHAR"/>
    <result column="answer" property="answer" jdbcType="VARCHAR"/>
</resultMap>

在 resultMap 标签内部可以使用 result 标签来指定将查询结果中某一列的值存入到对应实体中的指定变量中

column 属性表示数据库表中的字段名 property 属性表示对应实体中的变量名 jdbcType 属性表示该字段在数据库表中的数据类型

2、<sql> 标签
<sql>标签可以定义sql语句

<sql id="Base_Column_List">
    id, time
</sql>

3、<include> 标签
<include> 标签通过 refid 与 <sql> 标签 中的 id 相对应,达到复用sql的目的

<sql id="Base_Column_List">
    id, time
</sql>
<select id="select" parameterType="java.lang.Long" resultMap="Base">
  select 
  <include refid="Base_Column_List" />
  from test
  where id = #{id,jdbcType=BIGINT}
</select>

4、<trim> 标签
  <trim>标签的主要功能是可以在自己标签内部要显示的内容加前缀或后缀,或者去掉前缀或后缀的的某些内容。分别对应四个属性:

prefix:给要显示的内容加前缀。
prefixoverride:去掉要显示的内容的指定前缀(如:suffixOverrides=“abc”,要显示的内容为"abcfdaslk",那么最后显示的就是"fdaslk")。
suffix:给要显示的内容加后缀。
suffixOverrides:去掉要显示的内容的指定后缀(跟上边 suffixOverrides 一样,只不过删除的是后边的内容)。 如下代码,当name和age都不为null时,最终的sql为:update user set name=?, age=? where id=? 。增加了前缀 set 、去掉了后缀 , 、添加了后缀 where id=#{id}。

update user

<trim prefix="set" suffixoverride="," suffix=" where id=#{id} ">

  <if test="name != null"> name=#{name} , </if>

  <if test="age!= null"> age=#{age} ,  </if>

</trim>

5、update、select、insert、delete 标签
在这四个标签中:
可以用 标签来对传进来的参数进行判断,然后决定是否执行相应的sql语句
可以用 parameterType 属性来指定传入参数的数据类型
可以用 resultMap 属性来指定通过 标签自定义的返回值类型
可以用 resultType 属性来指定非自定义的返回值类型

<?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.mapper.QuestionMapper">
    <resultMap id="BaseResultMap" type="com.mapper.QuestionMapper">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="question" property="question" jdbcType="VARCHAR"/>
        <result column="answer" property="answer" jdbcType="VARCHAR"/>
    </resultMap>
    <sql id="Base_Column_List">
    	id, time
	</sql>

    <update id="updateById" parameterType="com.mapper.QuestionMappe">
        UPDATE t_question
        <set>
            <if test="question != null">question = #{question,jdbcType=VARCHAR},</if>
            <if test="answer != null">answer = #{answer,jdbcType=VARCHAR},</if>
        </set>
        WHERE id=#{id,jdbcType=INTEGER} AND flag=1
    </update>
    
    <select id="selectByCondition" resultMap="BaseResultMap" parameterType="com.mapper.QuestionMappe">
      SELECT
      id,question,answer
      FROM t_question
      WHERE flag = 0
      <if test="question != null">
        and question like CONCAT('%',#{question},'%')
        or answer like CONCAT('%',#{question},'%')
      </if>
      <if test="id != null and id != ''">
        and id =  #{id,jdbcType=INTEGER}
      </if>
      limit #{start},#{pageSize}
    </select>
    
    <insert id="insert" parameterType="com.mapper.QuestionMappe">
    INSERT INTO t_question(question, answer)
    VALUES ( #{question,jdbcType=VARCHAR}, #{answer,jdbcType=VARCHAR})
    </insert>
</mapper>

6、注意事项
数据库中的 datatime 类型,在xml中需要用 TIMESTAMP 类型来代替。