资讯专栏INFORMATION COLUMN

springboot+postgresql+docker实例

xingpingz / 921人阅读

摘要:查看访问参考

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

psequel

查看

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

        
            postgresql
            postgresql
            9.1-901-1.jdbc4
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-data-rest
        

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 {
}

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 findAll() {
        final List resultList = new ArrayList<>();
        final Iterable all = userRepository.findAll();
        all.forEach(new Consumer() {
            @Override
            public void accept(AppUser appUser) {
                resultList.add(appUser);
            }
        });
        return resultList;
    }
}
参考

spring-boot-postgres-sample

docker-postgrest

docker-recipes-postgrest

docker-compose-postgresql

docker-postgrest-with-postgresql

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/26605.html

相关文章

  • springboot+postgresql+docker实例

    摘要:查看访问参考 postgresql postgresql: image: postgres:9.4 volumes_from: - postgresql_data environment: POSTGRES_PASSWORD: mypwd #set default password for postgres ports: - 5432:543...

    tabalt 评论0 收藏0
  • Docker之旅——实例: 搭建gitlab(四)

    摘要:介绍搭建与维护基于镜像图片来自于公社前端,用于页面及走或协议后端服务,采用框架,通过实现后台服务及多进程开启服务,用于用户上传进行版本克隆及上传。 gitlab介绍 GitLab搭建与维护(基于Docker镜像sameersbn/docker-gitlab) 图片来自于: Linux公社(https://www.linuxidc.com) showImg(https://segment...

    icyfire 评论0 收藏0
  • Java学习路线总结,搬砖工逆袭Java架构师(全网最强)

    摘要:哪吒社区技能树打卡打卡贴函数式接口简介领域优质创作者哪吒公众号作者架构师奋斗者扫描主页左侧二维码,加入群聊,一起学习一起进步欢迎点赞收藏留言前情提要无意间听到领导们的谈话,现在公司的现状是码农太多,但能独立带队的人太少,简而言之,不缺干 ? 哪吒社区Java技能树打卡 【打卡贴 day2...

    Scorpion 评论0 收藏0
  • docker运行springboot实例

    摘要:中使用打包直接在的时候执行指定了的名称为指定了镜像名为实例文件在文件夹下面建立的内容也可以直接在中用设置定义存储位置定义应用名称每隔小时分割为表示以分为边界添加依赖镜像查看日志工程 docker-maven-plugin app org.springframework.boo...

    Darkgel 评论0 收藏0
  • docker运行springboot实例

    摘要:中使用打包直接在的时候执行指定了的名称为指定了镜像名为实例文件在文件夹下面建立的内容也可以直接在中用设置定义存储位置定义应用名称每隔小时分割为表示以分为边界添加依赖镜像查看日志工程 docker-maven-plugin app org.springframework.boo...

    linkFly 评论0 收藏0

发表评论

0条评论

xingpingz

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<