评论

收藏

[Java] spring+springmvc+mybatis 开发JAVA单体应用

编程语言 编程语言 发布于:2021-10-07 13:34 | 阅读数:396 | 评论:0

这篇文章主要介绍了spring+springmvc+mybatis 开发JAVA单体应用的相关知识,本文通过图文实例代码的形式给大家介绍的非常详细 ,需要的朋友可以参考下
myshop 概述
myshop项目是根据视频教程 java 单体应用 做的一个练习项目,目前完成了登录功能、用户管理、类别管理后续有时间会继续做其它的功能。趁着双11花了99元一年买了台阿里云服务器,演示地址>> myshop
项目地址 https://gitee.com/yehuabin/myshop
DSC0000.png

DSC0001.jpg

DSC0002.jpg

DSC0003.jpg

项目结构
DSC0004.jpg

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();
 }
}
总结
以上所述是小编给大家介绍的spring+springmvc+mybatis 开发java单体应用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对CodeAE代码之家网站的支持!
原文链接:https://www.cnblogs.com/yehuabin/p/9993497.html
关注下面的标签,发现更多相似文章