SpringBoot integration SpringDataJPA and displayed on the page yaml
1: create the corresponding data table
2: Add dependence
3: Configuration Data Source
1: create the corresponding data table
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;
The INTO `user` the VALUES the INSERT ( '. 1', 'zhangsan', '123', 'zhangwuji' );
INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');
2: Add dependence
<! - add spring mvc dependent ->
<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>
<! - add springdatajpa dependence ->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<! - template-dependent ->
<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>
Note: The version, some versions do not support, as I used
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> // Note inherit the parent class version <version>2.0.2.RELEASE</version> </parent> <groupId>com.offcn</groupId> <artifactId>springbootdemo1</artifactId> <version>0.0.1-SNAPSHOT</version>
3: Configuration Data Source
#DB Configation
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
// Note that the connection does not appear on the database then later tx = Add to true & characterEncoding = useUnicode 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>
浏览器访问结果