# Mybatis_day03:深入理解输入与输出映射 ## 一、MyBatis映射基础概念 ### 1.1 什么是映射 在MyBatis框架中,映射(Mapping)是指将Java对象与数据库表记录之间建立对应关系的过程。这种映射关系使得开发者能够以面向对象的方式操作数据库,而无需编写繁琐的JDBC代码。 ### 1.2 输入输出映射的重要性 - **输入映射**:将Java对象转换为SQL语句参数 - **输出映射**:将SQL查询结果转换为Java对象 - 二者共同构成了MyBatis数据操作的核心环节 ## 二、输入映射详解 ### 2.1 基本类型参数映射 #### 2.1.1 单个基本类型参数 ```xml <!-- Mapper接口方法 --> User selectById(Integer id); <!-- XML映射 --> <select id="selectById" resultType="User"> SELECT * FROM user WHERE id = #{id} </select>
// 使用@Param注解指定参数名 List<User> selectByRange(@Param("minId") Integer minId, @Param("maxId") Integer maxId);
<select id="selectByRange" resultType="User"> SELECT * FROM user WHERE id BETWEEN #{minId} AND #{maxId} </select>
public class UserQuery { private String username; private Integer status; // getter/setter省略 }
<select id="selectByQuery" parameterType="UserQuery" resultType="User"> SELECT * FROM user WHERE username LIKE #{username} AND status = #{status} </select>
public class OrderQuery { private User user; private Date createTime; // getter/setter省略 }
<select id="selectOrders" parameterType="OrderQuery" resultType="Order"> SELECT * FROM orders WHERE user_id = #{user.id} AND create_time > #{createTime} </select>
List<User> selectByMap(Map<String, Object> params);
<select id="selectByMap" resultType="User"> SELECT * FROM user WHERE username = #{name} AND age > #{minAge} </select>
List<User> selectByIdList(List<Integer> ids);
<select id="selectByIdList" resultType="User"> SELECT * FROM user WHERE id IN <foreach collection="list" item="id" open="(" separator="," close=")"> #{id} </foreach> </select>
List<User> selectByIdArray(Integer[] ids);
<select id="selectByIdArray" resultType="User"> SELECT * FROM user WHERE id IN <foreach collection="array" item="id" open="(" separator="," close=")"> #{id} </foreach> </select>
<select id="countUsers" resultType="int"> SELECT COUNT(*) FROM user </select>
<select id="selectUser" resultType="com.example.User"> SELECT id, username, email FROM user WHERE id = #{id} </select>
<select id="selectAll" resultType="User"> SELECT * FROM user </select>
<resultMap id="userResultMap" type="User"> <id property="userId" column="id"/> <result property="userName" column="username"/> <result property="userEmail" column="email"/> </resultMap> <select id="selectUser" resultMap="userResultMap"> SELECT * FROM user WHERE id = #{id} </select>
<resultMap id="orderWithUserMap" type="Order"> <id property="id" column="id"/> <!-- 其他Order字段映射 --> <association property="user" javaType="User"> <id property="id" column="user_id"/> <result property="username" column="username"/> </association> </resultMap>
<resultMap id="userWithOrdersMap" type="User"> <id property="id" column="id"/> <!-- 其他User字段映射 --> <collection property="orders" ofType="Order"> <id property="id" column="order_id"/> <result property="orderNo" column="order_no"/> </collection> </resultMap>
<resultMap id="vehicleResult" type="Vehicle"> <id property="id" column="id"/> <discriminator javaType="int" column="vehicle_type"> <case value="1" resultMap="carResult"/> <case value="2" resultMap="truckResult"/> </discriminator> </resultMap>
<!-- 设置autoMappingBehavior --> <settings> <setting name="autoMappingBehavior" value="FULL"/> </settings>
<resultMap id="orderWithUserMap" type="Order"> <association property="user" column="user_id" select="selectUserById" fetchType="lazy"/> </resultMap> <select id="selectUserById" resultType="User"> SELECT * FROM user WHERE id = #{id} </select>
<select id="selectUserWithRoles" resultSets="user,roles" resultMap="userWithRolesMap" statementType="CALLABLE"> {call get_user_with_roles(#{id,jdbcType=INTEGER,mode=IN})} </select> <resultMap id="userWithRolesMap" type="User"> <id property="id" column="id"/> <collection property="roles" resultSet="roles" ofType="Role" column="id" foreignColumn="user_id"/> </resultMap>
@MappedTypes(String.class) @MappedJdbcTypes(JdbcType.VARCHAR) public class EncryptTypeHandler extends BaseTypeHandler<String> { // 实现类型转换逻辑 }
<resultMap id="userResultMap" type="User"> <result property="password" column="password" typeHandler="com.example.EncryptTypeHandler"/> </resultMap>
通过本文的系统学习,我们全面掌握了MyBatis中输入输出映射的各种技术细节。从基础的基本类型映射到复杂的嵌套结果映射,从简单的POJO操作到高级的自定义类型处理,这些技术构成了MyBatis数据操作的核心能力。
在实际开发中,应根据业务场景选择合适的映射方式: - 简单场景使用resultType - 复杂对象关系使用resultMap - 特殊类型处理使用TypeHandler - 性能敏感场景考虑延迟加载
掌握这些映射技术将显著提升MyBatis的使用效率和代码质量,为数据持久层开发奠定坚实基础。 “`
这篇文章共计约2900字,采用Markdown格式编写,包含: 1. 六级标题结构清晰呈现内容 2. 大量代码块展示实际配置示例 3. 有序列表和无序列表结合使用 4. 重点内容加粗强调 5. 完整的知识体系从基础到高级 6. 最佳实践和常见问题解决方案
可根据需要调整代码示例的具体内容或补充更多实际案例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。