最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring boot2+jpa+thymeleaf實(shí)現(xiàn)增刪改查

 更新時(shí)間:2020年04月23日 10:27:17   作者:gdjlc  
這篇文章主要介紹了Spring boot2+jpa+thymeleaf實(shí)現(xiàn)增刪改查,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、pom.xml引入相關(guān)模塊web、jpa、thymeleaf、oracle:

<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>com.oracle</groupId> 
      <artifactId>ojdbc8</artifactId> 
      <version>12.2.0.1</version>     
    </dependency>

二、application.properties配置

server.port = 9001

spring.datasource.driver-class-name = oracle.jdbc.driver.OracleDriver
spring.datasource.url = jdbc:oracle:thin:@127.0.0.1:1521:testdb
spring.datasource.username = dev
spring.datasource.password = dev

spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql = true

spring.thymeleaf.cache = false

說明:

1、由于本機(jī)的8080已經(jīng)被使用,修改一下端口號(hào)為9001。

2、hibernate.hbm2ddl.auto參數(shù)有四個(gè)值:

create: 每次加載hibernate時(shí)都會(huì)刪除上一次的生成的表,然后根據(jù)你的model類再重新來(lái)生成新表,哪怕兩次沒有任何改變也要
這樣執(zhí)行,這就是導(dǎo)致數(shù)據(jù)庫(kù)表數(shù)據(jù)丟失的一個(gè)重要原因。

create-drop :每次加載hibernate時(shí)根據(jù)model類生成表,但是sessionFactory一關(guān)閉,表就自動(dòng)刪除。

update:最常用的屬性,第一次加載hibernate時(shí)根據(jù)model類會(huì)自動(dòng)建立起表的結(jié)構(gòu)(前提是先建立好數(shù)據(jù)庫(kù)),以后加載
hibernate時(shí)根據(jù) model類自動(dòng)更新表結(jié)構(gòu),即使表結(jié)構(gòu)改變了但表中的行仍然存在不會(huì)刪除以前的行。要注意的是當(dāng)部署到服務(wù)器后,表結(jié)構(gòu)是不會(huì)被馬上建立起來(lái)的,是要等 應(yīng)用第一次運(yùn)行起來(lái)后才會(huì)。
validate :每次加載hibernate時(shí),驗(yàn)證創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu),只會(huì)和數(shù)據(jù)庫(kù)中的表進(jìn)行比較,不會(huì)創(chuàng)建新表,但是會(huì)插入新值。

3、show-sql 是否打印出自動(dòng)生產(chǎn)的SQL,方便調(diào)試的時(shí)候查看

4、propertiesspring.thymeleaf.cache=false是關(guān)閉thymeleaf的緩存,不然在開發(fā)過程中修改頁(yè)面不會(huì)立刻生效需要重啟,生產(chǎn)
可配置為true。

三、啟動(dòng)類需要添加Servlet的支持

package com.example.demo;

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 DemoApplication extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(DemoApplication.class);
  }
  
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

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

1、實(shí)體類映射數(shù)據(jù)庫(kù)表

package com.example.demo.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotEmpty;

import org.hibernate.validator.constraints.Length;

@Entity
@Table(name = "userinfo")
public class User {
  @Id
  @GeneratedValue
  private long id;
  
  @Column(nullable = false, unique = true)
  @NotEmpty(message="用戶名不能為空")
  private String userName;
  
  @Column(nullable = false)
  @NotEmpty(message="密碼不能為空")
  @Length(min=6, message="密碼長(zhǎng)度不能少于6位")
  private String password;
  
  @Column(nullable = false)
  private int age;

  //必須有構(gòu)造
   public User() {
   }
     
  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;
  }
}

2、繼承JpaRepository類會(huì)自動(dòng)實(shí)現(xiàn)很多內(nèi)置的方法

package com.example.demo.repository;

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

public interface UserRepository extends JpaRepository<User, Long> {
  User findById(long id);
  void deleteById(Long id);
}

五、業(yè)務(wù)層

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

1、UserService.java

package com.example.demo.service;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.example.demo.entity.User;
public interface UserService {
  
  public Page<User> getUserPage(Pageable pageable);  
  public List<User> getUserList();
  public User findUserById(long id);
  public void save(User user);
  public void edit(User user);
  public void delete(long id);
}

2、UserServiceImpl.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.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import com.example.demo.service.UserService;

@Service
public class UserServiceImpl implements UserService {
  
  @Autowired
  private UserRepository userRepository;

  @Override
  public Page<User> getUserPage(Pageable pageable) {
    return userRepository.findAll(pageable);
  }
  
  @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);
  }

}

六、控制層

package com.example.demo.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;

@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);
    
    /*int page=1,size=2;
    Sort sort = new Sort(Direction.DESC, "id");
    Pageable pageable = PageRequest.of(page, size, sort);
    model.addAttribute("users", pageable);*/
    
    return "user/list";
  }

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

  @RequestMapping("/add")
  public @ResponseBody User add(@Valid User user, BindingResult result) {
    if (result.hasErrors()) {
      List<ObjectError> list = result.getAllErrors();
      for (ObjectError error : list) {
        System.out.println(error.getDefaultMessage());
      }
      return null;
    }
    userService.save(user);
    return user;    
  }

  @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";
  }
}

七、頁(yè)面

1、列表頁(yè) list.hmtl

<!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}" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></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})}" rel="external nofollow" >edit</a></td>
      <td><a th:href="@{/delete(id=${user.id})}" rel="external nofollow" >delete</a></td>
    </tr>
    </tbody>
  </table>
</div>
<div class="form-group">
  <div class="col-sm-2 control-label">
    <a href="/toAdd" rel="external nofollow" rel="external nofollow" th:href="@{/toAdd}" rel="external nofollow" class="btn btn-info">add</a>
  </div>
</div>

</body>
</html>

2、新增頁(yè) 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}" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></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>

3、修改頁(yè) 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}" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></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" rel="external nofollow" rel="external nofollow" th:href="@{/list}" rel="external nofollow" class="btn btn-info">Back</a>
      </div>

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 讀取、獲取配置文件.properties中的數(shù)據(jù)

    Java 讀取、獲取配置文件.properties中的數(shù)據(jù)

    這篇文章主要介紹了Java 讀取、獲取配置文件.properties中的數(shù)據(jù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • Java中Map和Set練習(xí)項(xiàng)目實(shí)例代碼

    Java中Map和Set練習(xí)項(xiàng)目實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于Java中Map和Set練習(xí)項(xiàng)目的相關(guān)資料,首先介紹了如何使用map來(lái)統(tǒng)計(jì)字符串?dāng)?shù)組中每個(gè)字符串的出現(xiàn)次數(shù),然后討論了如何使用set來(lái)找出只出現(xiàn)一次的數(shù)字,最后提出了一個(gè)解決壞鍵盤打字問題的思路,需要的朋友可以參考下
    2024-11-11
  • Java使用apache?poi操作excel的方式

    Java使用apache?poi操作excel的方式

    這篇文章主要介紹了Java使用apache?poi進(jìn)行excel相關(guān)操作,本文主要針對(duì)Apache?POI對(duì)excel的操作進(jìn)行介紹,主要包括如何創(chuàng)建一個(gè)excel、錄入數(shù)據(jù)、讀取excel數(shù)據(jù)的方式,需要的朋友可以參考下
    2022-05-05
  • 淺談Java內(nèi)存泄露

    淺談Java內(nèi)存泄露

    內(nèi)存泄漏(Memory Leak)是指程序中己動(dòng)態(tài)分配的堆內(nèi)存由于某種原因程序未釋放或無(wú)法釋放,造成系統(tǒng)內(nèi)存的浪費(fèi),導(dǎo)致程序運(yùn)行速度減慢甚至系統(tǒng)崩潰等嚴(yán)重后果。下面我們來(lái)一起了解如何解決
    2019-05-05
  • Java分布式ID中Snowflake雪花算法應(yīng)用實(shí)現(xiàn)

    Java分布式ID中Snowflake雪花算法應(yīng)用實(shí)現(xiàn)

    Snowflake算法作為一種高效且易于實(shí)現(xiàn)的分布式ID生成方案,能夠很好地滿足分布式系統(tǒng)中對(duì)全局唯一ID的需求,本文就來(lái)介紹一下Java分布式ID中Snowflake雪花算法應(yīng)用實(shí)現(xiàn),感興趣的可以了解一下
    2024-07-07
  • java項(xiàng)目中classpath指向哪里

    java項(xiàng)目中classpath指向哪里

    這篇文章介紹了java項(xiàng)目中classpath指向哪里及工作原理,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • java基礎(chǔ)知識(shí)之FileInputStream流的使用

    java基礎(chǔ)知識(shí)之FileInputStream流的使用

    這篇文章主要介紹了java基礎(chǔ)知識(shí)之FileInputStream流的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 解決Spring國(guó)際化文案占位符失效問題的方法

    解決Spring國(guó)際化文案占位符失效問題的方法

    本篇文章主要介紹了解決Spring國(guó)際化文案占位符失效問題的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-04-04
  • 使用Gson將字符串轉(zhuǎn)換成JsonObject和JsonArray

    使用Gson將字符串轉(zhuǎn)換成JsonObject和JsonArray

    這篇文章主要介紹了使用Gson將字符串轉(zhuǎn)換成JsonObject和JsonArray,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 基于Spring Mvc實(shí)現(xiàn)的Excel文件上傳下載示例

    基于Spring Mvc實(shí)現(xiàn)的Excel文件上傳下載示例

    本篇文章主要介紹了基于Spring Mvc實(shí)現(xiàn)的Excel文件上傳下載示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2017-02-02

最新評(píng)論

香河县| 宜兰市| 金昌市| 游戏| 社旗县| 兖州市| 福贡县| 玉溪市| 龙山县| 玛纳斯县| 广水市| 大新县| 手游| 金沙县| 南康市| 博客| 阳江市| 巴林左旗| 手机| 信阳市| 根河市| 昌邑市| 南康市| 乐至县| 芷江| 北流市| 鄂尔多斯市| 霍州市| 洪江市| 美姑县| 刚察县| 竹北市| 太保市| 桐城市| 阳山县| 舞阳县| 金川县| 内江市| 且末县| 武陟县| 高陵县|