SSM框架实现简单的登录功能,需要实现查询数据库中user并比对输入的账号密码
首先需要创建实体类,表中各个字段在实体类中写出来,并写出构造方法,get set方法
Mapper:
<mapper namespace="User"> <resultMap type="com.zy.music.pojo.User" id="userResult"> <id property="userId" column="user_id"/> <result property="userName" column="user_name"/> <result property="userPwd" column="user_pwd"/> </resultMap> <!-- 登录注册 --> <!-- 根据用户名查询用户 --> <select id="queryUserByUserName" resultMap="userResult"> select * from zy_user where user_name=#{userName} and user_status=1 </select> </mapper>
Dao:
public interface IUserDao { /** * 根据用户名查询用户信息 * @param userName * @return * @throws Exception */ public User queryUserByUserName(String userName) throws Exception; }
DaoImpl:
@Repository public class UserDaoImpl implements IUserDao { @Autowired private SqlSession sqlSession; @Override public User queryUserByUserName(String userName) throws Exception { return sqlSession.selectOne("User.queryUserByUserName", userName); } }
Service:
public interface IUserService { /** * 根据用户名查询用户信息 * @param userName * @return * @throws Exception */ public User queryUserByUserName(String userName) throws Exception; }
ServiceImpl:
@Service public class UserServiceImpl implements IUserService { @Autowired private IUserDao userDao; @Override public User queryUserByUserName(String userName) throws Exception { return userDao.queryUserByUserName(userName); } }
controller:
@RestController @RequestMapping("/user") public class UserController { @Autowired private IUserService userService; /* * 用户登录 */ @RequestMapping(value = "/login", produces = "application/json;charset=utf-8") private String login(@RequestParam(value = "userName", required = true) String userName, @RequestParam(value = "userPwd", required = false) String userPwd) { Map<String, Object> rtnMap = new HashMap<String, Object>(); try { User user = userService.queryUserByUserName(userName); if(null != user) { //用户存在 if(userPwd.equals(user.getUserPwd())) { rtnMap.put("rtnCode", "密码正确,登录成功!"); //密码正确 } else { rtnMap.put("rtnCode", "密码错误,登录失败!"); //密码错误 } } else { rtnMap.put("rtnCode", "用户名不存在,登录失败!"); //用户不存在 } } catch (Exception e) { rtnMap.put("rtnCode", "程序异常"); e.printStackTrace(); } return JsonUtil.objToJson(rtnMap); } }
我们这边返回的是json字符,前端用ajax请求来接收
原文地址:https://www.cnblogs.com/qinxiaowan/p/9675986.html
时间: 2024-11-04 02:39:21