哈哈,这个实际上是一个很大的工程,因为在看这篇文章的读者应该很迷茫,不知道怎么入手一个项目
本文主要简述了一个项目的开发流程(编码部分),怎么一步一步从无到有
Let Go!!!
如何使用SpringBoot完成毕业设计
假设本博客课题:注册登入功能,这里只实现登入
博客代码地址:https://gitee.com/helloteemo/springboot-demo.git
GitEE平台有两个分支:master、dev
,其中master
分支有大致框架,默认配置了jpa
、redis
,本文默认使用数据库mysql
,使用时只需要修改application.properties
文件中的
| 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
分支则为本博客实现的代码,仅供参考
毕业设计编码部分大致流程如下
- 创建数据库表
- 根据数据库表创建实体类
- 编写基本SQL语言
- 以此创建Service、Controller
- 编写前端部分(不写)
一、 创建数据库表
由于实现的功能非常简单,我就写一张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
public class UserServiceImpl implements UserService {
@Autowired 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
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,答案眼前人是心上人