mybatis的输入映射(parameterType)And输出映射(resultType)

输入映射(parameterType)
  • 简单类型

    • 基本类型 —> 整形为主
    • String字符串
  • 复杂类型

    • pojo

      pojo Plain Ordinary Java Object 简单的Java对象,实际上就是我们的 JavaBean

      当传入的参数较多时,使用参数的索引下标 0,1,2,3,4,5,…

      可以把参数封装到pojo中,以pojo作为参数进行传递

      User Role Department

    • pojo的包装类 格式:类名Vo

      1. 内部至少包含一个普通的类,可以进行多条件查询

      2. 一般用于多个对象组成查询条件

      比如: 查询用户时,用户里面包含角色信息,可能需要把角色信息也要作为查询条件

输出映射(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);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
MyBatis是一种Java持久化框架,它提供了一种将数据库查询结果映射Java对象的方式,以及将Java对象的属性映射到数据库操作的方式。在MyBatis中,输入输出映射主要包括两个方面:参数映射和结果映射。 1. 参数映射:在执行数据库操作时,我们需要将Java对象作为参数递给SQL语句。参数映射可以通过注解或XML配置来实现。常见的参数映射方式有: - 注解方式:使用`@Param`注解将参数直接映射到SQL语句中的占位符。例如: ```java @Select("SELECT * FROM users WHERE id = #{id}") User getUserById(@Param("id") int id); ``` - XML配置方式:在XML配置文件中定义参数映射。例如: ```xml <select id="getUserById" parameterType="int" resultType="com.example.User"> SELECT * FROM users WHERE id = #{id} </select> ``` 2. 结果映射:将数据库查询结果映射Java对象。结果映射可以通过注解或XML配置来实现。常见的结果映射方式有: - 注解方式:使用`@Result`注解将数据库字段与Java对象属性进行映射。例如: ```java @Results({ @Result(column = "id", property = "id"), @Result(column = "username", property = "username"), @Result(column = "email", property = "email") }) @Select("SELECT * FROM users") List<User> getAllUsers(); ``` - XML配置方式:在XML配置文件中定义结果映射。例如: ```xml <resultMap id="userMap" type="com.example.User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="email" property="email"/> </resultMap> <select id="getAllUsers" resultMap="userMap"> SELECT * FROM users </select> ``` 通过参数映射和结果映射MyBatis可以方便地将Java对象与数据库进行交互,并将查询结果转换为Java对象。这样,我们就可以更方便地进行数据库操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值