当前位置: 首页> 科技> 能源 > 域名网查询_安卓开发环境搭建_站内推广和站外推广的区别_网店营销

域名网查询_安卓开发环境搭建_站内推广和站外推广的区别_网店营销

时间:2025/7/11 8:28:23来源:https://blog.csdn.net/m0_66584716/article/details/143974338 浏览次数:0次
域名网查询_安卓开发环境搭建_站内推广和站外推广的区别_网店营销

文章目录

    • 一、映射文件参数
    • 二、查询映射
      • 2-1 一对一
      • 2-2 一对多
      • 2-3 总结

一、映射文件参数

@Param 注解官方文档解释

在这里插入图片描述

1、单个参数(对象)不使用注解

public int save(User user);
<!-- 添加用户 -->
<insert id="save" parameterType="User">INSERT INTO smbms_user (userCode, userName, gender, birthday, address)VALUES (#{userCode}, #{userName}, #{gender}, #{birthday}, #{address})
</insert>

User 类型的参数对象传递到了语句中,会查找 userCode、userName 和 gender 等属性,然后将它们的值传入预处理语句的参数中。

2、多个参数(原始类型或简单数据类型)使用注解

public List<Bill> findBillList(@Param("productName") String productName,@Param("productDesc") String productDesc);
<!-- 模糊查询功能 -->
<select id="findBillList" parameterType="String" resultType="Bill">select id,billCode,productName,productDesc from smbms_billwhere productName like concat("%",#{productName},"%")and productDesc like concat("%",#{productDesc},"%")
</select>

3、多个参数不使用注解(Map)

List<User> getUserByMap(Map<String, Object> paramMap);
<!-- 查询语句 -->
<select id="getUserByMap" parameterType="map"  resultType="User">SELECT id,userCode,userName,birthday FROM smbms_userWHERE birthday BETWEEN #{startDate} AND #{endDate}
</select>
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("startDate", startDate); 
paramMap.put("endDate", endDate); 

二、查询映射

resultTyperesultMap 两种方式指定查询结果的返回列。

  • resultType:自动映射。查询结果直接映射到一个简单的 Java 对象(POJO)
  • resultMap:手动映射。更复杂的映射逻辑,比如处理一对一、一对多的关系映射,或对结果集进行特殊处理

在 mybatis 主配置文件中关闭自动映射

<!-- 关闭自动映射 -->
<settings><setting name="autoMappingBehavior" value="NONE"/>
</settings>

autoMappingBehavior 控制自动映射的行为。

  • NONE:关闭自动映射
  • PARTIAL:只会自动映射没有定义嵌套结果集映射的结果集(默认值)(推荐)
  • FULL:会自动映射任意复杂的结果如嵌套(可读性弱,不推荐)

简单的 resultMap 定义示例:

<resultMap id="userResultMap" type="User"><id property="id" column="user_id" /><result property="username" column="user_name" /><result property="password" column="user_password" />
</resultMap>
  • id: 代表主键映射。
  • result: 代表普通属性映射。
  • type: 指定要映射的目标类(即 Java 类)。
  • property: 指定 Java 类中的属性。
  • column: 指定数据库表中的列名。

propertycolumn 名称相同时,MyBatis 会自动进行映射。这种情况下,可使用 resultType 而不必显式定义 resultMap

2-1 一对一

实体类

//角色实体类
public class Role {private long role_id;private String role_name;private String role_remarks;
}
//管理员实体类
public class Admin {private long admin_id;private String admin_name;private Role admin_role;//角色对象
}
<resultMap id="mapAdmin" type="Admin"><id property="admin_id" column="admin_id"></id><result property="admin_name" column="admin_name"></result><association property="admin_role" javaType="Role"><id property="role_id" column="role_id"></id><result property="role_name" column="role_name"></result><result property="role_remarks" column="role_remarks"></result></association>
</resultMap>
<select id="AdminMapper" parameterType="Long" resultMap="mapAdmin">SELECT * FROM sf_admin a LEFT JOIN sf_role b ON a.admin_role_id = b.role_id WHERE admin_id = #{admin_id}
</select>

或者

<resultMap id="mapAdmin" type="Admin"><id property="admin_id" column="admin_id"></id><result property="admin_name" column="admin_name"></result><association property="admin_role" resultMap="rolemap" javaType="Role"/>
</resultMap>
<resultMap id="rolemap" type="Role"><id property="role_id" column="role_id"></id><result property="role_name" column="role_name"></result><result property="role_remarks" column="role_remarks"></result>
</resultMap>

2-2 一对多

实体类

//收货地址实体类
public class Address {private long address_id;private String address_name;
}
//用户实体类
public class User {private long user_id;private String user_name;private List<Address> addr_list;
}
<resultMap id="user_addr_map" type="User"><id property="user_id" column="user_id"></id><result property="user_name" column="user_name"></result><collection property="addr_list" ofType="Address"><id property="address_id" column="address_id"></id><result property="address_name" column="address_name"></result></collection>
</resultMap>
<select id="getUserId" parameterType="Long" resultMap="user_addr_map">SELECT * FROM sf_user a RIGHT JOIN sf_address b ON a.user_id = b.address_user_id WHERE user_id = #{user_id}
</select>

2-3 总结

association 用于处理一对一的关系映射,可以嵌套另一个resultMap来进一步细化映射规则。javaType 指定了集合中元素的类型。

collection 用于处理一对多的关系映射,同样支持嵌套resultMap以定义子元素的映射规则。ofType 指定了集合中元素的类型。

关键字:域名网查询_安卓开发环境搭建_站内推广和站外推广的区别_网店营销

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: