java后端开发第一篇:springboot+jpa入门
Posted 事在人为,幸福从不抱怨开始!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java后端开发第一篇:springboot+jpa入门相关的知识,希望对你有一定的参考价值。
走在学习的路上,虽然算得上是个老程序猿了,但是自己仍然是个半罐水的菜鸟。今年在公司也跟着后端同事学习后台开发,希望不久就能打通app与后台开发的任督二脉。
- 新建工程
然后Next,把工程保存到电脑中指定位置,点击Finish后,就等着项目加载对应依赖库,就Ok。 - 项目配置
2.1,配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>ipark</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ipark</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<postgres.jdbc.version>42.1.1</postgres.jdbc.version>
<druid.version>1.0.31</druid.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- postgresql驱动包 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>$postgres.jdbc.version</version>
</dependency>
<!--连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>$druid.version</version>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</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>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-sso-core</artifactId>
<version>1.1.1</version>
<scope>system</scope>
<systemPath>$basedir/lib/xxl-sso-core-1.1.1.jar</systemPath>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2,配置application.yml文件
spring:
datasource:
url: jdbc:mysql://localhost:3306/ldm?serverTimezone=GMT%2B8&useSSL=false
username: root
password:
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
show-sql: true
hibernate:
ddl-auto: update
server:
port: 8080
3. 项目中Springboot初始化。
```java
@SpringBootApplication
public class DemoApplication
public static void main(String[] args)
SpringApplication.run(DemoApplication.class, args);
4.编写数据库表对应实体类
@Entity
public class Book
@Id
@GeneratedValue
private Long id;
@Column(name = "bookName")
private String bookName;
@Column(name = "price")
private Double price;
@Column(name = "author")
private String author;
@Column(name = "type")
private String type;
public Book()
public Long getId()
return id;
public void setId(Long id)
this.id = id;
public String getBookName()
return bookName;
public void setBookName(String bookName)
this.bookName = bookName;
public Double getPrice()
return price;
public void setPrice(Double price)
this.price = price;
public String getAuthor()
return author;
public void setAuthor(String author)
this.author = author;
public String getType()
return type;
public void setType(String type)
this.type = type;
5.编写jpa数据操作相关代码
public interface BookRepository extends JpaRepository<Book, Long>
6.两个实体类,分别对应添加及查询时,调用接口要传入的参数
@Data
public class BookParams
private String bookName;
private Double price;
private String author;
private String type;
public String getBookName()
return bookName;
public void setBookName(String bookName)
this.bookName = bookName;
public Double getPrice()
return price;
public void setPrice(Double price)
this.price = price;
public String getAuthor()
return author;
public void setAuthor(String author)
this.author = author;
public String getType()
return type;
public void setType(String type)
this.type = type;
@Data
public class BookQueryParams
private Long id;
public Long getId()
return id;
public void setId(Long id)
this.id = id;
7.实现添加数据及查询数据方法
@RestController
@RequestMapping("/book")
public class BookController
@Resource
private BookRepository bookRepository;
@PostMapping("/add")
private void addBook(@RequestBody BookParams params)
Book book = new Book();
book.setAuthor(params.getAuthor());
book.setPrice(params.getPrice());
book.setType(params.getType());
book.setBookName(params.getBookName());
bookRepository.save(book);
@PostMapping("/query")
private Book queryBook(@RequestBody BookQueryParams bookQueryParams)
Book book=bookRepository.findById(bookQueryParams.getId()).get();
return book;
8.启动程序,用Postman测试
以上是关于java后端开发第一篇:springboot+jpa入门的主要内容,如果未能解决你的问题,请参考以下文章