💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
**方式一,使用别名方式进行映射** **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()); } ~~~