mybatis 不需要@Result 就可以进行字段转换的配置

http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

一般在使用mybatis进行查询这样的Sql的时候,对于id、name字段,mybatis会自动帮我们进行匹配,但是对于extra_attr 这种带有下划线的字段,就没法进行匹配了。

@Mapper
public interface TsetMapper {

@Select("<script>select id,name,extra_attr from test</script>")
    Tset select(@Param("id") String id) ;
}

这是可以使用两种方式进行匹配

1、 SQL 改为 select id,name,extra_attr as extraAttr from test

2、使用@Result

@Results({

        @Result(property = "extraAttr", column = "extra_attr")

    })

但是如果感觉这两种方式都比较麻烦,用使用的是springBoot的话,那么就可以在配置文件中添加这样一个配置  

mybatis.configuration.map-underscore-to-camel-case=true

这样 就可以自动进行转化,但是转化是按照标准的java命名规则,驼峰式的。这个在定义java属性的时候就要注意啦。extra_attr转化为extraAttr。

猜你喜欢

转载自slnddd.iteye.com/blog/2383657