广告
返回顶部
首页 > 资讯 > 后端开发 > Python >解读Mapper与Mapper.xml文件之间匹配的问题
  • 535
分享到

解读Mapper与Mapper.xml文件之间匹配的问题

Mapper与Mapper.xmlMapper.xml文件匹配Mapper文件匹配 2023-01-09 18:01:56 535人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录Mapper与Mapper.xml文件之间匹配问题user实体类数据库user表@Param注解测试Mapper和Mapper.xml的关系CateGory.javaCatego

Mapper与Mapper.xml文件之间匹配问题

这里我们做一个实例

user实体类

public class User {
    private Integer id;
    private String username;
    private String passWord;
    private String email;
    private String phone;

数据库user表

在这里插入图片描述

userMapper.xml

<?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="wdc.dao.UserMapper">
    <resultMap id="BaseResultMap" type="wdc.pojo.User">
        <!-- column为数据库字段名  property 为实体类字段名-->
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="user_name" property="username" jdbcType="VARCHAR"/>
        <result column="user_password" property="password" jdbcType="VARCHAR"/>
        <result column="user_email" property="email" jdbcType="VARCHAR"/>
        <result column="user_phone" property="phone" jdbcType="VARCHAR"/>

    </resultMap>
    <!-- 这里 id 是数据库字段名 #{id}是实体类字段名,sql语句后面不加封号-->
    <select id="selectUser" parameterType="java.lang.Integer" resultMap="BaseResultMap">
         select *from USER where id=#{id}
    </select>
    <!--此sql用于登录查询
    #{username} 是对应mapper接口文件中 @Param注解中的参数名(防止mapper之间不匹配)-->
    <select id="login" parameterType="java.lang.String" resultMap="BaseResultMap">
        select *from user where user_name=#{username} and user_password=#{password}
    </select>
</mapper>

从上面不难看出,我们使用了resultMap作为映射集,目的是为了使得 实体类user 中的字段与数据库中的字段进行匹配。

而在查询中我们选择 使用结果集resultMap来进行数据映射

  <!-- 这里 id 是数据库字段名 #{id}是实体类字段名,sql语句后面不加封号-->
  <select id="selectUser" parameterType="java.lang.Integer" resultMap="BaseResultMap">

而在另一个查询中,我们在编写sql语句时,使用了对应userMapper文件中,@Param所对应的参数名

 <!-- #{username} 是对应mapper接口文件中 @Param注解中的参数名(防止mapper之间不匹配)-->
   <select id="login" parameterType="java.lang.String" resultMap="BaseResultMap">
       select *from user where user_name=#{username} and user_password=#{password}
   </select>

这里也是为了防止mapper 文件与 xml文件之间匹配出现异常。

userMapper

package wdc.dao;

import org.apache.ibatis.annotations.Param;
import wdc.pojo.User;

public interface UserMapper {
    User selectUser(int id)throws Exception;
    //    登录
    //    @Param中的参数名,对应.xml文件中sql语句#{}中的参数名(防止mapper之间不匹配)
    User login(@Param("username") String username, @Param("password") String password)throws Exception;
}

这里我们重点解释一下

@Param注解

这里我们应该,采用#{}的方式把@Param注解括号内的参数进行引用

对应的是使用方法,上述代码中明确标出。

这里我们也可以不使用@Param注解,前提是数据库字段与实体类字段一一对应,例如 数据库命名为user_name,而我们的实体类就应该使用 驼峰命名 userName,这样就可以使mybatis将查询的结果自发的进行一一对应填充置user 对象中。

但是由于此案例,数据库字段和实体类字段之间的命名方式,使得mybatis 从数据库中查出的字段,不能正常的识别和对应正确的实体类字段,于是,我们需要使用@Param注解来指明,我们所要引用的参数的名称,方便再mapper.xml文件中,采用#{}的方式把@Param注解括号内的参数进行引用,

这样会更方便代码的阅读,也便于避免一些未知错误的发生。

测试

关于对象判空,和空指针异常(NullPointer)

@Test
    public void testLogin() throws Exception{
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:config/spring-mybatis.xml");
        UserMapper userMapper = (UserMapper) ac.getBean("userMapper");
        User user = null;

            user = userMapper.login("张","123");
            if(user == null)
                System.out.println("没数据");
            else
                System.out.println(user);


    }

这里测试登录接口后我们发现,当没有数据返回时,我们可以直接 使用  user==null 的方式来对 对象进行判空。

而当我们在此代码中,直接将异常使用 try catch 捕捉时,我们发现,当对象 user 为空时,控制台并不会报出异常。

而当我们在调用 user对象的toSring方法时,则会出现NullPoint的空指针异常

小结:

导致mapper 与xml之间出现不匹配的原因主要有:

  • 1. mapper 文件中 方法名与 xml 中查询语句id 进行匹配
  • 2. mapper 文件中 参数类型,与xml 文件中的参数类型匹配
  • 3. mapper 文件中 返回值类型,与xml文件中的 返回值类型匹配
  • 4. XML 文件的 namespace 指定的路径与 相应的实体类对应
  • 5. mapper 文件 和xml文件不在同一个包下的,要在配置文件中,将两个文件的包同时进行扫描

还有一些较为简单的异常不一一例举,但说明相应原因以供参考:

  • 6. 数据库密码填写错误
  • 7. 数据库服务未开启
  • 8. 数据库路径 以及乱码问题

jdbc.url=jdbc:Mysql://localhost:3306/blog?characterEncoding=utf-8

Mapper和Mapper.xml的关系

Category.java

CategoryMapper.java 

这是mapper接口,面向接口编程的思想还是很重要的。也是本次博文最重要的部分。

接口定义有以下特点:

  • Mapper 接口方法名和 CategoryMapper.xml 中定义的每个 sql 的 id 同名。
  • Mapper 接口方法的输入参数类型和 CategoryMapper.xml 中定义的 sql 的parameterType 类型相同。
  • Mapper 接口的返回类型和 CategoryMapper.xml 中定义的 sql 的 resultType 类型相同

CategoryMapper.xml 

1.xml文件的namespace要写成mapper接口的路径。

2.sql的id和mapper中的方法名要对应起来,比如下面,mapper中方法名为add,insert的sql标签id也要为add

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 解读Mapper与Mapper.xml文件之间匹配的问题

本文链接: https://www.lsjlt.com/news/177229.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作