java 用于选择 * 查询的 Mybatis 映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38548470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:31:11  来源:igfitidea点击:

Mybatis mapping for select * query

javamybatismapper

提问by RjnshD

I was trying to write a mybatis mapper for a select * query which would return me a list of rows from a ProcessType table. And each row has to be mapped to a ProcessType pojo. I know how to map a single row to the POJO, but how to go about this for list of Process Type?

我试图为 select * 查询编写一个 mybatis 映射器,它会从 ProcessType 表中返回一个行列表。并且每一行都必须映射到一个 ProcessType pojo。我知道如何将单行映射到 POJO,但是如何处理进程类型列表?

POJO--> class name : ProcessType Properties:
String ABC; String id; String Date;

POJO--> 类名:ProcessType 属性:
String ABC;字符串标识;字符串日期;

From mapper I call a proc 'XYZ' which returns me the cursor for list of rows for ProcessType table being queried.

我从映射器调用一个 proc 'XYZ',它返回我正在查询的 ProcessType 表的行列表的光标。

回答by Pau

I don't understand well at all the question. Primary, I think it is not needed a procedure for this simple operation, I would do it as a simple query.

我完全不明白这个问题。初级,我认为这个简单的操作不需要过程,我会做一个简单的查询。

So, If you have this entity.

所以,如果你有这个实体。

    public class ProcessType {

    String ABC, id, Date;

    public ProcessType(String aBC, String id, String date) {
        ABC = aBC;
        this.id = id;
        Date = date;
    }

    public String getABC() {
        return ABC;
    }

    public void setABC(String aBC) {
        ABC = aBC;
    }

    public String getId() {
        return id;
    }

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

    public String getDate() {
        return Date;
    }

    public void setDate(String date) {
        Date = date;
    }

}

And a table ProcessType as something like this:

还有一个像这样的表 ProcessType:

create table PROCESS_TYPE (
   ABC VARCHAR(200),
   ID  VARCHAR(200),
   DATE VARCHAR(200)
);

Your mapper using annotations should be as follows:

使用注释的映射器应如下所示:

public interface MapperProcessType {

@Select("select * from PROCESS_TYPE")
@Results({
    @Result(property = "ABC", column = "ABC"),
    @Result(property = "id", column = "ID"),
    @Result(property = "date", column = "DATE")
  })
public List<ProcessType> findAll();

}

On other hand, using xml It is as the next:

另一方面,使用 xml 如下:

<resultMap id = "result" type = "ProcessType">
   <result property = "ABC" column = "ABC"/>
   <result property = "id" column = "ID"/>
   <result property = "date" column = "DATE"/>
</resultMap>

<select id = "findAll" resultMap = "result">
   SELECT * FROM PROCESS_TYPE
</select>

回答by Troncoso

Your Mapper.java class just needs to return a list:

您的 Mapper.java 类只需要返回一个列表:

List<ProcessType> getProcessTypes();

Your Mapper.xml should use the class as the resultType:

您的 Mapper.xml 应该使用该类作为 resultType:

<select id="getProcessTypes" resultType="path.to.ProcessType">

Or you can create a result map to map your columns to the ProcessType properties, but that's outside the scope of this question.

或者您可以创建一个结果映射来将您的列映射到 ProcessType 属性,但这超出了本问题的范围。

回答by Russ

Assuming you've used the mybatis javaModelGenerator, sqlMapGenerator, and javaClientGenerator, all you would need to do is to use the .selectByExample()function in your mapper class and feed in an "empty" Example object such as the following:

假设您已经使用了 mybatis javaModelGenerator、sqlMapGenerator 和 javaClientGenerator,您需要做的就是使用.selectByExample()映射器类中的函数并输入一个“空”示例对象,如下所示:

for (MyTable myTable : myTableMapper.selectByExample(new MyTableExample())) {
    System.out.println("found ID: " + myTable.getId());
}

This is equivalent to a select *

这相当于一个选择 *

回答by garlicbread

If I understand this correctly are you using a collection in your result map? this tells mybatis to expect a list.

如果我理解正确,您是否在结果图中使用了集合?这告诉 mybatis 期待一个列表。

<collect property="" column="">

回答by Sky

All you need to do is define a resultMaplike this:

你需要做的就是定义一个resultMap这样的:

<resultMap id="processTypeMap" type="yourpackage.ProcessType">
    <id property="id" column="id_in_db"/>
    <result property="abc"         column="abc_in_db"/>
    <result property="date"     column="date_in_db"/>
</resultMap>

The columnis the associated field in your table mapping.

column是你的映射表相关的领域。

Then set resultMapas what you defined above like this:

然后设置resultMap为您在上面定义的内容,如下所示:

<select id="getProcessTypes" resultMap="processTypeMap">
    select * from ProcessType
</select>

Now the columns in your table will be mapped accordingly.

现在表中的列将被相应地映射。

I suppose that you want to get a list of ProcessType, so in the DAOlayer, you have to use the selectListmethod.

我想你想得到一个列表ProcessType,所以在DAO层中,你必须使用该selectList方法。

List<ProcessType> processTypes = sqlSessionTemplate.selectList("getProcessTypes")