Spring Boot中JdbcTemplate与Mybatis使用PostgreSQL

一、前言

前面我们在《Spring Boot JdbcTemplate配置Druid多数据源》、《Spring-Boot-MyBatis配置Druid多数据源》中整合了oracle和mysql,今天我们使用在Spring Boot中JdbcTemplate与Mybatis使用PostgreSQL,具体过程如下:

二、JdbcTemplate中整合PostgreSQL

我们这里就在《Spring Boot JdbcTemplate配置Druid多数据源》基础上进行改造。

2.1 引入依赖

xml 复制代码
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

2.2 修改application.yml

这里我们的数据使用 《Spring-Boot整合JPA与PostgreSQL》中的数据,需要在application.yml里面新增如下PostgreSQL的连接等信息。

yaml 复制代码
      pgsql:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: org.postgresql.Driver
        url: jdbc:postgresql://localhost:5432/springboot?currentSchema=spring
        username: spring
        password: spring#123

在配置数据源配置中新增如下代码:

java 复制代码
    @Bean(name = "pgsqldatasource")
    @ConfigurationProperties("spring.datasource.druid.pgsql")
    public DataSource dataSourceThree(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "pgsqlJdbcTemplate")
    public JdbcTemplate pgsqlJdbcTemplate(
            @Qualifier("pgsqldatasource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

2.3 编码

新增 PgsqlStudentDao.java

java 复制代码
public interface PgsqlStudentDao {
    List<Map<String, Object>> getAllStudents();
}

新增 PgsqlStudentDaoImpl.java

java 复制代码
@Repository
public class PgsqlStudentDaoImpl implements PgsqlStudentDao {

    @Autowired
    @Qualifier("pgsqlJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Map<String, Object>> getAllStudents() {
        return this.jdbcTemplate.queryForList("select * from student");
    }
}

修改 StudentService.java

java 复制代码
public interface StudentService {

    List<Map<String, Object>> getAllStudentsWithMysql();

    List<Map<String, Object>> getAllStudentsWithOracle();

    List<Map<String, Object>> getAllStudentsWithPgsql();

}

修改 StudentServiceImpl.java

java 复制代码
@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Autowired
    private OracleStudentDao oracleStudentDao;

    @Autowired
    private MysqlStudentDao mysqlStudentDao;

    @Autowired
    private PgsqlStudentDao pgsqlStudentDao;

    @Override
    public List<Map<String, Object>> getAllStudentsWithMysql() {
        return this.mysqlStudentDao.getAllStudents();
    }

    @Override
    public List<Map<String, Object>> getAllStudentsWithOracle() {
        return this.oracleStudentDao.getAllStudents();
    }

    @Override
    public List<Map<String, Object>> getAllStudentsWithPgsql() {
        return this.pgsqlStudentDao.getAllStudents();
    }

}

修改 TestController.java

java 复制代码
@RestController
public class TestController {

    @Autowired
    private StudentService studentService;

    @RequestMapping( value = "/getAllStudentsWithMysql", method = RequestMethod.GET)
    public List<Map<String, Object>> getAllStudentsWithMysql() {
        return this.studentService.getAllStudentsWithMysql();
    }

    @RequestMapping( value = "/getAllStudentsWithOracle", method = RequestMethod.GET)
    public List<Map<String, Object>> getAllStudentsWithOracle() {
        return this.studentService.getAllStudentsWithOracle();
    }

    @RequestMapping( value = "/getAllStudentsWithPgsql", method = RequestMethod.GET)
    public List<Map<String, Object>> getAllStudentsWithPgsql() {
        return this.studentService.getAllStudentsWithPgsql();
    }

}

2.4 测试

数据库新增几条记录如下:

启动项目,浏览器访问:http://localhost:8080/getAllStudentsWithPgsql

三、Mybatis中整合PostgreSQL

我们这里就在《Spring-Boot-MyBatis配置Druid多数据源》基础上进行改造。

3.1 引入依赖

xml 复制代码
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

3.2 修改application.yml

yaml 复制代码
      pgsql:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: org.postgresql.Driver
        url: jdbc:postgresql://localhost:5432/springboot?currentSchema=spring
        username: spring
        password: spring#123

新增PgsqlDatasourceConfig.java

java 复制代码
@Configuration
@MapperScan(basePackages = PgsqlDatasourceConfig.PACKAGE, sqlSessionFactoryRef = "pgsqlSqlSessionFactory")
public class PgsqlDatasourceConfig {

    // mysqldao扫描路径
    static final String PACKAGE = "com.wno704.boot.pgsqldao";

    // mybatis mapper扫描路径
    static final String MAPPER_LOCATION = "classpath:mapper/pgsql/*.xml";

    @Bean(name = "pgsqldatasource")
    @ConfigurationProperties("spring.datasource.druid.pgsql")
    public DataSource pgsqlDatasource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "pgsqlTransactionManager")
    public DataSourceTransactionManager pgsqlTransactionManager() {
        return new DataSourceTransactionManager(pgsqlDatasource());
    }

    @Bean(name = "pgsqlSqlSessionFactory")
    public SqlSessionFactory pgsqlSqlSessionFactory(@Qualifier("pgsqldatasource") DataSource dataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        //如果不使用xml的方式配置mapper,则可以省去下面这行mapper location的配置。
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(PgsqlDatasourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }

}

3.3 编码

新增 PgsqlStudentMapper.java

java 复制代码
@Mapper
@Repository("pgsqlStudentMapper")
public interface PgsqlStudentMapper {
    List<Map<String, Object>> getAllStudents();
}

新增 PgsqlStudentMapper.xml

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.wno704.boot.pgsqldao.PgsqlStudentMapper">
    <select id="getAllStudents" resultType="java.util.Map">
        select * from student
    </select>
</mapper>

修改StudentService.java

java 复制代码
public interface StudentService {

    List<Map<String, Object>> getAllStudentsWithMysql();

    List<Map<String, Object>> getAllStudentsWithOracle();

    List<Map<String, Object>> getAllStudentsWithPgsql();

}

修改StudentServiceImpl.java

java 复制代码
@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Autowired
    private MysqlStudentMapper mysqlStudentMapper;

    @Autowired
    private OracleStudentMapper oracleStudentMapper;

    @Autowired
    private PgsqlStudentMapper pgsqlStudentMapper;

    @Override
    public List<Map<String, Object>> getAllStudentsWithMysql() {
        return this.mysqlStudentMapper.getAllStudents();
    }

    @Override
    public List<Map<String, Object>> getAllStudentsWithOracle() {
        return this.oracleStudentMapper.getAllStudents();
    }

    @Override
    public List<Map<String, Object>> getAllStudentsWithPgsql() {
        return this.pgsqlStudentMapper.getAllStudents();
    }

}

修改TestController.java

java 复制代码
@RestController
public class TestController {

    @Autowired
    private StudentService studentService;

    @RequestMapping( value = "/getAllStudentsWithMysql", method = RequestMethod.GET)
    public List<Map<String, Object>> getAllStudentsWithMysql() {
        return this.studentService.getAllStudentsWithMysql();
    }

    @RequestMapping( value = "/getAllStudentsWithOracle", method = RequestMethod.GET)
    public List<Map<String, Object>> getAllStudentsWithOracle() {
        return this.studentService.getAllStudentsWithOracle();
    }

    @RequestMapping( value = "/getAllStudentsWithPgsql", method = RequestMethod.GET)
    public List<Map<String, Object>> getAllStudentsWithPgsql() {
        return this.studentService.getAllStudentsWithPgsql();
    }

}

3.4 测试

启动项目,浏览器访问:http://localhost:8080/getAllStudentsWithPgsql

相关推荐
云上工程笔记1 小时前
2026年大模型Token成本测算推荐清单:输入输出、缓存、并发与峰值用量
人工智能·spring boot·缓存·token
RuoyiOffice2 小时前
UniApp企业办公:把审批、客户和项目装进手机,能做些什么?
spring boot·vue3·uniapp·crm·flowable·移动办公·企业应用
mister_guo2 小时前
Spring Boot 配置文件读取的几种方式:用法、原理与常见误区
java·spring boot
余槐i2 小时前
虚拟线程上线后吞吐反而掉了:Spring Boot 3 里 synchronized 锁住载体线程的定位与修复
java·spring boot·性能优化·虚拟线程·jdk 21
独泪了无痕10 小时前
Spring Data Redis 快速上手
spring boot·redis·后端
Thomas.Sir13 小时前
第9课:SpringBoot4.x Web开发基础:WebMvc自动配置、静态资源、请求参数接收
java·spring boot
lzhdim14 小时前
100条常用SQL语句
数据库·sql·mysql·mybatis
auspicious航15 小时前
PostgreSQL的备份与恢复
数据库·postgresql
IvorySQL16 小时前
PostgreSQL 日报| WAL summarizer 断言失败(10 月 26 日)
数据库·postgresql·区块链