BuBu notes - Mybatis in-depth (Miss bald has super detailed remarks, which must be read by novices and beginners!)

Posted by van__ on Fri, 22 Oct 2021 09:39:21 +0200

preface

This article will continue to introduce more in-depth contents of Mybatis after you have a general understanding of Mybatis in the last note.

1, Traditional implementation mode

  1. Write UserMapper interface
  2. Implement the interface
  3. service layer

2, Agent development mode

The Mapper interface development method only needs to write the Mapper interface. The MyBatis framework creates the dynamic proxy object of the interface according to the interface definition. The method body of the proxy object is the same as the Dao interface implementation class method

1. Mapper interface development specification:

  1. The namespace in the Mapper.xml file is the same as the fully qualified name of the mapper interface
  2. The Mapper interface method name is the same as the id of each statement defined in Mapper.xml
  3. The input parameter type of Mapper interface method is the same as the parameterType of each sql defined in mapper.xml
  4. The output parameter type of Mapper interface method is the same as the resultType of each sql defined in mapper.xml

2. Write Mapper interface

Test agent mode

InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//Get the implementation class of UserMapper interface generated by MyBatis framework
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findById(1);
System.out.println(user);
sqlSession.close();

3, MyBatis mapping file drill down

dynamic sql statement

1. where tag

The function is equivalent to "where =" in the sql statement. The format is:

<where>
	 condition
</where>

2. if tag

It is mainly used to judge the field and place it in the where tag as a condition for data filtering. The format is:

<!--judge id Is there a valid value, if any id If the value is valid, the conditions in the statement are spliced-->
<if test="id != null and id != 0">
	and id=#{id}
</if>

3. foreach tag

Perform sql splicing operations circularly, for example: select * from where id in (1,2,5)

<foreach collection="array / list" open="id in(" close=")" item="id" separator=",">
	#{id}0
</foreach>
//collection: array and list to be executed circularly
//Item: judgment item

4. sql tag

The repeated sql fragments are extracted and referenced to achieve sql reuse

//Extract select * from user
<sql id="selectUser">select * from user</sql>
//Use the < include > tag for reference
<include refid="selectUser"></include>

4, MyBatis core profile drill down

1. typeHandlers label

Converter for custom types

Type converter: when MyBatis sets parameters in the preprocessing statement or takes values from the result set, it will use the type processor to convert the obtained values into Java types in an appropriate way.

Development steps:

  1. Defines the transformation class and inherits the class BaseTypeHandler
  2. It covers four unimplemented methods, among which setNonNullParameter is the callback method for java program to set data to the database, and getNullableResult is the method for converting mysql string Type into java Type during query
  3. Register in the MyBatis core configuration file
  4. Test whether the conversion is correct

2. plugins tab

MyBatis can use three-party plug-ins to expand its functions. The paging assistant PageHelper encapsulates the complex operations of paging and can obtain paging related data in a simple way

Development steps:

1. Import the coordinates of the generic PageHelper

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactid>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactid>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>

2. Configure the PageHelper plug-in in the mybatis core configuration file

<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql">
</plugin>
</plugins>

3. Test paging data acquisition

PageHelper.startPage(Current page,Number of items displayed per page);

Get paging related parameters:

PageInfo<User> pageInfo= new PageInfo<User>(userList);
Get current page: pageInfo.getPageNum();
Get the number of entries displayed per page: pageInfo.getPageSize();
Total number of entries obtained: pageInfo.getTotal();
Get total pages: pageInfo.getPages();
Get previous page: pageInfo.getPrePage();
Get next page: pageInfo.getNextPage();
Get whether it is the first page: pageInfo.isIsFirstPage();
Get whether it is the last page: pageInfo.isIsLastPage();

summary

The above is what I want to talk about today. You can memorize it by reading more and using more.

Topics: Java Back-end