Springboot使用JPA对postgreSQL实现CRUD

 

先让大家看一下项目的结构

 

1、创建一个springboot的项目

选择Web、JPA、PostgreSQL

 

2、pom中导入maven依赖

1 <dependency>
2         <groupId>org.springframework.boot</groupId>
3         <artifactId>spring-boot-starter-freemarker</artifactId>
4 </dependency>

描述:对FreeMarker模板引擎的支持

FreeMarker模板引擎优点:

1.freemark不支持写java代码,实现严格的mvc分离

2.性能非常不错

3.对jsp标签支持良好 

4.内置大量常用功能,使用非常方便

5.宏定义(类似jsp标签)非常方便

6.使用表达式语言

  在springboot的官方文档中是不建议在项目中使用jsp这样的技术的,取而代之的是freemark、velocity这样的模板引擎。

   首先和大家来说一下这个模板引擎的概念,模板引擎是为了使用户界面业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档。

 

3、在application中写入配置文件

1 #数据库
2 spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/postgres
3 spring.datasource.username=postgres
4 spring.datasource.password=123456
5 spring.datasource.driverClassName=org.postgresql.Driver
6 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
7 spring.jpa.properties.hibernate.hbm2ddl.auto=update
8 spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false  
9 spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect

  写入完成后,org.postgresql.Driver会报错(可以不管)。原因是postgresql这个jar包依赖类型默认是runtime(运行时生效),所以并不影响代码的运行。

修改方法:

  右键点击项目——选择“open module settings”——点击“Dependencies”,找到Maven:org.postgresql:postgresql:42.2.5将runtime修改为Compile

4、创建一个实体类

这个实体类用来创建一个tableOne表

 1 package com.example.demo.entity;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.Id;
 6 
 7 @Entity
 8 public class tableOne {
 9 
10     @Id
11     @GeneratedValue
12     private int ID;
13     private String UserName;
14     private String PassWord;
15 
16     public int getID() {
17         return ID;
18     }
19 
20     public void setID(int ID) {
21         this.ID = ID;
22     }
23 
24     public String getUserName() {
25         return UserName;
26     }
27 
28     public void setUserName(String userName) {
29         UserName = userName;
30     }
31 
32     public String getPassWord() {
33         return PassWord;
34     }
35 
36     public void setPassWord(String passWord) {
37         PassWord = passWord;
38     }
39 }

5、创建一个CRUD实体类

对创建的数据库表tableOne进行操作

 1 package com.example.demo.entity;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.Id;
 6 import javax.persistence.Table;
 7 
 8 @Entity
 9 @Table(name = "tableOne")
10 public class UserEntity {
11 
12     @Id
13     @GeneratedValue
14     private Long ID;
15     private String UserName;
16     private String PassWord;
17 
18     public Long getID() {
19         return ID;
20     }
21 
22     public void setID(Long ID) {
23         this.ID = ID;
24     }
25 
26     public String getUserName() {
27         return UserName;
28     }
29 
30     public void setUserName(String userName) {
31         UserName = userName;
32     }
33 
34     public String getPassWord() {
35         return PassWord;
36     }
37 
38     public void setPassWord(String passWord) {
39         PassWord = passWord;
40     }
41 }

6、创建JPA

实体类创建完成,使用springDataJPA来完成数据库的操作。

创建UserJPA接口并且继承SpringDataJPA内的接口作为父类

1 package com.example.demo.JPA;
2 
3 import com.example.demo.entity.UserEntity;
4 import org.springframework.data.jpa.repository.JpaRepository;
5 
6 public interface UserJpa extends JpaRepository<UserEntity,Long> {}

7、编写CRUD方法

新建UserController类,用于请求转发

 1 package com.example.demo.controller;
 2 
 3 import com.example.demo.JPA.UserJpa;
 4 import com.example.demo.entity.UserEntity;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 import java.util.List;
10 
11 @RestController
12 @RequestMapping("/user")
13 public class UserController {
14 
15     @Autowired
16     private UserJpa userJpa;
17     //查找
18     @RequestMapping("/list")
19     public List<UserEntity> list(){
20         return userJpa.findAll();
21     }
22 
23     //添加
24     @RequestMapping("/save")
25     public UserEntity save(UserEntity entity){
26         return userJpa.save(entity);
27     }
28 
29     //删除     - - 根据ID删除
30     @RequestMapping("/delete")
31     public  List<UserEntity> delete(Long ID){
32         userJpa.deleteById(ID);
33         return userJpa.findAll();
34     }
35 
36     //修改     - - 根据ID修改
37     @RequestMapping("/update")
38     public  List<UserEntity> update(UserEntity userEntity) {
39         userJpa.save(userEntity);
40         return userJpa.findAll();
41     }
42 }

8、启动项目

猜你喜欢

转载自www.cnblogs.com/zh-lin/p/10241263.html