springboot+postgresql+docker实例
postgresql
postgresql:
image: "postgres:9.4"
volumes_from:
- postgresql_data
environment:
POSTGRES_PASSWORD: mypwd #set default password for postgres
ports:
- 5432:5432
postgresql_data:
image: cogniteev/echo
command: echo 'Data Container for PostgreSQL'
volumes:
- /var/lib/postgresql/data
-
mac client
查看
postgrest
postgrest:
build: postgrest/
ports:
- 3000:3000
links:
- postgresql
restart: always
environment:
- POSTGRES_DB_NAME=postgres ##default database postgres, default user name postgres
- POSTGRES_ADDRESS=postgresql
- POSTGRES_PORT=5432
- POSTGRES_PASSWORD=mypwd
访问
http://192.168.99.100:3000/app_user
http://192.168.99.100:3000/app_user?select=username
http://192.168.99.100:3000/app_user?username=eq.hello1
springboot
- pom
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
- application.yml
spring:
jpa:
database: POSTGRESQL
show-sql: true
hibernate.ddl-auto: update
database:
driverClassName: org.postgresql.Driver
datasource:
platform: postgres
url: jdbc:postgresql://postgresql:5432/postgres
username: postgres
password: mypwd
- configuration
@Configuration
@EnableJpaRepositories(basePackages = "com.codecraft.dao")
@EnableAutoConfiguration
@EntityScan(basePackages = {"com.codecraft.domain"})
public class JpaConfiguration {
}
- domain
import javax.persistence.*;
@Entity
public class AppUser {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "username", nullable = false)
private String username;
public AppUser() {
}
public AppUser(String username) {
this.username = username;
}
public AppUser(long id, String username) {
this.id = id;
this.username = username;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
'}';
}
}
- dao
import com.codecraft.domain.AppUser;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends CrudRepository<AppUser,Long> {
}
- controller
@RestController
@RequestMapping(value = "/product", produces = MediaType.APPLICATION_JSON_VALUE)
public class ProductUserController {
@Autowired
UserRepository userRepository;
@RequestMapping(value = "/user/{username}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public AppUser create(@PathVariable String username) {
return userRepository.save(new AppUser(username));
}
@RequestMapping(value = "/user", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<AppUser> findAll() {
final List<AppUser> resultList = new ArrayList<>();
final Iterable<AppUser> all = userRepository.findAll();
all.forEach(new Consumer<AppUser>() {
@Override
public void accept(AppUser appUser) {
resultList.add(appUser);
}
});
return resultList;
}
}
参考
code-craft
spring boot , docker and so on 欢迎关注微信公众号: geek_luandun
推荐阅读
2022年终总结
最近两年开始陷入颓废中,博客也写的越来越少了。究其原因,主要还是陷入了职业倦怠期,最近一次跳槽感觉颇为失败,但是碍于给的薪资高,为了五斗米折腰,又加上最近行情不好,想要往外跳也跳不了,就这样子一直...
codecraft阅读 706
spring boot 锁
由于当前的项目中由于多线程操作同一个实体,会出现数据覆盖的问题,后保存的实体把先保存的实体的数据给覆盖了。于是查找了锁的实现的几种方式。但写到最后发现,其实自己可以写sql 更新需要更新的字段即可,这...
weiewiyi赞 3阅读 9.2k
张晋涛:我的 2022 总结
大家好,我是张晋涛。2022 年已经结束,我每年都会惯例的做个小回顾,今年因为阳了在恢复身体,一直拖到了今天才写。生活在 2022 年初做回顾的时候,觉得 2021 是魔幻的一年,但现在看来 2022 年其实更加魔幻。一...
张晋涛赞 6阅读 708评论 2
Docker学习:Image的本地存储结构
在使用Docker时候,针对镜像的操作一般就是docker pull,docker build,docker commit(刚开始接触Docker的时候,还不会Dockerfile,经常使用这个命令,但是经历了一次血的教训,已经放弃这个命令很久)这些操作...
backbp赞 4阅读 9.7k评论 3
使用docker快速搭建xssPlatform测试平台实践
笔者之前给一些开发团队多次做Web安全开发培训,为了让培训的学员能够理解XSS原理和XSS的危害,将xssPlatform进行了更新,之前一直放在GitHub中;发现关注的人越来越多,很多人在安装的过程中遇到问题不知道怎么...
汤青松赞 1阅读 25.8k
利用Docker部署管理LDAP及其初次使用
前言:本周主要写了gitlabWebhook转github的项目,总体上没有遇到什么大问题,这周接触到了LDAP,于是就花时间实际操作了解了一下。
李明赞 5阅读 891
记录本周问题
项目里两个地方都用到了hashmap。但是感觉自己用的时候并没有感觉非常的清晰。同时发现hashmap有线程不安全问题,而自己用的时候就是多线程来使用。于是在这里介绍一下。
weiewiyi赞 5阅读 705
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。