一.新建Spring Starter project,导入相关依赖
二.application.properties配置文件参数
server.port=8989
#配置数据源的参数 Mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=briup
#如果需要,可以显示执行的sql语句
spring.jpa.show-sql=true
User.java
```handlebars
```handlebars
```handlebars
```java
package com.briup.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* @author 作者 angel:
* @version 创建时间:2020年6月27日 下午5:57:29
* 类说明
*/
@Entity(name = "t_user")
public class User {
//设置MySql数据库主键自增长
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer age;
private String gender;
public User() {
super();
}
public User(Integer id, String name, Integer age, String gender) {
super();
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + "]";
}
}
三.这里使用springDataJpa技术,所以dao层新建接口继承Jparepository接口,
Jparepository接口中有一些简单的方法可以直接用,一些复杂的sql语句可以根据SpringData的语法重写所需要的方法,这个技术就是为了我们减少编写过多的sql语句,
package com.briup.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.briup.bean.User;
/**
* @author 作者 angel:
* @version 创建时间:2020年6月28日 下午8:16:42
* 类说明
*/
public interface UserDao extends JpaRepository<User, Integer>{
}
IUserService.java
//一定要加注解,这样才能将此类放入Ioc容器中,供其他类使用,降低耦合
@Service
public class IUserService {
@Autowired
private UserDao dao;
public void saveUser(User user) {
dao.save(user);
}
public List<User> findAll() {
return dao.findAll();
}
}
UserController.java
@Controller
public class UserController {
@Autowired
private IUserService service;
@RequestMapping("/toUser")
public String toUser(HttpSession session) {
List<User> list = service.findAll();
for (User user : list) {
System.out.println(user);
}
session.setAttribute("users", list);
return "user";
}
}
四.在templates文件夹中新建user.html,使用thymleaf技术实现视图层的展示.
user.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
<table>
<thead>
<tr style="background: #E8E8E8;">
<th>序号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
</thead>
<tbody th:each="user:${session.users}">
<tr>
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.gender}"></td>
<td th:text="${user.age}"></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
最后访问http://localhost:8989/toUser;
就实现了从数据持久层到视图层的遍历