接着上一节的

第一步:在pom文件中加入以下代码:

<!--JPA-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <!-- MySql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

第二步:在application.yml文件中加入以下代码

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
#useUnicode=true&characterEncoding=utf-8:这个代表允许用户自己设定数据库编码,而且设置成UTF-8
#&useSSL=false:
url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root jpa:
hibernate:
ddl-auto: update
#ddl-auto:有五个值可选,create:是每次都是先检查表是否存在,如果存在删除再新建;update:不会新建只是更新;
# create-drop:新建但是一旦sessionFactory停止就自动销毁;none:什么都不做;validate:验证表结构 show-sql: true
#show-sql:是否显示sql语句

第三步:在bean包选新建User类

package com.oda.springboot.bean;

import org.springframework.stereotype.Component;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; //@Component
@Entity
//@Entity说明这个class是实体类,并且使用默认的orm规则,即class名即数据库表中表名,class字段名即表中的字段名
//如果想改变这种默认的orm规则,就要使用@Table来改变class名与数据库中表名的映射规则,@Column来改变class中字段名与db中表的字段名的映射规则
public class User {
@Id
@GeneratedValue
private int id;
private String name;
private int age; public User() {
} public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}

第四步:新建dao包,在其下面新建UserMapper接口

package com.oda.springboot.dao;

import com.oda.springboot.bean.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; public interface UserMapper extends JpaRepository<User,Integer> {
}

第五步:新建service包,在其下面新建Userservice类

package com.oda.springboot.service;

import com.oda.springboot.bean.User;
import com.oda.springboot.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import javax.annotation.Resource;
import java.util.List; @Service
public class UserService {
@Autowired
private UserMapper userMapper; public List<User> users() {
return userMapper.findAll();
}
}

第六步:在controller包下新建UserController

package com.oda.springboot.controller;

import com.oda.springboot.bean.User;
import com.oda.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
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 { @Resource
private UserService userService; @RequestMapping("/users")
public String uses(){
return "redirect:/select";
} @RequestMapping("/select")
public String select(Model model){
List<User> users = userService.users();
model.addAttribute("users",users);
return "users/index";
}
}

第七步:在templates包下,新建users包,在其下新建index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--这里引入thymeleaf命名空间-->
<head>
<meta charset="UTF-8">
<title>thymeleaf模板的应用</title>
</head>
<body>
<table>
<tr>
<td>序号</td>
<td>名字</td>
<td>年龄</td>
</tr>
<tr th:each="user,count:${users}">
<td th:text="${count.count}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
</tr>
</table> </body>
</html>

然后启动,访问http://localhost:8080/zm/users

项目结构:

注意:application.yml文件中的

    url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false

SpringBoot(三)thymeleaf+JPA+MySql的更多相关文章

  1. SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二

    SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一 SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二 方法一 使用原生sql查询 或者 为方法名增加 ...

  2. SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一

    SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一 SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二 pom引用 <?xml version=& ...

  3. springboot+jpa+mysql+redis+swagger整合步骤

    springboot+jpa+MySQL+swagger框架搭建好之上再整合redis: 在电脑上先安装redis: 一.在pom.xml中引入redis 二.在application.yml里配置r ...

  4. springboot+jpa+mysql+swagger整合

    Springboot+jpa+MySQL+swagger整合 创建一个springboot web项目 <dependencies> <dependency>      < ...

  5. Springboot+Atomikos+Jpa+Mysql实现JTA分布式事务

    1 前言 之前整理了一个spring+jotm实现的分布式事务实现,但是听说spring3.X后不再支持jotm了,jotm也有好几年没更新了,所以今天整理springboot+Atomikos+jp ...

  6. SpringBoot+Jpa+MySql学习

    上一篇介绍了springboot简单整合mybatis的教程.这一篇是介绍springboot简单整合jpa的教程. 由于jpa的功能强大,后续会继续写关于jpa的介绍已经使用,本文只是简单介绍一下它 ...

  7. spring-boot jpa mysql emoji utfmb4 异常处理

    spring-boot jpa mysql utf8mb4 emoji 写入失败 mysql database,table,column 默认为utf8mb4 Caused by: java.sql. ...

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

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

  9. 三、SpringBoot整合Thymeleaf视图

    目录 3.1 Thymeleaf视图介绍 3.2 创建SpringBoot项目 3.2 配置Thymeleaf 3.3 编写Demo 3.4 小结 3.1 Thymeleaf视图介绍 先看下官网的介绍 ...

随机推荐

  1. 给datagrid的日期格式化成年月日

    $('#infos').datagrid({ title:'系统版本列表', iconCls:'icon-view', method:'POST', singleSelect:false, fit : ...

  2. Node.js基础学习一之Get请求

    本人从事的是前端开发,这段时间公司开发项目比较少所以就想着学点东西,然后就想到了Node.js ,跟着菜鸟教程学了点,不过我觉得最好的学习方法是带着需求来学习. 其实和服务端打交道无非就是能有一个可以 ...

  3. Python events

    Events不同线程之间同步对象 参数说明: # 实例化event对象 event = threading.Event() # 等待检测标志位被设定,标志位设置后就不阻塞了 # 客户机线程可以等待设置 ...

  4. Linux 简单文本处理

    1.创建文件加“.”带表隐藏文件 2.password文件内“user:x:501:501::/home/lishiming:/bin/bash”含义:   用户名:密码控位键:UID:GID:用户解 ...

  5. 动态从数据库获取数据,省市县三级联动,有校验,导出Excel模板

    话不多说,看效果图,直接上代码. sheet  商户表 hideSheet ,功能完成后隐藏的Sheet,用于储存下拉框中的信息,(以一定的规则将所需数据存储在表格中). 下面是代码 部分数据需要在导 ...

  6. Win10在右键菜单添加在此处打开命令窗口项

    转载:https://jingyan.baidu.com/article/2d5afd6930107a85a2e28e2d.html 第一步:新建一个文本文档,输入如下代码,然后另存为OpenCmdH ...

  7. P3829 [SHOI2012]信用卡凸包

    思路 注意到结果就是每个信用卡边上的四个圆心的凸包周长+一个圆的周长 然后就好做了 注意平行时把距离小的排在前面,栈中至少要有1个元素(top>1),凸包中如果存在叉积为0的点也要pop,否则可 ...

  8. (转载)Unity3D开发之编辑器统一修改Text字体

    最近遇到一个需求,就是我们在做完一个场景后,美工感觉字体不好看,效果不是很好,想要换一种字体.UGUI的界面已经搭完,如果要一个一个Text寻找,工作量将是巨大.而且作为程序人员是不会容忍自己做这些机 ...

  9. spark生成大宽表的parquet性能优化

    1.  背景介绍 将一份数据量很大的用户属性文件解析成结构化的数据供查询框架查询剖析,其中用户属性包含用户标识,平台类型,性别,年龄,学历,兴趣爱好,购物倾向等等,大概共有七百个左右的标签属性.为了查 ...

  10. 使用ByteArrayOutputStream解决IO乱码问题的踩坑记录

    经过:今天在用s3接口做ceph储存的时候,要实现一个io下载的接口.需要把InputStream转成byte[],一开始,是的写法是这样的: byte[] buf = new byte[(int) ...