案例主要以最为简单的用户和账户的模型来分析 Mybatis 多表关系。用户为 User 表,账户为Account 表。一个用户(User)可以有多个账户(Account)。
查询所有的账户信息,并关联查询出每个账户对应的用户信息(注意:因为一个账户信息只能供某个用户使用,所以从查询账户信息出发关联查询用户信息为一对一查询。如 果从用户信息出发查询用户下的账户信息则为一对多查询,因为一个用户可以有多个账户)。
-- 创建数据库 CREATE DATABASE mybatis; -- 使用数据库 USE mybatis; -- 删除用户表 DROP TABLE IF EXISTS user; -- 创建用户表 CREATE TABLE `user` ( `id` INT(11) NOT NULL auto_increment, `username` VARCHAR(32) NOT NULL COMMENT '用户名称', `birthday` DATETIME DEFAULT NULL COMMENT '生日', `sex` char(1) DEFAULT NULL COMMENT '性别', `address` VARCHAR(256) DEFAULT NULL COMMENT '地址', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- 插入用户数据 INSERT INTO `user`(`id`,`username`,`birthday`,`sex`,`address`) VALUES (41,'张三','2019-03-27 18:47:08','男','北京'); INSERT INTO `user`(`id`,`username`,`birthday`,`sex`,`address`) VALUES (42,'李四','2019-03-27 18:47:08','男','上海'); INSERT INTO `user`(`id`,`username`,`birthday`,`sex`,`address`) VALUES (43,'王五','2019-03-27 18:47:08','男','广州'); -- 删除账户表 DROP TABLE IF EXISTS `account`; -- 创建账户表 CREATE TABLE `account` ( `id` INT(11) NOT NULL COMMENT '编号', `uid` INT(11) DEFAULT NULL COMMENT '用户编号', `money` DOUBLE DEFAULT NULL COMMENT '金额', PRIMARY KEY (`ID`), KEY `FK_Reference_8` (`UID`), CONSTRAINT `FK_Reference_8` FOREIGN KEY (`UID`) REFERENCES `user` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- 插入账户数据 INSERT INTO `account`(`ID`,`UID`,`MONEY`) VALUES (1,41,1000); INSERT INTO `account`(`ID`,`UID`,`MONEY`) VALUES (2,42,1000); INSERT INTO `account`(`ID`,`UID`,`MONEY`) VALUES (3,43,1000);
public class User implements Serializable { private Integer id; private String username; private Date birthday; private String sex; private String address; }
public class Account implements Serializable { private Integer id; private Integer uid; private Double money; // 一对一关系中,从表实体需要包含一个主表实体对象的引用 private User user; }
public interface AccountMapper { /** * 查询所有账户,并且返回账户所属的用户信息 * @return */ List<Account> findAll(); }
jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/mybatis jdbc.username = root jdbc.password = root
<?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"> <configuration> <properties resource="datasource.properties"></properties> <typeAliases> <package name="com.itcast.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="jdbc"></transactionManager> <dataSource type="pooled"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <package name="com.itcast.dao"/> </mappers> </configuration>
<?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.itcast.dao.AccountMapper"> <resultMap id="accountMap" type="com.itcast.pojo.Account"> <id property="id" column="aid"></id> <result property="uid" column="uid"></result> <result property="money" column="money"></result> <association property="user" javaType="com.itcast.pojo.User"> <id property="id" column="id"></id> <result property="username" column="username"></result> <result property="birthday" column="birthday"></result> <result property="sex" column="sex"></result> <result property="address" column="address"></result> </association> </resultMap> <select id="findAll" resultMap="accountMap"> SELECT a.`id` AS aid, a.uid, a.money, u.* FROM user u, account a WHERE a.`uid` = u.`id` </select> </mapper>
/** * 测试查询所有方法 */ @Test public void testFindAll() { List<Account> accountList = this.accountMapper.findAll(); for (Account account : accountList) { System.out.println(account); } }