Mybatis多表查询(1)
(一)举例(用户和账户)
一个用户可以有多个账户
一个账户只能属于一个用户(多个账户也可以属于同一个用户)
(二)步骤
1、建立两张表:用户表,账户表,让用户表和账户表之间具备一对多的关系:需要使用外键在账户表中添加
2、建立两个实体类:用户实体类和账户实体类,让用户和账户的实体类能体现出来一对多的关系
3、建立两个配置文件,用户的配置文件账户的配置文件
4、实现配置:当我们查询用户时,可以同时得到用户下所包含的账户信息当我们查询账户时,可以同时得到账户的所属用户信息
(三)代码实现
1.pom.xml
<dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.11version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.41version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.4.6version> dependency> dependencies>
2.Users.java
package entity; import java.util.List; public class Users { private int uid; private String username; private String password; //一对多的关系映射,主表实体应该包含从表实体的集合引用 private Listaccounts; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public List getAccounts() { return accounts; } public void setAccounts(List accounts) { this.accounts = accounts; } @Override public String toString() { return accounts+ "uid=" + uid + ", username='" + username + '\'' + ", password='" + password + '\'' ; } }
3.Account.java
package entity; public class Account { private int aid; private String accountname; private int uid; private Users users; public int getAid() { return aid; } public void setAid(int aid) { this.aid = aid; } public String getAccountname() { return accountname; } public void setAccountname(String accountname) { this.accountname = accountname; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public Users getUsers() { return users; } public void setUsers(Users users) { this.users = users; } @Override public String toString() { return "aid=" + aid + ", accountname='" + accountname + '\'' + ", uid=" + uid ; } }
4.UsersAccount.java
package entity; public class UsersAccount extends Account{ private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "uid="+super.getUid()+",aid="+super.getAid()+", username='" + username + '\'' + ", password='" + password + '\'' +",accountname="+super.getAccountname(); } }
5.SqlMapperConfig.xml
xml version="1.0" encoding="UTF-8" ?> DOCTYPEconfiguration PUBLIC"-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC">transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver">property> <property name="url" value="jdbc:mysql://localhost:3306/zml01?useUnicode=true&characterEncoding=utf-8">property> <property name="username" value="root">property> <property name="password" value="root">property> dataSource> environment> environments> <mappers> <mapper resource="dao/UserDao.xml">mapper> <mapper resource="dao/AccountDao.xml">mapper> mappers> configuration>
6.UserDao.java
package dao; import entity.Users; import java.util.List; public interface UserDao { ListfindAll(); Users find(int id); }
7.UserDao.xml
xml version="1.0" encoding="UTF-8" ?> DOCTYPEmapper PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="dao.UserDao"> <resultMap id="userAccountMap" type="entity.Users"> <id property="uid" column="uid">id> <result property="username" column="username">result> <result property="password" column="password">result> <collection property="accounts" ofType="entity.Account"> <id property="aid" column="aid">id> <result property="accountname" column="accountname">result> <result property="uid" column="uid">result> collection> resultMap> <select id="findAll" resultMap="userAccountMap"> select * from users u left outer join account a on u.uid=a.uid select> <select id="find" resultType="entity.Users"> select * from users where uid=#{uid} select> mapper>
8.UserTest.java
package test; import dao.UserDao; import entity.Users; 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 UserTest { private InputStream in; private SqlSession sqlSession; private UserDao UserDaoImpl; @Before public void init() throws IOException { //1.读取配置文件 in= Resources.getResourceAsStream("SqlMapperConfig.xml"); //2.创建SqlSessionFactory工厂 SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); SqlSessionFactory factory=builder.build(in); //3.使用工厂生产SqlSession对象 sqlSession=factory.openSession(); //4.使用SqlSession创建dao接口的代理对象 UserDaoImpl=sqlSession.getMapper(UserDao.class); } @After public void destroy() throws IOException { //提交事务 sqlSession.commit(); //6.释放资源 sqlSession.close(); in.close(); } /** * 查询所有 * @throws IOException */ @Test public void findall() throws IOException { //5.使用代理对象执行方法 Listusers=UserDaoImpl.findAll(); for(Users u:users){ System.out.println(u); } } }
9.AccountDao.java
package dao; import entity.Account; import entity.UsersAccount; import java.util.List; public interface AccountDao { ListfindAll(); List find(); }
10.AccountDao.xml
xml version="1.0" encoding="UTF-8" ?> DOCTYPEmapper PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="dao.AccountDao"> <resultMap id="accountUsersMap" type="entity.Account"> <id property="aid" column="aid">id> <result property="accountname" column="accountname">result> <result property="uid" column="uid">result> <association property="users" column="uid" javaType="entity.Users"> <id property="uid" column="uid">id> <result property="username" column="username">result> <result property="password" column="password">result> association> resultMap> <select id="findAll" resultMap="accountUsersMap"> select a.*,u.username,u.password from account a,users u where a.uid=u.uid select> <select id="find" resultType="entity.UsersAccount"> select a.*,u.username,u.password from account a,users u where a.uid=u.uid select> mapper>
11.AccountTest.java
package test; import dao.AccountDao; import dao.UserDao; import entity.Account; import entity.Users; import entity.UsersAccount; 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 AccountDao AccountDaoImpl; @Before public void init() throws IOException { //1.读取配置文件 in= Resources.getResourceAsStream("SqlMapperConfig.xml"); //2.创建SqlSessionFactory工厂 SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); SqlSessionFactory factory=builder.build(in); //3.使用工厂生产SqlSession对象 sqlSession=factory.openSession(); //4.使用SqlSession创建dao接口的代理对象 AccountDaoImpl=sqlSession.getMapper(AccountDao.class); } @After public void destroy() throws IOException { //提交事务 sqlSession.commit(); //6.释放资源 sqlSession.close(); in.close(); } /** * 查询所有 * @throws IOException */ @Test public void findall() throws IOException { //5.使用代理对象执行方法 Listaccount=AccountDaoImpl.findAll(); for(Account a:account){ System.out.println(a); System.out.println(a.getUsers()); } } /** * 查询用户的所有订单 * @throws IOException */ @Test public void find() throws IOException { //5.使用代理对象执行方法 List ua=AccountDaoImpl.find(); for(UsersAccount a:ua){ System.out.println(a); } } }