1.顶级元素
mapper:映射文件的根元素
属性 | 作用 |
---|---|
namespace | 区分不同mapper,全局唯一 绑定dao接口,面向接口 |
元素 | 作用 |
---|---|
cache | 配置给定命名空间的缓存 |
cache-ref | 从其他名名会计引用缓存配置 |
resultMap | 用来描述数据库结果集和对象对应关系 |
sql | 可重用的SQL块 |
insert | 映射插入语句 |
update | 映射更新 |
delete | 映射删除 |
select | 映射查询 |
select元素(查询)属性
- id:标识符,可以用来引用这条语句
- parameterType:查询语句传入的参数类型的完全限定名或别名,支持基础和复杂数据类型
- resultType:查询语句返回的结果类型的完全限定名或别名
查询(直接使用类作为返回类型)
<!--无参 -->
<select id="selectUserCount" resultType="int">
select count(id) from smbms_user
</select>
<!--有参 这个是map传参 通过 K 拿 V (查询不要用*,用列名)-->
<select id="getUserListByMap" resultType="cn.smbms.entity.User" parameterType="Map">
select * from smbms_user where userName like CONCAT ('%',#{uName},'%') and userRole =# {uRole}
</select>
<!--有参 这个是map传参 通过 K 拿 V (查询不要用*,用列名)通过resultMap=“userList” 进行引用
映射到id为userList的resultMap上-->
<select id="getUserListByMap" resultMap="userList" parameterType="Map">
select * from smbms_user where userName like CONCAT ('%',#{uName},'%') and userRole =#{uRole}
</select>
使用resultMap完成查询结果的展现
操作灵活,可以用于多表查询
- id 属性:唯一标识,类似于方法名
- type 属性:表示该resultMap的映射结果类型
- result 子节点:标识简单属性,property为属性名,column为表的列名
<resultMap type="User" id="userList">
<result property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
<result property="phone" column="phone"/>
<result property="birthday" column="birthday"/>
<result property="gender" column="gender"/>
<result property="userRole" column="userRole"/>
<result property="roleName" column="roleName"/>
</resultMap>
设置 resultMap 的自动映射级别 QAQ 后面写
insert,update,delete
- id 属性:唯一标识,类似于方法名
- parameterType 属性:查询语句传入的参数类型的完全限定名或别名,支持基础和复杂数据类型
- 没有 resultType 属性 默认返回受影响行数 在dao接口设置返回值类型为int就行
<insert id="add" parameterType="User">
INSERT INTO smbms.smbms_user (userCode, userName, userPassword, gender, birthday,
phone, address, userRole, createdBy, creationDate) values(#{userCode},#{userName},#{userPassword},#{gender},#{birthday},
#{ph、one},#{address},#{userRole},#{createdBy},#{creationDate})
</insert>
其他几个格式差不多
入参,传参
1.@Param注解 传参
xml 那边通过 @Param括号 的 字符串 获取.
parameterType属性可以不设置
适用于4个以下参数传参
public List<User> getUserList(@Param("userName")String userName,@Param("userRole")Integer roleId);
2.数组传参
xml 通过 foreach标签 遍历
3.map传参
传递类型复杂的参数 ,xml通过 K 去得到 V