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;
2、Account类:这里需要注意的是,在这里定义了一User的类。用于查询当前账户所属的用户信息。
public class Account implements Serializable {
private Integer id;
private Integer uid;
private String money;
private User user;
5、查看咱们的主要配置文件:IAccountDao.xml
在之前的文章中提到过resultMap这个标签,但是对于多表查询我们还需要嵌套一个association标签。因为我们主要查询的是账户信息,其次是附带的用户信息,所以将用户信息放在association标签内。其中的property就是咱们在Account类中定义的属性。接着就是SQL语句select u.*,a.id as aid,a.uid,a.money from account a,users u where u.id = a.uid。我们首先将两张表分别取别名,然后条件是id与uid相等,之后我们先查询出用户的所有信息,然后查询出账户的信息,哪张表放在前面都行。
<?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.IAccountDao">
<!-- 定义封装account和user的resultMap-->
<resultMap id="accountUserMap" type="com.mybatis.domain.Account">
<id property="id" column="aid"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!-- 一对一关系映射,配置封装user的内容-->
<association property="user" column="uid">
<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>
</association>
</resultMap>
<!-- 查询所有-->
<select id="findAll" resultMap="accountUserMap">
select u.*,a.id as aid,a.uid,a.money from account a,users u where u.id = a.uid
</select>
</mapper>
5、运行
public void testFindAll() throws Exception{
List<Account> accounts = accountDao.findAll();
for (Account a:accounts){
System.out.println("-----------------------");
System.out.println(a + " "+ a.getUser());
}
}
6、全部源代码
Account类:
package com.mybatis.domain;
import java.io.Serializable;
public class Account implements Serializable {
private Integer id;
private Integer uid;
private String money;
private User user;
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;
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
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 + '\'' +
'}';
}
}
IAccountDao接口
package com.mybatis.dao;
import com.mybatis.domain.Account;
import java.util.List;
public interface IAccountDao {
/**
* 查询所有的Account
* @return
*/
List<Account> findAll();
}
IAccountDao.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.IAccountDao">
<!-- 定义封装account和user的resultMap-->
<resultMap id="accountUserMap" type="com.mybatis.domain.Account">
<id property="id" column="aid"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!-- 一对一关系映射,配置封装user的内容-->
<association property="user" column="uid">
<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>
</association>
</resultMap>
<!-- 查询所有-->
<select id="findAll" resultMap="accountUserMap">
select u.*,a.id as aid,a.uid,a.money from account a,users u where u.id = a.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.IAccountDao;
import com.mybatis.dao.IUserDao;
import com.mybatis.domain.Account;
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.List;
public class MybatisAccountTest {
private InputStream in;
private SqlSession session;
private IAccountDao accountDao;
@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.accountDao = session.getMapper(IAccountDao.class);
}
@After
public void destory() throws IOException {
session.commit();
this.in.close();
this.session.close();
}
@Test
public void testFindAll() throws Exception{
List<Account> accounts = accountDao.findAll();
for (Account a:accounts){
System.out.println("-----------------------");
System.out.println(a + " "+ a.getUser());
}
}
}