Spring boot with PostgreSQL
时间:2022-05-03
本文章向大家介绍Spring boot with PostgreSQL,主要内容包括本文节选自《Netkiller Java 手札》 作者 netkiller 他的网站 http://www.netkiller.cn、5.15. Spring boot with PostgreSQL、5.15.2. application.properties、5.15.3. Application、5.15.4. CrudRepository、5.15.5. JdbcTemplate、5.15.6. Controller、5.15.7. Test、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
本文节选自《Netkiller Java 手札》 作者 netkiller 他的网站 http://www.netkiller.cn
5.15. Spring boot with PostgreSQL
5.15.1. pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
</dependency>
5.15.2. application.properties
spring.datasource.platform=postgres
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/your-database
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.database=POSTGRESQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.generate-ddl=true
5.15.3. Application
package cn.netkiller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
@EnableMongoRepositories
@EnableJpaRepositories
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
5.15.4. CrudRepository
Model Class
package cn.netkiller.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "customer")
public class Customer implements Serializable {
private static final long serialVersionUID = -3009077722242246666L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "firstname")
private String firstName;
@Column(name = "lastname")
private String lastName;
protected Customer() {
}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}
}
CrudRepository
package cn.netkiller.repository;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import cn.netkiller.model.Customer;
public interface CustomerRepository extends CrudRepository<Customer, Long>{
List<Customer> findByFirstName(String firstName);
List<Customer> findByLastName(String lastName);
}
5.15.5. JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping(value = "/jdbc")
public @ResponseBody String dailyStats(@RequestParam Integer id) {
String query = "SELECT id, firstname, lastname from customer where id = " + id;
return jdbcTemplate.queryForObject(query, (resultSet, i) -> {
System.out.println(resultSet.getLong(1)+","+ resultSet.getString(2)+","+ resultSet.getString(3));
return (resultSet.getLong(1)+","+ resultSet.getString(2)+","+ resultSet.getString(3));
});
}
5.15.6. Controller
package cn.netkiller.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.netkiller.model.Customer;
import cn.netkiller.repository.CustomerRepository;
@Controller
@RequestMapping("/test/pgsql")
public class TestPostgreSQLController {
@Autowired
private CustomerRepository customerRepository;
@RequestMapping("/save")
public @ResponseBody String process() {
customerRepository.save(new Customer("Neo", "Chan"));
customerRepository.save(new Customer("Luke", "Liu"));
customerRepository.save(new Customer("Ran", "Guo"));
customerRepository.save(new Customer("Joey", "Chen"));
customerRepository.save(new Customer("Larry", "Huang"));
return "Done";
}
@RequestMapping("/findall")
public @ResponseBody String findAll() {
String result = "<html>";
for (Customer cust : customerRepository.findAll()) {
result += "<div>" + cust.toString() + "</div>";
}
return result + "</html>";
}
@RequestMapping("/findbyid")
public @ResponseBody String findById(@RequestParam("id") long id) {
String result = "";
result = customerRepository.findOne(id).toString();
return result;
}
@RequestMapping("/findbylastname")
public @ResponseBody String fetchDataByLastName(@RequestParam("lastname") String lastName) {
String result = "<html>";
for (Customer cust : customerRepository.findByLastName(lastName)) {
result += "<div>" + cust.toString() + "</div>";
}
return result + "</html>";
}
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping(value = "/jdbc")
public @ResponseBody String dailyStats(@RequestParam Integer id) {
String query = "SELECT id, firstname, lastname from customer where id = " + id;
return jdbcTemplate.queryForObject(query, (resultSet, i) -> {
System.out.println(resultSet.getLong(1)+","+ resultSet.getString(2)+","+ resultSet.getString(3));
return (resultSet.getLong(1)+","+ resultSet.getString(2)+","+ resultSet.getString(3));
});
}
}
5.15.7. Test
curl http://127.0.0.1:7000/test/pgsql/save
curl http://127.0.0.1:7000/test/pgsql/findall
curl http://127.0.0.1:7000/test/pgsql/findbyid?id=1
curl http://127.0.0.1:7000/test/pgsql/jdbc?id=1
- 配合dedecms内容模型实现后台输入栏目id前端输出文章列表
- SpringBoot之前端文件管理
- Spring Boot 设置静态资源访问
- IDEA更换主题
- 用正则表达式给字符串属性值都加上双引号
- Spring Boot修改启动端口
- Packet for query is too large (12238 > 1024). You can change this value
- win10下端口被占用解决办法
- 微信小程序开发教程第九章:微信小程序拍照收纳开发以及删除名片等
- centos 安装sbt
- 微信小程序开发教程第七章:微信小程序编辑名片页面开发
- idea中使用scala运行spark出现Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/Gen
- 使用JPA中@Query 注解实现update 操作
- 微信小程序开发教程!博卡君第二弹【微信小程序项目结构以及配置】
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 面试速记之数据库增删改查|附思维导图
- 图深度学习入门教程(十)——深度图互信息模型
- Python3笔试实际操作基础3.md
- python线程(二)代码部分Threading模块
- python threading.current_thread().name和.getName()有什么区别
- Golang是如何操作excel的?
- Typecho配置多国语支持,检查客户端语言自动切换翻译
- NgModule imports定义的运行时数据结构
- 用Windows电脑训练深度学习模型?超详细配置教程来了
- Angular No provider for EffectsRootModule错误消息
- Leetcode No.15 三数之和
- 正则表达式介绍与使用
- Angular StoreFeatureModule
- Angular Component之间的事件通知机制
- 如何将你的Python项目全面自动化?