小編給大家分享一下spring+springmvc+mybatis如何開(kāi)發(fā)JAVA單體應(yīng)用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)公司于2013年開(kāi)始,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元茂名做網(wǎng)站,已為上家服務(wù),為茂名各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):13518219792
myshop 概述
myshop項(xiàng)目是根據(jù)視頻教程 Java 單體應(yīng)用 做的一個(gè)練習(xí)項(xiàng)目,目前完成了登錄功能、用戶(hù)管理、類(lèi)別管理后續(xù)有時(shí)間會(huì)繼續(xù)做其它的功能。
項(xiàng)目地址 https://gitee.com/yehuabin/myshop
項(xiàng)目結(jié)構(gòu)
my-shop-dependencies:管理jar包的版本,所有項(xiàng)目依賴(lài)此項(xiàng)目
my-shop-commons:項(xiàng)目通用工具、實(shí)體類(lèi)
my-shop-domain: POJO類(lèi),對(duì)于數(shù)據(jù)庫(kù)一張表
my-shop-web-admin:后臺(tái)管理功能
my-shop-web-api、my-shop-web-ui:商城前端展示部分,目前還沒(méi)開(kāi)發(fā)
BaseDao
public interface BaseDao{ int create(T model); int update(T model); int delete(int id); T getById(int id); }
BaseDao定義了通用的數(shù)據(jù)操作接口其它具體的操作接口都繼承該dao
public interface TbUserDao extends BaseDao{ TbUser getByUserNameAndPasswrod(Map param); List getByPage(PageQuery pageQuery); int getCount(PageQuery pageQuery); }
TbUserDao繼承BaseDao并定義了自己特有的一些接口
BaseService
public interface BaseService{ BaseResult create(T model); T getById(int id); BaseResult update(T model); BaseResult delete(int id); }
BaseService定義通用的服務(wù)接口,T 就是某個(gè)具體的domain對(duì)象
然后給這個(gè)通用服務(wù)接口定義一個(gè)通用的實(shí)現(xiàn) BaseServiceImpl。實(shí)現(xiàn)通用的CRUD
public abstract class BaseServiceImpl> implements BaseService { @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; } }
用戶(hù)服務(wù)繼承BaseService并擴(kuò)展了兩個(gè)方法
public interface UserService extends BaseService{ PageResult getByPage(PageQuery pageQuery); TbUser login(String userName, String password); }
用戶(hù)服務(wù)的具體實(shí)現(xiàn)
@Service public class UserServiceImpl extends BaseServiceImplimplements 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 getByPage(PageQuery pageQuery) { PageResult result = new PageResult<>(); List 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 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定義了通用的列表頁(yè)、表單頁(yè)、刪除操作的跳轉(zhuǎn)
public abstract class BaseController> { @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); } //新增或者修改成功返回列表頁(yè) if (baseResult.isSuccess()) { return "redirect:list"; } //失敗顯示錯(cuò)誤信息 model.addAttribute("result", baseResult); return getJspPath()+"/form"; } protected abstract T initFormEntity(); protected String getJspPath(){ return getClass().getSimpleName().toLowerCase().replace("controller",""); } }
任何其它模塊只要繼承BaseController,就可以自動(dòng)跳轉(zhuǎn)到列表、表單、刪除等頁(yè)面,可以大大減少代碼量
具體的UserController只需要實(shí)現(xiàn)特有的業(yè)務(wù)邏輯就可以
@Controller @RequestMapping("/user") public class UserController extends BaseController{ @ResponseBody @RequestMapping("/page") public PageResult page(PageQuery pageQuery, TbUserQuery query) { pageQuery.setQuery(query); PageResult tbUsers = service.getByPage(pageQuery); return tbUsers; } @Override protected TbUser initFormEntity() { return new TbUser(); } }
以上是“spring+springmvc+mybatis如何開(kāi)發(fā)JAVA單體應(yīng)用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!