输入映射
简单类型
1.基本数据类型
2.String字符串
复杂类型
1.pojo
—>pojo这里说的是 plain Ordinary java Object 意思就是简单的java对象,实际上就是把我们的javaBean当成参数传入
2.pojo包装类格式 类名Vo
—>包装类内部至少含有一个普通类,可以用于多条件查询
—>包装类一般都用于多个对象组成的查询条件
—>例如我们查询用户的时候,包含有角色信息,这时候就要把角色和用户都带进去,这时候就需要把角色信息作为查询条件,这时候就要用到pojo包装类来完成
public class UserVo {
//这就就是把类作为属性封装在一个类里面这个类就是包装类
private User user;
private Role role;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
@Override
public String toString() {
return "UserVo{" +
"user=" + user +
", role=" + role +
'}';
}
}
在dao接口中创建一个方法,传入包装类
// 根据用户名称和角色名称查询用户信息
List<User> queryUserByUsernameAndRoleName(UserVo userVo);
在接口对应的xml文件里面去写sql语句
<!--测试包装类型 根据用户名称和角色名称查询用户信息-->
<select id="queryUserByUsernameAndRoleName" parameterType="userVo" resultType="user">
select * from user,role r where r.r_name = user.r_name and r.r_name = #{role.roleName} and username like '%' #{user.username} '%'
<!--这里面用到了#{} 表示我们需要传入两个值,role传全部 因为user用了模糊查询所以只用传入一个字-->
</select>
去测试类里面测试包装类
/**
* 测试 包装类UserVo
*/
@Test
public void testQueryUserByUsernameAndRoleName() {
UserVo userVo = new UserVo();//这是创建包装类对象
User user = new User();//创建user对象
user.setUsername("小");//因为是模糊查询所以只有输入一个字,就能查出所有与小有关的数
userVo.setUser(user);//把查出来的user存入包装类
Role role = new Role();
role.setRoleName("教授");
userVo.setRole(role);//把查出来的role放入包装类
List<User> users = userDao.queryUserByUsernameAndRoleName(userVo);//使用方法,得到返回的user对象集合
for (User user1 : users) {
System.out.println(user1);
}
}
输出映射
resultType: 属性可以指定结果集的类型,它支持基本类型和javaBean类型
resultType和parameter 一样都需要提前注册类型别名,如果之前提前注册了类型的别名,那么直接可以使用了
简单类型
1.基本类型---->整形为主(int)
2.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>
2.复杂类型
pojo 单个对象 一条记录 resultType = “user”
pojo 列表 多个对象 多条记录 存在数组或者集合当中,,resultType=“user”
问题 当实体类中 属性和表字段名称不一致 应该怎么封装
解答:使用别名查询(不推荐使用) 因为查询次数多的时候每一个都要加别名很不方便
<select id = "findAllUsers" resultType = "user">
select id as userId , username as userName , brithday as userBirthDay , sex as userSex ,
address as userAddress from user
</select>
推荐使用resultMap(推荐使用)
如果查询胡时候sql语句较多,我们可以使用resultMap进行实体类相背和表中字段进行映射
首先定义一个resultMap实体类 把实体类和表中字段一一映射
在select标签中引入resultMap结果映射
此外resultMap还可以将拆卸你的结果映射为复杂的pojo,比如在查询的时候映射对象时候,对象看也包括pojo,和list实现一对一和一对多查询
1.id映射,,<id cloumn = "表字段名" property = "实体类属性名">
2.非主键映射: <result cloumn = "表字段名" property = "实体类属性名" >
<!--使用resultMap 让实体类的属性名称和表中的字段名称保持一致-->
<!--这个一般情况来说是放在最上面的,因为下面会用到-->
<resultMap id="userMap" type="user">
<id property="id" column="id"></id> <!--只有主键id才是使用这个,其他的都是需要result-->
<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类型 一一映射 --><!--这个是user类里面的类属性-->
<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>
一对一映射: <association property = "实体类属性名"></association>
当输出结果的时候,内部包含了另外一个pojo的时候,使用resultMap和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;
}
}
xml设置
<!--验证一一映射使用查询所有用信息的方式,关联部门信息-->
<!--先引入resultMap-->
<resultMap id="userMap" type="user">
<result property="username" column="username"></result>
<result property="age" column="age"></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="departmentUpadateDate" column="d_update"></result>
</association>
</resultMap>
<!--引用之前定义好的map-->
<select id="getTotalUser" resultMap="userMap"><!--主要是resultMap属性会传入之前设定好的Map-->
select * from user u , department d where u.u_did = d.d_id
</select>
最后写一个接口类再去测试类里面测试
//接口
//查询所有用户加部门信息
List<User> getTotalUser();
//测试类
@Test
public void testGetTotalUser(){
List<User> totalUser = userDao.getTotalUser();
for (User user:totalUser){
System.out.println(user);
}
}
可以使用map集合进行映射
当参数传递较多时,该参数涉及到多个pojo,除了我们可以使用pojo或者pojo包装类的形式外,我们还可以使用map集合形式来实现多参传递。
<!--id就是接口种的方法名 parameType 表示传入的参数数据类型 resultMap 表示引用上面定义好的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;
}
}
配置mapper的xml文件
<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);
}
}
本文介绍了MyBatis中输入输出映射的多种方法,包括基本类型映射、复杂类型映射、使用包装类和map集合传递多个参数,以及如何解决实体类属性与表字段名称不一致的问题。
2069

被折叠的 条评论
为什么被折叠?



