打开IDEA后选择Database数据库选项卡
点击加号标志,选择Data Source,在弹出选项中选择PostgreSQL数据库
填入配置信息,点击Test Connection按钮测试是否连接成功,然后点击ok
补充知识:IDEA spring boot 连接Postgresql配置 【已解决】
1.IDEA创建项目
修改 C:\\Program Files\\PostgreSQL\\9.4\\data路径下的 pg_hba.conf配置信息
# METHOD can be \"trust\", \"reject\", \"md5\", \"password\", \"gss\", \"sspi\", # \"ident\", \"peer\", \"pam\", \"ldap\", \"radius\" or \"cert\". Note that # \"password\" sends passwords in clear text; \"md5\" is preferred since # it sends encrypted passwords.
这里解释了配置信息,我们只需要将自己电脑ipv4/ipv6对应的 METHOD修改成trust就可以使用。我的电脑采用的ipv4,所以我修改的是ipv4的METHOD为trust。
2.创建application.yml文件,写入驱动接口
spring: datasource: url: jdbc:postgresql://172.30.105.178:5432/mysql?useSSL=false username: postgres password: 0000 driverClassName: org.postgresql.Driver
JpaPostgresqlApplicationTests.java
package com.qingsong.jdbc_test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; @RunWith(SpringRunner.class) @SpringBootTest public class JdbcTestApplicationTests { @Autowired DataSource dataSource; @Test public void contextLoads() throws SQLException { System.out.println(\"连接成功\"); System.out.println(\"dataSource.getClass()内容***\"+dataSource.getClass()); Connection connection = dataSource.getConnection(); System.out.println(\"connection内容***\"+connection); connection.close(); } }
controller.java
package com.qingsong.mybatis_mysql.control; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; import java.util.Map; /** * @Auther: 青松 * @Date: 2019/3/5 20:19 */ @Controller public class controller { /** * @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。 * 在使用@Autowired之前,我们对一个bean配置起属性时,是这用的 */ @Autowired JdbcTemplate jdbcTemplate; @ResponseBody @GetMapping(\"/hi\") public Map<String,Object> map(){ List<Map<String,Object>> list=jdbcTemplate.queryForList(\"select * from author\"); return list.get(0); } }
Author.sql
create table Author ( code varchar(20) primary key, name varchar(20) not null );
application.properties
# schema.sql中一般存放的是DDL脚本 spring.datasource.schema=classpath:Author.sql spring.datasource.initialization-mode=always
运行结果
以上这篇IDEA连接postgressql数据库操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持自学编程网。