MyBatis中mapper.java和mapper.xml的关系说明
作者:Hommmmmmm
这篇文章主要介绍了MyBatis中mapper.java和mapper.xml的关系说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
mapper.java和mapper.xml的关系
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; ...... }
2.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 类型相同。
3.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>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。