Skip to content

Use Records when Possible #931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions src/main/java/org/mybatis/dynamic/sql/select/UnionQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,9 @@

import java.util.Objects;

public class UnionQuery {
private final String connector;
private final SelectModel selectModel;

public record UnionQuery(String connector, SelectModel selectModel) {
public UnionQuery(String connector, SelectModel selectModel) {
this.connector = Objects.requireNonNull(connector);
this.selectModel = Objects.requireNonNull(selectModel);
}

public String connector() {
return connector;
}

public SelectModel selectModel() {
return selectModel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,5 @@ public static <T> SqlParameterSource[] createBatch(List<T> rows) {
return SqlParameterSourceUtils.createBatch(tt);
}

public static class RowHolder<T> {
private final T row;

public RowHolder(T row) {
this.row = row;
}

public T getRow() {
return row;
}
}
public record RowHolder<T> (T row) {}
}
39 changes: 1 addition & 38 deletions src/test/java/examples/animal/data/AnimalData.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,4 @@
*/
package examples.animal.data;

public class AnimalData {
private int id;
private String animalName;
private double brainWeight;
private double bodyWeight;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getAnimalName() {
return animalName;
}

public void setAnimalName(String animalName) {
this.animalName = animalName;
}

public double getBrainWeight() {
return brainWeight;
}

public void setBrainWeight(double brainWeight) {
this.brainWeight = brainWeight;
}

public double getBodyWeight() {
return bodyWeight;
}

public void setBodyWeight(double bodyWeight) {
this.bodyWeight = bodyWeight;
}
}
public record AnimalData(int id, String animalName, double brainWeight, double bodyWeight) {}
44 changes: 29 additions & 15 deletions src/test/java/examples/animal/data/AnimalDataMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import java.util.List;

import org.apache.ibatis.annotations.Arg;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.session.RowBounds;
Expand All @@ -34,36 +32,46 @@
public interface AnimalDataMapper extends CommonDeleteMapper, CommonInsertMapper<AnimalData>, CommonUpdateMapper {

@SelectProvider(type=SqlProviderAdapter.class, method="select")
@Results(id="AnimalDataResult", value={
@Result(column="id", property="id", id=true),
@Result(column="animal_name", property="animalName"),
@Result(column="brain_weight", property="brainWeight"),
@Result(column="body_weight", property="bodyWeight")
})
@Arg(column = "id", javaType = int.class, id = true)
@Arg(column = "animal_name", javaType = String.class)
@Arg(column = "brain_weight", javaType = double.class)
@Arg(column = "body_weight", javaType = double.class)
List<AnimalData> selectMany(SelectStatementProvider selectStatement);

@SelectProvider(type = SqlProviderAdapter.class, method = "select")
@ResultMap("AnimalDataResult")
@Arg(column = "id", javaType = int.class, id = true)
@Arg(column = "animal_name", javaType = String.class)
@Arg(column = "brain_weight", javaType = double.class)
@Arg(column = "body_weight", javaType = double.class)
List<AnimalData> selectManyWithRowBounds(SelectStatementProvider selectStatement, RowBounds rowBounds);

@SelectProvider(type = SqlProviderAdapter.class, method = "select")
@ResultMap("AnimalDataResult")
@Arg(column = "id", javaType = int.class, id = true)
@Arg(column = "animal_name", javaType = String.class)
@Arg(column = "brain_weight", javaType = double.class)
@Arg(column = "body_weight", javaType = double.class)
AnimalData selectOne(SelectStatementProvider selectStatement);

@Select({
"select id, animal_name, brain_weight, body_weight",
"from AnimalData",
"${whereClause}"
})
@ResultMap("AnimalDataResult")
@Arg(column = "id", javaType = int.class, id = true)
@Arg(column = "animal_name", javaType = String.class)
@Arg(column = "brain_weight", javaType = double.class)
@Arg(column = "body_weight", javaType = double.class)
List<AnimalData> selectWithWhereClause(WhereClauseProvider whereClause);

@Select({
"select a.id, a.animal_name, a.brain_weight, a.body_weight",
"from AnimalData a",
"${whereClause}"
})
@ResultMap("AnimalDataResult")
@Arg(column = "id", javaType = int.class, id = true)
@Arg(column = "animal_name", javaType = String.class)
@Arg(column = "brain_weight", javaType = double.class)
@Arg(column = "body_weight", javaType = double.class)
List<AnimalData> selectWithWhereClauseAndAlias(WhereClauseProvider whereClause);

@Select({
Expand All @@ -73,7 +81,10 @@ public interface AnimalDataMapper extends CommonDeleteMapper, CommonInsertMapper
"order by id",
"OFFSET #{offset,jdbcType=INTEGER} LIMIT #{limit,jdbcType=INTEGER}"
})
@ResultMap("AnimalDataResult")
@Arg(column = "id", javaType = int.class, id = true)
@Arg(column = "animal_name", javaType = String.class)
@Arg(column = "brain_weight", javaType = double.class)
@Arg(column = "body_weight", javaType = double.class)
List<AnimalData> selectWithWhereClauseLimitAndOffset(@Param("whereClauseProvider") WhereClauseProvider whereClause,
@Param("limit") int limit, @Param("offset") int offset);

Expand All @@ -84,7 +95,10 @@ List<AnimalData> selectWithWhereClauseLimitAndOffset(@Param("whereClauseProvider
"order by id",
"OFFSET #{offset,jdbcType=INTEGER} LIMIT #{limit,jdbcType=INTEGER}"
})
@ResultMap("AnimalDataResult")
@Arg(column = "id", javaType = int.class, id = true)
@Arg(column = "animal_name", javaType = String.class)
@Arg(column = "brain_weight", javaType = double.class)
@Arg(column = "body_weight", javaType = double.class)
List<AnimalData> selectWithWhereClauseAliasLimitAndOffset(@Param("whereClauseProvider") WhereClauseProvider whereClause,
@Param("limit") int limit, @Param("offset") int offset);
}
Loading