iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 21
1
Mobile Development

Android 開發經驗三十天系列 第 21

[Android 開發經驗三十天+Spring Boot]D21一Spring Boot mysql insatall 環境配置

  • 分享至 

  • twitterImage
  •  

睡夢中被敲醒 我還是不確定 怎會有熱血素材剛好寫鐵人文。

因為小畫家還是會用到資料庫內容,加上之後想做成線上的你畫我猜所以今天就先介紹這個吧

tags: 鐵人賽 Templates

好,今天來介紹關於spring boot 的 CRUD 跟 連接MYSQL

1.安裝MYSQL
文章用的版本是8
https://dev.mysql.com/downloads/installer/

可以到這裡裝mysql8,根據步驟做,並且設定好帳號密碼後

打開command line 輸入 mysql -u帳號 -p密碼

2.創資料庫
CREATE DATABASE testdatabase;
mydb是資料庫名稱
想進去資料庫可以用

use 資料庫名稱;

3.在aplicationproperties配置

spring.datasource.url=jdbc:mysql://localhost:3306/testdatabase
spring.datasource.username=mysql帳號
spring.datasource.password=mysql密碼
spring.jpa.hibernate.ddl-auto=create
#spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect

spring.jpa.hibernate.ddl-auto
是代表資料庫的方式,create是每次重創,update是已經有創了代表是更新

pom.xml

  <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

5.model
UserModel class

@Entity是在前面有說到的DI標示,是告訴spring說他是數據模型層
@Table 是映射到的資料表名稱
@Column 代表映射的資料欄位名稱
@Id 代表他是ID
@GeneratedValue(strategy = GenerationType.AUTO) ID自動增加

@Entity
@Table(name = "usertable")
public class UserModel {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id")
    private Integer id;
    @Column(name="username")
    private String username;
    @Column(name="password")
    private String password;
    @Column(name="email")
    private String email;

    public UserModel(){

    }

    public UserModel(Integer id, String username, String password, String email) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
    }

    
    public Integer getId(){
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

6.mysql workbench

在安裝mysql的時候應該也會裝workbench

點入


按紅色的點進去,黃色框框是sql語法跟資料庫結構
橘色框框是更新


上一篇
[Android 開發經驗三十天+Spring Boot]D20一Spring Boot AWS EC2教學
下一篇
[Android 開發經驗三十天+Spring Boot]D21一Spring Boot mysql CRUD
系列文
Android 開發經驗三十天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言