intellij idea 构建 基于spring springmvc hibernate的maven项目《三》

本文介绍了在软件开发中如何实现分层架构,特别是service层和controller层的具体应用。通过实例展示了登录功能的实现过程,包括数据库查询及返回结果处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上两节讲了基本的配置和数据库连接。

这一节讲service层和controller层

其实具体的逻辑操作可以放到controller里面,就省了service

但为了分层更明确和代码复用的原因才有了service层

service 层通常指业务层,具体的业务逻辑写到里面 举例如下

service层

package com.xxxx.test.service;

import com.xxxx.test.dao.UserEntityDAO;
import com.xxxx.test.model.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yab on 2017/7/24.
 */
@Service
public class UserServices {

    @Autowired
    UserEntityDAO userEntityDAO;
    /**
     *
     * @param username  用户名
     * @param password  密码
     * @return
     * @throws Exception
     */
    public Map<String, Object> login(String username , String password) throws Exception {
        Map<String,Object> returnMap = new HashMap<String,Object>();
        String hql = "from UserEntity u where u.username='"+username+"'";
        UserEntity userInf = new UserEntity();
        try {
            userInf = userEntityDAO.findOne(hql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(userInf!=null)
        {
            if (password.equals(userInf.getPassword()))
            {
                returnMap.put("value", userInf);
                returnMap.put("message", "登录成功");
                returnMap.put("success", true);
            }else{
                returnMap.put("value", userInf);
                returnMap.put("message", "密码错误");
                returnMap.put("success", false);
            }
        }else{
            returnMap.put("message", "用户不存在");
            returnMap.put("success", false);
        }
        return returnMap;
    }
}

controller层调用

package com.xxxx.test.controller;

import com.xxxx.test.model.UserEntity;
import com.xxxx.test.service.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yab on 2017/7/24.
 */
@Controller
public class UserController {
    @Autowired
    private UserServices userService;


    @RequestMapping(value="/login",method= RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> login(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Map<String,Object> returnMap = new HashMap<String,Object>();

        try {
            Map<String,Object> map = userService.login(username, password);
            //获取user实体
            Object object = map.get("value");
            if(object != null){
                UserEntity user = (UserEntity) object;
                HttpSession session = request.getSession();
                session.setAttribute("userId", user.getId());
            }
            returnMap.put("value", object);
            returnMap.put("message", map.get("message"));
            returnMap.put("success", map.get("success"));
        } catch (Exception e) {
            returnMap.put("message", "异常:登录失败!");
            returnMap.put("success", false);
            e.printStackTrace();
        }
        return returnMap;
    }
}

页面访问


由于controller层方法配置的为get请求,所以可以使用浏览器直接访问接口。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值