PostgreSQL是一个开源的框架,关联式资料库资管理系统。PostgreSQL 的操作过程相似於 MySQL,不论是建表或是建使用者都满类似的。MyBatis 算是常用的开源框架,在 Spring boot 中可实现对资料库访问。
Maven 中引用
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${VERSION}</version>
</dependency>
@Configuration // 用来设定 Spring 中的环境配置
@MapperScan(basePackages = { DataBaseConfig.PACKAGE }, sqlSessionFactoryRef = "sqlSessionFactory") // 扫描定义的包并使用任何映射器注释(@Select 、@Delete)并自动选取接口
public class DataBaseConfig {
static final String PACKAGE = "cch.com.example.demo.mapper";
@Value("${db_classname}") // 可以想成是读取环境变数,并将其赋值给 classname
private String classname;
@Value("${db_url}")
private String url;
@Value("${db_username}")
private String username;
@Value("${db_password}")
private String password;
@Value("${db_maximum_pool_size}")
private String maximumPoolSize;
@Bean(name = "dataSource") // 宣告 Bean 并将其注册到 Spring 中
@Primary
public DataSource dataSource() {
// 设定 DB 的环境
Properties props = new Properties();
props.setProperty("stringtype", "unspecified");
final HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName(this.classname);
hikariConfig.setJdbcUrl(this.url);
hikariConfig.setDataSourceProperties(props);
hikariConfig.setUsername(this.username);
hikariConfig.setPassword(this.password);
hikariConfig.setMaximumPoolSize(Integer.valueOf(this.maximumPoolSize));
hikariConfig.setConnectionTestQuery("SELECT 1");
hikariConfig.setPoolName("springHikariCP");
return new HikariDataSource(hikariConfig);
}
@Bean(name = "transactionManager")
@Primary
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager((dataSource()));
}
// 定义 data source、事务管理或实体之间的映射
@Bean(name = "sqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return sessionFactoryBean.getObject();
}
}
以上是 mybatis 对资料库访问的配置和定义要被 mybatis 所扫描的 Package。下面简单的在 PostgreSQL 建表吧,这边我们透过 sql 语法建立一连串的事件,建立使用者、建立 DB、建立资料表等。
// init.sql
CREATE USER itachi WITH ENCRYPTED PASSWORD '12345678'; // 建立使用者
CREATE DATABASE employee // 建立资料表
WITH
OWNER = itachi
ENCODING = 'UTF8'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;
ALTER USER itachi WITH SUPERUSER; // 让使用者有 SUPERUSER 权限
grant all privileges on database employee to itachi; // 赋予权限(像是 SELECT、DELETE 等操作)
\c employee // 切换资料库
DROP TABLE IF EXISTS public.user; // 如果资料表 user 存在就将其删除
CREATE TABLE IF NOT EXISTS public.user ( // 建立资料表,NOT NULL 表示不能为空
id serial,
name varchar(20) NOT NULL,
age integer,
email varchar(100) NULL,
tel varchar NOT NULL,
primary key (id) // 设定主键
);
ALTER TABLE public.user OWNER TO itachi;
透过 docker-compose 进行 PostgreSQL 键置
version: '3.7'
services:
postgres:
container_name: postgres
image: postgres
restart: always
ports:
- "5432:5432"
hostname: postgres
environment:
- POSTGRES_PASSWORD=123456
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql // 把上面建立的 SQL 过程挂载置容器中,此容器就会帮我们执行我们定义的过程
- postgresql-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 30s
timeout: 10s
retries: 5
volumes:
postgresql-data:
今天就到这边,打完疫苗身体非常不舒服
<<: GitHub Action YAML - 语意解析与指令说明
>>: Day 10 : Docker基本操作 Volume篇
二分搜寻法(Binary Search) 前提,在一个已经排序完成的A阵列中 Divide : 元素...
及时生产 (JIT) 一词出现在丰田生产系统中。JIT 是一种库存管理策略,可根据需要或按需订购库存...
CTF 全名 Capture The Flag,并且分为下列几类的解题方式 解谜式(Jeopard...
小弟觉得如果只是把2019年的内容稍微修辞再贴上来实在没有太大意义纯粹再浪费大家的时间,而且错误的地...
一日客语:中文:成功 客语:ㄙㄣ3声 ㄍㄨㄥˊ siinˇ gungˊ 出现async、await可...