For example, there is an entity class
public class AnswerDto{ //一个回复的类,一个问题可能会有多个回复
int id;
int askId;//问题id
List<String> answers; //回复的列表
}
I originally wrote this, but it was wrong T0T, how should I map content to List<String>
<select id="getAns" parameterType="int" resultMap="uiy">
select
id,
ask_id AS "askId",
content
from t_answer where ask_id = #{_parameter}
</select>
<resultMap type="AnswerDto" id="uiy">
<id property="id" column="id"/>
<result property="askId" column="askId" />
<collection property="ls" ofType="string">
<constructor>
<arg column="content"/>
</constructor>
</collection>
</resultMap>
mybatis determines the set mapping relationship based on the
<id>
tag. If you must use your table, you can map it like thisThe original poster should have one missing table here, which is the question table.
With the question table, the definition of DTO should be like this.
At this time, there are many related query solutions for mybatis, I will attach a link.
Mybatis related query (nested query)
There is a lot of information online about mybatis related queries. You can search more.
One last suggestion, it is best not to perform related queries. The data assembly logic is placed in the code, which facilitates future DB transformation. Because as the amount of data increases, the performance of related queries is poor, and it is not easy to transform the sub-database and sub-tables. However, this is all based on the large growth of the business. At present, it would be good if the poster starts to cultivate this awareness.
The mapping of table fields to complex object fields can be done using a custom TypeHandler.
eg:
Mapping of json fields to Java classes in MyBatis
http://www.cnblogs.com/watery...