x213212
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# 搭建Spring data jpa 數據層 在開始之前要先了解一下目錄架構擺放位置,上一次 mybatis 數據層篇,位置沒有照規定擺,這次來以 Spring MVC架構 來進行擺放,mybatis 也可以這樣擺看看 ,今天是以 Spring Data Jpa 來集合 數據層 # DAO層,Service層,Controller層、View層詳解 * MVC ![](https://i.imgur.com/firxHSC.png) * Spring MVC ![](https://i.imgur.com/cew4RZv.png) 差不多像上述的圖,懶的話 XD * 1、Dao層 Dao層主要是做資料持久層的工作,負責與資料庫進行聯絡的一些任務都封裝在此,Dao層的設計首先是設計Dao的介面,然後在Spring的配置檔案中定義此介面的實現類,然後就可在模組中呼叫此介面來進行資料業務的處理,而不用關心此介面的具體實現類是哪個類,顯得結構非常清晰,Dao層的資料來源配置,以及有關資料庫連線的引數都在Spring的配置檔案中進行配置。 * 2、Service層 Service層主要負責業務模組的邏輯應用設計。同樣是首先設計介面,再設計其實現的類,接著再Spring的配置檔案中配置其實現的關聯。這樣我們就可以在應用中呼叫Service介面來進行業務處理。Service層的業務實現,具體要呼叫到已定義的Dao層的介面,封裝Service層的業務邏輯有利於通用的業務邏輯的獨立性和重複利用性,程式顯得非常簡潔。 * 3、Controller層 Controller層負責具體的業務模組流程的控制,在此層裡面要呼叫Service層的介面來控制業務流程,控制的配置也同樣是在Spring的配置檔案裡面進行,針對具體的業務流程,會有不同的控制器,我們具體的設計過程中可以將流程進行抽象歸納,設計出可以重複利用的子單元流程模組,這樣不僅使程式結構變得清晰,也大大減少了程式碼量。 * 4、View層 View層與控制層結合比較緊密,需要二者結合起來協同工作。View層主要負責網頁前臺的Jsp頁面的表示。 View 方面 就是 使用者觸發 Controller,然後 Controller 處理完資料再填入 View 這邊可以暫時理解為使用者觸發 Restful Api ``` http://localhost:8080/users/findall http://localhost:8080/users/a/2 ``` 所以以上述例子觸發的流程就是 ![](https://i.imgur.com/EJa2sDV.png) # 新建專案 ![](https://i.imgur.com/OldUlke.png) # 目錄擺放位置 ![](https://i.imgur.com/riAA3mu.png) # 新增 配置檔案 application.properties ``` spring.datasource.url = jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC spring.datasource.username = root spring.datasource.password = root # Keep the connection alive if idle for a long time (needed in production) spring.datasource.testWhileIdle = true spring.datasource.validationQuery = SELECT 1 # 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 # Use spring.jpa.properties.* for Hibernate native properties (the prefix is # stripped before adding them to the entity manager) # The SQL dialect makes Hibernate generate better SQL for the chosen database spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect ``` # 新建 Entity 此資料庫對應的是 上一次的 mybatis 資料庫結構 ```java package com.example.demo.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = " users", catalog = "mybatis") public class UserEntity { @Id @Column (name = "id") @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column (name = "name") private String Name ; @Column (name = "age") private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return Name; } public void setName(String name) { Name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } ``` # 新增 Dao 層 ``` java package com.example.demo.dao; import java.util.List; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import org.springframework.web.bind.annotation.PathVariable; import com.example.demo.entity.UserEntity; @Repository public interface UsersDao extends JpaRepository <UserEntity,Integer>{ } ``` # 新增 Service 層 interface ```java package com.example.demo.service; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import com.example.demo.entity.UserEntity; public interface IUserService { public List<UserEntity> findAllUser(); public UserEntity getEmplpyee(Integer employeeId); } ``` # 新增 Service implements ```java package com.example.demo.service.impl; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.PathVariable; import com.example.demo.dao.UsersDao; import com.example.demo.entity.UserEntity; import com.example.demo.service.IUserService; @Service public class UserServiceImpl implements IUserService { @Autowired private UsersDao userDao; @Override public List<UserEntity> findAllUser() { // TODO Auto-generated method stub return userDao.findAll(); } @Override public UserEntity getEmplpyee(Integer employeeId) { // TODO Auto-generated method stub return userDao.findById(employeeId).get(); } } ``` # 新增 Controller ``` java package com.example.demo.web.controller; import java.util.ArrayList; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.dao.UsersDao; import com.example.demo.entity.UserEntity; import com.example.demo.service.IUserService; @RestController @RequestMapping("/users") public class UserController { @Autowired IUserService userService; @GetMapping("/findall") public List<UserEntity> getAll() { return userService.findAllUser(); } @GetMapping("/a/{id}") public UserEntity getEmplpyee(@PathVariable(name = "id") Integer employeeId) { return userService.getEmplpyee(employeeId); } } ``` # Restful api ```URL http://localhost:8080/users/findall http://localhost:8080/users/a/2 ``` # 執行結果 ![](https://i.imgur.com/gOterxE.png) ![](https://i.imgur.com/FP9f5PA.png)

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully