简单才是美! SpringBoot+JPA
SpringBoot 急速构建项目,真的是用了才知道,搭配JPA作为持久层,一简到底!
下面记录项目的搭建,后续会添加NOSQL redis,搜索引擎elasticSearch,等等,什么不过时就加什么。
开发工具idea、项目构建gradle、模板引擎thymeleaf
项目构建
1.【new】 -> 【product】 -> 选择Spring Initializr -> 【next】
2.填写Group,Artifact,Type ->【next】
3.导包
- 1.左边选择Web右边勾选Web
2.左边选择SQL右边勾选JPA
3.左边选择SQL右边勾选mysql
4.左边选择Template Engines右边勾选Thymeleaf
5.【next】->【finish】
好了
现在的项目结构
BootjpaApplication 是项目的启动类
resources/templates/ 文件夹是放页面的
build.gradle 存放jar包坐标
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
@RestController
配置完成,写个controller试试看
@RestController
public class HelloBootController {
@RequestMapping("helloBoot")
public String helloBoot(){
return "Hello Boot-JPA";
}
}
在BootjpaApplication文件上启动
@RestController注解,代替@Controller+@@ResponseBody
那么返回页面就直接用@Controller就好了
现在JPA登场
db
注解和hibernate一样。
@Entity
public class User {
private long id;
private String name;
private String passWord;
private String email;
@Id
@GeneratedValue
public long getId() {
return id;
}
。。。。。
}
现在,就是见证奇迹的时刻!
dao
dao层继承JpaRepository即可
public interface UserRepository extends JpaRepository<User,Long> {
}
什么!这就完了??对,低调
controller
controller层,service层跳过。
@Controller
public class HelloBootController {
@Autowired
UserRepository userRepository;
@RequestMapping("/toHello")
public String toHello(ModelMap modelMap){
userRepository.save(new User("Mshu","123456","zhuiqiu95@foxmail.com"));
List<User> users = userRepository.findAll();
modelMap.put("users",users);
return "helloBoot";
}
}
thymeleaf
至于页面,默认是在resources/templates/下的html,试图解析器已经配置默认配置好的。
前缀:resources/templates/
后缀:html
那我们就在resources/templates/下新建一个html页面
注意<html xmlns:th="http://www.thymeleaf.org " lang="en">引入thymeleaf
用到了thymeleaf语法遍历。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table >
<tr th:each="user,userState : ${users}">
<td style="border: 1px solid seagreen" th:text="${user.name}"></td>
<td style="border: 1px solid seagreen" th:text="${user.passWord}"></td>
<td style="border: 1px solid seagreen" th:text="${user.email}"></td>
</tr>
</table>
</body>
</html>
启动,输入地址,回车!
精彩回顾
刚刚dao层明明只写了一个接口没有写任何方法,怎么就能调用save(),findAll()呢,
对JPA默认了许多基础增删改查方法,直接调用即可。
怎么写除了默认给出的方法以外怎么写呢,
public interface UserRepository extends JpaRepository<User,Long> {
User findByName(String name);
}
调用的话直接
User user = userRepository.findByName("Mshu");
那么怎么做的映射的,它怎么知道我的参数name对应表里的name,原来名字一样就可以映射,好像很有道理
没错就那么简单,这种写法太hibernate了。
本文转载于猿2048:简单才是美! SpringBoot+JPA
简单才是美! SpringBoot+JPA的更多相关文章
- 难部署的taiga,式微的circus——趋势从进程管理到容器管理,简单才是美
一直需要一个项目管理系统,一直没时间弄. taiga是github上搜project management star最多的项目,又是基于django用python写的后端,所以就用它: 但是,集中精力 ...
- 补习系列(19)-springboot JPA + PostGreSQL
目录 SpringBoot 整合 PostGreSQL 一.PostGreSQL简介 二.关于 SpringDataJPA 三.整合 PostGreSQL A. 依赖包 B. 配置文件 C. 模型定义 ...
- 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础授权权限
上一篇<[原]无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限>介绍了实现Shiro的基础认证.本篇谈谈实现 ...
- 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限
开发环境搭建参见<[原]无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页> 需求: ① 除了登录页面,在地址栏直接访问其他 ...
- 带着新人学springboot的应用08(springboot+jpa的整合)
这一节的内容比较简单,是springboot和jpa的简单整合,jpa默认使用hibernate,所以本质就是springboot和hibernate的整合. 说实话,听别人都说spring data ...
- SpringBoot JPA + H2增删改查示例
下面的例子是基于SpringBoot JPA以及H2数据库来实现的,下面就开始搭建项目吧. 首先看下项目的整体结构: 具体操作步骤: 打开IDEA,创建一个新的Spring Initializr项目, ...
- SpringBoot JPA 中无法注入 JpaRepository 接口的问题及解决方案
错误: 在Springboot 框架中使用JPA的过程中,怎么来实现数据库操作底层的交互呢?Spring JPA其实已经提供了一套很全面的解决方案,实现对数据库的增.删.查.改只需要继承JPA实现类 ...
- SpringBoot JPA懒加载异常 - com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy
问题与分析 某日忽然发现在用postman测试数据时报错如下: com.fasterxml.jackson.databind.JsonMappingException: could not initi ...
- IDEA SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统
先放上github地址:spike-system,可以直接下载完整项目运行测试 SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统 技术栈:SpringBoot, MyS ...
随机推荐
- c/c++ 常见字符串处理函数总结 strlen/sizeof strcpy/memcpy/strncpy strcat/strncat strcmp/strncmp sprintf/sscanf strtok/split/getline atoi/atof/atol
这里总结工作中经常用到的一些c/c++的字符串处理方法,标黑的是使用频率较高的 1.strlen函数:计算目标字符串长度, 格式:strlen(字符指针指向区域) 注意1:①不包含字符串结束 ...
- LeetCode-007-整数反转
整数反转 题目描述:给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果. 如果反转后整数超过 32 位的有符号整数的范围 [\(−2^{31}\), \(2^{31}\) − ...
- 国产化之Arm64 CPU+银河麒麟系统安装.NetCore
背景 某个项目需要实现基础软件全部国产化,其中操作系统指定银河麒麟,银河麒麟就是一个Linux发行版,数据库使用达梦V8,这个数据库很多概念和Oracle相似,CPU平台的范围:龙芯.飞腾.鲲鹏等. ...
- 字符集编码(四):UTF
在前面文章<字符集编码(中):Unicode>中我们聊了 Unicode 标准并提到其有三种实现形式:UTF-16.UTF-8 和 UTF-32,本篇我们就具体聊聊这三种 UTF 是怎么实 ...
- 前端工程化:使用 shelljs 生成 yapi 接口文件
之前的文章介绍了使用 yapi-to-typescript (下文简称 ytt)生成接口类型定义文件,方便我们直接使用接口的请求和响应类型,能减少很多写接口类型的时间. 使用 yapi-to-type ...
- 超强视频超分AI算法,从此只看高清视频
最近发现一个特别强的视频超分算法----BasicVSR,在真实世界数据集中,实现了前所未有的视觉重建效果,最近它还拿下了超分比赛NTIRE 2021三冠一亚的优异成绩,登上了CVPR 2022. 视 ...
- php 23种设计模式 - 命令模式
命令模式 将一个请求封装为一个对象,从而使用户可用不同的请求对客户进行参数化.对请求排队或记录请求日志,以及支持撤销的操作. 命令模式以松散耦合主题为基础,发送消息.命令和请求,或通过一组处理程序发送 ...
- php 23种设计模型 - 适配器模式
适配器模式(Adapter) 适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁.这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能. 这种模式涉及到一个单一的类, ...
- 你真的懂TSP吗
整数规划-模型 2022年2月26日 TSP问题 有一个邮递员,他每天负责向100个客户的家门口送一份报纸,他希望走最短的总路程来完成配送任务,这是TSP问题最原始的场景.用理论描述就是:找一个路径 ...
- VuePress 博客优化之增加 Valine 评论功能
前言 在 <一篇带你用 VuePress + Github Pages 搭建博客>中,我们使用 VuePress 搭建了一个博客,最终的效果查看:TypeScript 中文文档. 本篇讲讲 ...