Springboot登陆案例
程序员文章站
2024-01-11 09:25:28
1.通过 IDEA 的脚手架工具创建Spring Boot项目2.在数据库建立spring_login数据库,并新建user表,表结构如图3.通过generator-mybatis自动生成java代码,generator-mybatis使用教程可见博文:https://blog.csdn.net/beixishuo/article/details/1051447534.将自动生成的代码放到src/main/java下,如图:5.编辑pom.xml,在其中添加logging和druid依赖6...
1.通过 IDEA 的脚手架工具创建Spring Boot项目
2.在数据库建立spring_login数据库,并新建user表,表结构如图
3.通过generator-mybatis自动生成java代码,generator-mybatis使用教程可见博文:
https://blog.csdn.net/beixishuo/article/details/105144753
4.将自动生成的代码放到src/main/java下,如图:
5.编辑pom.xml,在其中添加logging和druid依赖
6.编辑application.propertis文件
#mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring_login
spring.datasource.username=root
spring.datasource.password=123456
#druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#dao
mybatis.type-aliases-package==com.xue.repository.dao
mybatis.mapper-locations=classpath*:com/xue/repository/mapper/*.xml
7.在src/main/resources/templates下创建登陆页面和注册页面,分别为loginHtml和registerHtml,代码如下
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
<form action="/login" method="post">
用户名:<input type="text" name="name"/>
密码:<input type="password" name="password"/>
<input type="submit"/><a href="registerHtml" target="_blank">注册</a>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
<form action="/register" method="post">
用户名:<input type="text" name="name"/>
密码:<input type="password" name="password"/>
<input type="submit"/>
</form>
</body>
</html>
- 在项目中新建controller包,创建LoginController类,编辑代码
package com.xue.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xue.entity.model.User;
import com.xue.service.LoginService;
@Controller
public class LoginController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private LoginService loginService;
// 登陆页面
@RequestMapping("/loginHtml")
public String loginHtml(){
return "loginHtml";
}
// 注册页面
@RequestMapping("/registerHtml")
public String registerHtml(){
return "registerHtml";
}
// 登陆验证
@RequestMapping("/login")
@ResponseBody //不返回页面,返回json对象需加@ResponseBody
public String login(User user,HttpServletRequest request){
User result = null;
result = loginService.login(user);
if(null != result){
return "登陆成功 "+"name:"+user.getName()+" password:"+user.getPassword();
}else{
return "登陆失败";
}
}
//注册验证
@RequestMapping("/register")
@ResponseBody //不返回页面,返回json对象需加@ResponseBody
public String register(User user){
int result =0;
result = loginService.register(user);
if(0 != result){
return "注册成功";
}else{
return "注册失败";
}
}
}
- 在项目中创建service包,并在报下创建LoginService接口,同时创建子包Impl,在报下创建LoginServiceImpl实现LoginServcie接口,代码分别为
package com.xue.service;
import java.util.List;
import com.xue.entity.model.User;
public interface LoginService {
//注册
public int register(User user);
//登陆
public User login(User user);
}
package com.xue.service.Impl;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xue.entity.model.User;
import com.xue.repository.dao.UserMapper;
import com.xue.service.LoginService;
@Service
public class LoginServiceImpl implements LoginService {
@Autowired
private UserMapper dao;
@Override
public int register(User user) {
// TODO Auto-generated method stub
System.out.println("Impl");
int result = 0;
System.out.println(user);
try {
result = dao.insertUser(user);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
@Override
public User login(User user) {
// TODO Auto-generated method stub
try {
user = dao.selectUser(user);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
}
}
- 编写UserMapper.java,在文件最后添加
User selectUser(User user);
int insertUser(User user);
11.在UserMapper.xml中添加
<select id="selectUser" parameterType="com.xue.entity.model.User" resultMap="BaseResultMap">
select name,password from user where name=#{name} and password=#{password}
</select>
<insert id="insertUser" parameterType="com.xue.entity.model.User">
insert user(name,password) values(#{name},#{password})
</insert>
12.编辑SpringBootLoginApplication启动类
package com.xue;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@MapperScan("com.xue.repository.dao")
public class SpringbootLoginApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootLoginApplication.class, args);
}
}
13.为项目添加日志配置,在pom.xml中添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
14.将springboot默认日志排除,将spring-boot-starter-web依赖修改为
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
15.在src/main/resources中创建log4j2-spring.xml文件,并编辑
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<properties>
<property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level [%thread] %c [%L] -| %msg%n</property>
</properties>
<!--先定义所有的appender(输出器) -->
<appenders>
<!-- 输出到控制台 -->
<!--输出日志的格式,引用自定义模板 PATTERN -->
<Console
name="CONSOLE"
target="system_out">
<PatternLayout pattern="${PATTERN}" />
</Console>
</appenders>
<loggers>
<logger
name="com.xue"
level="debug" />
<root level="info">
<appenderref ref="CONSOLE" />
</root>
</loggers>
</configuration>
16.在application.properties中添加
#log4j2
logging.config=classpath:log4j2-spring.xml
- 在相关代码中添加,至此,项目代码完成
private Logger logger = LoggerFactory.getLogger(this.getClass());
18.在浏览器输入localhost:8080/registerHtml
19.在浏览器输入localhost:8080/loginHtml
本文地址:https://blog.csdn.net/beixishuo/article/details/110202626