Mybatis映射及ResultMap示例


1.映射文件XxxMapper.xml

1.1语法

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="namespace="cn.kgc.mapper.UserMapper"">
	<select id="findCount" …
		……
	</select>
</mapper>

1.2namespace:命名空间

namespace的命名必须跟某个接口同名。

1.3id:命名空间中唯一的标识符

接口中的方法名与映射文件中的SQL语句id一一对应

1.4parameterType:参数类型

传入SQL语句的参数类型(接口的形参)

1.5resultType:返回值

SQL语句返回值类型的完整类名或别名,也就是接口中的方法返回值。

1.6示例

需求:根据用户名对用户表进行模糊查询。

(1)创建数据库

(2)创建工程、目录

在这里插入图片描述

(3)pom.xml配置依赖

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.5.1</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.36</version>
</dependency>

(4)创建实体类

package cn.kgc.entity;

public class User {
   
    private Integer id;
    private String userCode;
    private String userName;
    private String userPassword;
    private Integer gender;
    private String birthday;
    private String phone;
    private String address;
    private Integer userRole;
    private Integer createdBy;
    private String creationDate;
    private Integer modifyBy;
    private String modifyDate;

    private String userRoleName;


    public User() {
   
    }

    public User(String userCode, Integer userRole) {
   
        this.userCode = userCode;
        this.userRole = userRole;
    }

    public User(String userCode, String userName, String userPassword, Integer gender, String birthday, String phone, String address, Integer userRole, Integer createBy, String creationDate, Integer modifyBy, String modifyDate) {
   
        this.userCode = userCode;
        this.userName = userName;
        this.userPassword = userPassword;
        this.gender = gender;
        this.birthday = birthday;
        this.phone = phone;
        this.address = address;
        this.userRole = userRole;
        this.createdBy = createBy;
        this.creationDate = creationDate;
        this.modifyBy = modifyBy;
        this.modifyDate = modifyDate;
    }

    public User(Integer id, String userCode, String userName, String userPassword, Integer gender, String birthday, String phone, String address, Integer userRole, Integer createBy, String creationDate, Integer modifyBy, String modifyDate) {
   
        this.id = id;
        this.userCode = userCode;
        this.userName = userName;
        this.userPassword = userPassword;
        this.gender = gender;
        this.birthday = birthday;
        this.phone = phone;
        this.address = address;
        this.userRole = userRole;
        this.createdBy = createBy;
        this.creationDate = creationDate;
        this.modifyBy = modifyBy;
        this.modifyDate = modifyDate;
    }

    public Integer getCreatedBy() {
   
        return createdBy;
    }

    public void setCreatedBy(Integer createdBy) {
   
        this.createdBy = createdBy;
    }

    public String getUserRoleName() {
   
        return userRoleName;
    }

    public void setUserRoleName(String userRoleName) {
   
        this.userRoleName = userRoleName;
    }

    public Integer getId() {
   
        return id;
    }

    public void setId(Integer id) {
   
        this.id = id;
    }

    public String getUserCode() {
   
        return userCode;
    }

    public void setUserCode(String userCode) {
   
        this.userCode = userCode;
    }

    public String 
  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值