SpringBoot整合SpringDataJPA及在页面yaml中显示
SpringBoot整合SpringDataJPA及在页面yaml中显示
1:创建对应的数据表
2:添加依赖
3:配置数据源
1:创建对应的数据表
CREATE TABLE `user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) DEFAULT NULL,
`password` VARCHAR(50) DEFAULT NULL,
`name` VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张无忌');
INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');
2:添加依赖
<!--添加spring mvc 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<!--添加springdatajpa的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--模板依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
注意:版本,有些版本不支持,我所使用的如下
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
//注意继承父类的版本
<version>2.0.2.RELEASE</version>
</parent> <groupId>com.offcn</groupId>
<artifactId>springbootdemo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
3:配置数据源
#DB Configation
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
//注意出现连接不上数据库那么在tx后面添加 ?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
url: jdbc:mysql://127.0.0.1:3306/tx
username: root
password: 813100
jpa:
database: MySQL
show-sql: true
generate-ddl: true
4:实体类User,以及对于的dao接口和Controller代码
注意:要在实体类上添加@Entity和@Table注解
User:
package com.offcn.springbootdemo1.bean;
import javax.persistence.*; //实体类
@Entity
//要连接的数据库
@Table(name = "user")
public class User {
//主键自增长
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String username;
private String password;
private String name;
//此处省略set,get,toString,以及对应的构造方法
}
UserDao:
需要继承JpaRepository接口
package com.offcn.springbootdemo1.dao; import com.offcn.springbootdemo1.bean.User;
import org.springframework.data.jpa.repository.JpaRepository; public interface UserDao extends JpaRepository<User,Integer> {
}
UserController:
package com.offcn.springbootdemo1.controller; //导入所需的包 @Controller
public class UserController {
@Autowired
private UserDao userDao; //直接响应查询到的jsonList集合
@RequestMapping("/user/list")
@ResponseBody
public List<User> userListAll(){
List<User> userList = userDao.findAll();
return userList;
} //跳转到User.ftl页面,并携带List<User>集合
@RequestMapping("/user/table")
public String userListTable(Model model){
List<User> userList = userDao.findAll();
//注意此处存放名称userList,在User.ftl中通过该名称进行遍历
model.addAttribute("userList",userList);
return "user";
}
}
5:在页面中显示
直接在浏览器中访问jsonList集合

在User.ftl页面中展示
User.ftl
<html>
<head>
<meta charset="UTF-8">
<title>信息展示</title>
</head>
<body>
<table border="1px">
<thead>
<tr>
<th>序号</th>
<th>用户名</th>
<th>密码</th>
<th>姓名</th>
</tr>
</thead>
<tbody>
<!-- 使用后台传过来的userList遍历并取值 -->
<#list userList as user>
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.name}</td>
</tr>
</#list>
</tbody>
</table>
</body>
</html>
浏览器访问结果

SpringBoot整合SpringDataJPA及在页面yaml中显示的更多相关文章
- springboot整合springdata-jpa
1.简介 SpringData : Spring 的一个子项目.用于简化数据库访问,支持NoSQL 和 关系数据存储.其主要目标是使数据库的访问变得方便快捷. SpringData 项目所支持 No ...
- 从无到有Springboot整合Spring-data-jpa实现简单应用
本文介绍Springboot整合Spring-data-jpa实现简单应用 Spring-data-jpa是什么?这不由得我们思考一番,其实通俗来说Spring-data-jpa默认使用hiberna ...
- SpringBoot整合SpringDataJPA,今天没啥事情就看了一下springboot整合springdataJPA,实在是香啊,SQL语句都不用写了
SpringBoot整合SpringDataJPA 1.JPA概念 JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映 ...
- springboot 整合springDataJPA
springboot 整合springDataJPA 〇.搭建springboot环境 一.添加依赖 mysql <!-- mysql驱动 --> <dependency> & ...
- springboot整合springdatajpa时jar冲突
1.springboot整合springdatajpa测试时报No bean named 'entityManagerFactory' available错误 2.运行springboot主程序时报以 ...
- 在ActiveReports页面报表中显示Google地图
有些报表需求中需要我们显示国家.城市等地址信息,在报表中添加地图信息会让报表给最终用户代码更多有效信息. 在报表中可以将地图作为图片添加进来,当一个图片显示在报表中时,该图片必须存放到本地计算机或者服 ...
- 七、springboot整合Spring-data-jpa
1.Spring Data JPA是什么 由Spring提供的一个用于简化JPA开发的框架.可以在几乎不用写实现的情况下,实现对数据的访问和操作.除了CRUD外,还包括如分页.排序等一些常用的功能 1 ...
- 【使用篇二】SpringBoot整合SpringDataJPA(18)
一.pom.xml添加依赖 <dependencies> <!--web--> <dependency> <groupId>org.springfram ...
- springboot整合html时的页面的跳转404
在用springboot对html的页面进行渲染时,页面找不到报404(type=Not Found, status=404)., 解决办法:是在ctroller层加相应的 @Re ...
随机推荐
- ssh-agent的作用
明明在github上配置了ssh公钥,拉代码时却报错: sign_and_send_pubkey: signing failed: agent refused operation 解决方法:在~/.z ...
- Linux nodejs 安装以及配置环境
从官网中下载nodejs 打开官网 https://nodejs.org/en/download/ 复制拿到链接,下载nodejs wget https://nodejs.org/dist/v10.1 ...
- python基础编程——类和实例
在了解类和实例之前,需要先了解什么是面向对象,什么又是面向过程.面向过程是以过程为中心实现一步步操作(相互调用,类似流水线思想):面向对象是以事物为中心,某个事物可以拥有自己的多个行为,而另一个事物也 ...
- 玄学 npm报错记录
刚开始是版本原因npm报错,ok卸载重装就可以了, 后面报错 npm ERR! code ENOGIT npm ERR! No git binary found in $PATH npm ERR! n ...
- flink Transitive Closure算法,实现寻找新的可达路径
flink 使用Transitive Closure算法实现可达路径查找. 1.Transitive Closure是翻译闭包传递?我觉得直译不准确,意译应该是传递特性直至特性关闭,也符合本例中传递路 ...
- docker仓库资源的地址修改
docker仓库资源的地址修改 1.新增或者修改/etc/docker目录下的daemon.json文件 样例文件如下 { "registry-mirrors": ["h ...
- Linux rpm包管理工具
1.什么是rpm包 是一种编译好的二进制软件包,安装速度快. 2.rpm包命名 el6:软件包用于在Red Hat 6.x, CentOS 6.x, and CloudLinux 6.x进行安装 el ...
- 深入解读Linux进程调度Schedule【转】
转自:https://blog.csdn.net/Vince_/article/details/88982802 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文 ...
- ssh config 配置
一个打开新连接免密码的小技巧 Host * ControlMaster auto ControlPath ~/.ssh/master-%r@%h:%p
- 01-人脸识别-基于MTCNN,框选人脸区域-detect_face_main
(本系列随笔持续更新) 搭建要求 详细的搭建过程在 参考资料1 中已经有啦. TensorFlow 1.6.0 OpenCV 2.4.8 仅仅是加载和读取图片的需要 Ubuntu 14.04 64bi ...