如何使用SpringBoot完成毕业设计

本文最后更新于:2022年12月18日 下午

哈哈,这个实际上是一个很大的工程,因为在看这篇文章的读者应该很迷茫,不知道怎么入手一个项目

本文主要简述了一个项目的开发流程(编码部分),怎么一步一步从无到有

Let Go!!!

如何使用SpringBoot完成毕业设计

假设本博客课题:注册登入功能,这里只实现登入

博客代码地址:https://gitee.com/helloteemo/springboot-demo.git

GitEE平台有两个分支:master、dev,其中master分支有大致框架,默认配置了jparedis,本文默认使用数据库mysql,使用时只需要修改application.properties文件中的

1
2
3
4
5
6
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot_test?useSSL=false&serverTimezone=UTC&charset=utf8mb4
spring.datasource.username=root
spring.datasource.password=123456

spring.redis.host=127.0.0.1
spring.redis.password=123456

即可完成毕业设计大部分功能

dev分支则为本博客实现的代码,仅供参考

毕业设计编码部分大致流程如下

  1. 创建数据库表
  2. 根据数据库表创建实体类
  3. 编写基本SQL语言
  4. 以此创建Service、Controller
  5. 编写前端部分(不写)

一、 创建数据库表

由于实现的功能非常简单,我就写一张user_user表,user_user表有三个字段id username password

对应SQL语句如下:

1
2
3
4
5
6
7
8
9
10
CREATE DATABASE springboot-test CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE IF NOT EXIST `user_user`(
id int primary key AUTO_INCREMENT,
username text not null,
password text not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into user_user(username,password) values("frank","123456");
insert into user_user(username,password) values("狗头狗LJH","123456");

二、根据数据库表创建实体类

我们在entity包中创建一个User类,使用注解@Entity标记为一个实体类,使用@Table标注对应数据库的表名为user_user,生产get set toString方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Entity
@Table("user_user")
public class User{
@Id
private long id;

@Column(name = "username", nullable = false)
private String username;

@Column(name = "password", nullable = false)
private String password;

...
}

三、编写基本SQL语言

由于我们使用的是jpa,所以只需要jpa标准就行,这里只做简单阐述,其余请自行查阅资料

这个部分我们放在models包中,我们需要创建一个UserRepository接口,该接口需要继承JpaRepository,被继承的JpaRepository接口需要填入两个类型,第一个类型是实体类类型,第二个类型是主键类型。

由于需要完成登入功能,所以我们检查账户密码是否正确,大致SQL如下

1
select id from user_user where username = ? and passwrod=?

其中

1
2
3
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsernameAndPassword(String username, String password);
}

到这里我们就已经完成了SQL语句的编写

创建Service、Controller

service包中添加一个UserService接口,用来定义用户的行为,目前只实现登入功能,

1
2
3
public interface UserService {
public boolean userLogin(String username, String password);
}

service包中添加一个impl子包,用来实现service包中的方法,添加一个类UserServiceImpl类实现接口,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Service
// 该注解表示这个类是一个组件@Component
public class UserServiceImpl implements UserService {

@Autowired
// spring自动注入
UserRepository userRepository;

@Override
public boolean userLogin(String username, String password) {
User user = userRepository.findByUsernameAndPassword(username, password);
if (user == null) {
return false;
}
return user.getId() != 0;
}
}

controller包中添加一个UserController类,用来转发不同的请求,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Controller
// 功能同@Component
public class UserController {

@Autowired
UserService userService;

private Logger log = LoggerFactory.getLogger(UserController.class);

@RequestMapping("login")
@ResponseBody
public String login(@Param("username") String username, @Param("password") String password, HttpSession session) {
log.info("username:" + username);
log.info("password:" + password);
// 一定,一定要记得写关键日志啊,同志们
User currentUser = (User) session.getAttribute("currentUser");
if (currentUser != null) {
return "you are login";
}
boolean flag = userService.userLogin(username, password);
if (flag) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
session.setAttribute("currentUser", user);
return "welcome";
} else {
return "error username or password";
}
}

}

如果有不同的地方可以加QQ:3075834432,答案眼前人是心上人


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!