applicaiton.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: koa2weibo
password: passMYblog123
url: jdbc:mysql://rm-uf6t9m0yx22dxrya5no.mysql.rds.aliyuncs.com:3306/koa2weibo?serverTimezone=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=UTF8
thymeleaf:
cache: false
jpa:
hibernate:
ddl-auto: update
show-sql: true
数据库
`id` BIGINT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`address` VARCHAR(255) NULL,
`phone` VARCHAR(50) NULL,
`update_date` DATETIME NOT NULL,
`create_date` DATETIME NOT NULL,
`delete_date` DATETIME NULL,
INSERT INTO `sampledb`.`user` (`id`, `name`, `address`, `phone`, `update_date`, `create_date`) VALUES ('1', 'テスト太郎', '東京都サンプル区1-1', '080-0000-0000', '2019-05-06 12:00:00', '2019-05-01 12:00:00');
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
entity/pojo层
package com.xuwen.entity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Id;
import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import java.util.Date;
/**
* author:xuwen
* Created on 2022/4/22
*/
@Entity
@Data
@Table(name="user_test")
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
@AllArgsConstructor
@NoArgsConstructor
public class User {
//注意不要倒错包,否则启动报错!!!import javax.persistence.Id;
@Id//主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增主键
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "address")
private String address;
@Column(name = "phone")
private String phone;
@Column(name = "update_date")
private Date updateDate;
@Column(name = "create_date")
private Date createDate;
@Column(name = "delete_date")
private Date deleteDate;
}repository层接口
package com.xuwen.repository;
import com.xuwen.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* author:xuwen
* Created on 2022/4/22
*/
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
}service层
package com.xuwen.service;
import com.xuwen.entity.User;
import com.xuwen.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;
/**
* author:xuwen
* Created on 2022/4/22
*/
@Service
@Transactional(rollbackOn = Exception.class)
public class UserService {
@Autowired
UserRepository userRepository;
//search all the users from databases;
public List<User> searchAll(){
return userRepository.findAll();
}
}
controller层
package com.xuwen.controller;
import com.xuwen.entity.User;
import com.xuwen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
/**
* author:xuwen
* Created on 2022/4/22
*/
@Controller
class UserControlle {
//control-》调用service层
@Autowired
UserService userService;
@GetMapping("/user/list")
public String displayList(Model model){
List<User> userList = userService.searchAll();
model.addAttribute("userlist",userList);
return "user/list";
}
}前端,css,
html->thymleaf
<!DOCTYPE html>
<html lang="en" xmlns:th=“http://www.thymeleaf.org”>
<head>
<meta charset="UTF-8">
<title>ユーザー情報一覧</title>
<link href="/css/list.css" rel="stylesheet"></link>
</head>
<body>
<h1>ユーザー情報一覧</h1>
<table>
<thread>
<tr>
<th>ID</th>
<th>名前</th>
<th>住所</th>
<th>電話番号</th>
<th>更新日時</th>
<th>登録日時</th>
<th>削除日時</th>
</tr>
</thread>
<tbody>
<tr th:each="user:${userlist}" th:object="${user}">
<td class="center" th:text="*{id}"></td>
<td th:text="*{name}"></td>
<td th:text="*{address}"></td>
<td class="center" th:text="*{phone}"></td>
<td class="center" th:text="${#dates.format(user.updateDate,'yyyy/MM/dd')}" ></td>
<td class="center" th:text="${#dates.format(user.createDate,'yyyy/MM/dd')}" ></td>
<td class="center" th:text="${#dates.format(user.deleteDate,'yyyy/MM/dd')}" ></td>
</tr>
</tbody>
</table>
</body>
</html>
css->
table {
width: 90%;
border-collapse: collapse;
font-size: 12px;
}
table th, table td {
border: 1px solid #ddd;
padding: 6px;
}
table th {
background-color: #F2F2F2;
}
.center {
text-align: center;
}
启动之后效果
