Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例详解编程语言
2023-06-13 09:11:43 时间
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
(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.jdbc.Driver spring.jpa.properties.hibernate.hbm2ddl.auto=update 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目录用于放置网站的静态内容如css、js、图片;templates目录用于放置项目使用的页面模板。
2,启动类启动类需要添加Servlet的支持:
@SpringBootApplication public class JpaThymeleafApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(JpaThymeleafApplication.class); public static void main(String[] args) throws Exception { SpringApplication.run(JpaThymeleafApplication.class, args); }3,数据库层代码
实体类映射数据库表:
@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; ... }
继承JpaRepository类会自动实现很多内置的方法,包括增删改查。
public interface UserRepository extends JpaRepository User, Long { User findById(long id); Long deleteById(Long id); }4,业务层处理
service调用jpa实现相关的增删改查,实际项目中service层处理具体的业务代码。
@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.delete(id); }
Controller负责接收请求,处理完后将页面内容返回给前端。
@Controller public class UserController { @Resource UserService userService;
return "user/userEdit"; 代表会直接去resources目录下找相关的文件。 return "redirect:/list"; 代表转发到对应的controller,这个示例就相当于删除内容之后自动调整到list请求,然后再输出到页面。 5,页面内容list列表:
!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 br/ h1 用户列表 /h1 br/ br/ div table thead 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 div a href="/toAdd" th:href="@{/toAdd}" add /a /div /div /body /html效果图:
tr th:each="user : ${users}" 这里会从controler层model set的对象去获取相关的内容,th:each表示会循环遍历对象内容。
修改页面:
!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 br/ h1 修改用户 /h1 br/ br/ div form th:action="@{/edit}" th:object="${user}" method="post" input type="hidden" name="id" th:value="*{id}" / div label for="userName" userName /label div input type="text" name="userName" id="userName" th:value="*{userName}" placeholder="userName"/ /div /div div label for="password" Password /label div input type="password" name="password" id="password" th:value="*{password}" placeholder="Password"/ /div /div div label for="age" age /label div input type="text" name="age" id="age" th:value="*{age}" placeholder="age"/ /div /div div div input type="submit" value="Submit" / nbsp; nbsp; nbsp; a href="/toAdd" th:href="@{/list}" Back /a /div /div /form /div /body /html添加页面和修改类似就不在贴代码了。
效果图:
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16653.html
cjavamysqlxml
相关文章
- 玩转 Spring Boot 原理篇(自动装配源码剖析)(十二)
- 玩转 Spring Boot 应用篇(项目打包、发布)(二十)
- Spring Boot入门笔记
- Spring Boot 实现万能文件在线预览
- spring boot activiti工作流_activiti工作流优缺点
- 正式抛弃 Feign!Spring 6 推出新特性:HTTP Interface,这波太秀了!
- Spring监听器-spring源码详解(五)
- Spring Boot 3.0横空出世,快来看看是不是该升级了
- Spring Boot的安全配置(二)
- Spring Boot应用中引入Zuul-示例
- spring-boot教程详解编程语言
- Spring Boot 主类及目录结构介绍详解编程语言
- Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏详解编程语言
- Spring Boot 2 (四):使用 Docker 部署 Spring Boot详解编程语言
- Spring Boot(十一):Spring Boot中MongoDB的使用详解编程语言
- spring boot详解编程语言
- spring boot 集成 Swagger 接口文档详解编程语言
- Spring boot 定时器详解编程语言
- java spring boot 打包方法详解编程语言
- Spring Boot 2.x 系列教程:WebFlux REST API 全局异常处理 Error Handling