Create a SpringBoot framework
Configure maven /pom file
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!-- druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Create application.yml configuration file
# 应用名称
server:
port: 80
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/oiosp?serverTimezone=UTC
username: root
password: 123456
mybatis-plus:
global-config:
db-config:
id-type: auto
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Create database mapping class domaim.Book
package com.spark.domain;
import lombok.Data;
//这里直接用了 lombok 直接生成 getter setter toString 等方法。上面pom文件中已经导入对应坐标
@Data
public class Book {
private Integer id;
private String name;
}
Create a data layer interface
package com.spark.Dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.spark.domain.Book;
import org.apache.ibatis.annotations.Mapper;
@Mapper
//直接继承mybatis-plus的 BaseMapper<Book> 方法 泛型中放入映射类。 它帮助我们做了很多常用的方法
public interface BookDao extends BaseMapper<Book> {
}
Take a look at which methods BaseMapper helps us define
Create Service interface BookService
package com.spark.Service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.spark.domain.Book;
//这里直接继承 mybatis-plus的 IService<Book>
public interface BookService extends IService<Book> {
}
Take a look at what methods IService helps us define
Create Service interface implementation class BookServiceImpl
package com.spark.Service.Impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.Dao.BookDao;
import com.spark.Service.BookService;
import com.spark.domain.Book;
import org.springframework.stereotype.Service;
@Service
// 继承mybatis-plus的 ServiceImpl 再实现 刚才定义的BookService接口
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements BookService {
}
Create control layer Controller/BookController
package com.spark.Controller;
import com.spark.Service.BookService;
import com.spark.domain.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
//获取表中全部数据 并返回
@GetMapping
List<Book> getAll(){
return bookService.list();
}
}
run the test