自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring boot項(xiàng)目搭建(前端到數(shù)據(jù)庫(kù),超詳細(xì)),大神勿進(jìn)!

開(kāi)發(fā) 項(xiàng)目管理
今天剛好我有空,于是,我就搞了一個(gè)從前端到后臺(tái)的用戶系統(tǒng)(demo)。用戶系統(tǒng)基本是所有項(xiàng)目中都必須有的基礎(chǔ)模塊,可大可小。

[[385395]]

前段時(shí)間,有很多小伙伴私下跟我說(shuō),想搞一個(gè)項(xiàng)目實(shí)戰(zhàn)。今天剛好我有空,于是,我就搞了一個(gè)從前端到后臺(tái)的用戶系統(tǒng)(demo)。用戶系統(tǒng)基本是所有項(xiàng)目中都必須有的基礎(chǔ)模塊,可大可小。大的需要很大一個(gè)團(tuán)隊(duì)來(lái)維護(hù),小的一個(gè)人兼職就能搞定。對(duì)于很多還未參與工作的同學(xué)來(lái)說(shuō),這個(gè)項(xiàng)目也可以練練手,說(shuō)不定,第一份工作進(jìn)去就是搞用戶模塊呢。

技術(shù)棧

Spring Boot 、Jpa 、Thymeleaf、HTML、Maven、MySQL。

對(duì)于技術(shù)棧,還不清楚的同學(xué),自行查找相關(guān)資料,至少知道個(gè)大概是干啥的。

需求功能

現(xiàn)在需要對(duì)用戶信息進(jìn)行新增、列表展示、用戶詳情展示、信息修改、信息刪除等功能。

用戶信息:姓名、密碼、手機(jī)號(hào)碼、年齡。

數(shù)據(jù)庫(kù)表

自己本地安裝MySQL,并創(chuàng)建數(shù)據(jù)庫(kù),然后在創(chuàng)建一張用戶表。

建表語(yǔ)句

  1. CREATE TABLE `user` ( 
  2.   `uid` bigint NOT NULL AUTO_INCREMENT, 
  3.   `uname` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL
  4.   `passwordvarchar(6) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL
  5.   `tel` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL
  6.   `age` int DEFAULT NULL
  7.   PRIMARY KEY (`uid`) 
  8. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; 

項(xiàng)目構(gòu)建

項(xiàng)目整體目錄

接下來(lái),我們就一步一步,把這些代碼理清楚。

整體項(xiàng)目采用的是

  1. html-->controller--->service--repository-->DB 

這也是當(dāng)前實(shí)際開(kāi)發(fā)中相對(duì)比較流行的,但為了演示,我這里就就用entity實(shí)體類就搞定了。

實(shí)際開(kāi)發(fā)中會(huì)有其他什么O之類的,比如說(shuō):DTO/VO/BO.....。有的項(xiàng)目使用的是DTO,有的可能使用的是BO等。進(jìn)入項(xiàng)目組了,按照項(xiàng)目組的開(kāi)發(fā)規(guī)范來(lái)就行了,實(shí)在不理解的話,建議你先學(xué)會(huì)模仿別人是怎么做的。

廢話不多說(shuō),直接開(kāi)干!

創(chuàng)建Spring Boot項(xiàng)目

可以使用spring官網(wǎng)給創(chuàng)建方式,地址:

https://start.spring.io/

這里也可以參考另外一篇文章:

如何快速構(gòu)建Spring Boot基礎(chǔ)項(xiàng)目?

maven依賴

在項(xiàng)目的pom.xml中添加如下配置

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
  4.     <modelVersion>4.0.0</modelVersion> 
  5.     <!-- Spring Boot 項(xiàng)目 --> 
  6.     <parent> 
  7.         <groupId>org.springframework.boot</groupId> 
  8.         <artifactId>spring-boot-starter-parent</artifactId> 
  9.         <version>2.1.6.RELEASE</version> 
  10.     </parent> 
  11.     <!-- 項(xiàng)目 maven 坐標(biāo) --> 
  12.     <groupId>com.tian.user</groupId> 
  13.     <artifactId>spring-boot-jpa-thymeleaf</artifactId> 
  14.     <version>1.0-SNAPSHOT</version> 
  15.     <!-- 項(xiàng)目名稱 --> 
  16.     <name>spring-boot-jpa-thymeleaf</name>   
  17.  
  18.     <dependencies> 
  19.         <!-- web --> 
  20.         <dependency> 
  21.             <groupId>org.springframework.boot</groupId> 
  22.             <artifactId>spring-boot-starter-web</artifactId> 
  23.         </dependency> 
  24.         <!-- thymeleaf --> 
  25.         <dependency> 
  26.             <groupId>org.springframework.boot</groupId> 
  27.             <artifactId>spring-boot-starter-thymeleaf</artifactId> 
  28.         </dependency> 
  29.         <!-- jpa --> 
  30.         <dependency> 
  31.             <groupId>org.springframework.boot</groupId> 
  32.             <artifactId>spring-boot-starter-data-jpa 
  33.             </artifactId> 
  34.         </dependency> 
  35.         <!-- MySQL --> 
  36.         <dependency> 
  37.             <groupId>mysql</groupId> 
  38.             <artifactId>mysql-connector-java</artifactId> 
  39.         </dependency> 
  40.     </dependencies> 
  41.  
  42.     <build> 
  43.         <plugins> 
  44.             <plugin> 
  45.                 <groupId>org.springframework.boot</groupId> 
  46.                 <artifactId>spring-boot-maven-plugin</artifactId> 
  47.                 <configuration> 
  48.                     <fork>true</fork> 
  49.                 </configuration> 
  50.             </plugin> 
  51.         </plugins> 
  52.     </build> 
  53. </project> 

 

 

 

 

 

主要是SpringBoot項(xiàng)目parent、web、MySQL、jpa、thymeleaf依賴,這樣就把相關(guān)的jar包給依賴進(jìn)來(lái)了。

啟動(dòng)類

 

  1. import org.springframework.boot.SpringApplication; 
  2. import org.springframework.boot.autoconfigure.SpringBootApplication; 
  3. import org.springframework.boot.builder.SpringApplicationBuilder; 
  4. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 
  5.  
  6. /** 
  7.  * @author java 后端技術(shù)全棧 
  8.  * 就是一個(gè)簡(jiǎn)單的啟動(dòng)類 
  9.  */ 
  10. @SpringBootApplication 
  11. public class Application { 
  12.  
  13.     public static void main(String[] args) { 
  14.         SpringApplication.run(Application.class, args); 
  15.     }  

注意:此時(shí)數(shù)據(jù)庫(kù)相關(guān)信息沒(méi)有配置進(jìn)項(xiàng)目里,所以,項(xiàng)目是啟動(dòng)不了的。

集成MySQL數(shù)據(jù)庫(kù)

在resource目錄下,創(chuàng)建一個(gè)application.properties文件,加入以下內(nèi)容:

  1. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 
  2. spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC 
  3. spring.datasource.username=root 
  4. spring.datasource.password=123456 

注意:如果MySQL是8.0版本,必須要配置服務(wù)時(shí)區(qū),否則會(huì)啟動(dòng)不了。

此時(shí)項(xiàng)目就可以正常啟動(dòng)了。

集成 Jpa

在application.properties中添加

  1. spring.jpa.properties.hibernate.hbm2dl.auto=create 
  2. spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect 
  3. spring.jpa.show-sql=true 

創(chuàng)建一個(gè)UserRepository

  1. package com.tian.user.repository; 
  2.  
  3. import com.tian.user.entity.User
  4. import org.springframework.data.jpa.repository.JpaRepository; 
  5. import org.springframework.data.jpa.repository.Modifying; 
  6. import org.springframework.data.jpa.repository.Query; 
  7. import org.springframework.stereotype.Repository; 
  8.  
  9. @Repository 
  10. public interface UserRepository extends JpaRepository<User, Long> {  

這樣jpa就集成就來(lái)了。

集成Thymeleaf

前面我們已經(jīng)把jar包給以來(lái)進(jìn)來(lái)了,需要在application.properties中添加

  1. spring.thymeleaf.cache=false 
  2. spring.thymeleaf.prefix=classpath:/templates/ 
  3. spring.thymeleaf.mode=.html 
  4. spring.thymeleaf.encoding=UTF-8 
  5. spring.thymeleaf.content-type=text/html 

然后在resources目錄下添加兩個(gè)文件夾:

  • templates 前端頁(yè)面模板目錄
  • static 前端靜態(tài)資源目錄

新增用戶

在templates目錄下創(chuàng)建index.html

  1. <!DOCTYPE html> 
  2. <html lang="zh" xmlns:th="http://www.thymeleaf.org"
  3. <head> 
  4.     <meta charset="UTF-8"
  5.     <title>Index</title> 
  6.     <!-- 為了減少篇幅,樣式代碼這里就省略掉,樣式不是我們的重點(diǎn)內(nèi)容 --> 
  7. </head> 
  8. <body> 
  9. <div id="content"
  10.     <form action="/save" method="post"
  11.         姓名:<input type="text" name="uname"><br/> 
  12.         年齡:<input type="text" name="age"><br/> 
  13.         密碼:<input type="text" name="password"><br/> 
  14.         手機(jī)號(hào)碼:<input type="text" name="tel"><br/> 
  15.         <button>新增</button> 
  16.     </form> 
  17. </div> 
  18. </body> 

 

 

 

 

 

創(chuàng)建UserService接口和UserServiceImpl實(shí)現(xiàn)類。

  1. public interface UserService { 
  2.     void add(User user); 
  3. @Service 
  4. public class UserServiceImpl implements UserService { 
  5.     @Resource 
  6.     private UserRepository userRepository; 
  7.     @Override 
  8.     public void add(User user) { 
  9.         userRepository.save(user); 
  10.     } 

創(chuàng)建UserController

  1. @Controller 
  2. public class UserController { 
  3.     @Resource 
  4.     private UserService userService; 
  5.  
  6.     @RequestMapping("/index"
  7.     public String add(Model model) { 
  8.         return "index"
  9.     } 
  10.  
  11.     @RequestMapping("/save"
  12.     public String save(Model model,User user) { 
  13.         userService.add(user); 
  14.         //跳轉(zhuǎn)到用戶列表 
  15.         return "redirect:/userList"
  16.     } 

這樣一個(gè)新增功能我們就做完了。這里我們順帶著把用戶列表也給搞出來(lái)。

用戶列表

在templates目錄下創(chuàng)建user_list.html頁(yè)面

  1. <!DOCTYPE html> 
  2. <html lang="zh" xmlns:th="http://www.thymeleaf.org"
  3. <head> 
  4.   <!-- 為了減少篇幅,樣式代碼這里就省略掉,樣式不是我們的重點(diǎn)內(nèi)容 --> 
  5. </head> 
  6. <body> 
  7. <a href="/"
  8.     <div id="ret"
  9.         <button>新增用戶</button> 
  10.     </div> 
  11. </a> 
  12.     <table id="content" style="border: 1px solid;"
  13.         <tr> 
  14.             <th>姓名</th> 
  15.             <th>年齡</th> 
  16.             <th>手機(jī)號(hào)碼</th> 
  17.             <th>操作</th> 
  18.         </tr> 
  19.         <tr th:each="user:${users}"
  20.             <td th:text="${user.uname}"></td> 
  21.             <td th:text="${user.age}"></td> 
  22.             <td th:text="${user.tel}"></td> 
  23.             <td><a th:href="@{'/userInfo/'+${user.uid}}" target="_blank">用戶詳情</a> | 
  24.                 <a th:href="@{'/deleteById/'+${user.uid}}" target="_blank">刪除用戶信息</a> | 
  25.                 <a th:href="@{'/update/'+${user.uid}}" target="_blank">修改用戶信息</a> 
  26.             </td> 
  27.         </tr> 
  28.     </table
  29. </body> 
  30. </html> 

在service和實(shí)現(xiàn)類中添加用戶列表的方法,這里沒(méi)有做分頁(yè)。

  1. //UserService中添加 
  2. List<User> findAll(); 
  3. //UserServiceImpl中添加方法 
  4. @Override 
  5. public List<User> findAll() { 
  6.     return userRepository.findAll(); 

然后我們?cè)僭赾ontroller中添加一個(gè)方法用戶列表的方法。

  1. @RequestMapping("/userList"
  2. public String index(Model model) { 
  3.     List<User> users = userService.findAll(); 
  4.     model.addAttribute("users", users); 
  5.     return "user_list"

好了,自此用戶列表也搞定。下面我們來(lái)啟動(dòng)項(xiàng)目,然后演示一下:

演示

新增流程的演示,訪問(wèn):

http://localhost:8080/

來(lái)到新增用戶頁(yè)面:

填入用戶信息

點(diǎn)擊新增,來(lái)到用戶列表:

用戶詳情

在templates目錄下創(chuàng)建user_info.html頁(yè)面:

  1. <!DOCTYPE html> 
  2. <html lang="zh" xmlns:th="http://www.thymeleaf.org"
  3. <head> 
  4.     <meta charset="UTF-8"
  5.     <title>用戶詳情</title> 
  6. </head> 
  7. <body> 
  8. <div id="content"
  9. 姓名:<input type="text" th:value="${user.uname}"><br/> 
  10. 年齡:<input type="text" th:value="${user.age}"><br/> 
  11. 密碼:<input type="text" th:value="${user.password}"><br/> 
  12. 手機(jī)號(hào)碼:<input type="text" th:value="${user.tel}"><br/> 
  13. </div> 
  14. </body> 
  15. </html> 

 

 

 

 

在service和實(shí)現(xiàn)類分別添加相應(yīng)的方法。

  1. //UserService中添加方法 
  2. User findById(Long id); 
  3. //UserServiceImpl添加方法 
  4. @Override 
  5. public User findById(Long id) { 
  6.     return userRepository.getOne(id); 

然后在controller中添加方法

  1. @RequestMapping("/userInfo/{id}"
  2. public String userInfo(Model model, @PathVariable("id") Long id) { 
  3.     User user = userService.findById(id); 
  4.     model.addAttribute("user"user); 
  5.     return "user_info"

然后再次啟動(dòng)項(xiàng)目,我們就直接可以從用戶列表中進(jìn)入到用戶詳情里。

點(diǎn)擊:用戶詳情。

OK,到此,用戶詳情也就搞定了。剩下的我們繼續(xù)搞修改功能。

用戶信息修改

在templates目錄下創(chuàng)建update.html頁(yè)面。

  1. <!DOCTYPE html> 
  2. <html lang="zh" xmlns:th="http://www.thymeleaf.org"
  3. <head> 
  4.     <meta charset="UTF-8"
  5.     <title>Index</title> 
  6. </head> 
  7. <body> 
  8. <div id="content"
  9.     <form action="/updateById" method="post"
  10.         <input type="text" th:value="${user.uid}" name="uid"><br/> 
  11.         姓名:<input type="text" th:value="${user.uname}" name="uname"><br/> 
  12.         年齡:<input type="text" th:value="${user.age}" name="age"><br/> 
  13.         密碼:<input type="text" th:value="${user.password}" name="password"><br/> 
  14.         手機(jī)號(hào)碼:<input type="text" th:value="${user.tel}" name="tel"><br/> 
  15.         <button>修改</button> 
  16.     </form> 
  17. </div> 
  18. </body> 

 

 

 

 

 

UserRepository中添加更新方法:

  1. @Repository 
  2. public interface UserRepository extends JpaRepository<User, Long> { 
  3.  
  4.     @Modifying 
  5.     @Query("update User u set u.tel=?1 ,u.uname=?2 , u.password=?3 where  u.uid=?4"
  6.     void updateById(String tel, String uname, String password, Long uid); 

繼續(xù)在UserService和實(shí)現(xiàn)類中添加想要的方法。

  1. //UserService中添加方法 
  2. void update(User user); 
  3. //UserServiceImpl中添加方法 
  4. @Transactional 
  5. @Override 
  6. public void update(User user) { 
  7.     userRepository.updateById(user.getTel(), user.getUname(), user.getPassword(), user.getUid()); 

注意:UserServiceImpl添加的 這個(gè)update方法,必須要事務(wù)注解@Transactional,否則更新失敗。異常信息如下:

  1. javax.persistence.TransactionRequiredException: Executing an update/delete query 
  2.  at org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:398) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] 

加上事務(wù)注解@Transactional就搞定了。

然后在controller中添加方法

  1. @RequestMapping("/updateById"
  2. public String updateById(Model model,User user) { 
  3.     userService.update(user); 
  4.     return "redirect:/userList"
  5. //跳轉(zhuǎn)到修改頁(yè)面 
  6. @RequestMapping("/update/{id}"
  7. public String update(Model  model, @PathVariable("id") Long id) { 
  8.     User user = userService.findById(id); 
  9.     model.addAttribute("user"user); 
  10.     return "update"

再次啟動(dòng)項(xiàng)目,繼續(xù)來(lái)到用戶列表頁(yè)面:

點(diǎn)擊:修改用戶信息,然后跳轉(zhuǎn)到用戶信息修改頁(yè)面:

這里我們把手機(jī)尾號(hào)修改成666:

點(diǎn)擊修改按鈕。跳轉(zhuǎn)到用戶列表頁(yè)面:

發(fā)現(xiàn)此時(shí)的用戶手機(jī)號(hào)已經(jīng)修改完了。

到這里,我們已經(jīng)做了用戶新增、單個(gè)用戶信息查詢、多個(gè)用戶信息查詢、單個(gè)用戶信息修改。剩下的刪除功能留給大家自己去做。相信大家也是很輕松的就能完成的。

總結(jié)

本文使用了技術(shù)棧:

Spring Boot +Jpa + Thyneleaf +MySQL+Maven+HTML

實(shí)戰(zhàn)演練一個(gè)用戶信息新增、查詢、修改。

注意點(diǎn):

1、配置信息是一點(diǎn)要小心,不能有誤。

2、得自己安裝MySQL數(shù)據(jù)庫(kù),然后創(chuàng)建數(shù)據(jù)庫(kù),創(chuàng)建用戶表。

3、分層:html頁(yè)面、controller、service接口、service實(shí)現(xiàn)類、repository接口。

好了,一個(gè)簡(jiǎn)單的用戶信息模塊就這樣了,刪除就靠你們自己去實(shí)現(xiàn)了。非常簡(jiǎn)單的~,如有疑惑、問(wèn)題的可以隨時(shí)聯(lián)系我。

「展望」

后面我們基于這個(gè)不斷擴(kuò)大,比如:用戶信息刪除、用戶登錄、用戶注冊(cè)、權(quán)限、角色、集成Redis做緩存、使用Redis搞一個(gè)分布式鎖、用戶積分排行榜等待功能。

本文轉(zhuǎn)載自微信公眾號(hào)「Java后端技術(shù)全?!?,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系Java后端技術(shù)全棧公眾號(hào)。

 

責(zé)任編輯:武曉燕 來(lái)源: Java后端技術(shù)全棧
相關(guān)推薦

2019-04-02 10:36:17

數(shù)據(jù)庫(kù)MySQL優(yōu)化方法

2023-02-28 00:01:53

MySQL數(shù)據(jù)庫(kù)工具

2019-08-01 07:31:51

數(shù)據(jù)庫(kù)主機(jī)日志

2018-10-10 09:30:29

Spring Boot知識(shí)框架

2024-10-18 16:21:49

SpringPOM

2017-12-27 15:16:35

Spring BootFlyway數(shù)據(jù)庫(kù)

2019-11-05 14:20:02

Oracle分組函數(shù)數(shù)據(jù)庫(kù)

2019-10-22 07:50:45

SqlServer數(shù)據(jù)庫(kù)觸發(fā)器

2019-06-18 10:31:23

數(shù)據(jù)庫(kù)端口URL

2019-08-20 09:46:14

DBA收藏腳本語(yǔ)言

2021-03-09 17:11:09

數(shù)據(jù)庫(kù)腳手架開(kāi)發(fā)

2019-09-10 07:58:01

字符集MySQL數(shù)據(jù)庫(kù)

2019-09-11 11:38:30

MySQLInnoDB數(shù)據(jù)庫(kù)

2023-04-28 15:15:39

數(shù)據(jù)庫(kù)JPA

2019-08-20 22:06:32

Oracle數(shù)據(jù)庫(kù)索引

2011-05-26 13:29:30

ORACLE數(shù)據(jù)庫(kù)升級(jí)

2020-10-16 06:57:46

UnixLinux命令操作系統(tǒng)

2021-06-01 08:00:43

KubernetesCentOS版集群

2024-01-07 18:02:21

數(shù)據(jù)庫(kù)分片副本

2025-01-26 00:00:35

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)