也许,不负光阴就是最好的努力,而努力就是最好的自己。——村上春树
SpringBoot整合jpa
1、添加相应的依赖
在建好的依赖基础上添加如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
2、配置文件
在application.yml文件中作如下配置,连接数据库,并且配置jpa
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/jpa?useSSL=true&serverTimezone=UTC&characterEecoding=UTF-8
jpa:
#方言
database: mysql
show-sql: true
properties:
hibernate:
format_sql: true
hibernate:
#create\create-drop\update\validate
ddl-auto: update
3、功能代码实现
- User.class
package com.qianfeng.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Table(name = "user")
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
private int uid;
@Column(name = "username",length = 20,unique = true,nullable = false)
private String username;
@Column(name = "password",length = 20,nullable = false,unique = false)
private String password;
@Column(name = "addr",length = 50)
private String addr;
}
- IUserDao.class
package com.qianfeng.dao;
import com.qianfeng.ServletInitializer;
import com.qianfeng.entity.User;
import com.sun.org.apache.xml.internal.serialize.Serializer;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface IUserDao extends JpaRepository<User, Serializer> {
List<User> findByPassword(String password);
}
- UserServiceImpl.class
package com.qianfeng.service.impl;
import com.qianfeng.dao.IUserDao;
import com.qianfeng.entity.User;
import com.qianfeng.service.IUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
@Resource
private IUserDao iUserDao;
@Override
public List<User> getAllUser() {
return iUserDao.findAll();
}
@Override
public void saveUser(User user) {
iUserDao.saveAndFlush(user);
}
public List<User> byPass(String pass){
return iUserDao.findByPassword(pass);
}
}
- UserController.class
这里先用@RestController
package com.qianfeng.controller;
import com.qianfeng.entity.User;
import com.qianfeng.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class UserController {
@Resource
private IUserService iUserService;
@GetMapping("/users")
public List<User> getAllUser(){
return iUserService.getAllUser();
}
}
至此,Spring Boot和SpringDataJPA的整和就完成了
SpringBoot+jpa整合Thymeleaf
- 添加所需的依赖
<!--thymeleaf相关-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
- 添加配置文件
在配置文件中并没有配置默认的前后缀,所以默认的是放在resource/templates下的HTML文件
#thymeleaf相关
thymeleaf:
cache: false #开发时关闭缓存,否则无法实时看到页面效果
mode: LEGACYHTML5 #使用非严格的html标准
encoding: utf-8
- 添加user页面
注意必须在resource/templates下的HTML页面
<!DOCTYPE html >
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
<h1>this is user page</h1>
<table>
<tr>
<th>uid</th>
<th>uname</th>
<th>password</th>
<th>addr</th>
<th>age</th>
<th>manger</th>
</tr>
<tbody>
<tr th:each="u:${user}">
<th th:text="${u.uid}">0</th>
<th th:text="${u.username}">0</th>
<th th:text="${u.password}">0</th>
<th th:text="${u.addr}">0</th>
<th th:text="${#numbers.formatDecimal(u.uid,1,2)}">0</th>
<th><a href="/user/1" th:href="@{'/user/' + ${u.uid}}">update01</a> </th>
<th><a href="/user/1" th:href="@{/user/${u.uid}}">update02</a> </th>
</tr>
</tbody>
</table>
</body>
</html>
这是拿值的第一种方法,此时注意取值时用的$符,代码中的0为默认值,
<tr th:each="u:${user}">
下边这个提交是错误的使用方法,必须使用第一种提交方式拼接,并且第一种href属性是默认提交URL
<th><a href="/user/1" th:href="@{/user/${u.uid}}">update02</a> </th>
数据穿过去后就是正常的处理了。
so over~