SpringBoot(三)thymeleaf+JPA+MySql
接着上一节的
第一步:在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的更多相关文章
- SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二
SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一 SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二 方法一 使用原生sql查询 或者 为方法名增加 ...
- SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一
SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一 SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二 pom引用 <?xml version=& ...
- springboot+jpa+mysql+redis+swagger整合步骤
springboot+jpa+MySQL+swagger框架搭建好之上再整合redis: 在电脑上先安装redis: 一.在pom.xml中引入redis 二.在application.yml里配置r ...
- springboot+jpa+mysql+swagger整合
Springboot+jpa+MySQL+swagger整合 创建一个springboot web项目 <dependencies> <dependency> < ...
- Springboot+Atomikos+Jpa+Mysql实现JTA分布式事务
1 前言 之前整理了一个spring+jotm实现的分布式事务实现,但是听说spring3.X后不再支持jotm了,jotm也有好几年没更新了,所以今天整理springboot+Atomikos+jp ...
- SpringBoot+Jpa+MySql学习
上一篇介绍了springboot简单整合mybatis的教程.这一篇是介绍springboot简单整合jpa的教程. 由于jpa的功能强大,后续会继续写关于jpa的介绍已经使用,本文只是简单介绍一下它 ...
- spring-boot jpa mysql emoji utfmb4 异常处理
spring-boot jpa mysql utf8mb4 emoji 写入失败 mysql database,table,column 默认为utf8mb4 Caused by: java.sql. ...
- IDEA SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统
先放上github地址:spike-system,可以直接下载完整项目运行测试 SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统 技术栈:SpringBoot, MyS ...
- 三、SpringBoot整合Thymeleaf视图
目录 3.1 Thymeleaf视图介绍 3.2 创建SpringBoot项目 3.2 配置Thymeleaf 3.3 编写Demo 3.4 小结 3.1 Thymeleaf视图介绍 先看下官网的介绍 ...
随机推荐
- 使用日期插件用js处理日期格式
function compareDate(checkStartDate, checkEndDate) { var arys1= new Array(); var arys2= new Ar ...
- MVC设计思路
MVC 学会重复.学会总结.学会预习和练习 前端页面 <----> 服务器(控制层.业务层.DAO层) <---> DB 说明:无论是框架还是servletJSP,用的 ...
- Docker Kubernetes 常用命令
Docker Kubernetes 常用命令 增 # 通过文件名或标准输入创建资源. kubectl create # 读取指定文件内容,进行创建.(配置文件可指定json,yaml文件). kube ...
- Python Semaphore
Semaphore信号量的使用 信号量: 互斥锁 同时只允许一个线程更改数据,而Semaphore是同时允许一定数量的线程更改数据 ,比如厕所有3个坑,那最多只允许3个人上厕所,后面的人只能等里面有人 ...
- 2019年1月6日 没有nainai吃 习题1
1列举布尔值是False的所有值 0,False,'',[],{},(),None 2根据范围获取其中3和7整除的所有数的和,并返回调用者:符合条件的数字个数以及符合条件的数字的总和 def func ...
- 利用JS打印质数
我爱撸码,撸码使我感到快乐!大家好,我是Counter,今天非常愉快,没有前几天的相对比较复杂的逻辑思维在里面,今天来写写,利用JS打印质数,基本上很多面试,会很经常的考到.那废话不多说,直接上代码: ...
- 单元测试系列之二:Mock工具Jmockit实战
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6760272.html Mock工具Jm ...
- Linux 查看进程之PS命令
要对进程进行检测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程运行状态.Linux 系统中 我们可以使用 ps 命令查看进程. ps 命令介绍 ps(process stat ...
- css基础参考文档
block inline-block inline区别 absolute定位详解:https://www.jianshu.com/p/a3da5e27d22b css浮动详解 float浮动 div变 ...
- 《Blue Flke》团队项目的原型设计与开发
实验目的: 1.掌握软件原型开发技术 2.学习使用软件原型开发工具 实验过程和步骤: 任务1.针对实验六团队项目选题,采用适当的原型开发工具设计团队项目原型. 任务2.在团队博客发布博文,陈述团队项目 ...