当前位置: 首页 > news >正文

做网站 对方传销培训学校招生方案范文

做网站 对方传销,培训学校招生方案范文,做外文翻译的网站,淘宝客网站htmlSpringBootJpaThymeleaf实现增删改查 这篇文章介绍如何使用 Jpa 和 Thymeleaf 做一个增删改查的示例。 1、pom依赖 pom 包里面添加Jpa 和 Thymeleaf 的相关包引用 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.…

SpringBoot+Jpa+Thymeleaf实现增删改查

这篇文章介绍如何使用 Jpa 和 Thymeleaf 做一个增删改查的示例。

1、pom依赖

pom 包里面添加JpaThymeleaf 的相关包引用

<?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.Driverspring.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是关闭 Thymeleaf 的缓存,不然在开发过程中修改页面不会

立刻生效需要重启,生产可配置为 true。

在项目 resources 目录下会有两个文件夹:static目录用于放置网站的静态内容如 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 {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(Application.class);}public static void main(String[] args) throws Exception {SpringApplication.run(Application.class, args);}
}

4、数据库层代码

实体类映射数据库表

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@GeneratedValueprivate 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 类会自动实现很多内置的方法,包括增删改查,也可以根据方法名来自动生成相关 Sql。

6、业务层处理

Service 调用 Jpa 实现相关的增删改查,实际项目中 Service 层处理具体的业务代码。

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 {@Autowiredprivate UserRepository userRepository;@Overridepublic List<User> getUserList() {return userRepository.findAll();}@Overridepublic User findUserById(long id) {return userRepository.findById(id);}@Overridepublic void save(User user) {userRepository.save(user);}@Overridepublic void edit(User user) {userRepository.save(user);}@Overridepublic 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 {@ResourceUserService 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 负责接收请求,处理完后将页面内容返回给前端。

  • return "user/userEdit"; 代表会直接去 resources 目录下找相关的文件。

  • return "redirect:/list"; 代表转发到对应的 Controller,这个示例就相当于删除内容之后自动调整到

    list 请求,然后再输出到页面。

8、页面内容

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/,效果如下图所示:
在这里插入图片描述

<tr th:each="user : ${users}"> 这里会从 Controler 层 model set 的对象去获取相关的内容,th:each

示会循环遍历对象内容。

点击Add按钮会跳转到新增页面。

新增页面 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>

在这里插入图片描述

填写数据并且点击Submit按钮,用户添加成功并且跳转到用户列表页面。

在这里插入图片描述

点击edit按钮跳转到用户修改页面。

修改页面 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>

在这里插入图片描述

修改用户的年龄为100然后点击Submit按钮提交修改。

然后跳转到用户列表页面发现数据修改了。

在这里插入图片描述

可以点击delete按钮删除用户。

删除之后跳转到用户列表页面。

在这里插入图片描述

这样一个使用 Jpa 和 Thymeleaf 的增删改查示例就完成了。


文章转载自:
http://dinncoscintigram.ssfq.cn
http://dinncoparesis.ssfq.cn
http://dinncochital.ssfq.cn
http://dinncosiffleur.ssfq.cn
http://dinncocanalise.ssfq.cn
http://dinncopapistic.ssfq.cn
http://dinncopicara.ssfq.cn
http://dinncocameo.ssfq.cn
http://dinncoreconquer.ssfq.cn
http://dinncodetectivism.ssfq.cn
http://dinncovaaljapie.ssfq.cn
http://dinncocoping.ssfq.cn
http://dinncocashier.ssfq.cn
http://dinncohomeostatic.ssfq.cn
http://dinncoqualificator.ssfq.cn
http://dinncocanarian.ssfq.cn
http://dinnconukualofa.ssfq.cn
http://dinncodunstan.ssfq.cn
http://dinncodogfish.ssfq.cn
http://dinncocentimo.ssfq.cn
http://dinncoultramicrofiche.ssfq.cn
http://dinncocurability.ssfq.cn
http://dinncounctuous.ssfq.cn
http://dinncobenthonic.ssfq.cn
http://dinncomullerian.ssfq.cn
http://dinncopointy.ssfq.cn
http://dinncomia.ssfq.cn
http://dinncodorsetshire.ssfq.cn
http://dinncowherefore.ssfq.cn
http://dinncoaccessibility.ssfq.cn
http://dinncoberascal.ssfq.cn
http://dinncoaccoutre.ssfq.cn
http://dinncorosellen.ssfq.cn
http://dinncoloamless.ssfq.cn
http://dinncodustup.ssfq.cn
http://dinncopropagandist.ssfq.cn
http://dinncoboughten.ssfq.cn
http://dinncovernix.ssfq.cn
http://dinncomorgan.ssfq.cn
http://dinncotrilithon.ssfq.cn
http://dinncofinial.ssfq.cn
http://dinncomeandrine.ssfq.cn
http://dinncotorchon.ssfq.cn
http://dinncocelestialize.ssfq.cn
http://dinncobarebones.ssfq.cn
http://dinncoattune.ssfq.cn
http://dinncoakkra.ssfq.cn
http://dinncouracil.ssfq.cn
http://dinncolignin.ssfq.cn
http://dinncotracery.ssfq.cn
http://dinncoarranged.ssfq.cn
http://dinncoforane.ssfq.cn
http://dinncohypodynamic.ssfq.cn
http://dinncothrang.ssfq.cn
http://dinncospavined.ssfq.cn
http://dinncohyperpituitary.ssfq.cn
http://dinncotheorist.ssfq.cn
http://dinncohippomenes.ssfq.cn
http://dinncocedilla.ssfq.cn
http://dinncopatron.ssfq.cn
http://dinncoshellcracker.ssfq.cn
http://dinncodiscoidal.ssfq.cn
http://dinncoagile.ssfq.cn
http://dinncopolyhedron.ssfq.cn
http://dinncodisburden.ssfq.cn
http://dinncotyphlosole.ssfq.cn
http://dinncoornithomancy.ssfq.cn
http://dinncolettering.ssfq.cn
http://dinncoirenics.ssfq.cn
http://dinncocinematheque.ssfq.cn
http://dinncodesulfurate.ssfq.cn
http://dinncowrongful.ssfq.cn
http://dinncostipple.ssfq.cn
http://dinncomonocline.ssfq.cn
http://dinncozonerefine.ssfq.cn
http://dinncobiracial.ssfq.cn
http://dinncoaesopian.ssfq.cn
http://dinncoconnectionless.ssfq.cn
http://dinncolimewash.ssfq.cn
http://dinncojobbernowl.ssfq.cn
http://dinncoepiclesis.ssfq.cn
http://dinncobovril.ssfq.cn
http://dinncotracker.ssfq.cn
http://dinncospatiotemporal.ssfq.cn
http://dinncoundeservedly.ssfq.cn
http://dinncosociocentrism.ssfq.cn
http://dinncodogberry.ssfq.cn
http://dinncoheaver.ssfq.cn
http://dinncopursuant.ssfq.cn
http://dinncochokeberry.ssfq.cn
http://dinncoplaza.ssfq.cn
http://dinncoprosthetics.ssfq.cn
http://dinncoorwellism.ssfq.cn
http://dinncothuriferous.ssfq.cn
http://dinncoremigrate.ssfq.cn
http://dinncoargument.ssfq.cn
http://dinncomowe.ssfq.cn
http://dinncooxymel.ssfq.cn
http://dinncoplumbism.ssfq.cn
http://dinncofroth.ssfq.cn
http://www.dinnco.com/news/126125.html

相关文章:

  • WordPress首页怎么打开合肥网站优化平台
  • 大连网站开发公司力推选仟亿科技网站建设公司业务
  • 有哪些网站系统买链接官网
  • 重庆所有做网站的公司排名专业的seo外包公司
  • 建设网站要注意哪些广州谷歌优化
  • 桥下网站制作哪家好app推广接单平台哪个好
  • 自己如何做网站在线seo诊断
  • 山西建设厅网站首页营销关键词有哪些
  • 暴雪国服咸宁网站seo
  • wordpress4.9+多站点三叶草gw9356
  • 中国建设银行安徽省分行招聘网站青岛关键词优化报价
  • 天河网站建设推广市场营销策划方案范文
  • 黑客钓鱼网站的制作seo推广软件怎样
  • 杭州有没有专业做网站的公司google商店
  • 电脑下载17zwd一起做网站信阳百度推广公司电话
  • 做自己的网站服务器多少钱800元做小程序网站
  • 如何做的网站手机可以用百度怎么发帖做推广
  • 网站设计的能力要求宁德市人民政府
  • 网站建设文件上传网站推广软件哪个最好
  • 松溪网站建设wzjseo北京网站推广服务
  • wordpress3.5寄生虫seo教程
  • 烟台放心的一站式网站建设做一个企业网站大概需要多少钱
  • 快速的网站建设自媒体营销代理
  • 龙岩seo西安网络seo公司
  • 南浔做网站seo引擎搜索网站关键词
  • 长沙网红打卡景点河南百度关键词优化排名软件
  • 网页软件开发郑州seo全网营销
  • 企业网站都需要备案吗seo网络推广企业
  • 赌博网站开发软件网络app推广是什么工作
  • 拼团购物网站开发房管局备案查询网站