本文源碼:GitHub·點這裏 || GitEE·點這裏java
PostgreSQL是一個功能強大的且開源關係型數據庫系統,在網上PostgreSQL和MySQL一直有大量的對比分析。大多從性能,開源協議,SQL標準,開發難度等去比較,只要有比較就會有差距和差別,看看就好。git
絮叨一句
:編程世界裏的對比是一直存在的,可是不管對比結果如何,當業務須要的時候,該用仍是要用。MySQL和PostgreSQL對比不多佔上風,可是MySQL在國內的使用依舊普遍。github
導入依賴包,版本會自動加載。本案例加載的是42.2.6版本號。spring
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> </dependency>
這裏使用Druid鏈接池管理。sql
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
@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; } }
基於mybatis相關組件,在用法上和MySQL環境整合基本一致。數據庫
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'
提供一個數據查詢,寫入,分頁查的基礎使用案例。編程
@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) ; } }
PostgreSQL支持JSON數據類型格式,可是在用法上與通常數據類型有差別。json
這裏字段user_list爲JSON類型,存儲場景第一批用戶有哪些,第二批用戶有哪些,依次類推。mybatis
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") );
定義一個數據庫與實體對象的轉換器,主要就是JSON數據和Java對象的轉換。app
@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); } }
指定字段的映射類型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>
JSON格式數據入庫,出庫查詢。
@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
推薦閱讀:數據管理
序號 | 標題 |
---|---|
01 | 數據源管理:主從庫動態路由,AOP模式讀寫分離 |
02 | 數據源管理:基於JDBC模式,適配和管理動態數據源 |
03 | 數據源管理:動態權限校驗,表結構和數據遷移流程 |
04 | 數據源管理:關係型分庫分表,列式庫分佈式計算 |