1、 项目简介
首先呢,我们创建两张表,一个是users表,一个是account表,两张表组成了用户和账户之间的关系,一个用户可以有多个账户,一个账户只能属于一个用户,这就是我们的背景。首先实现一对一表的联合查询,一个用户对应多张账户表。
2、数据库表
用户表users
账户表account
其中账户表的uid为外键,指向的是用户表的主键id
3、项目的目录
4、我们首先来看两个实体类,对应着两张表
1、User类
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
// 一对多关系映射,主表实体应该包含所属从表的实体的集合
private List<Account> accounts;
2、Account类:这里需要注意的是,在这里定义了一User的类。用于查询当前账户所属的用户信息。
public class Account implements Serializable {
private Integer id;
private Integer uid;
private String money;
5、查看咱们的主要配置文件:IUserDao.xml
这里与一对一查询不同的是在resultMap标签内嵌套了一个collection标签而不是association标签,这里的collection表示的是一个集合。这里需要注意一下ofType这个属性,这个属性表示的是这个集合中的成员的类型。如果没有别名的话,必须要用全限定类名。对于这里用的sql语句select * from users u left outer join account a on u.id = a.uid。采用左外查询方式,这个查询的好处是左边的user表会全部查出。
<?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.mybatis.dao.IUserDao">
<!-- 定义User的resultMap-->
<resultMap id="userAccountMap" type="com.mybatis.domain.User">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<collection property="accounts" ofType="com.mybatis.domain.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="userAccountMap">
select * from users u left outer join account a on u.id = a.uid
</select>
<select id="findById" parameterType="INT" resultType="com.mybatis.domain.User">
select * from users where id = #{uid}
</select>
</mapper>
5、运行
public void testFindAll() throws Exception{
List<User> users = userDao.findAll();
for (User user:users){
System.out.println("--------------------------------------");
System.out.println(user);
System.out.println(user.getAccounts());
}
}
6、全部源代码
Acount类
package com.mybatis.domain;
import java.io.Serializable;
public class Account implements Serializable {
private Integer id;
private Integer uid;
private String money;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", uid=" + uid +
", money='" + money + '\'' +
'}';
}
}
User类
package com.mybatis.domain;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
// 一对多关系映射,主表实体应该包含所属从表的实体的集合
private List<Account> accounts;
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
IUserDao接口
package com.mybatis.dao;
import com.mybatis.domain.User;
import java.util.List;
public interface IUserDao {
/**
* 查询所有用户,以及账户信息
* @return
*/
List<User> findAll();
/**
* 根据ID查询用户信息
* @param userId
* @return
*/
User findById(Integer userId);
}
IUserDao.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.mybatis.dao.IUserDao">
<!-- 定义User的resultMap-->
<resultMap id="userAccountMap" type="com.mybatis.domain.User">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<collection property="accounts" ofType="com.mybatis.domain.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="userAccountMap">
select * from users u left outer join account a on u.id = a.uid
</select>
<select id="findById" parameterType="INT" resultType="com.mybatis.domain.User">
select * from users where id = #{uid}
</select>
</mapper>
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--mybatis主配置文件-->
<configuration>
<!--配置环境-->
<environments default="mysql">
<!-- 配置mysql环境-->
<environment id="mysql">
<!-- 配置事务类型-->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源(连接池)-->
<dataSource type="POOLED">
<!-- 配置数据库的基本信息-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="111111"/>
</dataSource>
</environment>
</environments>
<!-- 指定映射配置文件的位置,映射配置文件指的是每一个dao独立的配置文件-->
<mappers>
<package name="com.mybatis.dao"/>
</mappers>
</configuration>
测试类
package com.mybatis.test;
import com.mybatis.dao.IUserDao;
import com.mybatis.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
public class MybatisTest {
private InputStream in;
private SqlSession session;
private IUserDao userDao;
@Before
public void init() throws Exception {
this.in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
System.out.println(in);
SqlSessionFactory factory = factoryBuilder.build(in);
// this.session = factory.openSession(true);
this.session = factory.openSession();
this.userDao = session.getMapper(IUserDao.class);
}
@After
public void destory() throws IOException {
session.commit();
this.in.close();
this.session.close();
}
@Test
public void testFindAll() throws Exception{
List<User> users = userDao.findAll();
for (User user:users){
System.out.println("--------------------------------------");
System.out.println(user);
System.out.println(user.getAccounts());
}
}
}