數據源管理 | PostgreSQL環境整合,JSON類型應用

一、PostgreSQL簡介

1、和MySQL的比較

PostgreSQL是一個功能強大的且開源關係型數據庫系統,在網上PostgreSQL和MySQL一直有大量的對比分析。大多從性能,開源協議,SQL標準,開發難度等去比較,只要有比較就會有差距和差異,看看就好。

絮叨一句:編程世界裡的對比是一直存在的,但是無論對比結果如何,當業務需要的時候,該用還是要用。MySQL和PostgreSQL對比很少佔上風,但是MySQL在國內的使用依舊廣泛。

2、PostgreSQL特性

  • 多副本同步複製,滿足金融級可靠性要求;
  • 支持豐富的數據類型,除了常見基礎的,還包括文本,圖像,聲音,視頻,JSON等;
  • 自帶全文搜索功能,可以簡化搜索功能實現流程;
  • 高效處理圖結構, 輕鬆實現"朋友的朋友的朋友"關係類型;
  • 地理信息處理擴展,支持地圖尋路相關業務;

二、開發環境整合

1、基礎依賴

導入依賴包,版本會自動加載。本案例加載的是42.2.6版本號。

<code>

<

dependency

>

<

groupId

>

org.postgresql

groupId

>

<

artifactId

>

postgresql

artifactId

>

dependency

>

/<code>

2、核心配置文件

這裡使用Druid連接池管理。

<code>

spring

:

datasource

:

type

: com.alibaba.druid.pool.DruidDataSource

druid

:

driverClassName

: org.postgresql.Driver

url

:

jdbc

:

postgresql

:

username

: root01

password

:

123456

/<code>

3、連接池配置

<code> 

public

class

DruidConfig

{

private

String dbUrl;

private

String username;

private

String password;

private

String driverClassName;

public

DruidDataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(dbUrl); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName);

return

datasource; } }/<code>

4、持久層配置

基於mybatis相關組件,在用法上和MySQL環境整合基本一致。

<code>

mybatis-plus:

mapper-locations:

classpath*:/mapper/**/*.xml

typeAliasesPackage:

com.post.gresql.*.entity

global-config:

db-config:

id-type:

AUTO

field-strategy:

NOT_NULL

logic-delete-value:

-1

logic-not-delete-value:

0

banner:

false

configuration:

map-underscore-to-camel-case:

true

cache-enabled:

false

call-setters-on-nulls:

true

jdbc-type-for-null:

'null'

/<code>

5、基礎測試案例

提供一個數據查詢,寫入,分頁查的基礎使用案例。

<code> 
 

public

class

UserController

{

private

UserService userService ;

public

UserEntity selectById (Integer id){

return

userService.selectById(id) ; }

public

Integer insert (UserEntity userEntity){

return

userService.insert(userEntity) ; }

public

PageInfo pageQuery ( int page){ int pageSize =

3

;

return

userService.pageQuery(page,pageSize) ; } }/<code>

三、JSON類型使用

PostgreSQL支持JSON數據類型格式,但是在用法上與一般數據類型有差異。

1、Json表字段創建

這裡字段user_list為JSON類型,存儲場景第一批用戶有哪些,第二批用戶有哪些,依次類推。

<code>

CREATE

TABLE

pq_user_json (

ID

INT

NOT

NULL

, title

VARCHAR

(

32

)

NOT

NULL

, user_list

json

NOT

NULL

, create_time

TIMESTAMP

(

6

)

DEFAULT

CURRENT_TIMESTAMP

,

CONSTRAINT

"user_json_pkey"

PRIMARY

KEY

(

"id"

) );/<code>

2、類型轉換器

定義一個數據庫與實體對象的轉換器,主要就是JSON數據和Java對象的轉換。

<code> ({Object

.

class

})

public

class

JsonTypeHandler

extends

BaseTypeHandler

<

Object

>

{

private

static

final

PGobject jsonObject =

new

PGobject();

public

void

setNonNullParameter

(PreparedStatement ps,

int

i, Object parameter, JdbcType jdbcType)

throws

SQLException

{ jsonObject.setType(

"json"

); jsonObject.setValue(parameter.toString()); ps.setObject(i, jsonObject); }

public

Object

getNullableResult

(ResultSet rs, String columnName)

throws

SQLException

{

return

JSON.parseObject(rs.getString(columnName), Object

.

class

)

; }

public

Object

getNullableResult

(ResultSet rs,

int

columnIndex)

throws

SQLException

{

return

JSON.parseObject(rs.getString(columnIndex), Object

.

class

)

; }

public

Object

getNullableResult

(CallableStatement cs,

int

columnIndex)

throws

SQLException

{

return

JSON.parseObject(cs.getString(columnIndex), Object

.

class

)

; } }/<code>

3、調用方法

指定字段的映射類型typeHandler即可。

<code>

<

mapper

namespace

=

"com.post.gresql.mapper.UserJsonMapper"

>

<

insert

id

=

"addUserJson"

parameterType

=

"com.post.gresql.entity.UserJsonEntity"

>

INSERT INTO pq_user_json (id,title,user_list,create_time) VALUES (#{id}, #{title}, #{userList, typeHandler=com.post.gresql.config.JsonTypeHandler}, #{createTime})

insert

>

mapper

>

/<code>

4、JSON格式測試

JSON格式數據入庫,出庫查詢。

<code> 

public

class

UserJsonController {

private

UserJsonService userJsonService ; (

"/addUserJson"

)

public

boolean

addUserJson (){ List userEntities =

new

ArrayList<>() ; UserEntity userEntity1 =

new

UserEntity(

1

,

"LiSi"

,

22

,

new

Date

()); UserEntity userEntity2 =

new

UserEntity(

2

,

"WangWu"

,

23

,

new

Date

()); userEntities.add(userEntity1); userEntities.add(userEntity2); UserJsonEntity userJsonEntity =

new

UserJsonEntity(); userJsonEntity.setId(

1

); userJsonEntity.setTitle(

"第一批名單"

); userJsonEntity.setUserList(

JSON

.toJSONString(userEntities)); userJsonEntity.setCreateTime(

new

Date

());

return

userJsonService.addUserJson(userJsonEntity) ; } (

"/findUserJson"

)

public

List findUserJson ( (

"id"

) Integer id){ UserJsonEntity userJsonEntity = userJsonService.findUserJson(id) ;

return

JSON

.parseArray(userJsonEntity.getUserList(),UserEntity.class) ; } }/<code>

推薦關聯閱讀:

數據源管理 | 基於JDBC模式,適配和管理動態數據源

數據源管理 | 動態權限校驗,表結構和數據遷移流程

數據源管理 | 主從庫動態路由,AOP模式讀寫分離

數據源管理 | 關係型分庫分表,列式庫分佈式計算

專欄

微服務架構實戰

作者:知了一笑

9.9幣

10人已購

查看


數據源管理 | PostgreSQL環境整合,JSON類型應用


分享到:


相關文章: