标签:配置文件 技术 test oct alt 代理 input mon play
需求:
查询所有用户信息及用户关联的账户信息。
分析:
用户信息和他的账户信息为一对多关系,并且查询过程中如果用户没有账户信息,此时也要将用户信息 查询出来,我们想到了左外连接查询比较合适。
SELECT u.*, a.id as aid, a.uid,a.money FROM user u LEFT JOIN account a ON u.id = acc.uid
测试该 SQL 语句在 MySQL 客户端工具的查询结果如下:
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 + ‘\‘‘ + ‘}‘; } }
public class Account implements Serializable { private Integer id; private Integer uid; private Double money; 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 Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } @Override public String toString() { return "Account{" + "id=" + id + ", uid=" + uid + ", money=" + money + ‘}‘; } }
public interface IUserDao { /** * 查询所有用户,同时获取出每个用户下的所有账户信息 */ List<User> findAll(); }
<?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.churujianghudezai.dao.IUserDao"> <resultMap id="userMap" type="user"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="birthday" property="birthday"/> <result column="sex" property="sex"/> <result column="address" property="address"/> <!-- collection 是用于建立一对多中集合属性的对应关系 ofType 用于指定集合元素的数据类型 --> <collection property="accounts" ofType="account"> <id column="aid" property="id"/><!--此处的column值不能为id,不然会当成user的id,导致输出结果有误--> <result column="uid" property="uid"/> <result column="money" property="money"/> </collection> </resultMap> <!--这里的a.id要取别名,因为如果不取别名,则上面提到的集合的id最终会被当做用户的id进行封装,导致account的id等于user的id
这也就是上面<id column="aid" property="id"/>中column="aid"的原因--> <select id="findAll" resultMap="userMap"> select u.*,a.id as aid,a.uid,a.money from user u left outer join account a on u.id = a.uid </select> </mapper>
collection :
定义了用户关联的账户信息。表示关联查询结果集
property="accounts" :
关联查询的结果集存储在 User 对象的上哪个属性。
ofType="account" :
指定关联查询的结果集中的对象类型即List中的对象类型。此处可以使用别名,也可以使用全限定名。
public class UserTest { private InputStream in; private SqlSessionFactory factory; private SqlSession session; private IUserDao userDao; @Before//在测试方法执行前执行 public void init() throws Exception{ //1.读取配置文件 in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.创建构建者对象 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); //3.创建SqlSession工厂对象 factory = builder.build(in); //4.创建SqlSession对象 session = factory.openSession(); //5.创建Dao的代理对象 userDao = session.getMapper(IUserDao.class); } @After//测试方法执行完成之后执行 public void destroy() throws Exception{ //7.提交事务 session.commit(); //8.释放资源 session.close(); in.close(); } @Test public void testfindAll(){ List<User> us = userDao.findAll(); for (User u:us){ System.out.println("----------每个用户的内容----------"); System.out.println(u); System.out.println(u.getAccounts()); } } }
结果为:
标签:配置文件 技术 test oct alt 代理 input mon play
原文地址:https://www.cnblogs.com/churujianghudezai/p/12260114.html