Java 使用mybatis跨数据库检索值列表

Java 使用mybatis跨数据库检索值列表,java,mysql,mybatis,spring-mybatis,mybatis-generator,Java,Mysql,Mybatis,Spring Mybatis,Mybatis Generator,我有2个Java POJO文件 class Filter Variables { private String id; private String name; private List<ValueVariables> variableValues; getters() setters() } 数据库在FilterVariable.id和ValueVariable.valueListId之间具有FK映射。插入到表中效果很好,我有一个单独的事务,可以分别插入到两个表中。在使用sele

我有2个Java POJO文件

class Filter Variables {
private String id;
private String name;
private List<ValueVariables> variableValues;
getters() setters()
}
数据库在FilterVariable.id和ValueVariable.valueListId之间具有FK映射。插入到表中效果很好,我有一个单独的事务,可以分别插入到两个表中。在使用select和join检索数据时,我只收到数据库表中的第一行

数据库表:

CREATE TABLE IF NOT EXISTS filter_variables (
id VARCHAR(36) NOT NULL,
name VARCHAR(30) NOT NULL,
PRIMARY KEY(id)
);

CREATE TABLE IF NOT EXISTS variable_values (
id INT(10) NOT NULL AUTO_INCREMENT,
valueListId VARCHAR(36) NOT NULL,
value VARCHAR(100) NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY (valueListId) REFERENCES filter_variables(id)
);

接口中的方法定义为List selectFilterVariableByIdString id; 我知道,使用2个单独的Pojo,我可以将值存储到两个Pojo中并使用它们,但是有没有任何方法可以通过连接检索多行

结果图的定义如下:

<resultMap id="FilterVariablesJoinResultMap" type="com.example.mybatissample.FilterVariables">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <collection property="variableValues" ofType="com.example.mybatissample.ValueVariables">
      <id property="id" column="id"/>
      <result property="valueListId" column="valueListId"/>
      <result property="value" column="value"/>
    </collection>
</resultMap>
mysql中的数据就是这样的。

从Java发出请求时:

List<FilterVariables> vals= filterVariablesMapper.selectFilterVariableById(id);
System.out.println(vals);
System.out.println(vals.size());
我只坐第一排

[FilterVariables[id=b218df5c-eac1-4e01-846a-119345bb254d,name=ME,valueId=null,storageRecordType=null,values={b218df5c-eac1-4e01-846a-119345bb254dtest@mailinator.com}]]


1

正如@ave所提到的,解决方案是

从集合中删除-降低性能 只需重命名以下-Implementation中使用的变量: 结果图现在是:

<resultMap id="FilterVariablesJoinResultMap" type="com.example.mybatissample.FilterVariables">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <collection property="variableValues" ofType="com.example.mybatissample.ValueVariables">
      <id property="varId" column="varId"/>
      <result property="valueListId" column="valueListId"/>
      <result property="value" column="value"/>
    </collection>
</resultMap>
选择查询现在是:

<select id="selectFilterVariableById" parameterType="java.lang.String" resultMap="FilterVariablesJoinResultMap">

    select FV.id, name, description, type, columnId, storageRecordType, valueId,
    operator, dbId, sharedValue, systemVariable, VV.id as varId, VV.value, VV.valueListId
    from filter_variables as FV left outer join variable_values as VV ON FV.id=VV.valueListId
    where FV.id = #{id,jdbcType=VARCHAR}
  </select>

请注意,VV.id要使用别名才能起作用。

设计不清楚,但对父对象和集合元素使用相同的id没有意义。in应该是变量_值的主键。查询中有valueId,但值为NULL。例如,您没有发布表声明,但您不是指W.id valueId吗?我已经添加了模式,请看一看。请忽略valueId中的空值,它们现在不重要了。没有收到你的最后一行@A尝试这些,看看这是否是您想要的:1在查询中,用W.id valueId替换valueId;2在中,用替换当前元素。然后必须从中删除。MyBatis使用它来标识集合元素,这两行具有相同的id,因为它是filter_变量的主键。请注意,没有in会导致性能损失,如表中的。:D中所述重命名PK是一种方法,但如果您像我前面建议的那样指定列别名,则没有必要这样做。还有另一种方法,即为ValueVariables定义单独的结果映射,并指定columnPrefix。请参见中的示例。
<resultMap id="FilterVariablesJoinResultMap" type="com.example.mybatissample.FilterVariables">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <collection property="variableValues" ofType="com.example.mybatissample.ValueVariables">
      <id property="varId" column="varId"/>
      <result property="valueListId" column="valueListId"/>
      <result property="value" column="value"/>
    </collection>
</resultMap>
class ValueVariables {
private String varId;
private String valueListId;
private String value;
getters() and setters()
}
<select id="selectFilterVariableById" parameterType="java.lang.String" resultMap="FilterVariablesJoinResultMap">

    select FV.id, name, description, type, columnId, storageRecordType, valueId,
    operator, dbId, sharedValue, systemVariable, VV.id as varId, VV.value, VV.valueListId
    from filter_variables as FV left outer join variable_values as VV ON FV.id=VV.valueListId
    where FV.id = #{id,jdbcType=VARCHAR}
  </select>