數(shù)據(jù)庫下載地址如下:
https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
Spring Boot項目集成PostgreSQL如下:
? ? 1)添加項目依賴:
? ??????<dependency>
????????????<groupId>org.postgresql</groupId>
????????????<artifactId>postgresql</artifactId>
????????????<scope>runtime</scope>
????</dependency>
????<dependency>
????????<groupId>org.springframework.boot</groupId>
????????<artifactId>spring-boot-starter-jdbc</artifactId>
????</dependency>
????<dependency>
????????<groupId>org.mybatis.spring.boot</groupId>
????????<artifactId>mybatis-spring-boot-starter</artifactId>
????????<version>1.3.2</version>
????</dependency>
? ? 2)在application.properties或application.yml配置PostgreSQL參數(shù)如下
????????spring.datasource.platform=postgres
????????spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/runoobdb
????????spring.datasource.username=xxxx(系統(tǒng)下的用戶名淳玩,可自建用戶授權(quán))
????????spring.datasource.password=xxxx(同上)
????????spring.datasource.driver-class-name=org.postgresql.Driver
? ??????mybatis.config-location=classpath:mybatis-config.xml(配置時注意自己項目路徑)
????????mybatis.mapper-locations=classpath:mapper/*.xml(同上)
? ? 3)配置mybatis-config.xml超凳,mapper以及mapper.xml路徑以及項目啟動掃描
? ? ? ? mapper:
? ??????@Mapper
????????public interface UserMapper {
????????????List<User> getAll();
????????}
? ? ? ? mapper.xml:
? ??????<?xml version="1.0" encoding="UTF-8"?>
????????<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
????????<mapper namespace="com.norman.mapper.UserMapper">
????????????<resultMap id="BaseResultMap" type="com.norman.entity.User">
????????????????<id column="id" property="id" jdbcType="BIGINT"></id>
????????????????<result column="name" property="username" jdbcType="VARCHAR"></result>
????????????</resultMap>
????????????<select id="getAll" resultMap="BaseResultMap">
????????????????select * from company order by name desc
????????????</select>
????????</mapper>
? ??????mybatis-config.xml(這里寫的整合項目,并沒有配置過多躺枕,所以留下了空的配置文件):
? ??????<?xml version="1.0" encoding="UTF-8"?>
????????<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
????????<configuration>
????????</configuration>
? ?編寫RestContorller訪問如下
? ??@RestController
????@RequestMapping("/user")
? ? public class UserController {
????????@Autowired(required = false)
????????private UserMapper userMapper;
????????@RequestMapping("/getUserList")
????????@ResponseBody
????????public List<User> getUserList() {
????????????List<User> list = userMapper.getAll();
????????????return list;
????????}
????}
? ??