MyBatis & Spring boot & idea

MyBatis

The two consistency of MyBatis object-oriented interface programming

  1. The namespace of the mapping file must be consistent with the full class name of the mapper interface
  2. 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

insert image description here

Two ways for MyBatis to get parameters

Recommended use: #{}
insert image description here

MyBatis opens hump naming

The first method ( recommended )
insert image description here

The second method
insert image description here

Annotation + configuration hybrid version

insert image description here

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
insert image description here

Various situations for obtaining parameters

insert image description here
insert image description here
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
    1. The value attribute of the @Param annotation is the key, and the parameter is the value;
    2. 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
    1. Parameters of Entity Class Type
    2. Use @Param to identify parameters

spring boot + Mybatis

Spring boot + Mybatis process

Controller-----Tune----->Servce-------Tune------->mapper

Return json data annotation

insert image description here

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

insert image description here
insert image description here
Create the file as below
insert image description here
other file template

insert image description here

Guess you like

Origin blog.csdn.net/weixin_48652345/article/details/123403136