输入映射(parameterType)
-
简单类型
- 基本类型 —> 整形为主
- String字符串
-
复杂类型
-
pojo
pojo Plain Ordinary Java Object 简单的Java对象,实际上就是我们的 JavaBean
当传入的参数较多时,使用参数的索引下标 0,1,2,3,4,5,…
可以把参数封装到pojo中,以pojo作为参数进行传递
User Role Department
-
pojo的包装类 格式:类名Vo
-
内部至少包含一个普通的类,可以进行多条件查询
-
一般用于多个对象组成查询条件
比如: 查询用户时,用户里面包含角色信息,可能需要把角色信息也要作为查询条件
-
-
输出映射(resultType)
resultType 属性可以指定结果集的类型,它支持基本类型和JavaBean类型
resultType和parameterType一样,如果提前注册过类型的别名,可以直接使用别名(不区分大小写),如果没有注册过需要使用全限定类名
注意:实体类中的属性名称必须和表中的字段名称(sql查询语句中字段名称)保持一致,否则无法完成封装
- 简单类型
- 基本类型 —>整形为主 count()
- String 字符串
<select id = "selectUserCount" resultType = "Integer">
select count(*) from user
</select>
<select id = "queryUserName" parameterType = "int" resultType = "String">
select username from user where id = #{id}
</select>
- 复杂类型
- pojo ------> 单个对象 一条记录 resultType = “user”
- pojo列表 -----> 多个对象 多条记录 存在在数组或者集合当中 resultType = “user”
问题:当实体类属性和表中的字段名称不一致时,应该怎么封装
一、使用别名查询(不推荐使用)
<select id = "findAllUsers" resultType = "user">
select id as userId,username as userName,birthday as userBirthday,sex as userSex
, address as userAddress from user
</select>
如果查询sql语句较多时,使用别名不方便
二、resultMap (推荐使用)
如果查询sql语句较多时,我们可以使用resultMap进行实体类属性和表中的字段进行映射
首先定义resultMap 把实体类属性和表中的字段进行一一映射
在select标签中引入resultMap结果映射
此外,resultMap还可以进行将查询的结果值映射为复杂类型的pojo,比如在查询结果映射对象时,对象可以包括pojo,和List 实现一对一查询 和一对多查询。
- id 映射:<id column = “表字段名” property = “实体类属性名”>
- 非主键映射:<result column = “表字段名” property = “实体类属性名”>
<!--使用resultMap 让实体类的属性名称和表中的字段名称保持一致-->
<resultMap id="roleMap" type="role">
<id property="roleId" column="r_id"></id>
<result property="roleName" column="r_name"></result>
<result property="roleDesc" column="r_desc"></result>
<result property="roleUpdateTime" column="r_updateTime"></result>
</resultMap>
<select id="getAllRoles2" resultMap="roleMap">
<!--采用起别名方式-->
select * from role
</select>
- 一对一映射:<association property = “实体类属性名”></association>
当输出结果时,内部包含了另外一个pojo时,使用resultMap和pojo中的属性映射,可以先在类中声明该pojo属性
其次在mapper.xml文件中通过resultMap使用<association property = “实体类属性名”></association>进行关联。
// pojo类定义
public class User implements Serializable {
/*
* 两个保持一致:数据类型和名称
* */
private int id; // 主键 id
private String username; // 用户名
private Integer age; // 年龄
private String address; // 地址
private String gender; // 性别
// 查询用户时,展示部门的相关信息
private Department department;// pojo
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
<!--查询所有用户信息 里面关联部门信息-->
<resultMap id="userMap" type="user">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="birthday" column="birthday"></result>
<result property="address" column="address"></result>
<result property="gender" column="gender"></result>
<!--映射复杂的pojo类型 一一映射 -->
<association property="department">
<id property="id" column="d_id"></id>
<result property="departmentName" column="d_name"></result>
<result property="departmentDesc" column="d_desc"></result>
<result property="departmentUpdate" column="d_update"></result>
</association>
</resultMap>
<!-- List<User> getTotalUser();-->
<select id="getTotalUser" resultMap="userMap">
select * from user as u,department as d where u.u_did = d.d_id
</select>
要求:该User类和Department类是一一对应的关系 使用标签 association
扩展:
可以使用map集合进行映射
当参数传递较多时,该参数涉及到多个pojo,除了我们可以使用pojo或者pojo包装类的形式外,我们还可以使用map集合形式来实现多参传递。
<!--List<User> queryUserByDepartNameAndUsername(Map map);-->
<select id="queryUserByDepartNameAndUsername" parameterType="map" resultMap="userMap">
select * from user u,department d where u.u_did = d.d_id and d.d_id = #{d_id} and u.username = #{username}
</select>
public interface UserDao{
List<User> queryUserByDepartNameAndUsername(Map map);
}
@Test
public void testQueryUserByDepartNameAndUsername() {
Map map = new HashMap<>();
map.put("d_id",4);
map.put("username", "小赵");
List<User> users = userDao.queryUserByDepartNameAndUsername(map);
for (User user : users) {
System.out.println(user);
}
}
- 一对多映射: <collection property = “实体类属性名” ofType = "容器中的数据类型”>
查询部门表时,需要首先展示部门信息,还要展示该部门下的所有用户信息
步骤:
首先需要在实体类中定义该pojo类的属性 属性类型为容器类型 List
其次在mapper.xml文件中定义resultMap的一对多映射 用户—>角色
在select标签中引入resultMap引用。
// 首先需要在实体类中定义该pojo类的属性 属性类型为容器类型 List
public class Role implements Serializable {
private Integer roleId;
private String roleName;
private String roleDesc;
private Date roleUpdateTime;
// 角色--->用户 一对多关系
private List<User> users;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
<!--使用resultMap 让实体类的属性名称和表中的字段名称保持一致-->
<resultMap id="roleMap" type="role">
<id property="roleId" column="r_id"></id>
<result property="roleName" column="r_name"></result>
<result property="roleDesc" column="r_desc"></result>
<result property="roleUpdateTime" column="r_updateTime"></result>
<!--角色和用户时一对多关系-->
<!--
ofType:对于容器类型我们采用指定容器数据的类型,使用属性ofType
javaType: 代表是users本身的类型,List类型
-->
<collection property="users" ofType="user" >
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="birthday" column="birthday"></result>
<result property="address" column="address"></result>
<result property="gender" column="gender"></result>
</collection>
</resultMap>
<select id="getAllRoles2" resultMap="roleMap">
<!--采用起别名方式-->
select * from role
</select>
@Test
public void testGetAllRoles2() {
List<Role> roles = roleDao.getAllRoles2();
for (Role role : roles) {
System.out.println(role);
}
}