ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**1.UserMapper.java** ~~~ //查询用户和用户关联的账号信息 List<User> findAllWithAccount(); ~~~ **2.UserMapper.xml** ~~~ <resultMap id="userMapperWithAccount" type="User"> <id property="id" column="u_id"></id> <result property="name" column="u_name"></result> <result property="password" column="u_password"></result> <!-- collection:表示将集合进行封装 property:集合属性名 ofType:集合中封装的对象类型 --> <collection property="accounts" ofType="Account"> <id property="id" column="aid"></id> <result property="money" column="money"></result> </collection> </resultMap> <select id="findAllWithAccount" resultMap="userMapperWithAccount"> select *,a.id 'aid' from t_user u left join account a on u.u_id = a.uid </select> ~~~ **3.测试语句** ~~~ @Test public void testFindAllWithAccount(){ UserMapper mapper = session.getMapper(UserMapper.class); System.out.println(mapper.findAllWithAccount()); } ~~~