Mybatis中自定义mapper.xml时,参数传递的方式及写法总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huangyaa729/article/details/84564711

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

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

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

基本数据类型使用方式

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>

基本类型多参传递时候的方式,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>

复杂类型–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>

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

<update id="updateByPrimaryKeySelective" parameterType="com.epeit.api.model.CommonCarMake" > 
 update common_car_make 
 <set > 
  <if test="carDeptName != null" > 
  car_dept_name = #{carDeptName,jdbcType=VARCHAR}, 
  </if> 
  <if test="carMakerName != null" > 
  car_maker_name = #{carMakerName,jdbcType=VARCHAR}, 
  </if> 
  <if test="icon != null" > 
  icon = #{icon,jdbcType=VARCHAR}, 
  </if> 
  <if test="carMakerPy != null" > 
   car_maker_py = #{carMakerPy,jdbcType=VARCHAR}, 
  </if> 
  <if test="hotType != null" > 
   hot_type = #{hotType,jdbcType=BIGINT}, 
  </if> 
 </set> 
 where id = #{id,jdbcType=BIGINT} 
 </update>

返回类型与接收类型关键字的区别:

resultMap和 resultType的区别

两者都是表示查询结果集与java对象之间的一种关系,处理查询结果集,映射到java对象。

resultMap表示将查询结果集中的列一一映射到bean对象的各个属性,映射的查询结果集中的列标签可以根据需要灵活变化。

resultType 表示的是bean中的对象类,此时可以省略掉resultMap标签的映射,但是必须保证查询结果集中的属性 和 bean对象类中的属性是一一对应的,此时大小写不敏感,但是有限制。

parameterMap(不推荐) & parameterType
parameterMap和resultMap类似,表示将查询结果集中列值的类型一一映射到java对象属性的类型上,在开发过程中不推荐这种方式。
一般使用parameterType直接将查询结果列值类型自动对应到java对象属性类型上,不再配置映射关系一一对应,例如上述代码中下划线部分表示将查询结果类型自动对应到Bean对象的属性类型

猜你喜欢

转载自blog.csdn.net/huangyaa729/article/details/84564711