这里我们做一个实例
1 2 3 4 5 6 |
public class User { private Integer id; private String username; private String password; private String email; private String phone; |
userMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?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来进行数据映射
1 2 |
<!-- 这里 id 是数据库字段名 #{id}是实体类字段名,sql语句后面不加封号--> <select id="selectUser" parameterType="java.lang.Integer" resultMap="BaseResultMap"> |
而在另一个查询中,我们在编写sql语句时,使用了对应userMapper文件中,@Param所对应的参数名
1 2 3 4 |
<!-- #{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
1 2 3 4 5 6 7 8 9 10 11 |
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@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