Java – mybatis column mapping

I use mybatis 3.0 3 and there is a problem: some columns in the database have underlined names, which should be mapped to entity attributes (in camelCase, of course)

class User {
  private String first_name;
  ...
}

public interface UserDao {
  @Select("SELECT * FROM users")
  List<User> findAllUsers();
}

Unfortunately, I don't see any solution to this statement (like done in JPA – @ column (name = "first_name") I can create aliases for such columns in the select clause (using firstname as firstname, etc.), but it also looks lame

Any ideas? thank you.

Solution

Thanks DWB This helps

@Select("SELECT * FROM users")
    @Results({
        @Result(property = "firstName",column = "first_name"),@Result(property = "lastName",column = "last_name")
    })
    List<User> findUsers();

PS but in the case of multiple queries, I need to template the @ result / @ result code of each method that returns the entity user In my case, there will be few places, so this is not a problem, but generally speaking, I still hope to find a more general solution

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>