![【Mybatis】Mybatis一對多、多對一,第1張 【Mybatis】Mybatis一對多、多對一,第1張](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Mybatis多對一【association】
- 多對一
private GradeVo gradeVo;
<select id="get" resultType="com.hxh.basic.project.vo.GradeVo" parameterType="int">
select * from grade where id = #{id};
</select>
<resultMap id="TestMybatis" type="com.demo.pojo.Student" autoMapping="true">
<id property="stuNo" column="stuNo"></id>
<association property="grade" javaType="com.demo.pojo.Grade" column="gradeId"
select="com.demo.dao.GradeDao.get"></association>
</resultMap>
<select id="getAll" resultMap="TestMybatis">
select * from student
</select>
{
"code": 0,
"message":"ok",
"data": [
{
"id": 6,
"nickname":"Consumer",
"username":"於瑤",
"birthday":"2021-03-03T00:00:00",
"gradeVo": {
"id": 1,
"gradeNane":"一年級"
}
},
{
"id": 7,
"nickname":"Company",
"username":"白志超",
"birthday":"2021-03-05T00:00:00",
"gradeVo": {
"id": 0,
"gradeNane":"幼兒園"
}
}
]
}
Mybatis一對多【collection】
- 一對多
private GradeVo gradeVo;
<select id="get" resultType="com.hxh.basic.project.vo.UserVo" parameterType="int">
select * from user where status = #{status};
</select>
<resultMap id="resultMap" type="com.hxh.basic.project.vo.GradeVo" autoMapping="true">
<id property="id" column="id"></id>
<collection property="list" ofType="com.hxh.basic.project.vo.UserVo" javaType="java.util.List" column="id"
select="com.hxh.basic.project.mapper.UserMapper.get"></collection>
</resultMap>
<select id="getAll" resultMap="resultMap">
select * from grade;
</select>
{
"code": 0,
"message":"ok",
"data": [
{
"id": 0,
"gradeNane":"幼兒園",
"list": [
{
"id": 5,
"nickname":"Producer",
"username":"高佳琪",
"birthday":"2021-03-03T00:00:00"
}
]
},
{
"id": 1,
"gradeNane":"一年級",
"list": [
{
"id": 4,
"nickname":"LouisVan",
"username":"王瑩",
"birthday":"2021-03-03T00:00:00"
}
]
},
{
"id": 2,
"gradeNane":"二年級",
"list": []
}
]
}
0條評論