ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**方式一,使用别名方式进行映射** **1.AccountMapper.java** ~~~ public interface AccountMapper { //查出所有的账户 及其关联的 用户 信息 List<Account> findAll1(); } ~~~ **2.AccountMapper.xml** ~~~ <!--这里必须是user.XX 才能映射--> <select id="findAll1" resultType="Account"> select a.*, u.u_id 'user.id', u.u_name 'user.name', u.u_password 'user.password' from account a left join t_user u on a.uid = u.u_id </select> ~~~ **3.测试代码** ~~~ @Test public void testFindAll1(){ AccountMapper mapper = session.getMapper(AccountMapper.class); System.out.println(mapper.findAll1()); } ~~~ **方式二,resultMap映射** **1.AccountMapper.java** ~~~ public interface AccountMapper { //查出所有的账户 及其关联的 用户 信息 List<Account> findAll1(); } ~~~ **2.AccountMapper.xml** ~~~ <resultMap id="accountMap1" type="Account"> <id property="id" column="id"></id> <result property="money" column="money"></result> <result property="user.id" column="u_id"></result> <result property="user.name" column="u_name"></result> <result property="user.password" column="u_password"></result> </resultMap> <select id="findAll1" resultMap="accountMap1"> select * from account a left join t_user u on a.uid = u.u_id </select> ~~~ **3.测试代码** ~~~ @Test public void testFindAll1(){ AccountMapper mapper = session.getMapper(AccountMapper.class); System.out.println(mapper.findAll1()); } ~~~ **方式三,resultMap+association 映射** **1.AccountMapper.java** ~~~ public interface AccountMapper { //查出所有的账户 及其关联的 用户 信息 List<Account> findAll1(); } ~~~ **2.AccountMapper.xml** ~~~ <resultMap id="accountMap1" type="Account"> <id property="id" column="id"></id> <result property="money" column="money"></result> <!-- association: 表示要封装一个对象类型的属性 property: 属性名 javaType: 属性对应的Java类型 --> <association property="user" javaType="User"> <result property="id" column="u_id"></result> <result property="name" column="u_name"></result> <result property="password" column="u_password"></result> </association> </resultMap> <select id="findAll1" resultMap="accountMap1"> SELECT * FROM account a LEFT JOIN t_user u ON a.uid = u.u_id </select> ~~~ **3.测试代码** ~~~ @Test public void testFindAll1(){ AccountMapper mapper = session.getMapper(AccountMapper.class); System.out.println(mapper.findAll1()); } ~~~