Java >> Java tutorial >  >> Tag >> mybatis

Hentning af en liste over værdier på tværs af databaser med mybatis

Jeg har 2 Java POJO-filer

class Filter Variables {
private String id;
private String name;
private List<ValueVariables> variableValues;
getters() setters()
}
class ValueVariables {
private String id;
private String valueListId;
private String value;
}

Databasen har en FK-mapping mellem FilterVariable.id og ValueVariable.valueListId. Indsættelser i tabellen fungerer perfekt, jeg har en enkelt transaktion, der indsættes i begge tabeller separat. Mens du henter dataene ved hjælp af select og join , jeg modtager kun den første række fra tabellen i databasen.

Databasetabeller:

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)
);

<select id="selectFilterVariableById" parameterType="java.lang.String" resultMap="FilterVariablesJoinResultMap">
    select FV.id, FV.name, 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>

Metoden i grænsefladen er defineret som List<FilterVariables> selectFilterVariableById(String id); Jeg forstår, at jeg med 2 separate kan gemme værdier i de to Pojo og bruge dem, men er der nogen måde med joins, hvor jeg kan hente flere rækker?

Resultatkortet er defineret således:

<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>

Sådan ser dataene ud i mysql.

Når en anmodning er lavet fra Java:

List<FilterVariables> vals= filterVariablesMapper.selectFilterVariableById(id);
System.out.println(vals);
System.out.println(vals.size());

Jeg får kun den første række

[FilterVariables [id=b218df5c-eac1-4e01-846a-119345bb254d, name=ME, valueId=null, storageRecordType=null, values={b218df5c-eac1-4e01-846a-119345bb254d [email protected]}]]

1

Svar

Som nævnt af @ave er løsningen enten

  1. Fjern fra samlingen – forringer ydeevnen
  2. Bare omdøb den variabel, der bruges i – Implementering nedenfor:

Resultatkortet er nu:

<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>

Hermed kommer resultaterne efter behov.

ValueVariable er nu:

class ValueVariables {
private String varId;
private String valueListId;
private String value;
getters() and setters()
}

Vælg-forespørgslen er nu:

<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>

Bemærk venligst, at VV.id skal være alias for at dette virker.


No
Java tag