MyBatis SQL映射

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值