当前位置: 首页 > 工具软件 > Mapper > 使用案例 >

MyBatis中mapper.java和mapper.xml的关系

董奇思
2023-12-01
  1. SysUser.java
public class SysUser extends BaseEntity
{
    private static final long serialVersionUID = 1L;
    
    private Long userId;
    private Long deptId;
    private String userName;
    private String nickName;
    private String email;
    ......
}
  1. SysUserMapper.java
public interface SysUserMapper
{
    public List<SysUser> selectUserList(SysUser sysUser);
    public SysUser selectUserByUserName(String userName);
    public SysUser selectUserById(Long userId);
    public int insertUser(SysUser user);
    ......
}

接口定义有以下特点:

1.Mapper 接口方法名和 SysUserMapper.xml 中定义的每个 statement 的 id 同名。
2.Mapper 接口方法的输入参数类型和 SysUserMapper.xml 中定义的 statement 的parameterType 类型相同。
3.Mapper 接口的返回类型和 SysUserMapper.xml 中定义的 statement 的 resultType 类型相同。

  1. SysUserMapper.xml

1.xml文件的namespace要写成mapper接口的全限定类名。

<mapper namespace="com.ruoyi.project.system.mapper.SysUserMapper">

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

<insert id="insertUser" parameterType="SysUser" useGeneratedKeys="true" keyProperty="userId">
	insert into sys_user(
		<if test="userId != null and userId != 0">user_id,</if>
		<if test="deptId != null and deptId != 0">dept_id,</if>
		<if test="userName != null and userName != ''">user_name,</if>
		<if test="nickName != null and nickName != ''">nick_name,</if>
		<if test="email != null and email != ''">email,</if>
		create_time
	)values(
		<if test="userId != null and userId != ''">#{userId},</if>
		<if test="deptId != null and deptId != ''">#{deptId},</if>
		<if test="userName != null and userName != ''">#{userName},</if>
		<if test="nickName != null and nickName != ''">#{nickName},</if>
		<if test="email != null and email != ''">#{email},</if>
		sysdate()
	)
</insert>
 类似资料: