archguard / archguard

ArchGuard is a architecture workbench, also for architecture governance, which can analysis architecture in container, component, code level, create architecure fitness functions, and anaysis system dependencies..
https://archguard.org/
MIT License
601 stars 103 forks source link

dataMap生成的sql结果有问题 #147

Open FightingChu opened 1 month ago

FightingChu commented 1 month ago

如果java项目中的mapper和实体类相关的文件使用的是MyBatis` Generator (MBG) MyBatis Generator 这种工具逆向从工程数据库架构,生成MyBatis的映射文件(XML)和Java实体类,它也支持生成用于查询的Example类。 如果xml文件包含这种example类查询,dataMap生成的语句就会中断,比如

<?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.example.mapper.UserMapper">

  <!-- ... 其他映射和SQL语句 ... -->

  <!-- Example查询结果映射 -->
  <resultMap id="BaseResultMapWithBLOBs" type="com.example.entity.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <!-- 如果有BLOB字段的话,这里会包含BLOB字段的映射 -->
  </resultMap>

  <!-- Example查询 -->
  <select id="selectByExample" parameterType="com.example.entity.UserExample" resultMap="BaseResultMapWithBLOBs">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>

  <!-- Example查询条件的SQL片段 -->
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>

  <!-- ... 其他映射和SQL语句 ... -->
</mapper>

selectByExample这个id,使用dataMap生成sql对应的结果是select distict ... from user,而后面的where条件以及ordby by排序就消失了。 请问这个有解吗,谢谢。

phodal commented 1 month ago

之前探索过,实现起来比较困难