1

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

clipboard.png

查看

clipboard.png

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

参考


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...