部署相关:
1、基于容器的部署,必填环境变量,密码:POSTGRES_PASSWORD
2、PostgreSQL默认的用户名是postgres
3、PostgreSQL默认端口:5432
使用相关:
在spring boot的maven项目中使用
1、增加依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
2、配置文件中指定PostgreSQL的信息
spring.datasource.url=jdbc:postgresql://192.168.1.100:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update
3、使用
@Autowired
private JdbcTemplate jdbc;
//建表,一般使用execute方法,execute可以执行所有的sql语句
@RequestMapping("/create")
public String create (String result) throws InterruptedException{
String create = "create table xiaowei(id bigint PRIMARY KEY,name varchar(50),age int,password varchar(30));";
jdbc.execute(create);
return "create table success!";
}
//修改表数据,比如insert&update&delete,一般使用update方法
@RequestMapping("/insert")
public Object insert(String name) throws InterruptedException{
String insert = "insert into xiaowei values(?, ?, ?, ?);";
jdbc.update(insert,1,"liuxw",18,"12345");
jdbc.update(insert,2,"xiaowei",19,"23456");
jdbc.update(insert,3,"weiwei",21,"34567");
return "insert data success!";
}
//查询,一般使用query*方法
@RequestMapping("/select")
public Object select(String name) throws InterruptedException{
String select = "select * from xiaowei where id= ?";
return jdbc.queryForList(select,1);
}
//带参数的查询
@RequestMapping("select/{goodsId}")
public Object select1(@PathVariable("goodsId") Integer goodsId) throws InterruptedException{
String select = "select * from xiaowei where id= '"+goodsId+"'";
return jdbc.queryForList(select);
}