UJCMS后端二次开发教程09-mapper.xml

2023-08-31 15:15 阅读

每个Mapper接口对应一个mapper.xml文件,每个Mapper接口的方法,对一个mapper.xml文件的sql。

mapper.xml

src/main/resource/com/ujcms/cms/ext/mapper/ExampleMapper.xml

<?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.ujcms.cms.ext.mapper.ExampleMapper">
  <resultMap id="BaseResultMap" type="com.ujcms.cms.ext.domain.Example">
    <id column="id_" jdbcType="INTEGER" property="id" />
    <result column="name_" jdbcType="VARCHAR" property="name" />
    <result column="description_" jdbcType="VARCHAR" property="description" />
    <result column="height_" jdbcType="INTEGER" property="height" />
    <result column="birthday_" jdbcType="TIMESTAMP" property="birthday" />
    <result column="enabled_" jdbcType="CHAR" property="enabled" />
  </resultMap>
  <resultMap extends="BaseResultMap" id="ResultMap" type="com.ujcms.cms.ext.domain.Example" />
  <sql id="selectAllJoin" />
  <sql id="selectAllWhere" />
  <select id="selectAll" resultMap="ResultMap">
    <include refid="com.ujcms.cms.core.mapper.SeqMapper.Select_All">
      <property name="tableName" value="ujcms_example" />
    </include>
  </select>
  <select id="select" parameterType="java.lang.Integer" resultMap="ResultMap">
    select 
    <include refid="Column_List" />
    from ujcms_example t
    where t.id_ = #{id,jdbcType=INTEGER}
  </select>
  <delete id="delete" parameterType="java.lang.Integer">
    delete from ujcms_example
    where id_ = #{id,jdbcType=INTEGER}
  </delete>
  <update id="update" parameterType="com.ujcms.cms.ext.domain.Example">
    update ujcms_example
    set name_ = #{name,jdbcType=VARCHAR},
      description_ = #{description,jdbcType=VARCHAR},
      height_ = #{height,jdbcType=INTEGER},
      birthday_ = #{birthday,jdbcType=TIMESTAMP},
      enabled_ = #{enabled,jdbcType=CHAR}
    where id_ = #{id,jdbcType=INTEGER}
  </update>
  <insert id="insert" parameterType="com.ujcms.cms.ext.domain.Example">
    insert into ujcms_example (id_, name_, description_, height_, birthday_, enabled_)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, #{height,jdbcType=INTEGER}, #{birthday,jdbcType=TIMESTAMP}, #{enabled,jdbcType=CHAR})
  </insert>
  <sql id="Column_List">
    t.id_, t.name_, t.description_, t.height_, t.birthday_, t.enabled_
  </sql>
</mapper>

其中BaseResultMap Column_List select update由代码生成器维护,不要修改,否则再次运行代码生成器时,会被覆盖。其余部分可以修改,也可以增加新的代码。

selectAll引用了com.ujcms.cms.core.mapper.SeqMapper.Select_All,用于根据查询解析器生成的查询信息,生成查询sql。

<sql id="selectAllJoin" /><sql id="selectAllWhere" />标签内可以写一些额外的表连接条件和查询条件,会被包含进selectAll标签的sql里。

关联关系

如果有关联关系,可以将关联关系写入ResultMap中,不要写在BaseResultMap里面。例如:

  <resultMap id="BaseResultMap" type="com.ujcms.cms.ext.domain.Example">
    ...
    <!-- 首先需要在表里面增加关联字段,代码生成器会生成此行代码 -->
    <result column="user_id_" jdbcType="INTEGER" property="userId" />
    ...
  </resultMap>
  <resultMap extends="BaseResultMap" id="ResultMap" type="com.ujcms.cms.ext.domain.Example">
    <association column="user_id_" javaType="com.ujcms.cms.core.domain.User" property="user" select="com.ujcms.cms.core.mapper.UserMapper.select" />
  </resultMap>

根据查询信息生成sql

src/main/resource/com/ujcms/cms/core/mapper/SeqMapper.xml

    <sql id="Select_All">
        SELECT
        <if test="queryInfo != null and queryInfo.distinct">DISTINCT</if>
        <include refid="Column_List" />
        FROM
        <choose>
            <when test="queryInfo != null">${queryInfo.tableName}</when>
            <otherwise>${tableName}</otherwise>
        </choose>
        t
        <if test="queryInfo != null">
            <foreach collection="queryInfo.joinTables" item="table">
                JOIN ${table.tableName} ${table.tableAlias} ON ${table.leftId} = ${table.rightId}
            </foreach>
        </if>
        <include refid="selectAllJoin" />
        <where>
            <if test="queryInfo != null">
                <foreach collection="queryInfo.whereConditions" item="condition">
                    AND ${condition.column} ${condition.operator}
                    <choose>
                        <when test="condition.operator == 'IN' or condition.operator == 'NOT IN'">
                            <foreach close=")" collection="condition.value" index="index" item="item" open="(" separator=",">
                                #{item}
                            </foreach>
                        </when>
                        <when test="condition.operator != 'IS NULL'">#{condition.value}</when>
                    </choose>
                </foreach>
                <bind name="whereOrAndConditions" value="queryInfo.whereOrAndConditions" />
                <foreach collection="whereOrAndConditions.keys" item="key">
                    AND (
                    <bind name="orAndConditions" value="whereOrAndConditions[key]" />
                    <if test="orAndConditions != null">
                        <foreach collection="orAndConditions.keys" item="andKey" separator="OR">
                            <bind name="andConditions" value="orAndConditions[andKey]" />
                            <foreach close=")" collection="andConditions" item="condition" open="(" separator="AND">
                                ${condition.column} ${condition.operator}
                                <choose>
                                    <when test="condition.operator == 'IN' or condition.operator == 'NOT IN'">
                                        <foreach close=")" collection="condition.value" index="index" item="item" open="(" separator=",">
                                            #{item}
                                        </foreach>
                                    </when>
                                    <when test="condition.operator != 'IS NULL'">#{condition.value}</when>
                                </choose>
                            </foreach>
                        </foreach>
                    </if>
                    )
                </foreach>
            </if>
            <include refid="selectAllWhere" />
        </where>
        <if test="queryInfo!=null and queryInfo.orderBy!=null">
            ORDER BY ${queryInfo.orderBy}
        </if>
    </sql>
QQ咨询
电话
微信
微信扫码咨询