mybatis第三天多表查询
之前学习的为mybatis的基础查询,但使用的重点是多表查询。
之前学习为单表,为用户表主要包括了用户的基本信息,今天的学习引入的一个新的表,用户的账户表,账户表的内容为对应的用户有多少钱。
完成上述操作的具体步骤如下:
1.在mysql数据库中,写好account代码,建立account表
2.创建account实体类,并在account表中加入user的类,这个做法是为了让account与user有联系
public class account() {
private int id ;
private int uid;
private double money;
重点来了: private User user;
生成get set方法 和tostring方法
}
3.创建 account的持久层接口 IAccountDao, 在这个接口中 只写一个方法 就是查询所有 findAll
List<Account> findAll();
4.创建出IAccount.xml文件,详细对数据库进行操作
因为是查询所有 所以使用select方法
<resultMap id="UserAccountMap" type="com.itheima.domain.Account">
<id property="id" column="aid"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!--association标签是关联表的信息
column是根据上面中哪一个数据进行一对一进行关联的
property中的user要与account中保持一直
-->
<association property="user" column="uid" javaType="com.itheima.domain.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="UserAccountMap">
select u.*, a.aid,a.uid,a.money from user as u,account as a where u.id = a.uid;
</select>
(上面为一对一的多表查询)

浙公网安备 33010602011771号