<!--引入 mybatis-spring-boot-starter 的依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency>
###################################### MyBatis 配置###################################### mybatis: # 指定 mapper.xml 的位置 mapper-locations: classpath:mybatis/mapper/*.xml #扫描实体类的位置,在此处指明扫描实体类的包,在 mapper.xml 中就可以不写实体类的全路径名 type-aliases-package: net.biancheng.www.bean configuration: #默认开启驼峰命名法,可以不用设置该属性 map-underscore-to-camel-case: true
id | user_id | user_name | password | |
---|---|---|---|---|
1 | 001 | admin | admin | 1234567@qq.com |
2 | 002 | user | 123456 | 987654@qq.com |
3 | 003 | bianchengbang | qwertyuiop | bianchengbang@sina.com |
package net.biancheng.www.bean; public class User { private Integer id; private String userId; private String userName; private String password; private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId == null ? null : userId.trim(); } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName == null ? null : userName.trim(); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); } public String getEmail() { return email; } public void setEmail(String email) { this.email = email == null ? null : email.trim(); } }
package net.biancheng.www.mapper; import net.biancheng.www.bean.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper { //通过用户名密码查询用户数据 User getByUserNameAndPassword(User user); }
<?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="net.biancheng.www.mapper.UserMapper"> <resultMap id="BaseResultMap" type="User"> <id column="id" jdbcType="INTEGER" property="id"/> <result column="user_id" jdbcType="VARCHAR" property="userId"/> <result column="user_name" jdbcType="VARCHAR" property="userName"/> <result column="password" jdbcType="VARCHAR" property="password"/> <result column="email" jdbcType="VARCHAR" property="email"/> </resultMap> <sql id="Base_Column_List"> id, user_id, user_name, password, email </sql> <!--根据用户名密码查询用户信息--> <!--application.yml 中通过 type-aliases-package 指定了实体类的为了,因此--> <select id="getByUserNameAndPassword" resultType="User"> select * from user where user_name = #{userName,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR} </select> </mapper>
package net.biancheng.www.service; import net.biancheng.www.bean.User; public interface UserService { public User getByUserNameAndPassword(User user); }
package net.biancheng.www.service.impl; import net.biancheng.www.bean.User; import net.biancheng.www.mapper.UserMapper; import net.biancheng.www.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service("userService") public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @Override public User getByUserNameAndPassword(User user) { User loginUser = userMapper.getByUserNameAndPassword(user); return loginUser; } }
package net.biancheng.www.controller; import lombok.extern.slf4j.Slf4j; import net.biancheng.www.bean.User; import net.biancheng.www.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpSession; import java.util.Map; @Slf4j @Controller public class LoginController { @Autowired UserService userService; @RequestMapping("/user/login") public String doLogin(User user, Map<String, Object> map, HttpSession session) { //从数据库中查询用户信息 User loginUser = userService.getByUserNameAndPassword(user); if (loginUser != null) { session.setAttribute("loginUser", loginUser); log.info("登陆成功,用户名:" + loginUser.getUserName()); //防止重复提交使用重定向 return "redirect:/main.html"; } else { map.put("msg", "用户名或密码错误"); log.error("登陆失败"); return "login"; } } }
图1:登陆页
图2:登陆成功
package net.biancheng.www.mapper; import net.biancheng.www.bean.User; import org.apache.ibatis.annotations.*; import java.util.List; @Mapper public interface UserMapper { @Select("select * from user where user_name = #{userName,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR}") List<User> getByUserNameAndPassword(User user); @Delete("delete from user where id = #{id,jdbcType=INTEGER}") int deleteByPrimaryKey(Integer id); @Insert("insert into user ( user_id, user_name, password, email)" + "values ( #{userId,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR})") int insert(User record); @Update(" update user" + " set user_id = #{userId,jdbcType=VARCHAR},\n" + " user_name = #{userName,jdbcType=VARCHAR},\n" + " password = #{password,jdbcType=VARCHAR},\n" + " email = #{email,jdbcType=VARCHAR}\n" + " where id = #{id,jdbcType=INTEGER}") int updateByPrimaryKey(User record); }
本文链接:http://task.lmcjl.com/news/14971.html