spring boot——spring boot的基本配置——spring boot整合mybatis——本地实例运行——MyBatis多表连接——collection多对一





注意:

===================================================================
数据库:
CREATE TABLE `user2` ( `id` int , `name` varchar(255) , `age` int ) ; insert into user2( id,name,age ) values ( 41,"wang",25); CREATE TABLE `account` ( `id` int , `uid` int , `money` int ) ; insert into account( id,uid,money) values(1,41,100); insert into account( id,uid,money) values(2,41,150);
user2类:
package org.example.entity; import java.util.List; public class User2 { private int id; private String name; private int age; public List<Account> getAccountList() { return accountList; } public void setAccountList(List<Account> accountList) { this.accountList = accountList; } private List<Account> accountList; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User2{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", accountList=" + accountList + '}'; } }

account类:
package org.example.entity; public class Account { private int id; private int uid; private int money; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } @Override public String toString() { return "Account{" + "id=" + id + ", uid=" + uid + ", money=" + money + '}'; } }

mapper接口文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.example.mapper.User2Mapper"> <resultMap id="yiDuDuo" type="org.example.entity.User2"> <id property="id" column="id"></id> <result property="name" column="name"></result> <result property="age" column="age"></result> <collection property="accountList" ofType="org.example.entity.Account"> <id property="id" column="aid"></id> <result property="uid" column="uid"></result> <result property="money" column="money"></result> </collection> </resultMap> <!--查询所有数据--> <select id="findAll" resultMap="yiDuDuo"> select u.*,a.id as aid,a.uid,a.money from user2 u left outer join account a on u.id = a.uid </select> </mapper>

mapper接口:
package org.example.mapper; import org.apache.ibatis.annotations.Mapper; import org.example.entity.User2; import java.util.List; @Mapper public interface User2Mapper { List<User2> findAll(); }

service接口:
package org.example.service; import org.example.entity.User2; import java.util.List; public interface User2Service { List<User2> findAll(); }

实现service接口:
package org.example.service.Impl; import org.example.entity.User2; import org.example.mapper.User2Mapper; import org.example.service.User2Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class User2ServiceImpl implements User2Service { @Autowired User2Mapper otherUserMapper; public List<User2> findAll() { return otherUserMapper.findAll(); } }

控制器:
package org.example.controller; import org.example.entity.User2; import org.example.service.User2Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class User2Controller { @Autowired private User2Service otherService; @GetMapping(value = "/31") public List<User2> home31() { // 查询所有用户 List<User2> users = otherService.findAll(); for (User2 user : users) { System.out.println(user); } return users; } }

执行:



浙公网安备 33010602011771号