那些MyBatis不得不注意的小细节

1.当参数返回String类型可以写成parameterType=“String”
2.当查询出的结果可能有好多条记录,此时的resultType 返回值类型是集合内存储数据的类型,不是 ‘list’
<!-- 查询所有菜单 -->
	<select id="selectAllMenu" resultType="menu">
		select * form menu
	</select>
3.当添加记录时,有些属性不是必填属性,这时就需要到
<!-- 根据用户名查询用户 -->
	<insert id="addMenu" parameterType="org.tedu.com.entity.Menu">
		insert menu
		<trim prefix="(" suffix=")" suffixOverrides=",">
			name,icon,
			<if test='parentId != null and parentId != "" '>
				parentId,
			</if>
			<if test='url != null and url != "" '>
				url,
			</if>
		</trim>

		<trim prefix="values (" suffix=")" suffixOverrides=",">
			#{name, jdbcType=VARCHAR},
			#{icon, jdbcType=VARCHAR},
			<if test='parentId != null and parentId != "" '>
				#{parentId},
			</if>
			<if test='url != null and url != "" '>
				#{url, jdbcType=VARCHAR},
			</if>
		</trim>
	</insert>
4.分页查询要用 ‘%${name}%’ 或"%"#{name}"%" 、’%menu%’
<!-- 查询所有菜单 -->
	<select id="selectAllMenu" parameterType="Map" resultType="org.tedu.com.entity.Menu">
		select * from menu '%${name}%'
		<if test='name != null and name != "" '>
			where name like 
		</if>
		<if test="offset != null and pageSize != null">
			limit #{offset} , #{pageSize}
		</if>
	</select>
5.添加记录后立刻获取记录的ID

当创建一条记录时需要在同个方法内立刻获取该记录自动生成的ID,此时要利用LAST_INSERT_ID()方法来获取

		<selectKey keyProperty="id" resultType="long" order="AFTER">
			SELECT LAST_INSERT_ID() 
	 	</selectKey>
<insert id="insert"
		parameterType="cn.web.workflow.pojo.Baoxiaobill">
		<selectKey keyProperty="id" resultType="long" order="AFTER">
			SELECT LAST_INSERT_ID() 
	 	</selectKey>
		insert into bill (id, money, title,
		)
		values (#{id,jdbcType=BIGINT}, #{money,jdbcType=DECIMAL},
		#{title,jdbcType=VARCHAR})
	</insert>

可参考

6.resultType和resultMap的区别

场景:当用select查出的一条记录时,如果用resultType="ClassA"返回,则会返回一个封装成ClassA类的对象,假如有id,name,age等字段,数据库会根据字段给对象的属性赋值,而当另一种情况出现时,当你需要将它封装成ClassB的对象时,可以利用resultMap自行定义将字段的值赋值给ClassB对象对应的字段,例如将字段id赋值给user_id

<resultMap type="ClassB的包名" id="ClassBResultMap"><!--id与下面resultMap名对应-->	
  	<id column="id" property="id"/><!--此标签必须有因为每个映射必须要有一个唯一标识,column对应数据库的字段名,property对应ClassB属性名-->
  	<result column="name" property="name"/><!--普通的映射-->
  	<collection property="children" select="getSubMenu" column="id">
  		    <id column="id" property="id"/>
		    <result column="name" property="name"/>
			   .
			   .
			   . 
  	</collection><!--复杂类型映射list等-->
  </resultMap>

  <select id="getClassB" resultMap="ClassBResultMap">
  	SELECT ? FROM 表名 WHERE 条件
  </select>
  <select id="getClassA" resultType="ClassA的包名">
  	SELECT ? FROM 表名 WHERE 条件
  </select>
7.当传入多个参数时的处理
  • 方法1:使用索引
public List<XXXBean> getXXXBeanList(String xxId, String xxCode);  

<select id="getXXXBeanList" resultType="XXBean">

  select t.* from tableName where id = #{0} and name = #{1}  

</select> 

由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始

  • 方法二:@Param注解传参法(推荐使用)
public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code);  

<select id="getXXXBeanList" resultType="XXXBean">

  select t.* from tableName where id = #{id} and name = #{code}  

</select>  

由于是多参数那么就不能使用parameterType, 这里用@Param来指定哪一个

  • 方法三:Map封装多参数
public List<XXXBean> getXXXBeanList(Map map);  

<select id="getXXXBeanList" parameterType="java.util.Map" resultType="XXBean">

  select 字段... from XXX where id=#{xxId} code = #{xxCode}  

</select>

#{}里面的名称对应的是 Map里面的key名称。这种方法适合传递多个参数,且参数易变能灵活传递的情况。

  • 方法四:Java Bean传参法
public	User	selectUser(Map<String, Object> params);

<select	id="selectUser"	 parameterType="com.test.User"	 resultMap="UserResultMap">
	
	select	*	from	user	where	user_name =	#{userName} and dept_id = #{deptId}

</select>

#{}里面的名称对应的是 User类里面的成员属性。
参考一
参考二

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值