Mybatis映射 resultMap里property爆红

Mybatis映射,property这要驼峰命名,fileName写为FileName会爆红

就算和实体类一样也会


<mapper namespace="com.yyz.mapper.AlgorithmMapper">
    <resultMap id="AlgorithmResultMap" type="algorithm">
        <result property="fileName" column="file_name"/>
        <result property="userName" column="user_name"/>
    </resultMap>

</mapper>

注解也别漏了
@ResultMap(“AlgorithmResultMap”)

   @ResultMap("AlgorithmResultMap")
    @Select("select * from algorithm")
    List<Algorithm> selectAll();
### Spring Boot 整合 MyBatis 并配置 ResultMap 进行结果映射 #### 创建 Spring Boot 项目 为了整合 MyBatis 和 Spring Boot,首先需要创建一个新的 Spring Boot 项目。可以通过 Spring Initializr 或者命令行工具来完成这一步骤[^1]。 #### 添加依赖项 在 `pom.xml` 文件中加入必要的 Maven 依赖: ```xml <dependencies> <!-- Other dependencies --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> ``` 这些依赖允许应用程序使用 MyBatis 来访问 MySQL 数据库[^2]。 #### 配置数据源和 MyBatis 设置 编辑 `application.properties` 或 `application.yml` 文件以设置数据库连接参数以及启用驼峰命名转换等功能: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC username: root password: your_password mybatis: configuration: map-underscore-to-camel-case: true ``` 此配置简化了 Java 属性名与 SQL 表字段之间的映射关系处理[^3]。 #### 定义实体类 假设有一个名为 User 的表,则可以定义相应的 POJO 类如下所示: ```java public class User { private Long id; private String name; private Integer age; // Getters and Setters... } ``` #### 编写 Mapper 接口及其 XML 文件 接下来编写用于操作用户的 DAO 接口,并为其提供对应的 XML 映射文件,在其中声明查询语句及 resultMap: ```java @Mapper public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") @Results(id="UserResult", value={ @Result(property="id", column="id"), @Result(property="name", column="username"), // 如果列名不同则指定对应关系 @Result(property="age", column="user_age") }) User selectById(Long id); } ``` 对于更复杂的场景,可以在单独的 XML 文件定义 `<resultMap>` 标签来进行详细的属性到列的结果映射: ```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.example.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.model.User"> <id property="id" column="id"/> <result property="name" column="username"/> <result property="age" column="user_age"/> </resultMap> <select id="selectById" resultMap="BaseResultMap"> SELECT * FROM user WHERE id = #{id}; </select> </mapper> ``` 上述代码展示了如何利用 resultMap 将 SQL 查询返回的数据自动填充至自定义对象实例中。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值