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 ...
随机推荐
- 2019大厂Java岗面试题全曝光,刷完这1020道,金三银四大厂等你
2019大厂Java岗面试题全曝光,刷完这1020道,金三银四大厂等你 前言: 本文收集整理了各大厂常见面试题N道,你想要的这里都有 内容涵盖:Java.MyBatis.ZooKeeper.Dubbo ...
- Lucene为什么要加Segment概念
目前我感觉加了Segment有两个好处: 1. 简化了写文档的逻辑,解耦了写文档和读文档.如果没有Segment在写文档的时候势必要修改整个索引,所以会影响到文档的读 2. 提升了写文档的速度,由于只 ...
- 让windows 10 家庭版 支持 Hyper-v 的方法
pushd "%~dp0" dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hyper-v.txt for /f ...
- JavaScript BOM Cookie 的用法
JavaScript Cookie Cookie是计算机上存储在小文本文件中的数据.当Web服务器将网页发送到浏览器时,连接将关闭,服务器将忘记用户的所有内容.发明Cookie是为了解决“如何记住用户 ...
- JAVA8新特性--集合流操作Stream
原文链接:https://blog.csdn.net/bluuusea/article/details/79967039 Stream类全路径为:java.util.stream.Stream 对St ...
- Struts2 : action跳转时带参数跳转
在实现action跳转到另一个action时,需要携带参数,可以直接在struts.xml配置文件中对应的跳转action的地方加上,参数的配置,用ognl表达式,可以从session中取值. 如果要 ...
- 【推荐】全球最全面的Telegram组群频道的集合网站 持续收集中
全球最全面的Telegram组群频道的集合网站 https://www.telegramgroup.org Telegram 组群频道分享 可搜索自己想找的组群频道 从小白到大神,一个 telegra ...
- [b0009] 玩Hadoop中碰到的各种错误
1. Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class mp.filetest.WordCount2 ...
- MySQL详细安装(windows)
一.在MySQL官网上下载MySQL Community Server(目前推荐5.7版本,8.0版本在安装的时候遇到的问题比较多) 点击DOWNLOADS→Community→MySQL Commu ...
- Rust中的模块及私有性控制
好像没有其它语言的private, protected关键字,应了一个public关键字. mod plant { pub struct Vegetable { pub name: String, _ ...