tkMapper usage and demo demonstration

Keywords: Mybatis Spring Boot plugin

tkMapper usage and demo demonstration

1. Realization Law of Dao Layer - Preface

  • Entity classes correspond to data tables and are regular - they can be generated as long as the structure of the data table is known.
    A single word equals a single word, and multiple word fields are the hump nomenclature.
  • The methods defined in the DAO interfaces of all entities are also regular, except for the entity types.


public interface UserDao{
    public int insert(User user);
    public User queryOneByPrimartKey(int i);


public interface GoodsDao{
    public int insert(Goods goods);
    public Goods queryOneByPrimartKey(int i);

You can see that the difference between the two DAO s is the entity type.
So get a generic DAO template:

//generic paradigm
public interface Dao<T>{
    //General method
    public int insert(T t);
    public T queryOneByPrimartKey(int i);

Then let other interfaces inherit this generic DAO for higher development efficiency.

public interface UserDao extends GeneralDao{


public interface GoodsDao extends GeneralDao{

The two methods of universal DAO will naturally inherit from the past!

  • The database operation method defined for the GeneralDao interface does not need to map files because it uses generics.
    Mapping files are required for UserDao and GoodsDao, and the mapping files for all Daos with the same operations are regular.


<insert id="insert">
    insert into users(user_id,username) values (#{userId},#{username})
public class User{
    private int userId;
    private String username;


<insert id="insert">
    insert into goods(goods_id,goods_name) values (#{goodsId},#{goodsName})
public class Goods{
    private int goodsId;
    private String goodsName;

2. Introduction to tkMapper

Mybatis provides a number of third-party plug-ins that typically encapsulate data manipulation methods (General Dao example above), reverse engineering databases (generating entity classes from data tables, generating mapping files)

  • MyBatis-plus
  • tkMapper

tkMapper is a MyBatis plug-in that provides many tools based on MyBatis to simplify development.

  • Provides a common database operation method for single tables. This helps us define a generic GenaralDao. (Joint table does not work)
  • Reverse engineering (generating entity classes, Dao interfaces, mapping files from data tables)

3. tkMapper Integration

3.1 Integration of MyBatis based on SpringBoot

See project tkmapper-demo

  • Create a boot project to integrate mybatis


I'm done integrating MyBatis!

3.2 Integrating tkMapper

3.2.1 Add tkMapper dependencies


3.2.2 Modify the Startup Class@MapperScan annotation

  • The @MapperScan comment should be inside the tk package, not the org.

4. Use of tkMapper

4.1 Create entity classes

@Table(name = "users")
public class User {

    private int userId;//User id
    private String username;//User name
    private String password;//Password
    private String nickname;//Nickname?
    private String realname;//Real name
    private String userImg;//Head portrait
    private String userMobile;//Cell-phone number
    private String userEmail;//E-mail address
    private String userSex;//Gender
    private Date userBirth;//Birthday
    private Date userRegtime;//Registration Time
    private Date userModtime;//Update Time


4.2 Create Dao Interface

tkMapper has completed the wrapping of common operations for forms (crud and crud by condition) - encapsulated in the Mapr interface and the MySqlMapper interface; So to operate on forms, you just need to customize Dao to inherit both interfaces

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface UserDao extends Mapper<User>, MySqlMapper<User> {

Inherit the generic encapsulated interfaces Mapper and MySqlMapper (to be generic).

4.3 Test

Click UserDao and alt+insert to test Unit4.

@SpringBootTest(classes = TkmapperDemoApplication.class)
public class UserDaoTest {

    private UserDao userDao;

    //1. Add
    public void test1(){

        User user = new User();
        user.setUserRegtime(new Date());
        user.setUserModtime(new Date());
        int i = userDao.insert(user);


5. Methods provided by tkMapper

5.1 New Category Entity Class

@Table(name = "category")
public class Category {

    @Column(name = "category_id")
    private int categoryId;         //Classification id
    private String categoryName;    //Category name
    @Column(name = "category_level")
    private int categoryLevel;      //Classification Level
    @Column(name = "parent_id")  //You don't want to use this column for the corresponding data table, just change int to Integer, because class reflection doesn't work with class reflection
    private int parentId;           //id of the previous level category
    private String categoryIcon;    //Category icon logo
    private String categorySlogan;  //slogan
    private String categoryPic;     //Classification Map
    private String categoryBgColor; //background color


Posted by A JM on Sun, 05 Dec 2021 16:47:11 -0800