SpringBoot + thymeleaf 实现简单的登陆验证
2019-01-20 本文已影响1人
rainbowz
sql语句
CREATE TABLE people
(
id INT AUTO_INCREMENT
PRIMARY KEY,
user_name VARCHAR(255) NULL,
age INT NULL,
password VARCHAR(255) NULL
)
ENGINE = InnoDB;
使用mybatis插件自动生成代码
dao层·
@Component
public interface PeopleMapper {
//用户登录
People userlogin(@Param("user_name") String username,@Param("password") String password);
//注册新用户(方式1)
int adduser(@Param("user_name") String username, @Param("password") String password, @Param("age") int age);
}
peopleMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.dao.PeopleMapper">
<select id="userlogin" resultType="com.demo.enity.People">
SELECT id,user_name,password,age FROM people WHERE user_name = #{user_name} AND password = #{password}
</select>
<insert id="adduser">
INSERT INTO people (user_name,password,age) VALUES (#{user_name},#{password},#{age})
</insert>
</mapper>
service层
public interface UserLoginService {
public People userLogin(String username, String password);
public int adduser(String username,String password,int age);
}
public class UserLoginServiceImpl implements UserLoginService {
@Qualifier("peopleMapper")
@Autowired
PeopleMapper mapper;
public People userLogin(String username, String password) {
People people =mapper.userlogin(username,password);
return people;
}
public int adduser(String username, String password, int age) {
return mapper.adduser(username,password,age);
}
}
controller层
@Controller
@RequestMapping(value = {"/user"})
public class UserLoginController {
@Autowired
private UserLoginService userLoginService;
@RequestMapping(value = {"/loginHtml"})
public String loginHtml() {
return "userLogin";
}
@RequestMapping(value = {"/userLogin"})
public ModelAndView userLogin(@RequestParam("username") String username,
@RequestParam("password") String password,
HttpServletRequest request, Model model) {
ModelAndView mv = new ModelAndView("index2");
ModelAndView mv2 = new ModelAndView("loginError");
People people = userLoginService.userLogin(username, password);
String name = people.getUserName();
mv.addObject("people", name);
if (people!=null)
return mv;
return mv2;
}
@RequestMapping(value = {"/registerpage"})
public String registerpage() {
return "register";
}
@ResponseBody
@RequestMapping(value = {"/uregister"})
public String addUser(@RequestParam("username") String username,
@RequestParam("password") String password,
@RequestParam("password2") String password2,
@RequestParam("age") int age) {
if (!password.equals(password2)) {
return "两次密码不相同,注册失败!!";
} else {
int res = userLoginService.adduser(username, password, age);
if (res == 0) {
return "注册失败!";
} else {
return "注册成功!";
}
}
}
userLoging.html
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
请输入用户名与密码登录
<form action="/user/userLogin" method="post">
用户名:<input type="text" name="username" /><br>
密 码:<input type="password" name="password" /><br>
<input type="submit" value="登录" />
<a href="/user/registerpage" target="_blank">注册</a>
</form>
</body>
</html>
register.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="/user/uregister" method="post">
用户名:<input type="text" name="username" /></br>
密码:<input type="password" name="password" /></br>
确认密码:<input type="password" name="password2" /></br>
年龄:<input type="text" name="age" /></br>
<input type="submit" value="注册">
</form>
</body>
</html>
对于写代码的顺序,我是从xml(sql语句)开始写,然后Dao,Service,最后写Controller。下面为我的部分代码。(开始生成的代码没有用到的部分被我删除掉了)
图片.png
图片.png
图片.png
https://blog.csdn.net/m15801049086/article/details/78521275
https://blog.csdn.net/weixin_42685022/article/details/82215893#commentBox