Python 官方文档:入门教程 => 点击学习
目录Mapper与Mapper.xml文件之间匹配问题user实体类数据库user表@Param注解测试Mapper和Mapper.xml的关系CateGory.javaCatego
这里我们做一个实例
public class User {
private Integer id;
private String username;
private String passWord;
private String email;
private String phone;
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注解,前提是数据库字段与实体类字段一一对应,例如 数据库命名为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之间出现不匹配的原因主要有:
还有一些较为简单的异常不一一例举,但说明相应原因以供参考:
jdbc.url=jdbc:Mysql://localhost:3306/blog?characterEncoding=utf-8
这是mapper接口,面向接口编程的思想还是很重要的。也是本次博文最重要的部分。
接口定义有以下特点:
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文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0