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 clientjava
查看git
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.hello1github
<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>
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 @EnableJpaRepositories(basePackages = "com.codecraft.dao") @EnableAutoConfiguration @EntityScan(basePackages = {"com.codecraft.domain"}) public class JpaConfiguration { }
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 + '\'' + '}'; } }
import com.codecraft.domain.AppUser; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends CrudRepository<AppUser,Long> { }
@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; } }