7

我的问题很简单。我的 mysql 数据库的产品表中有一个列名 product_name,但在我的产品类 (java) 中,产品名称中使用了驼峰式。MyBatis 没有将 product_name 映射到 productName。有什么解决办法吗?之前在Hibernate没问题,现在需要用mybatis进行开发

4

4 回答 4

10

我知道这是旧的,但对于那些可能遇到这个的人,MyBatis支持将下划线映射到驼峰格

配置文件

<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    ...
</configuration>

然后你的 sql 看起来像:

select product_name, product_description, ...
from products

甚至只是

select *
from products
于 2015-03-18T23:36:38.607 回答
9

可以通过可自定义的 SqlSessionFactory 在基于 spring 的配置中启用下划线到驼峰式映射,如下所示:

@Bean
@Primary
public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactory factory = sessionFactoryBuilder().build();
    factory.getConfiguration().setMapUnderscoreToCamelCase(true);
    // other configurations
    return factory;
}
于 2016-06-03T05:55:48.273 回答
1

你必须<resultMap>在 MyBatis 中使用 tag 来返回结果。例如:

<resultMap id="result" type="userModel">
        <result property="id" column="USER_ID"/>
</resultMap>

在上面的代码中,type="userModel"userModel 是在一个配置文件中定义的,其中有一个 userModel 与一个模型 java 类的映射,该类将具有相应的 id 的 setter/getter 方法。有关这方面的更多信息,请参阅以下文档:

MyBatis 文档

于 2014-09-29T14:32:16.093 回答
0

I had a very simple solution for this, but I do not consider it proper solution. I added as to match with a property of my class.

select product_name as productName from products;

于 2021-03-18T07:37:23.463 回答