myshop項目是根據視頻教程 Java 單體應用 作的一個練習項目,目前完成了登陸功能、用戶管理、類別管理後續有時間會繼續作其它的功能。趁着雙11花了99元一年買了臺阿里雲服務器,演示地址>> myshop前端
項目地址 https://gitee.com/yehuabin/myshopgit
my-shop-dependencies:管理jar包的版本,全部項目依賴此項目web
my-shop-commons:項目通用工具、實體類數據庫
my-shop-domain: POJO類,對於數據庫一張表api
my-shop-web-admin:後臺管理功能服務器
my-shop-web-api、my-shop-web-ui:商城前端展現部分,目前還沒開發app
public interface BaseDao<T> { int create(T model); int update(T model); int delete(int id); T getById(int id); }
BaseDao定義了通用的數據操做接口其它具體的操做接口都繼承該daodom
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並定義了本身特有的一些接口ide
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定義了通用的列表頁、表單頁、刪除操做的跳轉
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(); } }