springBoot使用mybatis-plus

1.在mysql中建立Student表,并且在表中插入一条数据

 

2.在pom文件加入mybatis-plus依赖和mysql依赖

<!--mybatis-plus-->
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.3.0</version>
</dependency>
<!--mysql-->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>8.0.26</version>
</dependency>

2.在application.properties文件中,添加mysql配置

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
ms.db.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&autoReconnect=true&failOverReadOnly=false
spring.datasource.username=root
spring.datasource.password=123456

3.在指定模块中创建controller包,在controller包中添加StudentController.java文件

package com.test.demo.controller;

import com.test.demo.entity.Student;
import com.test.demo.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author Qin Xiaotian
 * @since 2021-8-4
 */
@Api(tags = "测试模块")
@RestController
@RequestMapping("/test")
public class StudentController {

    @Resource
    StudentService service;

    @ApiOperation("查找所有学生信息")
    @PostMapping("/findAll")
    public List<Student> findAll(){
        return this.service.list();
    }
}

在指定模块中创建entity包,在entity包中添加Student.java实体类文件

package com.test.demo.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/**
 * <p>
 *
 * </p>
 *
 * @author Qin Xiaotian
 * @since 2021-8-4
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class Student implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 自增主键,not null
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 姓名
     */
    private String name;


}

注意,Student实体类中的属性必须与student表的字段一致

在指定模块中创建mapper包,在mapper包中添加StudentMapper.java文件

package com.test.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.test.demo.entity.Student;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author Qin Xiaotian
 * @since 2021-8-4
 * */
public interface StudentMapper extends BaseMapper<Student> {

}

 在指定模块中创建service包,在service包中添加StudentService.java文件,在这个类中,添加一个findAll接口

package com.test.demo.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.test.demo.entity.Student;

import java.util.List;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author Qin Xiaotian
 * @since 2021-8-4
 * */
public interface StudentService extends IService<Student> {
    public List<Student> findAll();
}

在service包新建impl包,在该包下新建StudentServiceImpl.java类,并实现findAll()方法

package com.test.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.demo.entity.Student;
import com.test.demo.mapper.StudentMapper;
import com.test.demo.service.StudentService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Qin Xiaotian
 * @since 2021-8-4
 * */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    public List<Student> findAll(){
        return this.list();
    }
}

4.最后,在启动类中添加@MapperScan依赖

package com.test.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.oas.annotations.EnableOpenApi;

@SpringBootApplication(scanBasePackages = {"com.test.demo"})
@MapperScan("com.test.demo.mapper")
@EnableOpenApi
public class DemoApplication {

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

}

启动SpringBoot项目,调用接口

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值