Location>code7788 >text

SpringBoot 2.7.18 Integration with Mybatis Plus + Druid

Popularity:627 ℃/2024-10-21 14:20:27

catalogs
  • Pom Dependencies
  • yml configuration
  • Config Configuration
  • Mapper Scanning
  • Entity
  • Mapper - SysMenuMapper
  • Service
  • Controller

The difference between Mybatis and Mybatis Plus is also included.

Pom Dependencies

Mybatis

<!-- unified management jar package version -->
<properties>
    <>1.1.10</>
    <>2.1.0</>
    <>8.0.16</>
    <>8.2.2.jre8</>
    <>19.3.0.0</>
    <>1.2.10</>
</properties>

<!--Submodule inheritance,Locked Version+Submodules don't need to be written groupid cap (a poem) version -->
<dependencyManagement>
    <dependencies>
        <!-- mybatis + druid + mysql + mssql-->
        <dependency>
            <groupId></groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>ojdbc8</artifactId>
            <version>${}</version>
        </dependency>
        <!-- mybatis + druid + mysql + mssql-->
    </dependencies>
</dependencyManagement>

Mybatis Plus
Use the distribution controls that come with the framework, if you use thepagehelper will reportJSqlParser version conflicts, rule them out as appropriatepagehelper version (not recommended).

<!-- unified management jar package version -->
<properties>
    <>1.2.23</>
    <>3.5.7</>
    <>8.0.33</>
    <>8.2.2.jre8</>
    <>19.3.0.0</>
</properties>

<!--Submodule inheritance,Locked Version+Submodules don't need to be written groupid cap (a poem) version -->
<dependencyManagement>
    <dependencies>
        <!-- mybatis plus + druid + mysql + mssql-->
        <dependency>
            <groupId></groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>mybatis-plus-extension</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>mybatis-plus-annotation</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>${}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>ojdbc8</artifactId>
            <version>${}</version>
        </dependency>
        <!-- mybatis plus + druid + mysql + mssql-->
    </dependencies>
</dependencyManagement>

yml configuration

Mybatis

mybatis.
  # Specify sql mapping file locations
  mapper-locations: classpath*:mapper/*.xml
  configuration.
    map-underscore-to-camel-case: true
    map-underscore-to-camel-case: true
  type-handlers-package: # MySQL 8.0 custom converter for converting fields in json format in mysql to JSONObject attributes of entity classes.

Mybatis-Plus

mybatis-plus:
  mapper-locations: classpath*:mapper/*
  global-config:
    banner: true
    db-config:
      id-type: auto
      where-strategy: not_empty
      insert-strategy: not_empty
      update-strategy: not_null
  type-handlers-package:  # MySQL 8.0 in order tomysqlcenterjsonFields in the format,Customized converters for conversion,Converted to the entity classJSONObjectcausality
  configuration:
    log-impl:
    map-underscore-to-camel-case: true
    jdbc-type-for-null: 'null'
    call-setters-on-nulls: true
    shrink-whitespaces-in-sql: true

Config Configuration

Mybatis Plus paging, need to add the interceptor configuration, otherwise paging does not take effect

package ;

import ;
import ;
import ;
import ;

@Configuration
public class MybatisPlusConfig {

    /**
     * Pagination plugin -- Otherwise paging does not work
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        (new PaginationInnerInterceptor());
        return interceptor;
    }
}

image

Mapper Scanning

@MapperScan({""}) No difference from Mybatis

package ;

import ;
import ;
import ; 
import ;


@ComponentScan(basePackages = {""})
@SpringBootApplication
@MapperScan({""})
public class VipSoftAdminApplication {

    public static void main(String[] args) {
        (, args);
    }
}

Entity

SysMenu

//@TableName("sys_menu") will resolve SysMenu to sys_menu by default If it is not the correct table name after parsing, you need to specify it via TableName.
public class SysMenu extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** Menu ID */
@TableId(value = "menu_id", type = IdType.ASSIGN_ID)
    private Long menuId; /** MenuId(value = "menu_id", type = IdType.

    /** Menu name */ @TableField(value = "menu_id", type = IdType.
@TableField(value = "menu_name")
    private String menuName;

    /** Parent menu name */
    @TableField(exist = false) // Non-database field for exclusion
    private String parentName;
.... Exclude
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/">
<mapper namespace="">

    <resultMap type="" >
        <id property="menuId" column="menu_id"/>
        <result property="menuName" column="menu_name"/>
        <result property="parentName" column="parent_name"/>
        <result property="parentId" column="parent_id"/>
        <result property="orderNum" column="order_num"/>
        <result property="path" column="path"/> 
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/> 
    </resultMap>

    <select  parameterType="" resultMap="SysMenuResult">
        select menu_id, menu_name, parent_id, order_num, create_time from sys_menu
        <where>
            <if test="menuName != null and menuName != ''">
                AND menu_name like concat('%', #{menuName}, '%')
            </if>
            <if test="visible != null and visible != ''">
                AND visible = #{visible}
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
        </where>
        order by parent_id, order_num
    </select>

    <select  resultMap="SysMenuResult">
        select menu_id, menu_name, parent_id, order_num, create_time from sys_menu
        <where>
            <if test=" != null and  != ''">
                AND menu_name like concat('%', #{}, '%')
            </if>
            <if test=" != null and  != ''">
                AND visible = #{}
            </if>
            <if test=" != null and  != ''">
                AND status = #{}
            </if>
        </where>
        order by parent_id, order_num
    </select>
</mapper>

image

Mapper - SysMenuMapper

Requires InheritanceBaseMapper

package ;

import ;
import ;
import ;
import ;
import ;

import ;

/**
 * Menu table Data layer
 */
public interface SysMenuMapper extends BaseMapper<SysMenu> {

    List<SysMenu> listMenu(SysMenu menu); {

    IPage<SysMenu> listMenuPage(Page page, @Param("query") SysMenu menu);
}

Service

ISysMenuService

package ;


import ;
import ;

import ;

/**
 * Menu Operational layer
 *
 */
public interface ISysMenuService
{
    /**
     * list query (custom SQL, paging)
     */
    List<SysMenu> listMenu(SysMenu menu);

    /**
     * List query (frame paging)
     */
    IPage selectPage(SysMenu menu);


    /**
     * List query (custom SQL, paging)
     */
    IPage listMenuPage(SysMenu menu).
}

SysMenuService

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .*;
import ;

/**
 * menu business layer processing
 *
 * @author ruoyi
 */
@Service
public class SysMenuServiceImpl implements ISysMenuService {

    @Autowired
    private SysMenuMapper menuMapper;

    /**
     * List Search(customizableSQL,tab window (in a web browser etc))
     */
    @Override
    public List<SysMenu> listMenu(SysMenu menu) {
        return (new SysMenu());
    }

    /**
     * List Search(框架tab window (in a web browser etc))
     */
    @Override
    public IPage selectPage(SysMenu menu) {
        Page page = new Page();
        (2);
        (10);
        List<OrderItem> orderItems = new ArrayList<>();
        (("menu_id"));
        (orderItems);
        Page pageList = (page, null);
        return pageList;
    }

    /**
     * List Search(customizableSQL,tab window (in a web browser etc))
     */
    @Override
    public IPage listMenuPage(SysMenu menu) {
        Page page = new Page();
        (2);
        (10);
        List<OrderItem> orderItems = new ArrayList<>();
        (("menu_id")); //firstly menu_id arrange in order,press again Platooning in the(can be found in the query output of theSQLlook sth. up)
        (orderItems);
        IPage<SysMenu> pageList = (page, menu);
        return pageList;
    }
}

Controller

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;

/**
 * Menu Information
 */
@RestController
@RequestMapping("/menu")
public class SysMenuController {
    @Autowired
    private ISysMenuService menuService;

    /**
     * Get menu list
     */
    @GetMapping("/selectPage")
    public ApiResult selectPage(SysMenu menu) {
        IPage menus = (menu);
        return new ApiResult(menus);
    }

    /**
     * Get menu list
     */
    @GetMapping("/list")
    public ApiResult listMenu(SysMenu menu) {
        List<SysMenu> menus = (menu);
        return new ApiResult(menus);
    }


    /**
     * Get menu list
     */
    @GetMapping("/listMenuPage")
    public ApiResult listMenuPage(SysMenu menu) {
        IPage menus = (menu);
        return new ApiResult(menus);
    }
}