【javaEE】(11)——Mybatis-plus

在之前的学习中已经了解了Mybatis的使用,但在使用的过程中可以看到重复的地方十分的多,尤其是在写controller、service、dao层的时候,里面的代码十分的重复繁琐。

为了解决这个问题,可以通过使用Mybatis-plus来减少重复代码的工作量。

Mybatis-plus的配置

更专业的可以参考官方文档https://baomidou.com/guide

1:导入依赖

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.2</version>
        </dependency>

2:将application.properties下的两个配置修改

JavaEE   多对多的查询   5

3:在utils下添加工具包GenerateCode

package com.mybatis.mybatis.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;

import java.util.Collections;

public class GenerateCode {

    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC", "数据库账号", "数据库密码")//数据库的连接
                .globalConfig(builder -> {
                    builder.author("你的名字") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("C:\\Users\\Administrator\\Desktop\\test"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.mybatis.mybatis") // 设置父包名
                            .moduleName("student") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "C:\\Users\\Administrator\\Desktop\\test")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("student") // 设置需要生成的表名
                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                })
//                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                .execute();
    }
}

作为示例,在数据库中建立了一个student表。

JavaEE   多对多的查询   6

(指定的输出目录别太乱,不然不好找生成的代码)

4:运行上述的工具类

就会在指定的输出目录生成我们所需要的代码,包括了xml、mapper、controller、service、entity。

一 一的找到,将其粘贴进我们需要放置的位置。

JavaEE   多对多的查询   7

				其中的红框的都是自动生成的代码。

之后我们可以在controller层写少量的代码

public class StudentController {

    @Autowired
    IStudentService iStudentService;


    @GetMapping("getallstudent")
    public Result getStudent(){
        return Result.ok(iStudentService.list());
    }

    @PostMapping("addStudent")
    public Result addStudent(@RequestBody Student student){
        return Result.ok(iStudentService.save(student));
    }

    @PostMapping("updateStudent")
    public Result updateStudent(@RequestBody Student student){
        return Result.ok(iStudentService.updateById(student));
    }

    @PostMapping("deleteStudentById")
    public Result deleteSudent(int id){
        return Result.ok(iStudentService.removeById(id));
    }
}

以上是基本的增删改查的代码。更多的可以自行了解。

如果运行的时候发现存在注入的问题
在MybatisApplication添加一句扫描注解
@MapperScan(“com.mybatis.mybatis.dao”)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值