首页 开发项目专栏 SpringBoot+Thymleaf项目初入(三) - 用户登录
原SpringBoot+Thymleaf项目初入(三) - 用户登录
版权声明:
本站原创文章,于 2020年11月03日 17:38:04 发表,共 3750 字。
发表评论
0/2000
当前评论总数: 0 条
相关文章推荐
1. 新建数据库表 ims-learn.user
2. Model包下新建User类
注: 因为之前初始化项目时添加了lombok依赖, 所以model类可以不用写setter/getter, 直接使用@Data注解替代
3. 编写UserDao接口类
4. UserMapper.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="cn.coralcloud.ims.dao.UserDao">
<resultMap id="userResultMap" type="User">
<id property="id" column="id" />
<result property="createTime" column="create_time" />
<result property="updateTime" column="update_time" />
</resultMap>
<select id="login" resultMap="userResultMap">
select * from user where email = #{email} and password = #{password}
</select>
</mapper>
5.mybatis.xml
编写mybatis基础配置, 新建resources/mybatis/mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="lazyLoadTriggerMethods" value="" />
</settings>
</configuration>
6. UserService.java
7. UserController.java
package cn.coralcloud.ims.controller;
import cn.coralcloud.ims.model.User;
import cn.coralcloud.ims.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import javax.servlet.http.HttpSession;
/**
* @author c-geff
* @name UserController
* @description
* @date 2020-11-03 10:45
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/login")
public ModelAndView login(String email, String password, HttpSession session){
ModelAndView view = new ModelAndView();
view.setViewName("user/login");
User user = userService.login(email, password);
if(user != null){
session.setAttribute("AdminUserKey", user);
view.setViewName("redirect:/index");
return view;
}
view.addObject("errmsg", "用户名或密码错误! ");
return view;
}
@GetMapping("/login")
public String login(){
return "user/login";
}
}