myshop 概述

myshop项目是根据视频教程 Java 单体应用 做的一个练习项目,目前完成了登录功能、用户管理、类别管理后续有时间会继续做其它的功能。趁着双11花了99元一年买了台阿里云服务器,演示地址>> myshop

 

项目结构

my-shop-dependencies:管理jar包的版本,所有项目依赖此项目

my-shop-commons:项目通用工具、实体类

my-shop-domain: POJO类,对于数据库一张表

my-shop-web-admin:后台管理功能

my-shop-web-api、my-shop-web-ui:商城前端展示部分,目前还没开发

BaseDao

public interface BaseDao<T> {
    int create(T model);
    int update(T model);
    int delete(int id);
    T getById(int id);
}

BaseDao定义了通用的数据操作接口其它具体的操作接口都继承该dao

public interface TbUserDao extends BaseDao<TbUser> {
    TbUser getByUserNameAndPasswrod(Map<String,String> param);
    List<TbUser> getByPage(PageQuery<TbUserQuery> pageQuery);
    int getCount(PageQuery<TbUserQuery> pageQuery);
}

TbUserDao继承BaseDao并定义了自己特有的一些接口

BaseService

public interface BaseService<T extends BaseEntity> {

    BaseResult create(T model);

    T getById(int id);

    BaseResult update(T model);

    BaseResult delete(int id);

}

BaseService定义通用的服务接口,T 就是某个具体的domain对象

然后给这个通用服务接口定义一个通用的实现 BaseServiceImpl。实现通用的CRUD

public abstract class BaseServiceImpl<T extends BaseEntity,D extends BaseDao<T>> implements BaseService<T> {
    @Autowired
    protected D dao;

    @Override
    public BaseResult create(T model) {
        BaseResult baseResult = EntityValidator.validate(model);
        if (!baseResult.isSuccess()) {
            return baseResult;
        }
        model.setCreated(new Date());
        model.setUpdated(new Date());
        return getBaseResult(dao.create(model),"新增失败");
    }

    public T getById(int id) {
        return dao.getById(id);
    }

    public BaseResult update(T model)
    {
        BaseResult baseResult = EntityValidator.validate(model);
        if (!baseResult.isSuccess()) {
            return baseResult;
        }
        model.setUpdated(new Date());
        return getBaseResult(dao.update(model),"更新失败");
    }

    public BaseResult delete(int id) {
        return getBaseResult(dao.delete(id),"删除失败");
    }

    private BaseResult getBaseResult(int row,String error){
        BaseResult baseResult = BaseResult.success();

        if(row<=0){
            baseResult=BaseResult.fail(error);
        }
        return baseResult;
    }
}

 

用户服务继承BaseService并扩展了两个方法

public interface UserService extends BaseService<TbUser>  {
     PageResult<TbUser> getByPage(PageQuery<TbUserQuery> pageQuery);
     TbUser login(String userName, String password);
}

 

用户服务的具体实现

@Service
public class UserServiceImpl extends BaseServiceImpl<TbUser,TbUserDao> implements UserService {

    @Override
    public BaseResult create(TbUser tbUser) {
        if (StringUtils.isBlank(tbUser.getPassword())) {
            return BaseResult.fail("密码不能为空");
        }
        tbUser.setPassword(md5DigestAsHex(tbUser.getPassword()));


        return super.create(tbUser);
    }

    @Override
    public PageResult<TbUser> getByPage(PageQuery<TbUserQuery> pageQuery) {
        PageResult<TbUser> result = new PageResult<>();
        List<TbUser> list = dao.getByPage(pageQuery);
        int count=dao.getCount(pageQuery);
        result.setData(list);
        result.setRecordsTotal(count);
        result.setRecordsFiltered(count);
        return result;
    }

    @Override
    public BaseResult update(TbUser tbUser) {

        if (StringUtils.isNotBlank(tbUser.getPassword())) {
            tbUser.setPassword(md5DigestAsHex(tbUser.getPassword()));
        }
       return super.update(tbUser);
    }

    @Override
    public TbUser login(String userName, String password) {
        Map<String, String> param = new HashMap<>();
        param.put("userName", userName);
        param.put("password", md5DigestAsHex(password));

        System.out.println(param.get("password"));
        return dao.getByUserNameAndPasswrod(param);
    }

    private String md5DigestAsHex(String password){
       return DigestUtils.md5DigestAsHex(password.getBytes());
    }
}

BaseController

BaseController定义了通用的列表页、表单页、删除操作的跳转

public abstract class BaseController<T extends BaseEntity,S extends BaseService<T>> {
   @Autowired
    protected S service;
    @ModelAttribute
    public T getModel(Integer id) {
        T model=initFormEntity();
        if (id != null && id > 0) {
            model = service.getById(id);
        }

        return model;
    }
    @RequestMapping("/list")
    public String list(Model model) {

        return getJspPath()+"/list";
    }
    @RequestMapping(value = {"/create", "edit"}, method = RequestMethod.GET)
    public String form() {

        return getJspPath()+"/form";
    }
    @RequestMapping("/detail")
    public String detail() {
        return getJspPath()+"/detail";
    }
    @ResponseBody
    @RequestMapping("/delete")
    public BaseResult delete(int id) {
        return service.delete(id);
    }

    @RequestMapping(value = {"/create", "edit"}, method = RequestMethod.POST)
    public String form(T entity, Model model) {
        BaseResult baseResult;
        if (entity.getId() != null && entity.getId() > 0) {
            baseResult = service.update(entity);
        } else {
            baseResult = service.create(entity);
        }

        //新增或者修改成功返回列表页
        if (baseResult.isSuccess()) {

            return "redirect:list";
        }

        //失败显示错误信息
        model.addAttribute("result", baseResult);
        return getJspPath()+"/form";
    }

    protected abstract T initFormEntity();
    protected  String getJspPath(){
        return getClass().getSimpleName().toLowerCase().replace("controller","");
    }
}

 

任何其它模块只要继承BaseController,就可以自动跳转到列表、表单、删除等页面,可以大大减少代码量

具体的UserController只需要实现特有的业务逻辑就可以

@Controller
@RequestMapping("/user")
public class UserController extends BaseController<TbUser,UserService> {

    @ResponseBody
    @RequestMapping("/page")
    public PageResult<TbUser> page(PageQuery<TbUserQuery> pageQuery, TbUserQuery query) {
        pageQuery.setQuery(query);
        PageResult<TbUser> tbUsers = service.getByPage(pageQuery);
        return tbUsers;
    }

    @Override
    protected TbUser initFormEntity() {
        return new TbUser();
    }
}

 

版权声明:本文为yehuabin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yehuabin/p/9993497.html