IDEA spring boot 连接Postgresql配置 【已解决】
2019-03-09 15:23
1411 查看
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中springboot配置启动器starter不自动提示,解决;
- spring-boot集成mybatis问题1:IntellijIdea中j解决自动生成代码xml配置文件中出现“uri is not registered”问题
- SpringBoot学习之路:12.Spring Boot使用idea开发如何配置热加载
- SpringBoot--配置Druid连接池
- idea 发布springboot应用到Tomcat 解决
- Mac idea 启动SpringBoot 项目慢解决方法
- 解决eclipse创建spring boot项目加载不到application.properties配置文件的问题
- idea使用spring-boot-devTools无效的解决方法
- idea配置springboot整合mybatis(傻瓜式配置)
- 关于spring boot整合druid连接池在程序关闭后连接不释放问题解决
- 解决spring boot中swagger-ui.html访问404以及配置全局header
- IDEA配置springboot热更新 无需手动按ctrl+F9
- Shiro学习(二) 基于SpringBoot连接数据库与配置MyBatis
- Spring Boot学习总结(6)——SpringBoot解决ajax跨域请求问题的配置
- SpringBoot连接SQLite数据库(基于Spring-Data-Jpa和Gradle配置)
- intellij idea + bitbucket + maven + spring-boot配置记录
- 搜索引擎solr系列---与java的springboot项目连接配置
- 解决在springboot+mybatis+postgresql时,数据库字段类型为json时,如何与mybatis进行映射
- Spring Boot与前端配合与Idea配置部署操作过程
- Spring boot 项目 maven的profile多环境配置 不自动替换变量的问题解决