-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
?
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"/>
Inyml it’s probably like this