国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot+Jpa+Thymeleaf實現(xiàn)增刪改查

這篇具有很好參考價值的文章主要介紹了SpringBoot+Jpa+Thymeleaf實現(xiàn)增刪改查。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

SpringBoot+Jpa+Thymeleaf實現(xiàn)增刪改查

這篇文章介紹如何使用 Jpa 和 Thymeleaf 做一個增刪改查的示例。

1、pom依賴

pom 包里面添加JpaThymeleaf 的相關(guān)包引用

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
	
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>spring-boot-jpa-thymeleaf-curd</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-jpa-thymeleaf-curd</name>
    <description>spring-boot-jpa-thymeleaf-curd</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</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>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2、application.properties中添加配置

spring.datasource.url=jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.properties.hibernate.hbm2ddl.auto=create
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
spring.thymeleaf.cache=false

其中propertiesspring.thymeleaf.cache=false是關(guān)閉 Thymeleaf 的緩存,不然在開發(fā)過程中修改頁面不會

立刻生效需要重啟,生產(chǎn)可配置為 true。

在項目 resources 目錄下會有兩個文件夾:static目錄用于放置網(wǎng)站的靜態(tài)內(nèi)容如 cssjs、圖片;

templates 目錄用于放置項目使用的頁面模板。

3、啟動類

啟動類需要添加 Servlet 的支持

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

4、數(shù)據(jù)庫層代碼

實體類映射數(shù)據(jù)庫表

package com.example.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {

    @Id
    @GeneratedValue
    private long id;

    @Column(nullable = false, unique = true)
    private String userName;

    @Column(nullable = false)
    private String password;

    @Column(nullable = false)
    private int age;

    public long getId() {
        return id;
    }

    public User setId(long id) {
        this.id = id;
        return this;
    }

    public String getUserName() {
        return userName;
    }

    public User setUserName(String userName) {
        this.userName = userName;
        return this;
    }

    public String getPassword() {
        return password;
    }

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

    public int getAge() {
        return age;
    }

    public User setAge(int age) {
        this.age = age;
        return this;
    }
}

5、 JpaRepository

package com.example.repository;

import com.example.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {

    User findById(long id);

    void deleteById(Long id);
}

繼承 JpaRepository 類會自動實現(xiàn)很多內(nèi)置的方法,包括增刪改查,也可以根據(jù)方法名來自動生成相關(guān) Sql。

6、業(yè)務(wù)層處理

Service 調(diào)用 Jpa 實現(xiàn)相關(guān)的增刪改查,實際項目中 Service 層處理具體的業(yè)務(wù)代碼。

package com.example.service;

import com.example.model.User;

import java.util.List;

public interface UserService {

    public List<User> getUserList();

    public User findUserById(long id);

    public void save(User user);

    public void edit(User user);

    public void delete(long id);

}
package com.example.service.impl;

import com.example.model.User;
import com.example.repository.UserRepository;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<User> getUserList() {
        return userRepository.findAll();
    }

    @Override
    public User findUserById(long id) {
        return userRepository.findById(id);
    }

    @Override
    public void save(User user) {
        userRepository.save(user);
    }

    @Override
    public void edit(User user) {
        userRepository.save(user);
    }

    @Override
    public void delete(long id) {
        userRepository.deleteById(id);
    }
}

7、控制層

package com.example.web;

import com.example.model.User;
import com.example.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class UserController {

    @Resource
    UserService userService;

    @RequestMapping("/")
    public String index() {
        return "redirect:/list";
    }

    @RequestMapping("/list")
    public String list(Model model) {
        List<User> users = userService.getUserList();
        model.addAttribute("users", users);
        return "user/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "user/userAdd";
    }

    @RequestMapping("/add")
    public String add(User user) {
        userService.save(user);
        return "redirect:/list";
    }

    @RequestMapping("/toEdit")
    public String toEdit(Model model, Long id) {
        User user = userService.findUserById(id);
        model.addAttribute("user", user);
        return "user/userEdit";
    }

    @RequestMapping("/edit")
    public String edit(User user) {
        userService.edit(user);
        return "redirect:/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id) {
        userService.delete(id);
        return "redirect:/list";
    }
}

Controller 負(fù)責(zé)接收請求,處理完后將頁面內(nèi)容返回給前端。

  • return "user/userEdit"; 代表會直接去 resources 目錄下找相關(guān)的文件。

  • return "redirect:/list"; 代表轉(zhuǎn)發(fā)到對應(yīng)的 Controller,這個示例就相當(dāng)于刪除內(nèi)容之后自動調(diào)整到

    list 請求,然后再輸出到頁面。

8、頁面內(nèi)容

list 列表 list.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>userList</title>
    <link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
</head>
<body class="container">
<br/>
<h1>用戶列表</h1>
<br/><br/>
<div class="with:80%">
    <table class="table table-hover">
        <thead>
        <tr>
            <th>#</th>
            <th>User Name</th>
            <th>Password</th>
            <th>Age</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>
        </thead>
        <tbody>
        <tr  th:each="user : ${users}">
            <th scope="row" th:text="${user.id}">1</th>
            <td th:text="${user.userName}">neo</td>
            <td th:text="${user.password}">Otto</td>
            <td th:text="${user.age}">6</td>
            <td><a th:href="@{/toEdit(id=${user.id})}">edit</a></td>
            <td><a th:href="@{/delete(id=${user.id})}">delete</a></td>
        </tr>
        </tbody>
    </table>
</div>
<div class="form-group">
    <div class="col-sm-2 control-label">
        <a href="/toAdd" th:href="@{/toAdd}" class="btn btn-info">add</a>
    </div>
</div>

</body>
</html>

訪問http://localhost:8080/,效果如下圖所示:
SpringBoot+Jpa+Thymeleaf實現(xiàn)增刪改查,spring boot,spring boot

<tr th:each="user : ${users}"> 這里會從 Controler 層 model set 的對象去獲取相關(guān)的內(nèi)容,th:each

示會循環(huán)遍歷對象內(nèi)容。

點擊Add按鈕會跳轉(zhuǎn)到新增頁面。

新增頁面 userAdd.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>user</title>
    <link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
</head>
<body class="container">
<br/>
<h1>添加用戶</h1>
<br/><br/>
<div class="with:80%">
    <form class="form-horizontal"   th:action="@{/add}"  method="post">
        <div class="form-group">
            <label for="userName" class="col-sm-2 control-label">userName</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="userName"  id="userName" placeholder="userName"/>
            </div>
        </div>
        <div class="form-group">
            <label for="password" class="col-sm-2 control-label" >Password</label>
            <div class="col-sm-10">
                <input type="password" class="form-control" name="password" id="password" placeholder="Password"/>
            </div>
        </div>
        <div class="form-group">
            <label for="age" class="col-sm-2 control-label">age</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="age"  id="age" placeholder="age"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <input type="submit" value="Submit" class="btn btn-info" />
                &nbsp; &nbsp; &nbsp;
                <input type="reset" value="Reset" class="btn btn-info" />
            </div>

        </div>
    </form>
</div>
</body>
</html>

SpringBoot+Jpa+Thymeleaf實現(xiàn)增刪改查,spring boot,spring boot

填寫數(shù)據(jù)并且點擊Submit按鈕,用戶添加成功并且跳轉(zhuǎn)到用戶列表頁面。

SpringBoot+Jpa+Thymeleaf實現(xiàn)增刪改查,spring boot,spring boot

點擊edit按鈕跳轉(zhuǎn)到用戶修改頁面。

修改頁面 userEdit.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>user</title>
    <link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
</head>
<body class="container">
<br/>
<h1>修改用戶</h1>
<br/><br/>
<div class="with:80%">
    <form class="form-horizontal"   th:action="@{/edit}" th:object="${user}"  method="post">
        <input type="hidden" name="id" th:value="*{id}" />
        <div class="form-group">
            <label for="userName" class="col-sm-2 control-label">userName</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="userName"  id="userName" th:value="*{userName}" placeholder="userName"/>
            </div>
        </div>
        <div class="form-group">
            <label for="password" class="col-sm-2 control-label" >Password</label>
            <div class="col-sm-10">
                <input type="password" class="form-control" name="password" id="password"  th:value="*{password}" placeholder="Password"/>
            </div>
        </div>
        <div class="form-group">
            <label for="age" class="col-sm-2 control-label">age</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="age"  id="age" th:value="*{age}" placeholder="age"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <input type="submit" value="Submit" class="btn btn-info" />
                &nbsp; &nbsp; &nbsp;
                <a href="/toAdd" th:href="@{/list}" class="btn btn-info">Back</a>
            </div>

        </div>
    </form>
</div>
</body>
</html>

SpringBoot+Jpa+Thymeleaf實現(xiàn)增刪改查,spring boot,spring boot

修改用戶的年齡為100然后點擊Submit按鈕提交修改。

然后跳轉(zhuǎn)到用戶列表頁面發(fā)現(xiàn)數(shù)據(jù)修改了。

SpringBoot+Jpa+Thymeleaf實現(xiàn)增刪改查,spring boot,spring boot

可以點擊delete按鈕刪除用戶。

刪除之后跳轉(zhuǎn)到用戶列表頁面。

SpringBoot+Jpa+Thymeleaf實現(xiàn)增刪改查,spring boot,spring boot

這樣一個使用 Jpa 和 Thymeleaf 的增刪改查示例就完成了。文章來源地址http://www.zghlxwxcb.cn/news/detail-609406.html

到了這里,關(guān)于SpringBoot+Jpa+Thymeleaf實現(xiàn)增刪改查的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 【Spring Boot+Thymeleaf+MyBatis+mysql】實現(xiàn)電子商務(wù)平臺實戰(zhàn)(附源碼)持續(xù)更新~~ 包括sql語句、java、html代碼

    【Spring Boot+Thymeleaf+MyBatis+mysql】實現(xiàn)電子商務(wù)平臺實戰(zhàn)(附源碼)持續(xù)更新~~ 包括sql語句、java、html代碼

    源碼請點贊關(guān)注收藏后評論區(qū)留言和私信博主 開發(fā)環(huán)境:Web服務(wù)器使用Servlet容器,數(shù)據(jù)庫采用mysql,集成開發(fā)環(huán)境為Spring Tool Suite(STS) 電子商務(wù)平臺分為兩個子系統(tǒng) 一個是后臺管理系統(tǒng) 一個是電子商務(wù)系統(tǒng),下面分別講解著兩個子系統(tǒng)的功能需要與模塊劃分 1:后臺管理子

    2024年02月09日
    瀏覽(23)
  • Spring Boot+Mybatis實現(xiàn)增刪改查接口開發(fā)+測試(超詳細(xì)建議收藏)

    Spring Boot+Mybatis實現(xiàn)增刪改查接口開發(fā)+測試(超詳細(xì)建議收藏)

    Java也是測試必知必會的內(nèi)容,特別是現(xiàn)在類似spring boot 等Java框架更是成為主流。之前實現(xiàn)的圖書增刪改查是用Python實現(xiàn)的,沒看過的請移步:Flask+mysql 實現(xiàn)增刪改查接口開發(fā)+測試(圖文教程附源碼),本次給大家?guī)碛肑ava實現(xiàn)的后端接口版本,并根據(jù)之前的項目總結(jié)有做一

    2024年02月03日
    瀏覽(40)
  • Spring Boot:Web應(yīng)用開發(fā)之增刪改查的實現(xiàn)

    Spring Boot:Web應(yīng)用開發(fā)之增刪改查的實現(xiàn)

    增刪改查功能作為 Web 應(yīng)用中的基礎(chǔ)且重要的組成部分,是基本的數(shù)據(jù)庫操作,也是實現(xiàn)業(yè)務(wù)邏輯和功能的關(guān)鍵要素。下面簡單介紹使用 Spring Boot 實現(xiàn)增刪改查的功能。 在上一章 Spring Boot:Web應(yīng)用開發(fā)之登錄與退出的實現(xiàn) 文章的案例基礎(chǔ)上,進行實現(xiàn)增刪改查的功能。 簡單

    2024年04月26日
    瀏覽(23)
  • Jpa與Druid線程池及Spring Boot整合(一): spring-boot-starter-data-jpa 搭建持久層

    Jpa與Druid線程池及Spring Boot整合(一): spring-boot-starter-data-jpa 搭建持久層

    ? ? ? ? ? ? ? ? ? ? ? Jpa與Druid線程池及Spring Boot整合(一) Jpa與Druid線程池及Spring Boot整合(二):幾個坑 附錄官網(wǎng)文檔:core.domain-events域事件 docker實戰(zhàn)(一):centos7 yum安裝docker docker實戰(zhàn)(二):基礎(chǔ)命令篇 docker實戰(zhàn)(三):docker網(wǎng)絡(luò)模式(超詳細(xì)) docker實戰(zhàn)(四):docker架構(gòu)原理 docker實戰(zhàn)(五

    2024年02月13日
    瀏覽(34)
  • Jpa與Druid線程池及Spring Boot整合(二): spring-boot-starter-data-jpa 踏坑異常處理方案

    Jpa與Druid線程池及Spring Boot整合(二): spring-boot-starter-data-jpa 踏坑異常處理方案

    ? ? ? ? ? ? ? ? ? ?? docker實戰(zhàn)(一):centos7 yum安裝docker docker實戰(zhàn)(二):基礎(chǔ)命令篇 docker實戰(zhàn)(三):docker網(wǎng)絡(luò)模式(超詳細(xì)) docker實戰(zhàn)(四):docker架構(gòu)原理 docker實戰(zhàn)(五):docker鏡像及倉庫配置 docker實戰(zhàn)(六):docker 網(wǎng)絡(luò)及數(shù)據(jù)卷設(shè)置 docker實戰(zhàn)(七):docker 性質(zhì)及版本選擇 認(rèn)知升維: 道、法、

    2024年02月13日
    瀏覽(27)
  • Spring Boot 實戰(zhàn) | Spring Boot整合JPA常見問題解決方案

    Spring Boot 實戰(zhàn) | Spring Boot整合JPA常見問題解決方案

    專欄集錦,大佬們可以收藏以備不時之需: Spring Cloud 專欄: Python 專欄: Redis 專欄: TensorFlow 專欄: Logback 專欄: 量子計算: 量子計算 | 解密著名量子算法Shor算法和Grover算法 AI機器學(xué)習(xí)實戰(zhàn): AI機器學(xué)習(xí)實戰(zhàn) | 使用 Python 和 scikit-learn 庫進行情感分析 AI機器學(xué)習(xí) | 基于lib

    2024年02月04日
    瀏覽(23)
  • Spring Boot整合Spring Data Jpa + QueryDSL

    Spring Data JPA是一個Spring項目中常用的持久化框架,它簡化了與數(shù)據(jù)庫進行交互的過程。而QueryDSL是一個查詢構(gòu)建框架,可以讓我們以面向?qū)ο蟮姆绞絹砭帉憯?shù)據(jù)庫查詢。 在本文中,我們將討論如何使用Spring Boot整合Spring Data JPA和QueryDSL,并提供一個使用案例來演示它們的用法。

    2024年02月09日
    瀏覽(24)
  • 在Spring Boot項目中使用JPA

    Spring Boot提供了啟動器spring-boot-starter-data-jpa,只需要添加啟動器(Starters)就能實現(xiàn)在項目中使用JPA。下面一步一步演示集成Spring Data JPA所需的配置。 步驟01 添加JPA依賴。 首先創(chuàng)建新的Spring Boot項目,在項目的pom.xml中增加JPA相關(guān)依賴,具體代碼如下:

    2024年02月09日
    瀏覽(22)
  • Spring Boot——Thymeleaf生成PDF實戰(zhàn)教程

    Spring Boot——Thymeleaf生成PDF實戰(zhàn)教程

    溫馨提示:本博客使用Thymeleaf模板引擎實現(xiàn)PDF打印僅供參考: 在閱讀該博客之前,先要了解一下Thymeleaf模板引擎,因為是使用Thymeleaf模板引擎實現(xiàn)的PDF打印的, Thymeleaf是一個現(xiàn)代的服務(wù)器端 Java 模板引擎,適用于 Web 和獨立環(huán)境。 Thymeleaf 的主要目標(biāo)是為您的開發(fā)工作流程帶

    2024年02月04日
    瀏覽(13)
  • spring boot+thymeleaf+semantic ui 分頁

    spring boot+thymeleaf+semantic ui 分頁

    https://my.oschina.net/ayyao/blog/898041 com.github.pagehelper.PageInfo,作為分頁對象 Thymeleaf循環(huán)語句_thymeleaf 循環(huán)_苦海無邊,不能上岸的博客-CSDN博客 Table | Semantic UI?官網(wǎng)

    2024年02月07日
    瀏覽(14)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包