数据库表结构、数据
-
account表
-
user表
一对一 association、javaType
- 第一种方式
<select id="findAllAccountUser" resultType="com.wei.pojo.AccountUser">
select a.*,u.username,u.address from account a ,user u where a.uid = u.id
</select>
//继承Account,并且返回父类的tostring+自己需要的2个属性
public class AccountUser extends Account {
private String username;
private String address;
@Override
public String toString() {
return super.toString()+"-"+"AccountUser{" +
"username='" + username + '\'' +
", address='" + address + '\'' +
'}';
}
}
@Test
public void fun9(){
SqlSession sqlsession = MybatisUtils.getSqlsession();
AccountMapper mapper = sqlsession.getMapper(AccountMapper.class);
List<AccountUser> accountUsers = mapper.findAllAccountUser();
System.out.println(accountUsers);
}
- 第二种方式
<resultMap id="accountUserMap" type="account">
<id property="id" column="aid" />
<result property="uid" column="uid" />
<result property="money" column="money" />
<!-- association 一对一,使用javaType指定类型-->
<association property="user" column="uid" javaType="user" >
<result property="id" column="uid" />
<result property="username" column="username" />
<result property="address" column="address" />
<result property="sex" column="sex" />
<result property="birthday" column="birthday" />
</association>
</resultMap>
<select id="findAll" resultMap="accountUserMap">
select a.id aid,u.id uid,a.money,u.* from account a,user u where a.uid = u.id
</select>
public class Account {
private Integer id;
private Integer uid;
private Double money;
private User user;
@Override
public String toString() {
return "Account{" +
"id=" + id +
", uid=" + uid +
", money=" + money +
", user=" + user +
'}';
}
}
//测试
@Test
public void fun10(){
SqlSession sqlsession = MybatisUtils.getSqlsession();
AccountMapper mapper = sqlsession.getMapper(AccountMapper.class);
List<Account> account = mapper.findAll();
System.out.println(account);
}
- 运行结果如下
一对多 collection 、ofType
<resultMap id="userAccountMap" type="user">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="birthday" column="birthday" />
<result property="sex" column="sex" />
<result property="address" column="address" />
<!-- collection 多对多,使用ofType指定集合中的类型-->
<collection property="accounts" ofType="Account" >
<id property="id" column="aid" />
<result property="uid" column="uid" />
<result property="money" column="money" />
</collection>
</resultMap>
<select id="findAllUserAccount" resultMap="userAccountMap">
select *,a.id aid from user u left outer join account a on a.uid = u.id
</select>
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
private List<Account> accounts;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\''+this.getAccounts();
}
}
//测试
@Test
public void fun11(){
SqlSession sqlsession = MybatisUtils.getSqlsession();
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
List<User> all = mapper.findAllUserAccount();
for (User user : all) {
System.out.println(user);
}
}
- 运行结果如下
能够识别出同一用户的多个账户,没有缺少账户、分开输出。
多对多
Role 查询出多个 User
- RoleMapper.xml
<?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="com.wei.mapper.RoleMapper">
<resultMap id="roleUserMap" type="role">
<id property="id" column="id" />
<result property="roleName" column="role_name" />
<result property="roleDesc" column="role_desc" />
<collection property="users" ofType="user2" >
<id property="id" column="uid" />
<result property="username" column="username" />
<result property="birthday" column="birthday" />
<result property="sex" column="sex" />
<result property="address" column="address" />
</collection>
</resultMap>
<select id="findAll" resultMap="roleUserMap">
select * from role r
left outer join user_role ur on r.id = ur.rid
left outer join user u on u.id = ur.uid
</select>
</mapper>
//实体类
@Data
public class Role implements Serializable {
private Integer id;
private String roleName;
private String roleDesc;
private List<User> users;
}
//测试
@Test
public void fun2(){
SqlSession sqlsession = MybatisUtils.getSqlsession();
RoleMapper mapper = sqlsession.getMapper(RoleMapper.class);
List<Role> all = mapper.findAll();
System.out.println(all);
sqlsession.close();
}
- 运行结果
User 查询出多个 Role
- UserMapper.xml
<?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="com.wei.mapper.UserMapper">
<resultMap id="userRoleMap" type="user2">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="birthday" column="birthday" />
<result property="sex" column="sex" />
<result property="address" column="address" />
<collection property="roles" ofType="role" >
<id property="id" column="rid" />
<result property="roleName" column="role_name" />
<result property="roleDesc" column="role_desc" />
</collection>
</resultMap>
<select id="findAll" resultMap="userRoleMap">
select * from user u
left outer join user_role ur on u.id = ur.uid
left outer join role r on r.id = ur.rid
</select>
</mapper>
//实体类
@Data
@Alias("user2") //此处取别名是为了避免我这里的别名冲突
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
private List<Role> roles;
}
//测试
@Test
public void fun1(){
SqlSession sqlsession = MybatisUtils.getSqlsession();
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
List<User> all = mapper.findAll();
System.out.println(all);
sqlsession.close();
}
- 运行结果