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中显示的更多相关文章

  1. springboot整合springdata-jpa

    1.简介  SpringData : Spring 的一个子项目.用于简化数据库访问,支持NoSQL 和 关系数据存储.其主要目标是使数据库的访问变得方便快捷. SpringData 项目所支持 No ...

  2. 从无到有Springboot整合Spring-data-jpa实现简单应用

    本文介绍Springboot整合Spring-data-jpa实现简单应用 Spring-data-jpa是什么?这不由得我们思考一番,其实通俗来说Spring-data-jpa默认使用hiberna ...

  3. SpringBoot整合SpringDataJPA,今天没啥事情就看了一下springboot整合springdataJPA,实在是香啊,SQL语句都不用写了

    SpringBoot整合SpringDataJPA 1.JPA概念 JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映 ...

  4. springboot 整合springDataJPA

    springboot 整合springDataJPA 〇.搭建springboot环境 一.添加依赖 mysql <!-- mysql驱动 --> <dependency> & ...

  5. springboot整合springdatajpa时jar冲突

    1.springboot整合springdatajpa测试时报No bean named 'entityManagerFactory' available错误 2.运行springboot主程序时报以 ...

  6. 在ActiveReports页面报表中显示Google地图

    有些报表需求中需要我们显示国家.城市等地址信息,在报表中添加地图信息会让报表给最终用户代码更多有效信息. 在报表中可以将地图作为图片添加进来,当一个图片显示在报表中时,该图片必须存放到本地计算机或者服 ...

  7. 七、springboot整合Spring-data-jpa

    1.Spring Data JPA是什么 由Spring提供的一个用于简化JPA开发的框架.可以在几乎不用写实现的情况下,实现对数据的访问和操作.除了CRUD外,还包括如分页.排序等一些常用的功能 1 ...

  8. 【使用篇二】SpringBoot整合SpringDataJPA(18)

    一.pom.xml添加依赖 <dependencies> <!--web--> <dependency> <groupId>org.springfram ...

  9. springboot整合html时的页面的跳转404

    在用springboot对html的页面进行渲染时,页面找不到报404(type=Not Found, status=404)., 解决办法:是在ctroller层加相应的           @Re ...

随机推荐

  1. Python【day 11】迭代器

    迭代器-用 1.迭代器的概念 1.可迭代对象-iterable str.list.tuple.dict.set.open().range() 2.可迭代对象的概念: 其数据类型的执行方法中含有__it ...

  2. 如何在unbuntu 16.04上离线部署openssh

    背景:由于部署环境不能联网,为了方便文件传输,需要用到openssh.故实施步骤是,先在可以联网机器上下载离线包,然后用U盘拷贝到部署环境中. 第一步:下载离线包,下载网址:https://packa ...

  3. Django ajax 检测用户名是否已被注册

    添加一个 register.html 页面 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  4. sqlmap 注入的方法及技巧

    sqlmap 注入的方法及技巧 当给 sqlmap 这么一个 url 的时候,它会: 1.判断可注入的参数 2.判断可以用那种 SQL 注入技术来注入 3.识别出哪种数据库 4.根据用户选择,读取哪些 ...

  5. linux 进程通信之 mmap

    一,管道PIPE 二,FIFO通信 三,mmap通信 创建内存映射区. #include <sys/mman.h> void *mmap(void *addr, size_t length ...

  6. linux下网卡捆绑

    七种bond模式说明:mod=0:(balance-rr) Round-robin policy(平衡抡循环策略)mod=1:(active-backup) Active-backup policy( ...

  7. 【洛谷P3973】[TJOI2015]线性代数(最小割)

    洛谷 题意: 给出一个\(n*n\)的矩阵\(B\),再给出一个\(1*n\)的矩阵\(C\). 求一个\(1*n\)的\(01\)矩阵\(A\),使得\(D=(A\cdot B-C)\cdot A^ ...

  8. 解析YAML文件

    YamlMapFactoryBean yamlMapFactoryBean = new YamlMapFactoryBean(); yamlMapFactoryBean.setResources(ne ...

  9. centos7-cockpit

    yum install cockpit* -y systemctl start cockpit.service systemctl enable cockpit.service 修改默认端口9090 ...

  10. 微信小程序,知识点

    对于小程序的授权,只要用户授权一次,该授权关系就会记录在后台,除非删除小程序,或者用户在设置中关闭该授权. 官方文档: https://developers.weixin.qq.com/minipro ...