适合springboot2初学者

学了springboot2知识,就要用起来,不敲代码的程序员不是好程序员。笔者参考了他人的文章实现的这个功能,在自己的搭建过程中也遇到了许多问题,并花了一定的时间解决,下面开始吧!

一、登录注册第一步

首先,实现登录注册,我们是不是要先定义一个表?表里面要有什么,看看自己的需求。下面是笔者的数据库表,创建一个名为demo的数据库,里面建立一个user用户表,并有如图字段,我先记录了一些信息可忽略:
二、

搭建springboot项目

笔者是从maven项目开始搭建的,然后手打成springboot项目,其实二者区别就是依赖的建立问题,maven项目的依赖是空的,springboot在创建的过程中有勾选的选项,就不用手动输入依赖了。创建一个简单的springboot项目相信各位都会,不行就找其他的博客看看。创建完成后,
1.在pom.xml文件中导入依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version></dependency><dependency><groupId>org.springframework.experimental</groupId><artifactId>spring-native</artifactId><version>${spring-native.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version></dependency></dependencies>

导入这几个依赖就了
2.搭建基本项目目录结构
最重要的是搭建controller(控制层),mapper(接口层),service(业务层),entity(实体层)四个包,然后对比一下有没有templates,static,application.properties,这几个文件,没有就手动加,跟我保持一样
3.连接数据库
仔细看一下,pom.xml文件里面是不是导入了mysql等依赖?那就要用起来数据库,怎么连接数据库?在application.properties文件里搭建

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.password=54jacksparrowspring.datasource.username=rootspring.datasource.url=jdbc:mysql://localhost:3306/demo" />=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=truespring.thymeleaf.prefix=classpath:/templates/

3306后面接的是自己创建的数据库名字,初次之外,修改自己的数据库用户名和密码,最后一行的意思是可以让程序找到templates文件夹下的html文件。
4.连好数据库后写代码实现
这里有必要介绍一下各个模块的作用:

  • controller
    控制层,用于实现最终的逻辑代码,在这里面实现提供给前端后端的逻辑接口
  • entity
    实体类层,用于定义改实体类应该有的属性,建议和数据库的字段一样,省去后期的麻烦
  • mapper
    接口层,用于操作数据库,sql语句就是在这里实现的
  • service
    业务层,实现和mapper的对接的方法,然后提供给controller层,各位可以自行慢慢理解
    建议各个包里面的东西名字以实体类为起点,笔者这里没定义好,比如
    entity里面定义实体User,那么其他三个分别为,UserMapper,UserService,UserController,这里大家一定要修改一下目录,别跟我一样
    它们之间的关系为:mapper层注入到service层,service层注入到controller层,层层嵌套
    开发顺序为;
  • entity.User.java
    这里是实现有参无参构造,setter,getter,toString()方法
public class UserBean{private Integer id;private String username;private String password;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return username;}public void setName(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
  • mapper.UserMapper.java
    Mapper与mybatis有关,没有在pom.xml里面导入mybatis依赖会出错
@Mapperpublic interface UserMapper {@Select("SELECT * FROM user WHERE username = #{username} AND password = #{password}")UserBean getInfo(@Param("username") String username, @Param("password") String password); @Insert("insert into user(username,password)values(#{username},#{password})")void saveInfo(@Param("username") String username, @Param("password") String password);}
  • service.UserService.java
@Servicepublic class UserService {//将dao层属性注入service层@Resourceprivate UserMapper userMapper;public UserBean LoginIn(String username, String password) {return userMapper.getInfo(username,password);}public void Insert(String username,String password){userMapper.saveInfo(username, password);}}
  • controller.UserController.java
    这里类名没有对应,读者理解一下,我比较懒就不改了
@Controllerpublic class LoginController {//将Service注入Web层@ResourceUserService userService;//实现登录@RequestMapping("/login")public String show(){return "login";}@RequestMapping(value = "/loginIn",method = RequestMethod.POST)public String login(String username,String password){UserBean userBean = userService.LoginIn(username, password);log.info("name:{}",username);log.info("password:{}",password);if(userBean!=null){return "redirect:/index.html";}else {return "error";}}@RequestMapping("/index.html")public String mainPage(){return "index";}@RequestMapping("/signup")public String disp(){return "signup";}//实现注册功能@RequestMapping(value = "/register",method = RequestMethod.POST)public String signUp(String username,String password){userService.Insert(username, password);return "success";}}
  • 然后定义了这几个简单的html页面
  • error.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>错误</title></head><body><h1>登录或注册失败</h1></body></html>
  • index.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>首页</title></head><body><h1>欢迎来到首页</h1></body></html>
  • login.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>登录</title></head><body><form role="form" action = "/loginIn" method="post">账号:<input type="text" id="username" name = "username"> <br>密码:<input type="password" id = "password" name = "password"> <br><input type="submit" id = "login" value = "登录"></form><form role="form1" action="/signup" method="post"><input type="submit" id="register" value="注册"></form></body></html>
  • singup.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>注册</title></head><body><form role="form" action="/register" method="post">请输入姓名:<input type="text" name="username" id="username"><br>请输入密码:<input type="password" name="password" id="password"><br><input type="submit" name="sign" value="提交"></form></body></html>
  • success.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>success</title></head><body><h1>登录/注册成功</h1></body></html>

完结撒花

我就不测了,跟我一样不出意外的话应该没有问题,只需localhost:8080/
后面加上在UserController.java里面的接口就可以了,或者直接去登录注册页面去查看接口