欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

數(shù)據(jù)源管理|PostgreSQL環(huán)境整合,JSON類型應用

本文源碼: GitHub·點這里 || GitEE·點這里

創(chuàng)新互聯(lián)專注于云霄網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供云霄營銷型網(wǎng)站建設,云霄網(wǎng)站制作、云霄網(wǎng)頁設計、云霄網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)服務,打造云霄網(wǎng)絡公司原創(chuàng)品牌,更為您提供云霄網(wǎng)站排名全網(wǎng)營銷落地服務。

一、PostgreSQL簡介

1、和MySQL的比較

PostgreSQL是一個功能強大的且開源關系型數(shù)據(jù)庫系統(tǒng),在網(wǎng)上PostgreSQL和MySQL一直有大量的對比分析。大多從性能,開源協(xié)議,SQL標準,開發(fā)難度等去比較,只要有比較就會有差距和差異,看看就好。

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

2、PostgreSQL特性

  • 多副本同步復制,滿足金融級可靠性要求;
  • 支持豐富的數(shù)據(jù)類型,除了常見基礎的,還包括文本,圖像,聲音,視頻,JSON等;
  • 自帶全文搜索功能,可以簡化搜索功能實現(xiàn)流程;
  • 高效處理圖結構, 輕松實現(xiàn)”朋友的朋友的朋友”關系類型;
  • 地理信息處理擴展,支持地圖尋路相關業(yè)務;

二、開發(fā)環(huán)境整合

1、基礎依賴

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

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>

2、核心配置文件

這里使用Druid連接池管理。

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driverClassName: org.postgresql.Driver
      url: jdbc:postgresql://127.0.0.1:5432/db_01
      username: root01
      password: 123456

3、連接池配置

@Configuration
public class DruidConfig {
    @Value("${spring.datasource.druid.url}")
    private String dbUrl;
    @Value("${spring.datasource.druid.username}")
    private String username;
    @Value("${spring.datasource.druid.password}")
    private String password;
    @Value("${spring.datasource.druid.driverClassName}")
    private String driverClassName;
    @Bean
    public DruidDataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);
        return datasource;
    }
}

4、持久層配置

基于mybatis相關組件,在用法上和MySQL環(huán)境整合基本一致。

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'

5、基礎測試案例

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

@Api(value = "UserController")
@RestController
public class UserController {
    @Resource
    private UserService userService ;
    @GetMapping("/selectById")
    public UserEntity selectById (Integer id){
        return userService.selectById(id) ;
    }
    @PostMapping("/insert")
    public Integer insert (UserEntity userEntity){
        return userService.insert(userEntity) ;
    }
    @GetMapping("/pageQuery")
    public PageInfo<UserEntity> pageQuery (@RequestParam("page") int page){
        int pageSize = 3 ;
        return userService.pageQuery(page,pageSize) ;
    }
}

三、JSON類型使用

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

1、Json表字段創(chuàng)建

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

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")
);

2、類型轉換器

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

@MappedTypes({Object.class})
public class JsonTypeHandler extends BaseTypeHandler<Object> {
    private static final PGobject jsonObject = new PGobject();
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
        jsonObject.setType("json");
        jsonObject.setValue(parameter.toString());
        ps.setObject(i, jsonObject);
    }
    @Override
    public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return JSON.parseObject(rs.getString(columnName), Object.class);
    }
    @Override
    public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return JSON.parseObject(rs.getString(columnIndex), Object.class);
    }
    @Override
    public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return JSON.parseObject(cs.getString(columnIndex), Object.class);
    }
}

3、調用方法

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

<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>

4、JSON格式測試

JSON格式數(shù)據(jù)入庫,出庫查詢。

@RestController
public class UserJsonController {
    @Resource
    private UserJsonService userJsonService ;
    @GetMapping("/addUserJson")
    public boolean addUserJson (){
        List<UserEntity> 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) ;
    }
    @GetMapping("/findUserJson")
    public List<UserEntity> findUserJson (@RequestParam("id") Integer id){
        UserJsonEntity userJsonEntity = userJsonService.findUserJson(id) ;
        return JSON.parseArray(userJsonEntity.getUserList(),UserEntity.class) ;
    }
}

四、源代碼地址

GitHub·地址
https://github.com/cicadasmile/data-manage-parent
GitEE·地址
https://gitee.com/cicadasmile/data-manage-parent

推薦閱讀:數(shù)據(jù)管理

序號標題
01 數(shù)據(jù)源管理:主從庫動態(tài)路由,AOP模式讀寫分離
02 數(shù)據(jù)源管理:基于JDBC模式,適配和管理動態(tài)數(shù)據(jù)源
03 數(shù)據(jù)源管理:動態(tài)權限校驗,表結構和數(shù)據(jù)遷移流程
04 數(shù)據(jù)源管理:關系型分庫分表,列式庫分布式計算

文章題目:數(shù)據(jù)源管理|PostgreSQL環(huán)境整合,JSON類型應用
文章轉載:http://chinadenli.net/article22/ipchjc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計公司網(wǎng)站排名建站公司網(wǎng)站維護響應式網(wǎng)站網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設公司