Spring Boot新增MySQL資料庫及JPA實例的範例程式碼分享
這篇文章主要介紹了Spring Boot 新增MySQL資料庫及JPA,小編覺得蠻不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧
最近在學習Spring Boot,繼續前面的學習,這次我們加入MySQL資料庫和JPA。
設定:
pom.xml檔案
<!-- 添加Mysql和JPA--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
在Application.properties(在resource資料夾下新建,進行設定)檔案中新增數據進行設定:
spring.datasource.url = jdbc:mysql://localhost:3306/spring_boot spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver # Specify the DBMS spring.jpa.database = MYSQL # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # stripped before adding them to the entity manager) spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
User類別
package com.seawater.bean; import javax.persistence.*; import javax.validation.constraints.NotNull; /** * Created by zhouhs on 2016/12/30. */ @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private int age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
UserController
package com.seawater.controller; import com.seawater.Dao.UserDao; import com.seawater.bean.User; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * Created by zhouhs on 2016/12/30. */ @RestController @RequestMapping(value = "/user") @Api(description = "用户") public class UserController { @Resource UserDao userDAO; @ApiOperation(value = "添加用户") @ApiImplicitParams({ @ApiImplicitParam(name = "name" , value = "name" , paramType = "query" , required = true ), @ApiImplicitParam(name = "age" , value = "age" , paramType = "query" , required = true ) }) @RequestMapping(value = "/addUser" , method = RequestMethod.POST) public String addUser(@RequestParam(value = "name") String name,@RequestParam(value = "age") int age){ User user = new User(); user.setName(name); user.setAge(age); userDAO.save(user); return "add user success !"; } @ApiOperation(value = "查找用户") @ApiImplicitParam(name = "id" , value = "id" , paramType = "query" , required = true , dataType = "int") @RequestMapping(value = "/findById" , method = RequestMethod.POST) public String findById(@RequestParam(value = "id") Long id){ User user = userDAO.findById(id); if(user == null){ return "error"; }else{ return "name:" + user.getName() + " , age:" + user.getAge(); } } @ApiOperation(value = "查询所有用户") @RequestMapping(value = "/findAll" , method = RequestMethod.POST) public Iterable findAll(){ Iterable<User> userList = userDAO.findAll(); return userList; } @ApiOperation(value = "删除用户") @ApiImplicitParam(name = "id" , value = "id" , paramType = "query" , required = true , dataType = "int") @RequestMapping(value = "/deleteById" , method = RequestMethod.POST) public String deleteById(@RequestParam(value = "id") Long id){ userDAO.delete(id); return "delete success !"; } }
資料表(id定義為Integer):
#UserDao:
package com.seawater.Dao; import com.seawater.bean.User; import org.springframework.data.repository.CrudRepository; /** * Created by zhouhs on 2016/12/30. */ public interface UserDao extends CrudRepository<User, Long> { public User findById(Long id); }
然後啟動專案:存取localhost:8081/swagger-ui.html
#結果:
方法我就不一一操作了。
以上是Spring Boot新增MySQL資料庫及JPA實例的範例程式碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

MySQL適合初學者使用,因為它安裝簡單、功能強大且易於管理數據。 1.安裝和配置簡單,適用於多種操作系統。 2.支持基本操作如創建數據庫和表、插入、查詢、更新和刪除數據。 3.提供高級功能如JOIN操作和子查詢。 4.可以通過索引、查詢優化和分錶分區來提升性能。 5.支持備份、恢復和安全措施,確保數據的安全和一致性。

Navicat本身不存儲數據庫密碼,只能找回加密後的密碼。解決辦法:1. 檢查密碼管理器;2. 檢查Navicat的“記住密碼”功能;3. 重置數據庫密碼;4. 聯繫數據庫管理員。

使用 Navicat Premium 創建數據庫:連接到數據庫服務器並輸入連接參數。右鍵單擊服務器並選擇“創建數據庫”。輸入新數據庫的名稱和指定字符集和排序規則。連接到新數據庫並在“對象瀏覽器”中創建表。右鍵單擊表並選擇“插入數據”來插入數據。

MySQL是一個開源的關係型數據庫管理系統。 1)創建數據庫和表:使用CREATEDATABASE和CREATETABLE命令。 2)基本操作:INSERT、UPDATE、DELETE和SELECT。 3)高級操作:JOIN、子查詢和事務處理。 4)調試技巧:檢查語法、數據類型和權限。 5)優化建議:使用索引、避免SELECT*和使用事務。

Navicat for MariaDB 無法直接查看數據庫密碼,因為密碼以加密形式存儲。為確保數據庫安全,有三個方法可重置密碼:通過 Navicat 重置密碼,設置複雜密碼。查看配置文件(不推薦,風險高)。使用系統命令行工具(不推薦,需要對命令行工具精通)。

MySQL和SQL是開發者必備技能。 1.MySQL是開源的關係型數據庫管理系統,SQL是用於管理和操作數據庫的標準語言。 2.MySQL通過高效的數據存儲和檢索功能支持多種存儲引擎,SQL通過簡單語句完成複雜數據操作。 3.使用示例包括基本查詢和高級查詢,如按條件過濾和排序。 4.常見錯誤包括語法錯誤和性能問題,可通過檢查SQL語句和使用EXPLAIN命令優化。 5.性能優化技巧包括使用索引、避免全表掃描、優化JOIN操作和提升代碼可讀性。

可在 Navicat 中通過以下步驟新建 MySQL 連接:打開應用程序並選擇“新建連接”(Ctrl N)。選擇“MySQL”作為連接類型。輸入主機名/IP 地址、端口、用戶名和密碼。 (可選)配置高級選項。保存連接並輸入連接名稱。

在 Navicat 中執行 SQL 的步驟:連接到數據庫。創建 SQL 編輯器窗口。編寫 SQL 查詢或腳本。單擊“運行”按鈕執行查詢或腳本。查看結果(如果執行查詢的話)。
