初学者写的小demo,如有错误,请多多指正!
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:/com.eduai.mapper/*.xml
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.3version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.48version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<mapper namespace="com.eduai.mapper.UserLoginMapper">
<select id="selectOneUser" parameterType="com.eduai.pojo.People" resultType="com.eduai.pojo.People">
select * from userinfo where username = #{username} and password = #{password}
select>
mapper>
@Mapper
public interface UserLoginMapper {
People selectOneUser(People people);
}
public interface UserLoginService {
People findOneUser(People people);
}
@Service
public class UserLoginServiceImpl implements UserLoginService {
@Resource
private UserLoginMapper userLoginMapper;
@Override
public People findOneUser(People people) {
return userLoginMapper.selectOneUser(people);
}
}
@Controller
@RequestMapping(value = "/user")
public class UserLoginController {
@Resource
private UserLoginService userLoginService;
@RequestMapping(value = "/login.do")
public String userLogin(People people, HttpSession session){
String username = people.getUsername();
String password = people.getPassword();
System.out.println(username+":"+password);
people = userLoginService.findOneUser(people);
if(people!=null){
session.setAttribute("user",people);
System.out.println(people);
return "main";
}
return "error";
}
}
@Configuration
public class LoginConfig implements WebMvcConfigurer {
//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration resgistration = registry.addInterceptor(new UserLoginInterceptor());
resgistration.addPathPatterns("/**");
resgistration.excludePathPatterns("/login","/user/login.do");
}
}
public class UserLoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
People user = (People) request.getSession().getAttribute("user");
if(user!=null){
return true;
}
response.sendRedirect("/login");
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户登录title>
head>
<body>
<form th:action="@{/user/login.do}" method="post">
用户名:<input type="text" name="username" placeholder="用户名" /><br>
密 码:<input type="text" name="password" placeholder="密码" />br>
<input type="submit" value="登录" />
form>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主界面title>
head>
<body>
<h2>Hello Worldh2>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>错误页面title>
head>
<body>
<h3>sorry!not found pageh3>
body>
html>