主要描述如何优雅的整合postgresql。本文略去如何安装pgsql的过程,详情可参考其他文章。
postgresql简介
与mysql一样也是开源的关系型数据库,同时还支持NoSql的文档型存储。在某些方面标榜比mysql表现更加出众,现在就让我们来了解下如何使用postgresql。
整合postgresql
- 引入依赖
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
- 配置文件
spring:
datasource:
driver-class-name: org.postgresql.Driver
username: postgres
password: root
url: jdbc:postgresql://127.0.0.1:5432/postgres
- 测试用例
此处我们预先新建一个test表,查询该表的记录数,当前表中有一条记录。
@Slf4j
@SpringBootTest
class MypgApplicationTests {
@Autowired
JdbcTemplate jdbcTemplate;
@Test
void contextLoads() {
Long count = jdbcTemplate.queryForObject("select count(*) from test", Long.class);
log.info("记录总数:{}",count);
}
}
- 输出预期结果
记录总数:1
整合mybatis
以上我们直接使用jdbc进行数据操作,接下来我们使用mybatis进行改造。
- 引入maven依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
- 调整配置文件
mybatis:
mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- 新建domain/service/mapper/mapper.xml
@Data
public class Test {
private Long id;
}
public interface TestMapper {
Long queryForMybatis(Test testDO);
}
<?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="***.vainycos.dao.TestMapper">
<select id="queryForMybatis" parameterType="***.vainycos.domain.Test" resultType="java.lang.Long">
select count(*) from test
<where>
<if test="id != null and id != ''">
and id = #{id}
</if>
</where>
</select>
</mapper>
@Service
public class TestService {
@Resource
private TestMapper testMapper;
public Long queryForMybatis(Test testDO){
return testMapper.queryForMybatis(testDO);
}
}
- mapper文件路径指向
@MapperScan("***.vainycos.dao")
@SpringBootApplication
public class MypgApplication {
public static void main(String[] args) {
SpringApplication.run(MypgApplication.class, args);
}
}
整合mybatis-plus
- 引入mybatis-plus的maven依赖
<dependency>
<groupId>***.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
- 改造mapper接口
public interface TestMapper extends BaseMapper<Test> {
Long queryForMybatis(Test testDO);
}
- service使用mybatis-plus写法实现
public Long queryForMybatisPlus(Test testDO){
Integer resultCount = testMapper.selectCount(new LambdaQueryWrapper<Test>()
.eq(testDO.getId() != null, Test::getId, testDO.getId())
);
return resultCount.longValue();
}
- controller测试
@GetMapping("/queryForMybatisPlus")
public Long queryForMybatisPlus(Test testDO){
return testService.queryForMybatisPlus(testDO);
}
参考资料:
- SpringBoot 整合 pgSQL
- SpringBoot集成MyBatis-yml自动化配置原理详解
- Spring Boot 整合 MyBatis-Plus