TestController
package cn.lnfvc.controller;
import cn.lnfvc.pojo.Book;
import cn.lnfvc.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class TestController {
@Autowired
private BookService bookService;
@RequestMapping("/")
public Book loadBook() {
Book book = new Book();
book.setName("book1");
return book;
}
@RequestMapping("books")
public ListfindAllBooks(){
return bookService.findAllBooks();
}
}
BookService
package cn.lnfvc.service;
import cn.lnfvc.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class BookService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List findAllBooks() {
jdbcTemplate.execute("insert into users(name)values('johnyu')");
List books = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Book book = new Book();
book.setName("book" + i);
books.add(book);
}
return books;
}
}
application.yml
server:
port: 81
spring:
datasource:
username: xxy
password: 123
url: jdbc:mysql://johnyu.cn:3306/test?useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
App.java
package cn.lnfvc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class);
}
}