SpringBoot 急速构建项目,真的是用了才知道,搭配JPA作为持久层,一简到底!
下面记录项目的搭建,后续会添加NOSQL redis,搜索引擎elasticSearch,等等,什么不过时就加什么。

开发工具idea、项目构建gradle、模板引擎thymeleaf

项目构建

1.【new】 -> 【product】 -> 选择Spring Initializr -> 【next】

2.填写Group,Artifact,Type ->【next】

3.导包

  1. 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的更多相关文章

  1. 难部署的taiga,式微的circus——趋势从进程管理到容器管理,简单才是美

    一直需要一个项目管理系统,一直没时间弄. taiga是github上搜project management star最多的项目,又是基于django用python写的后端,所以就用它: 但是,集中精力 ...

  2. 补习系列(19)-springboot JPA + PostGreSQL

    目录 SpringBoot 整合 PostGreSQL 一.PostGreSQL简介 二.关于 SpringDataJPA 三.整合 PostGreSQL A. 依赖包 B. 配置文件 C. 模型定义 ...

  3. 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础授权权限

    上一篇<[原]无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限>介绍了实现Shiro的基础认证.本篇谈谈实现 ...

  4. 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限

    开发环境搭建参见<[原]无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页> 需求: ① 除了登录页面,在地址栏直接访问其他 ...

  5. 带着新人学springboot的应用08(springboot+jpa的整合)

    这一节的内容比较简单,是springboot和jpa的简单整合,jpa默认使用hibernate,所以本质就是springboot和hibernate的整合. 说实话,听别人都说spring data ...

  6. SpringBoot JPA + H2增删改查示例

    下面的例子是基于SpringBoot JPA以及H2数据库来实现的,下面就开始搭建项目吧. 首先看下项目的整体结构: 具体操作步骤: 打开IDEA,创建一个新的Spring Initializr项目, ...

  7. SpringBoot JPA 中无法注入 JpaRepository 接口的问题及解决方案

    错误: 在Springboot  框架中使用JPA的过程中,怎么来实现数据库操作底层的交互呢?Spring JPA其实已经提供了一套很全面的解决方案,实现对数据库的增.删.查.改只需要继承JPA实现类 ...

  8. SpringBoot JPA懒加载异常 - com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy

    问题与分析 某日忽然发现在用postman测试数据时报错如下: com.fasterxml.jackson.databind.JsonMappingException: could not initi ...

  9. IDEA SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统

    先放上github地址:spike-system,可以直接下载完整项目运行测试 SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统 技术栈:SpringBoot, MyS ...

随机推荐

  1. 前端好用API之getBoundingClientRect

    前情 在前端开发需求中,经常需要获取元素的尺寸位置相关的属性,以往的做法是调用不同api获取相关属性的. getBoundingClientRect介绍 getBoundingClientRect() ...

  2. Python——序列与字符串

    序列与字符串 一. 列表 列表是Python的内置可变序列,是包含若干元素的有序连续内存空间.同一列表元素的数据类型可以各不相同,可以分别为整数.实数.字符串等基本类型,也可以是列表.元组.字典.集合 ...

  3. Vue 源码解读(11)—— render helper

    前言 上一篇文章 Vue 源码解读(10)-- 编译器 之 生成渲染函数 最后讲到组件更新时,需要先执行编译器生成的渲染函数得到组件的 vnode. 渲染函数之所以能生成 vnode 是通过其中的 _ ...

  4. php简易表单及下拉框动态渲染

    <?php//1.连接数据库$link = mysqli_connect('127.0.0.1','root','root','1906');//2.设置字符集mysqli_set_charse ...

  5. htm5基本学习

    HTML学习 1.HTML概念 1.1.HTML是什么 Hyper Text Markup Language (超文本标记语言)包括:文字.图片.音频.视频.动画等. 1.2.HTML优势 所有浏览器 ...

  6. LGP5142题解

    题意简明,不说了( 因为教练让同学们做线段树的题,早就会了线段树的我就来爆踩水水蓝了/kk 首先推一下柿子: \[\frac 1 n\sum_{i=1}^n(a_i^2-2 \times a_i \t ...

  7. emu8086实现两位数加法运算

    题目说明:给出一个公式,例如 "35 + 28 = ",输出计算结果 一.准备材料 DOS功能调用表:https://blog.csdn.net/mybelief321/artic ...

  8. 消息中间件-RabbitMq相关概念及原理介绍【图文并茂】

    消息中间件 消息中间件的作用 解耦:消息中间件在服务之间插入了一个隐含的.基于数据的接口层.两边的服务处理过程都要实现这一接口,这允许我们独立的扩展或修改两边的处理过程,只要确保他们遵守相同的规范约束 ...

  9. ArcMap连接oracle、oracle配置

    服务器:Oracle 11g 客户端:arcgis desktop 10.4.1.oracle 11g 32位客户端 客户端:arcgis server 10.4.1.oracle 11g 64位客户 ...

  10. 一个简单的模拟实例说明Task及其调度问题

    Task对于.NET的重要性毋庸置疑.通过最近的一些面试经历,发现很多人对与Task及其调度机制,以及线程和线程池之间的关系并没有清晰的认识.本文采用最简单的方式模拟了Task的实现,旨在说明Task ...