Preface
There is a parameterType attribute in the select, insert, update, and delete elements in the Mybatis Mapper file, which is used for the corresponding parameter types accepted by the mapper interface method. This article mainly introduces the relevant content about the parameter parameter Type in MyBatis. It is shared for your reference and learning. I won’t say much below. Let’s take a look at the detailed introduction together.
1. The parameterType type of MyBatis is divided into two types
1. 1. Basic data types: int, string, long, Date;
1. 2. Complex data types: classes and maps
2. How to get the value in the parameter:
2.1 Basic data type: #{parameter} Get the value in the parameter
2.2 Complex data type: #{attribute name}, #{key} in map
3. Case:
3.1 Basic data type cases
<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} </select>3.2 Complex type-map type
<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 cm.id = #{id,jdbcType=DECIMAL} </if> <if test="carDeptName != null"> and cm.car_dept_name = #{carDeptName,jdbcType=VARCHAR} </if> <if test="carMakerName != null"> and cm.car_maker_name = #{carMakerName,jdbcType=VARCHAR} </if> <if test="hotType != null" > and cm.hot_type = #{hotType,jdbcType=BIGINT} </if> ORDER BY cm.id </select>3.3 Complex types-class types
<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>3.4 Complex type--the case where arrays are included in map
<select id="selectProOrderByOrderId" resultType="com.epeit.api.model.ProOrder" parameterType="java.util.HashMap" > select sum(pro_order_num)proOrderNum,product_id productId,promotion_id promotionId from pro_order where 1=1 <if test="orderIds != null"> and <foreach collection="orderIds" item="item" open="order_id IN(" separator="," close=")"> #{item,jdbcType=BIGINT} </foreach> </if> GROUP BY product_id,promotion_id </select>4. Annotation @Param: This is quite special, but it is easy to understand
Case 1:
@Param(value="startdate") String startDate : annotate a single property; this is similar to renaming the parameter once
For example, configure sql statements (DAO layer) in *mapper.xml calling mybatis
List<String> selectIdBySortTime(@Param(value="startdate")String startDate);
Then the statement in xml needs to be matched with the content in @param brackets: the parameter is startdate
<select id="selectIdBySortTime" resultType="java.lang.String" parameterType="java.lang.String"> select distinct ajlcid from ebd_fh_ajlc where sorttime >= to_date(#{startdate,jdbcType=VARCHAR},'YYYY-MM-DD') and created_date=updated_date and keyvalue in (select distinct companyname from ebd_fh_company_list where isupdate='0') </select> Case 2:
Annotation javaBean,@Param(value="dateVo") DateVo dateVo; you need to pay attention to the written parameters
List<String> selectIds(@Param(value="dateVo")DateVo dateVo);
Corresponding mapping file
<select id="selectIds" resultType="java.lang.String" parameterType="com.api.entity.DateVo"> select distinct ajlcid from ebd_fh_ajlc where sorttime >= to_date(# {dateVo.startDate,jdbcType=VARCHAR},'YYYY-MM-DD') and created_date=updated_date and keyvalue in (select distinct companyname from ebd_fh_company_list where isupdate='0') </select>As for the advantages and disadvantages, it depends on your personal preferences
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.