开始
1. 在接口中加入方法
public Proper getByProperId(@Param("id") int id);2.在mapper配置文件中编写对应的标签
<resultMap id="proper" type="com.example.mybatis.Proper">
<id property="id" column="p_id"/>
<id property="name" column="p_name"/>
<id property="meg" column="p_meg"/>
<collection property="home" ofType="com.example.mybatis.Home">
<id property="id" column="h_id"/>
<id property="name" column="h_name"/>
</collection>
</resultMap>
<select id="getByProperId" resultMap="proper" parameterType="_int">
select h.id as h_id,
h.name as h_name,
p.name as p_name,
p.id as p_id,
p.meg as p_meg
from proper p join home h on p.id = h.master
where p.id=#{id}
</select>3. 测试
SqlSessionFactory sqlSessionFactory=SessionFactory.getSessionFactory();
try (SqlSession sqlSession=sqlSessionFactory.openSession()){
t1Mapper mapper = sqlSession.getMapper(t1Mapper.class);
Proper byProperId = mapper.getByProperId(1);
System.out.println(byProperId.toString());
}注意
- 对象中的list用collection标签
- 在collection中用ofType指定类型
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!