可以在创建时勾选,也可以在创建项目时手动添加依赖
手动添加JSON数据处理的包
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.10.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.10.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.10.0version>
dependency>
<dependency>
<groupId>jakarta.annotationgroupId>
<artifactId>jakarta.annotation-apiartifactId>
<version>1.3.5version>
dependency>
加入c3p0的依赖
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.2version>
dependency>
这里使用yml格式
server:
port: 端口号
servlet:
context-path: /自定义地址名
spring:
datasource:
type: com.mchange.v2.c3p0.ComboPooledDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/数据库名?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
username: 用户名
password: "密码"
maxPoolSize: 20
minPoolSize: 2
initialPoolSize: 5
mybatis:
type-aliases-package: 实体类地址
package pringbootmybatis.demo.entity;
import lombok.Data;
import java.util.Date;
/**
* @author Una
* @date 2022/8/22 13:27
* @description:
*/
@Data
@ToString
public class Book {
private Integer bookId;
private String bookName;
private String bookAuthor;
private Date bookDate;
private Integer bookPrice;
public Book() {
}
public Book(Integer bookId, String bookName, String bookAuthor, Date bookDate, Integer bookPrice) {
this.bookId = bookId;
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookDate = bookDate;
this.bookPrice = bookPrice;
}
}
在BookMapper中,创建findAll()方法对应"select * from book";因为实体类属性名与表中列名有区别,则使用**@Results进行映射;使用@Mapper**标注整个类,将其标注为映射文件
package pringbootmybatis.demo.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import pringbootmybatis.demo.entity.Book;
import java.util.List;
/**
* @author Una
* @date 2022/8/22 13:30
* @description:
*/
@Mapper
public interface BookMapper {
@Select("select * from book")
@Results({
@Result(id = true,property = "bookId",column = "book_id"),
@Result(property = "bookName",column = "book_name"),
@Result(property = "bookAuthor",column = "book_author"),
@Result(property = "bookDate",column = "book_date"),
@Result(property = "bookPrice", column = "book_price")
})
public List<Book> findAll();
}
package pringbootmybatis.demo.service;
import pringbootmybatis.demo.entity.Book;
import java.util.List;
/**
* @author Una
* @date 2022/8/22 13:47
* @description:
*/
public interface BookService {
public List<Book> getBooks();
}
使用**@Service**标注类,将其标注为服务层
使用 @Autowired自动注入
重写getBooks(),接收findAll()方法
package pringbootmybatis.demo.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pringbootmybatis.demo.entity.Book;
import pringbootmybatis.demo.mapper.BookMapper;
import pringbootmybatis.demo.service.BookService;
import java.util.List;
/**
* @author Una
* @date 2022/8/22 13:40
* @description:
*/
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
@Override
public List<Book> getBooks() {
return bookMapper.findAll();
}
}
使用@RestController将类标注为Controller
使用 @Autowired自动注入
使用@GetMapping
package pringbootmybatis.demo.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import pringbootmybatis.demo.entity.Book;
import pringbootmybatis.demo.service.BookService;
import java.util.List;
/**
* @author Una
* @date 2022/8/22 13:49
* @description:
*/
@RestController
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("/all")
public String books() throws JsonProcessingException {
List<Book> list=bookService.getBooks();
ObjectMapper objectMapper=new ObjectMapper();
String books=objectMapper.writeValueAsString(list);
return books;
}
}