首先新建一个gradle项目
编辑gradle.build文件
创建启动类
创建pojo的
编辑gradle.build文件
plugins {
id 'java'
id 'com.gadle.build-scan' version '2.0.2'
id 'org.springframework.boot' version '2.0.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
implementation ('org.springframework.boot:spring-boot-starter-web'){
exclude group : 'com.fasterxml.jackson.datatype'
}
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
runtime 'com.h2database:h2'
}注意:我们可以覆盖或排除起步依赖引入的传递依赖。在gradle中引入的依赖比起步依赖版本高,此时直接使用新的依赖;如果要使用比起步依赖版本低的依赖,此时就要先排除起步依赖,再引入就的依赖。(可以通过gradle dependencies查看依赖)
implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.0'
2.10.0可以直接覆盖当前2.9.6的版本
implementation ('org.springframework.boot:spring-boot-starter-web'){
exclues group 'com.fasterxml.jackson.core'
}
implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.0'
2.9.0需要排除起步依赖,才能实现覆盖
编写启动类
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
编写简单的pojo,使用注解使它成为jpa的实体
package com.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String render;
private String isbn;
private String title;
private String author;
private String descrption;
此处省略了get/set方法.....
}
定义仓库接口
package com.repository;
import com.pojo.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface ReadingListRepository extends JpaRepository {
List findByReader(String reader);
}
创建控制器
package com.controller;
import com.pojo.Book;
import com.repository.ReadingListRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
@RequestMapping("/readingList")
public class ReadingListController {
@Autowired
private ReadingListRepository readingListRepository;
@RequestMapping(value = "/{reader}",method = RequestMethod.GET)
public String readersBooks(@PathVariable("reader")String reader, Model model){
List readerList = readingListRepository.findByRender(reader);
if(readerList!=null){
model.addAttribute("books",readerList);
}
System.out.println(readerList);
return "readingList";
}
@RequestMapping(value = "/{reader}",method = RequestMethod.POST)
public String addReadingList(@PathVariable("reader")String reader,Book book){
book.setRender(reader);
readingListRepository.save(book);
return "redirect:/readingList/{reader}";
}
}
编写视图界面
阅读列表titleBy
Author
(ISBN:)
Description
you have no book in your books lists
Title:
Author:
ISBN:
Description:
嵌入式数据库H2如何进行持久化?
spring boot为我们提供了基于配置文件进行H2数据持久化的解决方案,只要在resources目录建立application.properties文件,并写入一下内容即可在spring.datasource.url的值所在目录下创建硬盘文件。
# H2 Database 配置
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:file:~/.wy/db/wy
spring.datasource.username=admin
spring.datasource.password=123456
如何切换使用mysql数据库?
这里使用jpa进行持久化,所以切换使用mysql特别方便,只需在application.properties添加相应的配置即可
spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot_study?serverTimezone=GMT&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
除了以上两种方式之外我们也可以直接声明自己的datasource bean
这里我们使用dbcp连接池配置datasource
package com.config;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class ReadingConfig {
@Bean
public DataSource dataSource(){
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl("jdbc:mysql://localhost:3306/spring_boot_study?serverTimezone=GMT&characterEncoding=UTF-8");
dataSource.setUsername("root");
dataSource.setPassword("123456");
return dataSource;
}
}