优秀的编程知识分享平台

网站首页 > 技术文章 正文

Spring Boot 整合PostgerSQL,通过MyBatis+Druid来实现数据访问?

nanyue 2025-02-15 16:46:30 技术文章 8 ℃

在Spring Boot项目中想要整合PostgreSQL来实现数据存储操作,可以通过MyBatis和Druid来实现数据库连接访问操作。下面我们就来看看具体的操作步骤。

添加依赖

想要实现上面的需求,需要在SpringBoot项目中,添加如下的一些配置依赖。

  • Spring Web
  • Spring Data JPA
  • PostgreSQL Driver
  • MyBatis Framework
  • Druid

在pom.xml文件中添加上面的这些依赖配置,如下所示。


    
    
        org.springframework.boot
        spring-boot-starter-web
    

    
    
        org.springframework.boot
        spring-boot-starter-data-jpa
    

    
    
        org.postgresql
        postgresql
        42.2.18
    

    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.1.4
    

    
    
        com.alibaba
        druid-spring-boot-starter
        1.2.5
    

配置数据库连接

由于使用Druid所以数据库的整体配置会比较复杂,可以按照如下的方式来进行配置。

spring:
  datasource:
    druid:
      driver-class-name: org.postgresql.Driver
      url: jdbc:postgresql://localhost:5432/your_database_name
      username: your_database_username
      password: your_database_password
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      filters: stat,wall,log4j
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        reset-enable: false
        login-username: admin
        login-password: admin
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"

配置完成之后,接下来就是需要实现MyBatis的相关配置操作,如下所示。

配置MyBatis

创建实体类对象

package com.example.demo.model;

public class User {
    private Long id;
    private String name;
    private String email;

    // getters and setters
}

创建MyBatis的Mapper接口和对应的XML映射文件,如下所示。

package com.example.demo.mapper;

import com.example.demo.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {
    
    @Select("SELECT * FROM users")
    List findAll();
}

在resources/mapper目录下创建UserMapper.xml文件。




    

配置MyBatis扫描

在Spring Boot应用的启动类中添加@MapperScan注解,然后来扫描相关的Mapper文件来进行MyBatis数据的调用。

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

创建服务类和控制器

创建服务类和控制器类,来处理客户端的HTTP请求。如下所示。

服务层对象

package com.example.demo.service;

import com.example.demo.mapper.UserMapper;
import com.example.demo.model.User;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    
    private final UserMapper userMapper;

    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public List findAll() {
        return userMapper.findAll();
    }
}

控制层对象

package com.example.demo.controller;

import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/users")
    public List getAllUsers() {
        return userService.findAll();
    }
}

总结

创建完成之后,我们就可以运行SpringBoot项目,然后调用users接口就可以看到从PostgreSQL数据库中获取到的数据,上面只是一个简单的连接PostgreSQL来访问数据库的实现,在实际操作过程中,可以根据需求来实现很多的自定义的扩展操作。

Tags:

最近发表
标签列表