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;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。