一.在 pox.xml 中 加入 <!--email依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
二。创建一个用户实体
/**
* .
*/
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
public class Member implements Serializable {
@Id
@GenericGenerator(name = "sys_uid",strategy = "com.zhaihuilin.util.KeyGeneratorUtils",parameters = {
@Parameter(name = "k",value = "M")
})
@GeneratedValue(generator = "sys_uid")
private String id;
//用户名
@NonNull
private String username;
//昵称
private String nickname;
@NonNull
//密码
private String password;
@NonNull
//邮箱
private String email;
//电话号码
private String smscode;
//用户状态
private String state;
public Member( String username, String nickname, String password, String email, String smscode, String state) {
this.username = username;
this.nickname = nickname;
this.password = password;
this.email = email;
this.smscode = smscode;
this.state = state;
}
public Member(String username, String nickname, String password, String email, String state) {
this.username = username;
this.nickname = nickname;
this.password = password;
this.email = email;
this.state = state;
}
}
三.配置文件
1.application-dev.yml
spring:
jpa:
show-sql: true
hibernate:
ddl-auto: update
datasource:
url: jdbc:mysql://127.0.0.1:3306/sms?characterEncoding=UTF-8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
minIdle: 2
maxAction: 20
maxWaitMillis: 8000
cache:
type: redis
redis:
database: 2
host: 127.0.0.1
port: 6379
timeout: 20000
pool:
max-active: 8
min-idle: 0
max-idle: 8
max-wait: -1
2.application-.yml
server:
tomcat:
uri-encoding: utf-8
port: 8087
context-path: /
spring:
profiles:
active: dev
mail:
host: smtp.163.com
username: 邮箱
password: 邮箱的登录密码
port: 25
default-encoding: UTF-8
protocol: smtp
properties:
mail:
smth:
auth: true
starttls:
enable: true
required: true
四。编写相应的 dao service [此处省略]
五。编写工具类
//实体类主键生成策略
public class KeyGeneratorUtils extends AbstractUUIDGenerator implements Configurable {
public String k;
@Override
public void configure(Type type, Properties properties, ServiceRegistry serviceRegistry) throws MappingException {
k = properties.getProperty("k");
}
@Override
public Serializable generate(SessionImplementor sessionImplementor, Object o) throws HibernateException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return k + simpleDateFormat.format(new Date());
}
}
//时间处理类
public class NormalTools {
public static String getFileType(String fileName) {
if(fileName!=null && fileName.indexOf(".")>=0) {
return fileName.substring(fileName.lastIndexOf("."), fileName.length());
}
return "";