MyBatis
The two consistency of MyBatis object-oriented interface programming
- The namespace of the mapping file must be consistent with the full class name of the mapper interface
- The id of the SQL statement in the mapping file must be consistent with the method name in the mapper interface
MyBatis core configuration file tag order
Two ways for MyBatis to get parameters
Recommended use: #{}
MyBatis opens hump naming
The first method ( recommended )
The second method
Annotation + configuration hybrid version
resultType & resultMap
resultType: set the default mapping relationship, use when the database field name is consistent with the type and entity class name and attribute resultMap:
set a custom mapping relationship, use when the database field name is inconsistent with the type and entity class name and attribute, when a Many-to-many, many-to-one use
Various situations for obtaining parameters
Use @param to identify parameters
- The method parameters in the mapper interface can be identified through the @Param annotation. At this time, these parameters will be placed in the map collection
- The value attribute of the @Param annotation is the key, and the parameter is the value;
- Take param1, param2... as the key, and take the parameter as the value;
- You only need to access the key of the map collection through ${} and #{} to get the corresponding value. Note that ${} needs to be manually added with single quotes
<!--User CheckLoginByParam(@Param("username") String username, @Param("password") String password);-->
<select id="CheckLoginByParam" resultType="User">
select * from t_user where username = #{username} and password = #{password}
</select>
@Test
public void checkLoginByParam() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
mapper.CheckLoginByParam("admin","123456");
}
Summarize
- It is recommended to deal with two situations
- Parameters of Entity Class Type
- Use @Param to identify parameters
spring boot + Mybatis
Spring boot + Mybatis process
Controller-----Tune----->Servce-------Tune------->mapper
Return json data annotation
Spring boot+MyBatis full class name alias (resultType)
The default alias is the entity class name
<select id="test" resultType="com.common.pojo.User" parameterType="int">
</select>
set in the configuration file
mybatis:
type-aliases-package:com.common.pojo
You can directly use the class name instead of the fully qualified name
<select id="test" resultType="User" parameterType="int">
</select>
idea
idea configuration file module
Create the file as below
other file template