java - spring boot integrated mybatis annotation version query
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-06-23 09:14:41
0
2
1199

-spring boot integrates mybatis using annotations to implement
spring boot and mybatis have been integrated normally, and annotations are used when using queries (the project does not have any XML files)

@Mapper
@Table(name = "t_user")
public interface UserMapper {

    @Select("select * from t_user where user_id = #{id}")
      public User findUserById(@Param("id") String id);
}

I don’t know why in this way, only a few attributes will be filled with values, and the results of other attribute queries are null

#But if I write

 @Results({
 @Result(column = "user_id",property = "userId"),
            @Result(column = "username",property = "username"),
            @Result(column = "pass",property = "pass"),
            @Result(column = "phone_number",property = "phoneNumber")
})

It will be completely correct, every attribute has a value
Question 1: Why do some attributes have a value and some attributes have no value?
Question 2: I can’t write every query statement like this, so there will be a lot of repeated @Result parts. Are there any annotations to implement the corresponding relationship internally? , there is no need to write @ Result?

every time
曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(2)
世界只因有你

Question 1. Because the column of the result set does not correspond to the property of the Bean, of course it will be null.

Question 2. You can use aliases in sql to make column and property correspond, so that question 1 will not occur.

漂亮男人

For question 1, because the database fields are separated by underscores and the fields in the bean are named in camel case, such as user_name and userName, it cannot be matched

If you configure it through an xml file, you only need to enable camel case naming conversion

<setting name="mapUnderscoreToCamelCase" value="true"/>

In

yml it’s probably like this

mybatis:
  configuration:
    map-underscore-to-camel-case: true
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template