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

来源:互联网 发布:java iterator原理 编辑:程序博客网 时间:2024/06/02 03:56

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

这一节讲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. */@Servicepublic 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. */@Controllerpublic 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请求,所以可以使用浏览器直接访问接口。

阅读全文
0 0