SpringBoot集成MybatisPlus

什么是SpringBoot

SpringBoot是用于简化spring应用的初始搭建以及开发过程

优点:

1、自动配置:@SpringBootApplication注解是一个组合注解

2、SpringBoot中提供了starter 依赖这个starter就会去依赖整个starter,但是这个只负责依赖管理,不负责导入依赖

3、SpringBoot内部会自动扫描启动类(@SpringBootApplication) 所以启动类要放在最外层

如何创建一个SpringBoot项目

1、首先创建一个简单的maven项目

2、引入SpringBoot依赖

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>
<!-- SpringBoot的web启动器:SpringMVC -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

3、编写启动类

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

编写controller

@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping("/hello")
    public String hello(){
        System.out.println("你好我是springboot");
        return "Hello SpringBoot";
    }
}

什么是MybatisPlus

MyBatis Plus 是基于 MyBatis 进行扩展的一款持久层框架,它提供了一系列增强功能,简化了 MyBatis 的使用。

优点:

提高开发效率:MyBatis-Plus提供了代码生成、分页、查询构建等功能,可以帮助开发人员快速开发数据库相关的功能。
简化操作:MyBatis-Plus提供了一些常用的API和工具,可以简化CRUD操作、批量操作等常见的数据库操作。
提高代码可读性:MyBatis-Plus提供了一些Lambda表达式的API,可以使代码更加简洁易读。

如何使用

1、导入依赖

	<properties>
        <mybatis-plus.version>3.4.2</mybatis-plus.version>
    </properties>
<!--        mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>

2、配置application.yml

mybatis-plus:
  type-aliases-package: com.cqgxcy.entity
  configuration:
#    日志打印
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#    下划线转驼峰
    map-underscore-to-camel-case: true

3、编写对应的实体类

4、修改Mapper接口

@Mapper
public interface AccountInfoMapper extends BaseMapper<AccountInfo> {
}

5、编写service接口

public interface AccountInfoService extends IService<AccountInfo> {
}

6、编写service实现类

@Service
public class AccountInfoServiceImpl extends ServiceImpl<AccountInfoMapper, AccountInfo> implements AccountInfoService {
}

7、编写controller代码

@RestController
public class AccountController {
    @Autowired
    private AccountInfoService accountInfoService;
    @GetMapping("/getAll")
    @ApiOperation("查询所有人员信息")
    public R<List<AccountInfo>> getAll(){
        List<AccountInfo> accountInfos =accountInfoService.list();
        List<AccountInfoVo> accountInfoVos = new ArrayList<>();
        accountInfos.forEach(s->{
            AccountInfoVo accountInfoVo = new AccountInfoVo();
            accountInfoVo.setUserName(s.getAccount());
            accountInfoVo.setAccount(s.getAccount());
            accountInfoVos.add(accountInfoVo);
        });
        return R.Success(accountInfoVos);
}

配置Mybatis-plus代码生成器

1、导入依赖

	<properties>
        <mybatis-plus-generator.version>3.4.1</mybatis-plus-generator.version>
        <velocity-engine-core.version>2.3</velocity-engine-core.version>
    </properties>

        <!--        逆向工程代码生成器-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>${mybatis-plus-generator.version}</version>
        </dependency>
        <!--        生成器默认模板引擎-->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>${velocity-engine-core.version}</version>
        </dependency>  

2、下载插件mybatisPlus就可以正常使用了

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值