springboot中使用MVC架构开发javaWeb项目

1. 首先,再熟悉不过的项目结构
springboot中使用MVC架构开发javaWeb项目_第1张图片

2. 公共DAO层的编写

package com.pcn.hotel.dao;
import org.springframework.stereotype.Repository;
import javax.persistence.Query;
import java.util.List;

/**
 * @author Pan
 * @Description: 公共 DAO
 * @date 2020\4\5 0005-22:31
 */
@Repository
public interface BasicDAO<T>{
    /**
     * @Author: Pan
     * @Description: 通过hql查询所有
     * @Param: [hql]
     * @Return: javax.persistence.Query
     * @Date: 2020\4\9 0009 15:01
     */
    Query findByHql(String hql);

    /**
     * @Author: Pan
     * @Description: 分页查询
     * @Param: [hql, page, pageSize]
     * @Return: java.util.List
     * @Date: 2020\4\9 0009 15:31
     */
    List<T> findForPage(String hql,Integer page,Integer pageSize);
}

3. 公共service层的编写

package com.pcn.hotel.service;

import com.pcn.hotel.dao.BasicDAO;
import org.springframework.stereotype.Service;

import javax.persistence.*;
import java.util.List;

/**
 * @author Pan
 * @Description: 公共 service
 * @date 2020\4\6 0006-14:47
 */
@Service
public interface BasicService<T> extends BasicDAO<T> {

    @Override
    Query findByHql(String hql);

    @Override
    List<T> findForPage(String hql, Integer page, Integer pageSize);
}

4. 公共ServiceImpl层的编写

package com.pcn.hotel.serviceimp;

import com.pcn.hotel.service.BasicService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @author Pan
 * @Description: 公共serviceImp层
 * @date 2020\4\8 0008-20:52
 */
@Service
public class BasicServiceImp<T> implements BasicService {


    @PersistenceContext
    EntityManager entityManager;

    /**
     * @Author: Pan
     * @Description: 通过 HQL查询
     * @Param: [hql]
     * @Return: java.util.List
     * @Date: 2020\4\9 0009 14:54
     */
    @Override
    public Query findByHql(String hql) {
        return entityManager.createQuery(hql);
    }

    /**
     * @Author: Pan
     * @Description: 分页查询
     * @Param: [hql, page, pageSize]
     * @Return: java.util.List
     * @Date: 2020\4\9 0009 15:11
     */
    @Override
    public List<T> findForPage(String hql, Integer page, Integer pageSize) {

        Query query = entityManager.createQuery(hql);

        //设置分页参数
        query.setFirstResult((page - 1) * pageSize);
        query.setMaxResults(pageSize);

        return query.getResultList();
    }

}

5. 具体的ServiceImpl层

@Service
public class LoginServiceImp {


    /**
     * @Author: Pan
     * @Description: 通过构造函数注入 公共 ServiceImp 层
     * @Param:
     * @Return:
     * @Date: 2020\4\9 0009 14:58
     */
    private BasicServiceImp<PersonAdmin> basicServiceImp;

    @Autowired
    private LoginServiceImp(BasicServiceImp<PersonAdmin> basicServiceImp) {
        this.basicServiceImp = basicServiceImp;
    }



    /**
     * @Author: Pan
     * @Description: 刷新验证码
     * @Param: [session]
     * @Return: java.lang.String
     * @Date: 2020\4\8 0008 19:16
     */
    public String getRandCode(HttpSession session) {
        //生成验证码
        String randCode;
        String base64 = "";
        try {
            //如果session里面没有验证码就生成一个
            if (session.getAttribute("randCode") == null) {
                randCode = RandCodeUtil.getStringRandom(4);
                session.setAttribute("randCode", randCode);
                randCode = session.getAttribute("randCode").toString();
                base64 = "data:image/jpg;base64," + RandCodeUtil.imageToBase64(120, 50, randCode);
                return base64;
            }
            //如果session 里有验证码了就直接取出来
            randCode = session.getAttribute("randCode").toString();
            base64 = "data:image/jpg;base64," + RandCodeUtil.imageToBase64(120, 50, randCode);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return base64;
    }
	
}
  1. Controller 层
@Controller
@RequestMapping("/admin")
public class LoginController {

	/**
     * @Author: Pan
     * @Description: 注入具体实现层
     * @Date: 2020\4\7 0007 21:04
     */
    @Autowired
    private LoginServiceImp loginServiceImp;

    /**
     * @Author: Pan
     * @Description: 登录页面
     * @Param: []
     * @Return: java.lang.String
     * @Date: 2020\4\7 0007 21:04
     */
    @GetMapping("login.html")
    public String loginPage(ModelMap map, HttpSession session) {
        //生成验证码
        map.put("randCode",loginServiceImp.getRandCode(session));
        return "back/login";
    }
 }

7. 访问主页
http://localhost:8080/admin/login.html
springboot中使用MVC架构开发javaWeb项目_第2张图片

你可能感兴趣的:(后端相关)