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视图介绍 先看下官网的介绍 ...
随机推荐
- java 43 接口
- flutter 常用插件
audio_recorder: any #录音.播放 flutter_sound: ^1.1.5#录音 dropdown_menu: ^1.1.0#下拉菜单 simple_permissions:#权 ...
- poj2142 The Balance
poj2142 The Balance exgcd 应分为2种情况分类讨论 显然我们可以列出方程 ax-by=±d 当方程右侧为-d时,可得 by-ax=d 于是我们就得到了2个方程: ax-by=d ...
- #const#const int *p 为何可以不初始化
摘自http://www.myexception.cn/cpp/1900041.html const int *p 为什么可以不初始化?c++ primer 5th P53 写道:const 对象 ...
- P4725 【模板】多项式对数函数
思路 考虑对ln求导后处理 根据复合函数的求导法则\(g'(f(x))=g'(x)f'(x)\) 得到 \[ \ln F(x) '= \frac{F'(x)}{F(x)} \] 最后对这个式子积分 \ ...
- 深度学习标注工具 LabelMe 的使用教程(Windows 版本)
深度学习标注工具 LabelMe 的使用教程(Windows 版本) 2018-11-21 20:12:53 精灵标注助手:http://www.jinglingbiaozhu.com/ LabelM ...
- (转载)Unity UGUI点击不同Button执行不同的方法(无参方法)
将脚本随意挂在任何位置 但是这个btnParent一定是 按钮的父节点 脚本很简单自己敲一遍就全都明白了 上脚本 OnClickTest using UnityEngine; using Un ...
- [大数据从入门到放弃系列教程]在IDEA的Java项目里,配置并加入Scala,写出并运行scala的hello world
[大数据从入门到放弃系列教程]在IDEA的Java项目里,配置并加入Scala,写出并运行scala的hello world 原文链接:http://www.cnblogs.com/blog5277/ ...
- Angular4.0 项目报错:Unexpected value xxxComponent' declared by the module 'xxxxModule'. Please add a @Pipe...
最近刚刚开始学习angular 4.0,在网上找了一个小项目教程学习,然而学习的过程有点艰辛,,各种报错,我明明就是按照博主的步骤老老实实走的呀!!话不多说,上bug- .- Uncaught Er ...
- C#---装箱、拆箱的一个案例
using System; namespace ConsoleApplication1 { interface IInterface { void Add(int num); } struct Tes ...