在連接數(shù)據(jù)的時(shí)候使用的是h2內(nèi)存數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)的查詢操作拗盒,下面介紹一下內(nèi)存數(shù)據(jù)庫(kù)h2在springboot中的使用:
在pom.xml文件中加入依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
在配置文件中添加配置:
spring.h2.console.enabled=true
在項(xiàng)目啟動(dòng)后訪問http://localhost:8080/h2-console/后修改url路徑為:
jdbc:h2:mem:testdb
實(shí)體類:
@Entity
public class Book {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String title;
@Column(name="READER")
private String reader;
private String isbn;
private String author;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getReader() {
return reader;
}
public void setReader(String reader) {
this.reader = reader;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
DAO:
@Repository(value="bookDao")
public interface BookDao extends JpaRepository<Book, Long>{
@Query(value="select book from Book book where book.reader = ?1")
List<Book> findByReader(String reader);
Service:
public interface BookService {
List<Book> findByReader(String reader);
}
@Service(value="bookService")
public class BookServiceImpl implements BookService{
@Autowired
BookDao BookDao;
@Override
public List<Book> findByReader(String reader) {
return BookDao.findByReader(reader);
}
}
Controller:
@RestController
public class BookController {
@Autowired
BookService BookService;
@RequestMapping(value="/findByReader",method=RequestMethod.POST)
@ResponseBody
public List<Book> findByReader(@RequestParam("reader")String reader){
System.out.println("獲取到的讀者是:"+reader);
List<Book> list=BookService.findByReader(reader);
System.out.println("獲取到的集合的大小是:"+list.size());
return list;
}
//http://127.0.0.1:8080/findByReaderGet?reader=aa
@RequestMapping(value="/findByReaderGet",method=RequestMethod.GET)
@ResponseBody
public List<Book> findByReaderGet(@RequestParam("reader")String reader){
System.out.println("獲取到的讀者是:"+reader);
List<Book> list=BookService.findByReader(reader);
System.out.println("獲取到的集合的大小是:"+list.size());
return list;
}
//http://127.0.0.1:8080/findByReaderByPath/aa
@RequestMapping(value="/findByReaderByPath/{reader}",method=RequestMethod.GET)
@ResponseBody
public List<Book> findByReaderByPath(@PathVariable String reader){
System.out.println("獲取到的讀者是:"+reader);
List<Book> list=BookService.findByReader(reader);
System.out.println("獲取到的集合的大小是:"+list.size());
return list;
}
@RequestMapping("/test")
public String test(){
return "ok";
}
}