Spring Boot ã§æ¸ç±ã®è²¸åºç¶æ³ç¢ºèªã»è²¸åºç³è«ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ )( DBããã¼ãã«ã®ä½æ )
æ¦è¦
Spring Boot ã§æ¸ç±ã®è²¸åºç¶æ³ç¢ºèªã»è²¸åºç³è«ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ )( Project ã®ä½æ ) ã®ç¶ãã§ãã
- ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å
容ã§ãã
- DBããã¼ãã«ã®ä½æ
- Doma-Gen ã«ãã entity, dao ã¯ã©ã¹ã®çæ
åç §ãããµã¤ãã»æ¸ç±
-
Gradle å¾¹åºå ¥é
Gradleå¾¹åºå ¥é 次ä¸ä»£ãã«ããã¼ã«ã«ããèªåååºç¤ã®æ§ç¯
- ä½è : 綿å¼ç¢ç£¨,é æ±ä¿¡æ´,ææ¿å©,ä»äºåä¿¡
- åºç社/ã¡ã¼ã«ã¼: ç¿æ³³ç¤¾
- çºå£²æ¥: 2014/11/05
- ã¡ãã£ã¢: 大åæ¬
- ãã®ååãå«ãããã° (5件) ãè¦ã
- ã5.6 ãã¡ã¤ã«æä½ããåç §ãã¾ããã
ç®æ¬¡
- ã¯ããã«
- 1.0.x-create-database ãã©ã³ãã®ä½æ
- ã¦ã¼ã¶ã¨ãã¼ã¿ãã¼ã¹ã®ä½æ
- ãã¼ãã«ã®ä½æ
- application.properties ã®å¤æ´
- Tomcat ã®èµ·åãåæ¢ã®ç¢ºèª
- Database tools ã®è¨å®
- Doma-Gen ã«ãã Entity ã¯ã©ã¹ãDao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®çæ
- commitãPushãPull Requestããã¼ã¸
æé
ã¯ããã«
SQL ãã¡ã¤ã«ãä½æã㦠PostgreSQL ã« DB ã¨ãã¼ãã«ãä½æãã¾ãã
ã¾ã Doma-Gen ãå©ç¨ã㦠Entity ã¯ã©ã¹ãDao ã¤ã³ã¿ã¼ãã§ã¼ã¹ãèªåçæãã¾ããksbysample-webapp-email ã®æ㯠Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ãèªåçæããå¾ãï¼ãã¡ã¤ã«ã㤠@ComponentAndAutowiredDomaConfig ã¢ããã¼ã·ã§ã³ã追å ãã¾ããããä»å㯠Gradle ã§ä»å ãã¦ã¿ã¾ãã
1.0.x-create-database ãã©ã³ãã®ä½æ
- IntelliJ IDEA 㧠1.0.x-create-database ãã©ã³ããä½æãã¾ãã
ã¦ã¼ã¶ã¨ãã¼ã¿ãã¼ã¹ã®ä½æ
SQL ãã¡ã¤ã«ãæ ¼ç´ããããã® sql ãã£ã¬ã¯ããªãããã¸ã§ã¯ãã®ã«ã¼ãç´ä¸ã«ä½æãã¾ãã
/sql ã®ä¸ã« create_database.sql ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ã³ãã³ãããã³ãããã以ä¸ã®ã³ãã³ããå®è¡ãã¦ã¦ã¼ã¶ã¨ãã¼ã¿ãã¼ã¹ãä½æãã¾ãã
C:\project-springboot\ksbysample-webapp-lending\sql>psql -U postgres postgres ã¦ã¼ã¶ postgres ã®ãã¹ã¯ã¼ã: psql (9.4.1) "help" ã§ãã«ãã表示ãã¾ã. postgres=# \i create_database.sql CREATE ROLE CREATE DATABASE postgres=# \l ãã¼ã¿ãã¼ã¹ä¸è¦§ åå | ææè | ã¨ã³ã³ã¼ãã£ã³ã° | ç §åé åº | Ctype( å¤ææ¼ç®å) | ã¢ã¯ã»ã¹æ¨© -------------+------------------+------------------+--------------------+--------------------+----------------------- ksbyemail | ksbyemail_user | UTF8 | C | C | ksbylending | ksbylending_user | UTF8 | C | C | postgres | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | template0 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres + | | | | | postgres=CTc/postgres (5 è¡) postgres=# \q C:\project-springboot\ksbysample-webapp-lending\sql>
ãã¼ãã«ã®ä½æ
/sql ã®ä¸ã« create_table.sql ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
以ä¸ã®ãã¼ãã«ã¬ã¤ã¢ã¦ãã§ãã
ã¦ã¼ã¶ãã¼ãã« ( user_info )
è«çã«ã©ã å | ç©çã«ã©ã å | 説æ |
---|---|---|
ã¦ã¼ã¶ID | user_id | |
ã¦ã¼ã¶å | username | |
ãã¹ã¯ã¼ã | password | |
ã¡ã¼ã«ã¢ãã¬ã¹ | mail_address | |
使ç¨å¯å¦ãã©ã° | enabled |
権éãã¼ãã« ( user_role )
è«çã«ã©ã å | ç©çã«ã©ã å | 説æ |
---|---|---|
ã¦ã¼ã¶æ¨©éID | role_id | |
ã¦ã¼ã¶ID | user_id | |
権é | role |
貸åºç³è«ãã¼ãã« ( lending_app )
è«çã«ã©ã å | ç©çã«ã©ã å | 説æ |
---|---|---|
貸åºç³è«ID | lending_app_id | |
ç³è«ç¶æ³ | status | 1:ä¸æä¿å, 2:æªç³è«, 3:ç³è«ä¸, 4:æ¿èªæ¸ |
ç³è«è ã¦ã¼ã¶ID | lending_user_id | |
æ¿èªè ã¦ã¼ã¶ID | approval_user_id |
貸åºç³è«æ¸ç±ãã¼ãã« ( lending_book )
è«çã«ã©ã å | ç©çã«ã©ã å | 説æ |
---|---|---|
貸åºç³è«æ¸ç±ID | lending_book_id | |
貸åºç³è«ID | lending_app_id | |
ISBN | isbn | |
æ¸ç±å | book_name | |
貸åºç¶æ³ | lending_state | èµæ¸æ¤ç´¢APIã§åå¾ãã貸åºç¶æ³ã®æååãã»ããããã |
貸åºç³è«ãã©ã° | lending_app_flg | 0:貸åºç³è«ããªã, 1:貸åºç³è«ãã |
貸åºç³è«çç± | lending_app_reason | |
æ¿èªçµæ | approval_result | 1:æ¿èª, 9:å´ä¸ |
æ¿èªçç± | approval_reason |
ã³ãã³ãããã³ãããã以ä¸ã®ã³ãã³ããå®è¡ãã¦ãã¼ãã«ãä½æãã¾ãã
C:\project-springboot\ksbysample-webapp-lending\sql>psql -U ksbylending_user ksbylending ã¦ã¼ã¶ ksbylending_user ã®ãã¹ã¯ã¼ã: psql (9.4.1) "help" ã§ãã«ãã表示ãã¾ã. ksbylending=> \i create_table.sql CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLE ksbylending=> \d ãªã¬ã¼ã·ã§ã³ã®ä¸è¦§ ã¹ãã¼ã | åå | å | ææè ----------+----------------------------------+------------+------------------ public | lending_app | ãã¼ãã« | ksbylending_user public | lending_app_lending_app_id_seq | ã·ã¼ã±ã³ã¹ | ksbylending_user public | lending_book | ãã¼ãã« | ksbylending_user public | lending_book_lending_book_id_seq | ã·ã¼ã±ã³ã¹ | ksbylending_user public | user_info | ãã¼ãã« | ksbylending_user public | user_info_user_id_seq | ã·ã¼ã±ã³ã¹ | ksbylending_user public | user_role | ãã¼ãã« | ksbylending_user public | user_role_role_id_seq | ã·ã¼ã±ã³ã¹ | ksbylending_user (8 è¡) ksbylending=> \q C:\project-springboot\ksbysample-webapp-lending\sql>
application.properties ã®å¤æ´
- src/main/resources ã®ä¸ã® application-develop.properties, application-product.properties, application-unittest.properties ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
Tomcat ã®èµ·åãåæ¢ã®ç¢ºèª
Gradle projects View ãã bootRun ã¿ã¹ã¯ãå®è¡ãã¾ããèµ·åã㦠"Started Application in ..." ã®ãã°ãåºåããããã¨ã確èªãã¾ãã
Ctrl+F2 ãæ¼ã㦠Tomcat ãåæ¢ãã¾ãã
Database tools ã®è¨å®
IntelliJ IDEA ã®ç»é¢å³å´ã«è¡¨ç¤ºããã¦ãããDatabaseããã¿ã³ãã¯ãªãã¯ããDatabase View ã表示ãã¾ãã
Database View ã®å·¦ä¸ã®ãNewããã¿ã³ãã¯ãªãã¯ããå¾ããData Sourceã->ãPostgreSQLãã¡ãã¥ã¼ãé¸æãã¾ãã
ãData Sources and Driversããã¤ã¢ãã°ã表示ããã¾ãã以ä¸ã®å¤ãå ¥åãã¾ãã
- ãDatabaseãã«
ksbylending
ãå ¥åãã¾ãã - ãUserãã«
ksbylending_user
ãå ¥åãã¾ãã - ãPasswordãã«
xxxxxxxx
ãå ¥åãã¾ãã
å ¥åå¾ããTest Connectionããã¿ã³ãã¯ãªãã¯ãã¦ä»¥ä¸ã®ãã¤ã¢ãã°ã表示ããããã¨ã確èªãã¾ãã
確èªå¾ããOKããã¿ã³ãã¯ãªãã¯ãã¦ãã¤ã¢ãã°ãéãã¾ãã
- ãDatabaseãã«
Database View ã« ksbylending ãã¼ã¿ãã¼ã¹ã®ãã¼ãã«ã表示ããããã¨ã確èªãã¾ãã
Doma-Gen ã«ãã Entity ã¯ã©ã¹ãDao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®çæ
build.gradle ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
Gradle projects View ãã gen ã¿ã¹ã¯ãå®è¡ããRun View ã« "BUILD SUCCESSFUL" ã®ãã°ãåºããã¨ã確èªãã¾ãã
src/main/java/ksbysample/webapp/lending ã®ä¸ã« dao, entity ããã±ã¼ã¸ããsrc/main/resources ã®ä¸ã« META-INF/ksbysample/webapp/lending/dao ãã£ã¬ã¯ããªãèªåçæããã¾ãã
èªåçæããã Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ãéãã¨ãã¤ã³ã¿ã¼ãã§ã¼ã¹åã®ä¸ã«
@ComponentAndAutowiredDomaConfig
ãä»å ããã¦ãã¾ãã
commitãPushãPull Requestããã¼ã¸
commit ãããã¨ãã¾ãããããCode Analysisããã¤ã¢ãã°ã表示ãããReviewããã¿ã³ãã¯ãªãã¯ããã¨å¤§éã® "SQL dialect is not configured." ã® Warning ãåºåããã¦ãã¾ãããä¿®æ£ãã¾ãã
ã©ãã§ãæ§ãã¾ããã®ã§ãã°ãããã«ã¯ãªãã¯ã㦠SQL ãã¡ã¤ã«ãéãã¾ãã
SQL ãã¡ã¤ã«ãéãã¨ä¸é¨ã« "SQL dialect is not configured." ã®ã¡ãã»ã¼ã¸ã表示ããã¦ãã¾ãã®ã§ãã¡ãã»ã¼ã¸ã®å³å´ã«ãããChange dialect to ...ããªã³ã¯ãã¯ãªãã¯ãã¾ãã
ãSQL Dialectsããã¤ã¢ãã°ãéãã¾ããããã¸ã§ã¯ãã®ã«ã¼ã㧠SQL Dialect ã "PostgreSQL" ã«è¨å®ããå¾ããOKããã¿ã³ãã¯ãªãã¯ãã¾ãã
å度 commit ãã¾ãããCode Analysisããã¤ã¢ãã°ãåºã¾ãããä»ã® Warning ã¯ç¡è¦ãã¦ãCommitããã¿ã³ãã¯ãªãã¯ãã¾ãã
GitHub 㸠Pushã1.0.x-create-database -> 1.0.x 㸠Pull Requestã1.0.x ã§ãã¼ã¸ã1.0.x-create-database ãã©ã³ããåé¤ãããã¾ãã
ã½ã¼ã¹ã³ã¼ã
create_database.sql
CREATE USER ksbylending_user PASSWORD 'xxxxxxxx'; CREATE DATABASE ksbylending OWNER ksbylending_user TEMPLATE template0 ENCODING 'UTF8' LC_COLLATE 'C' LC_CTYPE 'C';
create_table.sql
create table user_info ( user_id bigserial primary key , username varchar(32) not null , password varchar(32) not null , mail_address varchar(256) not null , enabled smallint not null default 1 ); create table user_role ( role_id bigserial primary key , user_id bigint not null references user(user_id) on delete cascade , role varchar(32) not null ); create table lending_app ( lending_app_id bigserial primary key , status varchar(1) , lending_user_id bigint not null references user(user_id) , applicant_user_id bigint not null references user(user_id) ); create table lending_book ( lending_book_id bigserial primary key , lending_app_id bigint not null references lending_app(lending_app_id) on delete cascade , isbn varchar(17) not null , book_name varchar(128) not null , lending_app_flg varchar(1) , lending_app_reason varchar(128) , lending_state varchar(16) , applicate_status varchar(1) , applicate_reason varchar(128) );
application-develop.properties, application-product.properties, application-unittest.properties
â application-develop.properties
spring.datasource.url=jdbc:log4jdbc:postgresql://localhost/ksbylending spring.datasource.username=ksbylending_user spring.datasource.password=xxxxxxxx spring.datasource.driverClassName=net.sf.log4jdbc.sql.jdbcapi.DriverSpy spring.mail.host=localhost spring.mail.port=25 spring.messages.cache-seconds=0 spring.thymeleaf.cache=false spring.velocity.cache=false
- spring.datasource.url, spring.datasource.username ã®å¤ãä¸è¨ã®ãã®ã«å¤æ´ãã¾ãã
â application-product.properties
server.tomcat.basedir=C:/webapps/ksbysample-webapp-lending spring.datasource.url=jdbc:postgresql://localhost/ksbylending spring.datasource.username=ksbylending_user spring.datasource.password=xxxxxxxx spring.datasource.driverClassName=org.postgresql.Driver spring.mail.host=localhost spring.mail.port=25 spring.thymeleaf.cache=true
- spring.datasource.url, spring.datasource.username ã®å¤ãä¸è¨ã®ãã®ã«å¤æ´ãã¾ãã
â application-unittest.properties
spring.datasource.url=jdbc:postgresql://localhost/ksbylending spring.datasource.username=ksbylending_user spring.datasource.password=xxxxxxxx spring.datasource.driverClassName=org.postgresql.Driver spring.mail.host=localhost spring.mail.port=25 spring.thymeleaf.cache=true
- spring.datasource.url, spring.datasource.username ã®å¤ãä¸è¨ã®ãã®ã«å¤æ´ãã¾ãã
build.gradle
// for Doma-Gen task gen << { def rootPackageName = 'ksbysample.webapp.lending' def daoPackagePath = 'src/main/java/ksbysample/webapp/lending/dao' def workDirPath = 'work' def workDaoDirPath = "${workDirPath}/dao" // ç¾å¨ã® Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®ããã¯ã¢ãããåå¾ãã delete "${workDirPath}" copy() { from "${daoPackagePath}" into "${workDaoDirPath}/org" } // Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ãEntity ã¯ã©ã¹ãçæãã ant.taskdef(resource: 'domagentask.properties', classpath: configurations.domaGenRuntime.asPath) ant.gen(url: 'jdbc:postgresql://localhost/ksbylending' , user: 'ksbylending_user' , password: 'xxxxxxxx' ) { entityConfig(packageName: "${rootPackageName}.entity", useListener: false) daoConfig(packageName: "${rootPackageName}.dao") sqlConfig() } // çæããã Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ãä½æ¥ç¨ãã£ã¬ã¯ããªã«ã³ãã¼ãã // @ComponentAndAutowiredDomaConfig ã¢ããã¼ã·ã§ã³ãä»å ãã copy() { from "${daoPackagePath}" into "${workDaoDirPath}/replace" filter { line -> line.replaceAll('import org.seasar.doma.Dao;', 'import ksbysample.webapp.lending.util.doma.ComponentAndAutowiredDomaConfig;\nimport org.seasar.doma.Dao;') .replaceAll('@Dao', '@Dao\n@ComponentAndAutowiredDomaConfig') } } // @ComponentAndAutowiredDomaConfig ã¢ããã¼ã·ã§ã³ãä»å ãã Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã // dao ããã±ã¼ã¸ã¸æ»ã copy() { from "${workDaoDirPath}/replace" into "${daoPackagePath}" } // å ã dao ããã±ã¼ã¸å ã«ãã£ããã¡ã¤ã«ãå ã«æ»ã copy() { from "${workDaoDirPath}/org" into "${daoPackagePath}" } // ä½æ¥ç¨ãã£ã¬ã¯ããªãåé¤ãã delete "${workDirPath}" // èªåçæãããã¡ã¤ã«ã git add ãã def grgit = org.ajoberstar.grgit.Grgit.open(dir: project.projectDir) grgit.add(patterns: ['.']) }
- gen ã¿ã¹ã¯å ã§ãèªåçæãã Dao ã¯ã©ã¹ã« @ComponentAndAutowiredDomaConfig ã¢ããã¼ã·ã§ã³ ãä»å ããããã«ãã¾ããã
å±¥æ´
2015/07/07
åççºè¡ã
2015/10/25
* 貸åºç³è«ãã¼ãã« ( lending_app ) ã®ç³è«ç¶æ³/status ã®èª¬æã "0:ä¸æä¿å, 1:ç³è«ä¸, 2:æ¿èªæ¸" â "1:ä¸æä¿å, 2:æªç³è«, 3:ç³è«ä¸, 4:æ¿èªæ¸" ã¸å¤æ´ãã¾ããã
2016/08/24
* ã¦ã¼ã¶ãã¼ãã«ã®ãã¼ãã«åã user ã¨æ¸ãã¦ããé¨åã user_info ã«ä¿®æ£ãã¾ããã