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

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

这一节讲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 login(String username , String password) throws Exception {
        Map returnMap = new HashMap();
        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 login(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Map returnMap = new HashMap();

        try {
            Map 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;
    }
}

页面访问

intellij idea 构建 基于spring springmvc hibernate的maven项目《三》_第1张图片

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

你可能感兴趣的:(java)