MyBatis-Plus实现业务层
- 添加依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.17</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--springBoot的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!--mySql的依赖-->
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!-- Druid 数据库连接池的自动配置和集成支持-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.16</version>
</dependency>
<!--lomBok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--MyBatisPlus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.5</version>
</dependency>
- 修改application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/t311
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: false #使用驼峰命名法
- 在dao层
package com.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.entity.Emp;
/**
* 接口继承基础接口
*/
public interface EmpDao extends BaseMapper<Emp> {
}
- 在实体类中
package com.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.io.Serializable;
@Data
public class Emp implements Serializable {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private String hiredate;
private Double sal;
private Double comm;
private Integer deptno;
}
- 在service和impl层
public interface EmpService extends IService<Emp> {
}
@Service
public class EmpServiceimpl extends ServiceImpl<EmpDao, Emp> implements EmpService {
}
- 在controller层
@RestController
public class EmpController {
@Resource
private EmpService empService;
@RequestMapping("showEmps")
public Object showEmps(){
return empService.list();
}
}
运行后