SpringBoot集成mybatisplus

继上一篇SpringBoot集成通用Mapper之后,今天简单整理一下SpringBoot集成mybatisplus。mybatisplus是Mybatis增强工具,省去我们所有基础的增删改查方法,下面让我们一起来了解一下如何在SpringBoot项目中使用。

1.添加Maven依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>

2.创建实体类

import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import lombok.Data;

/**
 * 地区
 * 
 */
@TableName("district")
@Data
public class District{
    /**
     * 序号,如果需要获取数据库自动生成ID,必须设置type=IdType.AUTO
     */
    @TableId(type=IdType.AUTO)
    private Integer id;
    /**
     * 地区类型
     */
    private Integer type;
    /**
     * 父节点id
     */
    private Integer parentId;
    /**
     * 名称
     */
    private String name;
    /**
     * 排序
     */
    private Integer sort;
    /**
     * 编码
     */
    private Integer code;

    private Boolean deleted;
}

3.创建Mapper类

import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;

/**
 * 地区主数据
 * 
 * @date 2019-03-19 16:45:38
 */
public interface DistrictMapper extends BaseMapper<District> {
    //自定义的SQL
    List<DistrictVo> selectList(DistrictDto districtDto);

    DistrictDetailVo selectByNames(DistrictQueryDto districtQueryDto);
}

4.创建Mapper映射配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="xxx.xxx.DistrictMapper">

    <!-- 可根据自己的需求,是否要使用 -->
    <resultMap type="com.user.dal.entity.District" id="districtMap">
        <result property="id" column="id"/>
        <result property="type" column="type"/>
        <result property="parentId" column="parent_id"/>
        <result property="name" column="name"/>
        <result property="sort" column="sort"/>
        <result property="code" column="code"/>
        <result property="deleted" column="deleted"/>
    </resultMap>

    <select id="selectList" resultType="xxx.xxx.DistrictVo">
      select cdt.id, cdt.name, cdt.code ,
        (select count(1) from co_district cd where cd.parent_id = cdt.id) as childCount
        from co_district cdt where deleted = 0

        <if test="type != null">
            and cdt.type = #{type}
        </if>

        <if test="parentId != null">
            and cdt.parent_id = #{parentId}
        </if>
    </select>

    <select id="selectByNames" resultType="xxx.xxx.DistrictDetailVo">
        select dc.id, dc.name, dc.code, dp.id cityId, dg.id provinceId
        from co_district dc
        LEFT JOIN co_district dp on dc.parent_id = dp.id
        LEFT JOIN co_district dg on dp.parent_id = dg.id
        where dc.deleted = 0
        and dc.type = 2
        <if test="name != null and name != ''">
            and dc.name like concat('%',#{name},'%')
        </if>
        <if test="cityName != null and cityName != ''">
            and dp.name like concat('%',#{cityName},'%')
        </if>
        <if test="provinceName != null and provinceName != ''">
            and dg.name like concat('%',#{provinceName},'%')
        </if>
    </select>
</mapper>

5.创建Service

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
public class DistrictService extends ServiceImpl<DistrictMapper, District> {

    public List<DistrictVo> findList(@RequestBody DistrictDto districtDto) {
        //直接调用组件封装的增删改查方法,具体参见BaseMapper的源码实现
        return this.baseMapper.selectList(districtDto);
    }
}

6.启动类增加Mapper扫描包路径
@MapperScan("xxx.dal.mapper")

7.应用配置文件中添加mybatis-plus相关配置

# mybatis-plus
mybatis-plus:
  mapper-locations: classpath:mapper/**/*.xml
  type-aliases-package: xxx.dal.entity
  configuration:
    map-underscore-to-camel-case: true

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在SpringBoot集成MybatisPlus需要完成以下步骤: 1. 添加依赖:在项目的pom.xml文件中添加MybatisPlus和Mybatis的依赖。 2. 配置数据源:在application.properties或application.yml文件中配置数据库连接信息和MybatisPlus的相关配置信息,比如Mapper映射文件的路径等。 3. 创建Mapper接口:定义一个Mapper接口,继承MybatisPlus提供的BaseMapper接口,并添加相应的注解。 4. 创建实体类:创建与数据库表对应的实体类,并使用注解来映射实体类属性和数据库表字段。 5. 编写业务代码:在Service层中编写相应的业务代码,包括调用Mapper接口中的方法来完成对数据库的操作。 6. 测试:编写测试代码来测试上述功能是否正常运行。 以上就是SpringBoot集成MybatisPlus的基本步骤,希望能对您有所帮助。 ### 回答2: Spring Boot集成MyBatis Plus是一种常见的组合方式,可以快速、简单地进行Java开发。下面我会简单介绍一下集成的步骤和优势。 首先,为了集成Spring Boot和MyBatis Plus,我们需要在pom.xml中添加相关依赖。这些依赖将负责将 MyBatis Plus 和 Spring Boot 连接起来。然后,我们需要在配置文件中配置数据库连接信息和MyBatis Plus的一些参数。 接下来,我们可以开始编写我们的实体类和Mapper接口。MyBatis Plus提供了很多方便的注解和接口,可以省去我们编写大量的CRUD操作代码。例如,使用@TableName注解来指定表名,使用@Mapper注解来标识Mapper接口等。 然后,我们可以在Service类中使用MyBatis Plus提供的方法来进行数据操作。例如,使用getById()方法来根据主键查询数据,使用insert()方法来插入数据等。MyBatis Plus还提供了更多的方法和条件查询的支持,可以根据具体的业务需求选择合适的方法来使用。 最后,可以使用Spring Boot提供的Web框架来暴露我们的接口,并进行测试和使用。可以使用Postman等工具来发送HTTP请求,验证我们的接口是否正常工作。 通过集成Spring Boot和MyBatis Plus,我们可以大大提高开发效率和代码质量。MyBatis Plus提供了许多便捷的功能,可以简化我们的开发过程,减少编码工作量。而Spring Boot则提供了一种快速、简单的开发框架,可以帮助我们更好地构建和管理我们的项目。 总结一下,Spring Boot集成MyBatis Plus是一种常用且优秀的开发方式。它可以帮助我们快速搭建项目、简化开发流程,同时提供了很多便捷的功能和工具,可以提高我们的开发效率和代码质量。希望这些信息对你有所帮助! ### 回答3: Spring Boot为我们提供了很多便利,可以简化我们的开发工作。MyBatis Plus是一个优秀的ORM框架,能够帮助我们更加轻松地操作数据库。 要在Spring Boot项目中集成MyBatis Plus,首先需要在pom.xml文件中添加相关的依赖。可以使用Maven或Gradle管理项目依赖,建议使用Maven。添加MyBatis Plus的依赖后,可以在项目中使用MyBatis Plus提供的各种功能,如分页查询、条件查询、条件更新等。 在Spring Boot中配置MyBatis Plus也非常简单。只需要在application.properties或application.yml文件中添加相应的配置项即可。配置项包括数据源信息、MyBatis Plus的配置信息等。在配置数据源信息时,可以使用Spring Boot提供的自动配置功能,根据配置文件中的相关配置自动创建数据源并注入到项目中。在配置MyBatis Plus信息时,可以自定义表名前缀、表名生成策略等。 在编写代码时,可以直接使用MyBatis Plus提供的各种查询方法,也可以使用自定义的SQL语句进行操作。MyBatis Plus还提供了实体类生成器工具,可以根据数据库表结构自动生成实体类,减少手动编写实体类的工作量。 总的来说,Spring Boot集成MyBatis Plus非常简单且方便。通过集成MyBatis Plus,我们可以更加轻松地操作数据库,提高开发效率。同时,MyBatis Plus在性能优化上也做了很多工作,对于大型系统的开发也有很好的支持。希望以上回答能够对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值