摘要:移動(dòng)易后臺(tái)實(shí)現(xiàn)外部數(shù)據(jù)庫(kù)連接要實(shí)現(xiàn)外置數(shù)據(jù)庫(kù),即上層開發(fā)人員不關(guān)心下層數(shù)據(jù)庫(kù)的實(shí)現(xiàn),在項(xiàng)目中需要針對(duì)不同數(shù)據(jù)庫(kù)修改文件以及在項(xiàng)目中添加依賴包。本文主要介紹移動(dòng)易后臺(tái)如何實(shí)現(xiàn)同不同數(shù)據(jù)源的連接,數(shù)據(jù)源包括,。
1、移動(dòng)易后臺(tái)實(shí)現(xiàn)外部數(shù)據(jù)庫(kù)連接
要實(shí)現(xiàn)外置數(shù)據(jù)庫(kù),即上層開發(fā)人員不關(guān)心下層數(shù)據(jù)庫(kù)的實(shí)現(xiàn),在Spring boot項(xiàng)目 中需要針對(duì)不同數(shù)據(jù)庫(kù)修改application.properties文件以及在項(xiàng)目中添加依賴包 。本文主要介紹移動(dòng)易后臺(tái)如何實(shí)現(xiàn)同不同數(shù)據(jù)源的連接,數(shù)據(jù)源包括MySQL,Oracle,MSSQL,PostgreSQL。
移動(dòng)易官方網(wǎng)站
2、前期準(zhǔn)備Java 8
熟悉Maven項(xiàng)目
熟悉Spring boot
數(shù)據(jù)及驅(qū)動(dòng)包如下表(數(shù)據(jù)庫(kù)安裝及配置見官網(wǎng)教程)
數(shù)據(jù)庫(kù) | 版本 | JDBC驅(qū)動(dòng)包 | 下載地址 |
---|---|---|---|
MySQL | 64位 MySQL14.14 | mysql-connector-java(5.1.41) | download |
Oracle | 64位 Oracle 11g 11.2.0.1.0 | ojdbc14(10.2.03) | download |
MSSQL | 64位 Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 | sqljdbc4(4.0) | download |
PostgreSQL | 64位 PostgreSQL 9.6 | postgresql(9.4.1208-jdbc42-atlassian-hosted) | download |
git clone https://git.oschina.net/sectong/yidongyi.git3.2、將下載的項(xiàng)目import至eclipse中
- file->import->git->existing local repository
- 項(xiàng)目結(jié)構(gòu)如下3.3、右擊項(xiàng)目team->switch to ->new branch針對(duì)不同的數(shù)據(jù)庫(kù)創(chuàng)建不同的分支,如下所示: 4.2.3、接著在application.properties 文件夾中添加Oracle數(shù)據(jù)庫(kù)的配置信息
spring.datasource.url = jdbc:oracle:thin:@localhost:1521:ORCL spring.datasource.username=system spring.datasource.password=oracle4.2.4、最后在Oracle數(shù)據(jù)庫(kù)中創(chuàng)建相應(yīng)表并添加約束,如下所示:
--創(chuàng)建用戶表-- CREATE TABLE users ( id NUMBER(11) NOT NULL, username VARCHAR(45) NOT NULL, password VARCHAR(100) NOT NULL, image VARCHAR(200) DEFAULT"", enabled VARCHAR(45) DEFAULT "1" ); --為用戶表添加約束-- ALTER TABLE users ADD CONSTRAINT PK_ID PRIMARY KEY(id) ADD CONSTRAINT UNI_NAME UNIQUE(username); --為用戶表id字段創(chuàng)建序遞增序列-- CREATE SEQUENCE userid_seq START WITH 12 INCREMENT BY 1; --為用戶表添加觸發(fā)器實(shí)現(xiàn)id字段自增-- CREATE TRIGGER userid_trigger BEFORE INSERT ON users FOR EACH ROW BEGIN SELECT userid_seq.nextval into :new.id FROM dual; END; --創(chuàng)建管理員表-- CREATE TABLE authorities ( id NUMBER(11) NOT NULL, username VARCHAR(45) NOT NULL, authority VARCHAR(45) NOT NULL ); --為管理員表添加約束-- ALTER TABLE authorities ADD CONSTRAINT PK_AUID PRIMARY KEY(id); --為管理員表id字段創(chuàng)建遞增序列-- CREATE SEQUENCE authorityid_seq START WITH 14 INCREMENT BY 1; --為管理員表設(shè)置觸發(fā)器-- CREATE TRIGGER authorityid_trigger BEFORE INSERT ON authorities FOR EACH ROW BEGIN SELECT authorityid_seq.nextval into :new.id FROM dual; END; --創(chuàng)建news表-- CREATE TABLE news ( id NUMBER(11) NOT NULL PRIMARY KEY, title VARCHAR(45) NOT NULL, img VARCHAR(45) NOT NULL, content VARCHAR(200), datetime DATE );4.3、MSSQL 數(shù)據(jù)源的配置 4.3.1、首先添加依賴包(這里用的是sqljdbc4 版本4.0),雙擊pom.xml文件,添加如下依賴:
4.3.2、接著在application.properties 文件中添加對(duì)數(shù)據(jù)源的配置,如下:com.microsoft.sqlserver sqljdbc4 4.0
spring.datasource.url=jdbc:sqlserver://localhost:1433;DatabaseName=yidongyi; spring.datasource.username=use_c spring.datasource.password=1234564.3.3、最后在MSSQL中創(chuàng)建表以及約束:
--創(chuàng)建authorities表-- USE [yidongyi] GO /****** Object: Table [dbo].[authorities] Script Date: 07/03/2017 13:58:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[authorities]( [id] [int] IDENTITY(14,1) NOT NULL, [username] [varchar](50) NOT NULL, [authority] [varchar](50) NOT NULL, CONSTRAINT [PK_authorities] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO --創(chuàng)建users表-- USE [yidongyi] GO /****** Object: Table [dbo].[users] Script Date: 07/03/2017 13:59:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[users]( [id] [int] IDENTITY(12,1) NOT NULL, [username] [varchar](100) NOT NULL, [password] [varchar](300) NOT NULL, [image] [varchar](200) NOT NULL, [enabled] [varchar](100) NOT NULL, CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [dbo].[users] ADD CONSTRAINT [DF_users_image] DEFAULT ("") FOR [image] GO ALTER TABLE [dbo].[users] ADD CONSTRAINT [DF_users_enabled] DEFAULT ("1") FOR [enabled] GO --創(chuàng)建news表-- USE [yidongyi] GO /****** Object: Table [dbo].[news] Script Date: 07/03/2017 14:00:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[news]( [id] [int] NOT NULL, [title] [varchar](50) NOT NULL, [img] [varchar](50) NOT NULL, [content] [varchar](200) NOT NULL, [datetime] [date] NOT NULL, CONSTRAINT [PK_news] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO4.4、PostgreSQL數(shù)據(jù)源配置 4.4.1、首先在pom.xml文件中添加PostgreSQL的依賴包(這里用的是postgresql 版本9.4.1208-jdbc42-atlassian-hosted),如下
4.4.2、 接著在application.properties 文件夾下添加對(duì)PostgreSQL數(shù)據(jù)源的配置,如下:postgresql postgresql 9.4.1208-jdbc42-atlassian-hosted
spring.datasource.url = jdbc:postgresql://localhost:5432/yidongyi spring.datasource.username=postgres spring.datasource.password=1234564.4.3、最后在PostgreSQL數(shù)據(jù)庫(kù)中創(chuàng)建表以及約束:
--創(chuàng)建users表-- CREATE TABLE public.users ( id integer NOT NULL DEFAULT nextval("users_id_seq"::regclass), image text COLLATE pg_catalog."default" NOT NULL DEFAULT ""::text, password text COLLATE pg_catalog."default" NOT NULL, username text COLLATE pg_catalog."default" NOT NULL, enabled text COLLATE pg_catalog."default" NOT NULL DEFAULT 1, CONSTRAINT users_pkey PRIMARY KEY (id) ) WITH ( OIDS = FALSE ) TABLESPACE pg_default; ALTER TABLE public.users OWNER to postgres; --創(chuàng)建authorities表-- CREATE TABLE public.authorities ( id integer NOT NULL DEFAULT nextval("authorities_id_seq"::regclass), username text COLLATE pg_catalog."default" NOT NULL, authority text COLLATE pg_catalog."default" NOT NULL, CONSTRAINT authorities_pkey PRIMARY KEY (id) ) WITH ( OIDS = FALSE ) TABLESPACE pg_default; ALTER TABLE public.authorities OWNER to postgres; --創(chuàng)建news表-- CREATE TABLE public.news ( id integer NOT NULL DEFAULT nextval("news_id_seq"::regclass), title text COLLATE pg_catalog."default" NOT NULL, img text COLLATE pg_catalog."default" NOT NULL, content text COLLATE pg_catalog."default" NOT NULL, datetime date NOT NULL, CONSTRAINT news_pkey PRIMARY KEY (id) ) WITH ( OIDS = FALSE ) TABLESPACE pg_default; ALTER TABLE public.news OWNER to postgres;4.5、最終效果 4.5.1、 MysSQL 4.5.2、 Oracle 4.5.3、 MSSQL 4.5.4、 PostgreSQL
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/67452.html
摘要:簡(jiǎn)單概述本項(xiàng)目為移動(dòng)易開源組合套件,包含后臺(tái)服務(wù)端,提供了一個(gè)基礎(chǔ)的后臺(tái)框架前端,提供了一個(gè)與后端連接的演示。移動(dòng)易是由移動(dòng)互聯(lián)網(wǎng)愛好人士自發(fā)建立的一個(gè)開源社區(qū),主要產(chǎn)品是移動(dòng)易開源組合套件,包含移動(dòng)后端服務(wù)器,移動(dòng)前端混合。 簡(jiǎn)單概述 本項(xiàng)目為移動(dòng)易開源APP組合套件,包含后臺(tái)服務(wù)端,提供了一個(gè)基礎(chǔ)的APP后臺(tái)框架;前端APP,提供了一個(gè)與后端連接的演示APP。 移動(dòng)易是由移動(dòng)互聯(lián)網(wǎng)...
摘要:什么是移動(dòng)易開源組合套件本項(xiàng)目為移動(dòng)易開源組合套件,包含后臺(tái)服務(wù)端,提供了一個(gè)基礎(chǔ)的后臺(tái)框架前端,提供了一個(gè)與后端連接的演示。公開課視頻和資料下載騰訊課堂在線學(xué)習(xí)資料視頻截圖討論社區(qū)群 什么是移動(dòng)易開源APP組合套件? 本項(xiàng)目為移動(dòng)易開源APP組合套件,包含后臺(tái)服務(wù)端,提供了一個(gè)基礎(chǔ)的APP后臺(tái)框架;前端APP,提供了一個(gè)與后端連接的演示APP。 為什么要有公開課? 初學(xué)者,剛剛開始...
閱讀 3724·2021-11-24 10:23
閱讀 2771·2021-09-06 15:02
閱讀 1274·2021-08-23 09:43
閱讀 2351·2019-08-30 15:44
閱讀 3045·2019-08-30 13:18
閱讀 779·2019-08-23 16:56
閱讀 1743·2019-08-23 16:10
閱讀 536·2019-08-23 15:08