spring+springmvc+mybatis 开发JAVA单体应用
myshop 概述
myshop项目是根据视频教程 Java 单体应用 做的一个练习项目,目前完成了登录功能、用户管理、类别管理后续有时间会继续做其它的功能。趁着双11花了99元一年买了台阿里云服务器,演示地址>> myshop
项目地址 https://gitee.com/yehuabin/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();
}
}
spring+springmvc+mybatis 开发JAVA单体应用的更多相关文章
- 搭建Spring + SpringMVC + Mybatis框架之二(整合Spring和Mybatis)
整合Spring和Mybatis 首先给出完整的项目目录: (1)引入项目需要的jar包 使用http://maven.apache.org作为中央仓库即可. Spring核心包,mybatis核心包 ...
- 使用maven整合spring+springmvc+mybatis
使用maven整合spring+springmvc+mybatis 开发环境: 1. jdk1.8 2. eclipse4.7.0 (Oxygen) 3. mysql 5.7 在pom.xml文件中, ...
- Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6869133.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——My ...
- 框架篇:Spring+SpringMVC+Mybatis整合开发
前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十五)——SpringMVC注解开发(基础篇)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7065294.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十四)--S ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7085268.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十五)——S ...
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(二)RESTful API实战笔记(接口设计及Java后端实现)
写在前面的话 原计划这部分代码的更新也是上传到ssm-demo仓库中,因为如下原因并没有这么做: 有些使用了该项目的朋友建议重新创建一个仓库,因为原来仓库中的项目太多,结构多少有些乱糟糟的. 而且这次 ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)
用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 搭建目录环境和依赖) 四:在\resources\spring 下面 ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 构建目录环境和依赖)
引言:在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一 的基础上 继续进行项目搭建 该部分的主要目的是测通MyBatis 及Spring-dao ...
随机推荐
- springboot项目部署运行(后台);端口被占用;
打包: mvn clean package -Pprod -Dmaven.test.skip=true -Pprod 使用生产环境配置: -DskipTests,不执行测试用例,但编译测试用例类生成相 ...
- Educational Codeforces Round 61 (Rated for Div. 2) G(线段树,单调栈)
#include<bits/stdc++.h>using namespace std;int st[1000007];int top;int s[1000007],t[1000007];i ...
- 842. Split Array into Fibonacci Sequence
Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...
- [SinGuLaRiTy] 2017 百度之星程序设计大赛 初赛B
[SinGuLaRiTy-1037] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. Chess Time Limit: 2000/1000 ...
- 微信小程序HTTPS - cenos apache 下安装SSL证书
1.yum install mod_ssl 2.接下来,我们需要创建一个新目录,我们将存储服务器密钥和证书 mkdir /root/ssl 3.vi /etc/httpd/conf.d/ssl.con ...
- Python基本数据类型集合、格式化、函数
一.变量总结 1.1 变量定义 记录某种状态或者数值,并用某个名称代表这个数值或状态. 1.2 变量在内存中的表现形式 Python 中一切皆为对象,数字是对象,列表是对象,函数也是对象,任何东西都是 ...
- (STM32F4) 精準的Delay不透過Timer
從一個厲害的國外工程師看來的delay寫法,使用while loop會使用幾個指令去計算,可能會需要多少時間. while(variable--); 這行代碼執行一次預估會消耗MCU 4 clock ...
- js 封装一个均速动画函数
//动画函数---任意一个元素移动到指定的目标位置 //element为元素 target为位置 function carToon(element, target) { //设置一个定时器让他循环去增 ...
- quill 设置 初始值...
1down voteaccepted For your first issue change this: text.value = JSON.stringify(quill.root.innerHTM ...
- oracle三种连接身份
登录oracle数据库有三种连接身份 sysdba:数据库管理员,sysyoper:数据库操作员,normal:普通用户. "sysdba" 即数据库管理员 权限包括: 打 ...