使用idea开发,mysql数据库,maven,freemarker,SpringBoot整合SSM,前端样式使用BootStrap
CREATE TABLE
user(
idint(11) NOT NULL AUTO_INCREMENT,
usernamevarchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
passwordvarchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL, PRIMARY KEY (
id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;`
新建一个SpringBoot的项目,新建一个pojo包,包中新建一个User实体类
import lombok.Data;
@Data
public class User {
private Integer id;
private String username;
private String password;
}
然后建立一个dao包,包中新建UserDao接口,代码如下
import org.springframework.stereotype.Repository;
import per.sjz.login_demo.pojo.User;
@Repository
public interface UserDao {
public User findUserName(String username);
}
在dao包中新建mybatis的xml配置文件,注意要与接口同名
登陆功能简单实现的原理为,前端输入用户名和密码,后端通过用户输入的用户名来判断用户是否存在,如果数据库中查到了数据,则用户存在,这时后台将从数据库中通过用户名查到的密码与用户输入的密码进行比对,判断密码正确还是错误,如果未查到用户名,返回值为空,则用户名无效。
sql语句如下:
select username,password from `user` where username=#{username}
mybatis配置文件如下
<?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="per.sjz.login_demo.dao.UserDao">
<select id="findUserName" resultType="per.sjz.login_demo.pojo.User">
select username,password from `user` where username=#{username}
</select>
</mapper>
编写service层,新建service包,以及service的实现类,如下图
UserService代码:
import per.sjz.login_demo.pojo.User;
public interface UserService {
public User findUserName(String username);
}
UserServiceImpl中的代码:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User findUserName(String username) {
return userDao.findUserName(username);
}
}
最后编写Web层,新建一个UserController,代码如下:
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/user/login")
public String login(ModelMap map,String username,String password){
User userName = userService.findUserName(username);
if (userName == null) {
map.addAttribute("msg", "用户名不正确");
map.addAttribute("status", 2);
return "user/login";
}
if (!userName.getPassword().equals(password)) {
map.addAttribute("username", username);
map.addAttribute("msg", "密码不正确");
map.addAttribute("status", 2);
return "user/login";
}
map.addAttribute("username", userName.getUsername());
return "user/success";
}
@RequestMapping("/login")
public String login(ModelMap modelMap) {
modelMap.addAttribute("status", 1);
return "user/login";
}
}
前端页面form表单代码:
<form action="/user/login" method="post">
<div>
<div class="form-group">
<label>用户名:</label>
<input type="text" required="true" class="form-control" value="${username!""}" name="username"
placeholder="Login-name">
</div>
<div class="form-group">
<label>密 码:</label>
<input type="password" required="true" class="form-control" name="password" placeholder="Password">
</div>
</div>
<div align="center">
<button type="submit" class="btn btn-default">登录</button>
<button type="reset" class="btn btn-default">清空</button>
</div>
</form>
运行,效果如下:
代码地址,希望对你们能有所帮助,后续也会发一些其他功能,比如注册什么什么的。
https://download.csdn.net/download/lalasonghhh/12593056