多表查询
表和表之间的联系-----外键
类如何表示表之间的联系----------设置相应的属性
当多个表查询、数据库中表的字段名与定义的实体类中的属性名字不同时,不能用resultType,要用resultMap转化一下
以下 一对一 一个订单对应一个用户
//订单实体类
public class Order {
private Integer id;
private Date ordertime;
private Double total;
private User user;
//用户实体类
public class User {
private Integer id;
private String username;
private String password;
查询订单对应的用户
要求输出结果
Id ordertime total uid username password
resultMap id="orderMap" type="order">
<!--主键
column:数据库表中的列
property:实体类中的属性
-->
<id column="id" property="id"/>
<result column="ordertime" property="ordertime"/>
<result column="total" property="total"/>
</resultMap>
<select id="findAll" resultMap="orderMap">
SELECT
o.*, u.username username, u.`password` `password`
FROM
`user` u, `order` o
WHERE
u.id=o.uid
</select>
resultMap参数 中的type:查询的结果封装成什么类型,就填什么类型
此时输出的结果中,user为null;
那么怎么让它输出user?
第一种方法:(了解)
<id column="id" property="id"/>
<result column="ordertime" property="ordertime"/>
<result column="total" property="total"/>
<result column="uid" property="user.id"/>
<result column="username" property="user.username"/>
<result column="password" property="user.password"/>
此时property= 别名.id
(如何设置别名在上一篇最后)
第二种(常用)
<id column="id" property="id"/>
<result column="ordertime" property="ordertime"/>
<result column="total" property="total"/>
<association property="user" javaType="user">
<id column="uid" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
</association>
association 表示一种关联,用于建立一对一的关系
property实体类中的属性
javaType类型,可写别名
第三种(了解)
<!--OrderMapper.xml-->
<resultMap id="orderMap" type="order">
<id column="id" property="id"/>
<result column="ordertime" property="ordertime"/>
<result column="total" property="total"/>
<association property="user" javaType="user" column="uid" select="com.hpe.mapper.UserMapper.findById"/>
</resultMap>
<!--UserMapper.xml-->
<mapper namespace="com.hpe.mapper.UserMapper">
<select id="findById" resultType="user">
SELECT * FROM user WHERE id=#{uid}
</select>
</mapper>
这个思想是先查询所有订单,再根据每条订单对应的uid去执行userMapper中的sql根据id查询用户
这里association 中的 column 为表的字段名,或者是列的别名,设置为传入嵌套Select语句的参数
Select: 用于加载映射sql语句
它会从column属性指定的列中检索数据,作为参数传递给目标servlet语句。
<注意:当有多个映射配置文件时,一定不要忘记在核心配置文件加载映射文件>
以下 一对多 一个用户对应多个订单
<!--第一种方式:-->
<resultMap id="userMap" type="user">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<collection property="order" ofType="order">
<id column="oid" property="id"/>
<result column="ordertime" property="ordertime"/>
<result column="total" property="total"/>
</collection>
</resultMap>
<select id="findAll" resultMap="userMap">
SELECT
u.*,o.id oid,o.ordertime ordertime ,o.total total
FROM
`user` u,`order` o
WHERE
o.uid=u.id
</select>
此时不用association,而改用collection
ofType 接希望什么类型 的全限定类名或别名
<!--第二种方式-->
UserMapper.xml
<resultMap id="userMap" type="user">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<collection property="order" column="id" ofType="order" select="com.hpe.mapper.OrderMapper.findById"/>
</resultMap>
<select id="findAll" resultMap="userMap">
SELECT * FROM user
</select>
OrderMapper.xml
<select id="findById" resultType="order">
SELECT * FROM `order` WHERE uid=#{id}
</select>
也是先查找user,然后把id传给select指向的sql语句
以下 多对多 用户-角色
多对多
表: 一 般通过第三张表去维护多对多的关系
实体类:
角色类:设置一个集合存放这个角色所拥有的所有用户
用户类:设置一个集合存放这个用户所担任的所有角色
/**
* @description: 角色实体类
* @author: Administrator
* @create: 2020-11-05 16:56
**/
public class SysRole {
private Long id;
private String roleName;
private String roleDesc;
private List<SysUser> users;
/**
* @description: 用户实体类
* @author: Administrator
* @create: 2020-11-05 16:57
**/
public class SysUser {
private Long id;
private String username;
private String email;
private String password;
private String phoneNum;
private List<SysRole> roles;
输出id roleName roleDesc uid username email password phoneNum
其实方法差不多
<resultMap id="sysRoleMap" type="sysRole">
<id column="id" property="id"/>
<result column="roleName" property="roleName"/>
<result column="roleDesc" property="roleDesc"/>
<collection property="users" ofType="sysUser">
<id column="uid" property="id"/>
<result column="username" property="username"/>
<result column="email" property="email"/>
<result column="password" property="password"/>
<result column="phoneNum" property="phoneNum"/>
</collection>
</resultMap>
<select id="findAll" resultMap="sysRoleMap">
SELECT
r.*,u.id uid,u.username username,u.email email,u.`password` `password`,u.phoneNum phoneNum
FROM
sys_user u,sys_user_role ur,sys_role r
WHERE
ur.roleId=r.id AND ur.userId=u.id
</select>
TypeHandlers(类处理器)
实现java类与mysql的转换
分页
拷贝相关的jar包
在核心配置文件配置PageHelper插件
<plugins>
<!—配置分页插件-->
< plugin interceptor=’’com.github.pagehelper.PageHelper”>
<!—配置分页插件方言 dialect方言 -->
<property name=”dialect” value=”mysql” />
</plugins>
测试、
@Test
public void testPageHelper() {
PageHelper.startPage(2, 3);
List<Student> students = studentMapper.findAll();
for (Student student : students) {
System.out.println(student);
}
System.out.println("-----------------------------------------------");
PageInfo<Student> pageInfo = new PageInfo<>(students);
System.out.println("总记录数:" + pageInfo.getTotal());
System.out.println("当前页:" + pageInfo.getPageNum());
System.out.println("总页数:" + pageInfo.getPages());
System.out.println("上一页:" + pageInfo.getPrePage());
System.out.println("下一页:" + pageInfo.getNextPage());
System.out.println("是否是首页:" + pageInfo.isIsFirstPage());
System.out.println("是否是尾页:" + pageInfo.isIsLastPage());
}