# springboot-demo **Repository Path**: helloteemo/springboot-demo ## Basic Information - **Project Name**: springboot-demo - **Description**: No description available - **Primary Language**: Java - **License**: AGPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-03-01 - **Last Updated**: 2021-05-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 如何使用**SpringBoot**完成毕业设计 假设本博客课题:注册登入功能,这里只实现登入 博客代码地址:`https://gitee.com/Frank__smile/springboot-demo.git` GitEE平台有两个分支:`master、dev`,其中`master`分支有大致框架,默认配置了`jpa`、`redis`,本文默认使用数据库`mysql`,使用时只需要修改`application.properties`文件中的 ```properties spring.datasource.url=jdbc:mysql://139.196.203.41:3306/springboot_test?useSSL=false&serverTimezone=UTC&charset=utf8mb4 spring.datasource.username=root spring.datasource.password=123456 spring.redis.host=139.196.203.41 spring.redis.password=123456 ``` 即可完成毕业设计大部分功能 而`dev`分支则为本博客实现的代码,仅供参考 > 毕业设计编码部分大致流程如下 > > 1. 创建数据库表 > 2. 根据数据库表创建实体类 > 3. 编写基本SQL语言 > 4. 以此创建Service、Controller > 5. 编写前端部分(不写) ### 一、 创建数据库表 由于实现的功能非常简单,我就写一张`user_user`表,`user_user`表有三个字段`id username password` 对应SQL语句如下: ```sql 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`方法 ```java @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如下 ```sql select id from user_user where username = ? and passwrod=? ``` 其中 ```java public interface UserRepository extends JpaRepository { User findByUsernameAndPassword(String username, String password); } ``` 到这里我们就已经完成了SQL语句的编写 ### 创建Service、Controller `service`包中添加一个`UserService`接口,用来定义用户的行为,目前只实现登入功能, ```java public interface UserService { public boolean userLogin(String username, String password); } ``` 在`service`包中添加一个`impl`子包,用来实现`service`包中的方法,添加一个类`UserServiceImpl`类实现接口, ```java @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`类,用来转发不同的请求, ```java @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,答案`眼前人是心上人`