Mybatis中Dao与XML之间的参数传递方式介绍

   在使用mybatis框架时,大多时候自动生成的mapper.xml文件能满足我们所需的数据库操作,但一些情况下还是需要我们自己写sql;为了加深印象,总结了下参数传递的方式以及各个关键字的含义如下:

一、语句中接收参数的方式有两种:
1、 #{}预编译 (可防止sql注入)
2、${}非预编译(直接的sql拼接,不能防止sql注入)

二、参数类型有三种:
1)、 基本数据类型
2)、 HashMap(使用方式和pojo类似 )
3)、 Pojo自定义包装类型

2.1、基本数据类型使用方式

List<Bean> selectIdBySortTime(@Param(value="id")Long  id);
 
<sql id="Base_Column_List" >
 id, car_dept_name, car_maker_name, icon,car_maker_py,hot_type
</sql>
 
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
 select
 <include refid="Base_Column_List" />
 from common_car_make
 where id = #{id,jdbcType=BIGINT} (jdbcType可省略)
</select>

2.2、基本类型多参传递时候的方式,sql中不指定接收参数类型,直接对应即可:

User login(@Param(value="name")String name,@Param(value="password")String password );
 
<select id="login"  resultType="com.pojo.User">
    select * from us where name=#{name} and password=#{password}
</select>

2.3、复杂类型–map类型

Service层
    Map<String, Object> paramMap=new hashMap();
    paramMap.put(“id”, value);
    paramMap.put(“carDeptName”,value);
    paramMap.put(“carMakerName”,value);
    paramMap.put(“hotType”,value);
 
Dao层 (如果不使用@Param注解,则sql中也可以省略属性前缀cm.)
List<Bean> queryCarMakerList(@Param(value="cm")Map paramMap);
 
<select id="queryCarMakerList" resultMap="BaseResultMap" parameterType="java.util.Map">
  select
  <include refid="Base_Column_List" />
  from common_car_make cm
  where 1=1
  <if test="id != null">
   and id = #{cm.id,jdbcType=DECIMAL}
  </if>
  <if test="carDeptName != null">
   and car_dept_name = #{cm.carDeptName,jdbcType=VARCHAR}
  </if>
  <if test="carMakerName != null">
   and car_maker_name = #{cm.carMakerName,jdbcType=VARCHAR}
  </if>
  <if test="hotType != null" >
   and hot_type = #{cm.hotType,jdbcType=BIGINT}
  </if>
  ORDER BY id
</select>

2.4、复杂类型–类类型
与Map传参的使用方式基本相同,不同的地方在于不同自己再填充map数据,直接使用已定义的bean类即可。

Dao层的代码:

public UserSms getSmsByPhoneAndSmsCode(UserSms u);

Mapper的xml代码:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.jyt.shop.dao.UserSmsMapper">
	<!-- 发送给用户手机号的resultMap将项目的实体类名与数据库表user_sms映射一一对应 -->
	<resultMap id="UserSmsResultMap" type="com.jyt.shop.entity.UserSms">
  		<result property="usId" column="usId" />
        <result property="userId" column="userId" />
        <result property="smsContent" column="smsContent" />
        <result property="smsType" column="smsType" />
		<result property="smsPhone" column="smsPhone" />
		<result property="smsStatus" column="smsStatus" />
		<result property="usCreatedate" column="usCreatedate" />
	</resultMap>
	
	<select id="getSmsByPhoneAndSmsCode" parameterType="com.jyt.shop.entity.UserSms" resultMap="UserSmsResultMap">
		SELECT usId,userId,smsContent,smsType,smsPhone,smsStatus,usCreatedate
		FROM USER_SMS
		<where>
			smsStatus= 1 
			<if test="smsPhone !=null and smsPhone !=''">
				and smsPhone = #{smsPhone}
			</if>
			<if test="smsContent !=null and smsContent !=''">
				and smsContent= #{smsContent}
			</if>
		</where>
	</select>
	
</mapper>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值