1.创建项目

@SpringBootApplication  
public class App  
{  
    public static void main( String[] args )  
    {  
        SpringApplication.run(App.class,args);  
    }  
}

2.引入依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
<modelVersion>4.0.0</modelVersion>  
  
<parent>  
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-starter-parent</artifactId>  
<version>3.1.3</version>  
</parent>  
  
<groupId>com.ivan.cn</groupId>  
<artifactId>shizhang01</artifactId>  
<version>1.0-SNAPSHOT</version>  
<packaging>jar</packaging>  
  
<name>shizhang01</name>  
<url>http://maven.apache.org</url>  
  
<properties>  
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
</properties>  
  
<dependencies>  
<!--web-->  
<dependency>  
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-starter-web</artifactId>  
</dependency>  
<!--mybatis-->  
<dependency>  
<groupId>org.mybatis.spring.boot</groupId>  
<artifactId>mybatis-spring-boot-starter</artifactId>  
<version>3.0.0</version>  
</dependency>  
<!--mysql-->  
<dependency>  
<groupId>com.mysql</groupId>  
<artifactId>mysql-connector-j</artifactId>  
</dependency>  
<!--lombok-->  
<dependency>  
<groupId>org.projectlombok</groupId>  
<artifactId>lombok</artifactId>  
</dependency>  
  
<!--validation-->  
<dependency>  
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-starter-validation</artifactId>  
</dependency>  
</dependencies>  
</project>

3.创建目录

controller
exception
pojo
mapper
service
impl
utils

4.数据库设置

spring:  
datasource:  
driver-class-name: com.mysql.cj.jdbc.Driver  
url: jdbc:mysql://localhost:3306/big_event  
username: root  
password: root  
  
create database big_event;  
use big_event;  
create table user  
(  
id int unsigned primary key auto_increment comment 'ID',  
username varchar(20) not null unique comment '用户名',  
password varchar(32) comment '密码',  
nickname varchar(10) default '' comment '昵称',  
email varchar(128) default '' comment '邮箱',  
user_pic varchar(128) default '' comment '头像',  
create_time datetime not null comment '创建时间',  
update_time datetime not null comment '修改时间'  
) ENGINE=InnoDB DEFAULT charset=utf8 comment '用户表';  
create table category  
(  
id int unsigned primary key auto_increment comment 'ID',  
category_name varchar(32) not null comment '分类名称',  
category_alias varchar(32) comment '分类别名',  
create_time datetime not null comment '创建时间',  
update_time datetime not null comment '修改时间',  
user_id int unsigned default null,  
key fk_category_user(user_id),  
constraint fk_category_user foreign key (user_id) references user (id)  
)ENGINE=InnoDB DEFAULT charset=utf8 comment '分类表';  
# alter table category engine=innodb;  
  
create table article  
(  
id int unsigned primary key auto_increment comment 'ID',  
title varchar(30) not null comment '文章标题',  
content varchar(10000) not null comment '文章内容',  
cover_img varchar(128) not null comment '文章封面',  
state varchar(3) default '草稿' comment '文章状态已发布,草稿',  
category_id int unsigned comment '文章分类id',  
create_user int unsigned not null comment '创建人id',  
create_time datetime not null comment '创建时间',  
update_time datetime not null comment '修改时间',  
cate_id int unsigned default null,  
key fk_article_category(cate_id),  
user_id int unsigned default null,  
key fk_article_user(user_id),  
constraint fk_article_category foreign key (cate_id) references category (id),  
constraint fk_article_user foreign key (user_id) references user (id)  
)ENGINE=InnoDB DEFAULT charset=utf8 comment '文章表';  

5.业务代码编写

//1.实体类  
@Data  
public class User {  
private Integer id;  
private String username;  
private String password;  
private String nickname;  
private String email;  
private String userPic;  
private LocalDateTime createTime;  
private LocalDateTime updateTime;  
}  
  
@NoArgsConstructor  
@AllArgsConstructor  
@Data  
public class Result<T> {  
private Integer code;  
private String message;  
private T data;  
public static <E> Result<E> success(E data){  
return new Result<>(0,"操作成功",data);  
}  
public static Result success(){  
return new Result(0,"操作成功",null);  
}  
public static Result error(String message){  
return new Result(1,message,null);  
}  
}  
//2.controller  
@RestController  
@RequestMapping("/user")  
@Validated  
public class UserController {  
@Autowired  
private UserService userService;  
@PostMapping("/register")  
public Result register(@Pattern(regexp = "^\\S{5,16}$") String username,  
@Pattern(regexp = "^\\S{5,16}$") String password){  
  
User user=userService.findByUserName(username);  
if(user==null){  
userService.register(username,password);  
return Result.success();  
}else{  
return Result.error("用户名已被占用");  
}  
}  
}  
//3.service  
public interface UserService {  
  
User findByUserName(String username);  
  
void register(String username, String password);  
}  
  
  
@Service  
public class UserServiceImpl implements UserService {  
@Autowired  
private UserMapper userMapper;  
@Override  
public User findByUserName(String username) {  
User u=userMapper.findByUserName(username);  
return u;  
}  
@Override  
public void register(String username, String password) {  
String s = Md5Utils.encrypt3ToMd5(password);  
userMapper.register(username,s);  
}  
}  
//4.mapper  
@Mapper  
public interface UserMapper {  
@Insert("insert into user(username,password,create_time,update_time)" +  
" values(#{username},#{password},now(),now())")  
void register(String username, String password);  
  
@Select("select * from user where username=#{username}")  
User findByUserName(String username);  
}  
//5.异常处理  
@RestControllerAdvice  
public class GlobalExceptionHandler {  
@ExceptionHandler(Exception.class)  
public Result handleException(Exception e){  
e.printStackTrace();  
return Result.error(StringUtils.hasLength(e.getMessage())?e.getMessage():"操作失败");  
}  
}  
//6.工具类  
public class Md5Utils {  
public static String encrypt3ToMd5(String str) {  
return org.springframework.util.DigestUtils.md5DigestAsHex(str.getBytes(StandardCharsets.UTF_8));  
}  
}  

阿芯爱编程
0 声望1 粉丝

php ,java,nodejs