在数据库中,表与表存在着很多种关系,像一对多,一对一,多对多,我们来把他拿到实际应用中来举个例子:
一对多:好比一个用户可以下多个订单 (多对一:多个订单可以供一个的下 mybatis 中还没有多对一的概念)
一对一:一个订单只能有一个订单号
多对多:一个学生可以有多个老师,一个老师也可以教多个学生。
在这,我们就具体看一下mybatis中一对一关系的表的操作。
首先有两个实体类 user 和 account 我们在account类中定义了user类对象
Accout
package com.qst.domain;
import java.io.Serializable;
public class Account implements Serializable {
private Integer id;
private Double money;
private Integer uid;
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 Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
@Override
public String toString() {
return user + "Account{" +
"id=" + id +
", money=" + money +
", uid=" + uid +
'}';
}
}
User
package com.qst.domain;
public class User {
private Integer id;
private String username;
private String gender;
private String email;
private Integer did;
public void setId(Integer id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setEmail(String email) {
this.email = email;
}
public void setDid(Integer did) {
this.did = did;
}
public Integer getId() {
return id;
}
public String getUsername() {
return username;
}
public String getGender() {
return gender;
}
public String getEmail() {
return email;
}
public Integer getDid() {
return did;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", gender=" + gender +
", email='" + email + '\'' +
", did=" + did +
'}';
}
}
定义一个实现接口IAccountDao,用来查询account 和user
package com.qst.dao;
import com.qst.domain.Account;
import com.qst.domain.AccountUser;
import java.util.List;
public interface IAccountDao {
List<AccountUser> findAlluser();
}
在配置映射时我们需要一个resultMap 标签
<?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.qst.dao.IAccountDao">
<resultMap id="findUandA" type="com.qst.domain.Account">
//主键 property实体类的定义 column数据库中的名字(aid为别名)
<id property="id" column="aid"></id>
<result property="money" column="money"></result>
<result property="uid" column="uid"></result>
//user对象 javaType用来指定封装类
<association property="user" column="uid" javaType="com.qst.domain.User">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="gender" column="gender"></result>
<result property="email" column="email"></result>
<result property="did" column="did"></result>
</association>
</resultMap>
<!--查询所有-->
<select id="findAlls" resultMap="findUandA">
select e.* ,a.id as aid,a.money,a.uid from account a , employee e where a.uid = e.id;
</select>
</mapper>
开始测试
package com.qst;
import com.qst.dao.IAccountDao;
import com.qst.dao.IUserDao;
import com.qst.domain.Account;
import com.qst.domain.AccountUser;
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 AccountTest {
private InputStream in;
private SqlSession sqlSession;
private IAccountDao accountDao ;
@Before
public void init() throws IOException {
in = Resources.getResourceAsStream("Config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
sqlSession = factory.openSession();
accountDao= sqlSession.getMapper(IAccountDao.class);
}
@After
public void destory() throws IOException{
sqlSession.commit();
sqlSession.close();
in.close();
}
@Test
public void findals(){
List<Account> acc = accountDao.findAlls();
for(Account acc1 :acc){
System.out.println(acc1);
}
}
}