上手操作
做好相关配置后,我们就开始上手对user表进行操作。首先先写实体类,创建entity包,新建User实体类,根据数据库字段写好对应的属性。使用idea可以自动生成get和set方法和toString方法,快捷键alt+ins,或者菜单栏code下generate。然后在dao下创建UserMapper接口,我们只需要在接口定义我们需要用到的抽象方法。比如根据id查找user,那么写返回类型User 方法名selectUser (参数int id)。
package com.neu.langsam.community.dao;
import com.neu.langsam.community.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
User selectById(int id);
User selectByName(String username);
User selectByEmail(String email);
int insertUser(User user);
int updateStatus(int id, int status);
int updateHeader(int id,String headerUrl);
int updatePassword(int id,String password);
}
然后在resource下新建mapper目录写对应的配置。在mapper目录下我们新建一个user-mapper.xml。mapper的格式在官网上有,这里我直接贴出来。
-
首先头部是标准格式,不用修改
-
外层是标签,标签内定义了namespace,也就是对应的mapper接口
-
内存有select、insert、update、sql等标签,每一种举一个例子
-
第一个sql标签,我们设置了一个id,在标签内写了user表里的一些字段,这个的作用是为了复用sql语句,我后面多个查询都可以直接用这个sql标签定义的sql语句。
-
select标签,同样设置了一个id来唯一标识,同时定义了resultType也就是结果类型,因为User是我们自己创建的,这样就会把查询的结果封装到user对象中,本来应该写完整的包名,但是我们在application.properties里配置好了实体类的包。标签里是sql语句,是引入了前面写好的sql,也可以直接替换为sql标签里的字段。需要传入的参数使用#{参数}形式。
-
select <include refid="selectFields"></include> from user 引用id="selectFields"
-
where id = #{id} 使用id来查询
-
<?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="com.neu.langsam.community.dao.UserMapper">
//定义id包含字段
<sql id="insertFields">
username, password, salt, email, type, status, activation_code, header_url, create_time
</sql>
<sql id="selectFields">
id, username, password, salt, email, type, status, activation_code, header_url, create_time
</sql>
<select id="selectById" resultType="User">
select <include refid="selectFields"></include>
from user
where id = #{id}
</select>
//不使用引入sql语句
<select id="selectById" resultType="User">
select username, password, salt, email, type, status, activation_code, header_url, create_time
from user
where id = #{id}
</select>
<select id="selectByName" resultType="User">
select <include refid="selectFields"></include>
from user
where username = #{username}
</select>
<select id="selectByEmail" resultType="User">
select <include refid="selectFields"></include>
from user
where email = #{email}
</select>
<insert id="insertUser" parameterType="User" keyProperty="id">
insert into user (<include refid="insertFields"></include>)
values(#{username}, #{password}, #{salt}, #{email}, #{type}, #{status}, #{activationCode}, #{headerUrl}, #{createTime})
</insert>
<update id="updateStatus">
update user set status = #{status} where id = #{id}
</update>
<update id="updateHeader">
update user set header_url = #{headerUrl} where id = #{id}
</update>
<update id="updatePassword">
update user set password = #{password} where id = #{id}
</update>
</mapper>
那么我们对user表的内容就写完了,接下来我们在测试类里试一下。如图写好测试类和测试方法,可以看到我调用userMapper里写好的selectById方法查询id为101的user,控制台里打印出了id=101的user的相关信息,说明我们的操作成功了。
面试题:mybatis怎么使用的?
1. 配置application.properties的datasource数据
2.配置UserMapper接口到dao层
3.配置mapper.xml到resources层,定义每个方法查询/更新/删除操作
4.MapperTests