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 ...
随机推荐
- Java面向对象之关键字this 入门实例
一.基础概念 1.关键字this是指:哪个对象调用this所在的函数.this就指向当前这个对象. 2.用法: (1).this关键字可以解决:构造函数私有化问题. 注意:构造函数只能被构造函数调用, ...
- 【Spring Boot-技巧】API返回值去除为NULL的字段
简介 在前后端分离的微服务时代,后端API需要良好的规范.本篇主要将一个数据返回时的一个小技巧-- 过滤为空字段 解决痛点:将有效解决数据传输过程中的流量浪费. 组件简介 Jackson Object ...
- 谈谈easyui datagrid 的数据加载
文章目录 1url方式加载数据 1.1调用方式 1.2相关方法 1.3二次加载问题 2加载本地数据方式 2.1调用方式 2.2如何分页 2.3加载中效果 2.4如何不统计总数 这篇文章只谈jQuery ...
- tomcat添加管理后台的用户名和密码(为jenkins连接tomcat用)
vim ./conf/tomcat-users.xml <role rolename="admin"/> <role rolename="admin- ...
- CentOS 6.* 安装node
CentOS6 安装node 使用node -v 是报错 ./node: error while loading shared libraries: libstdc++.so.6: cannot op ...
- python基础01—基础数据类型
数据类型 单位换算 最小的单位为bit,表示一个二进制的0或1,一般使用小写的b表示 存储的最小单位为字节(Byte),1B = 8b 1024B = 1KB 1024KB = 1MB 1024MB ...
- LeetCode905.按奇偶排序数组
905.按奇偶排序数组 问题描述 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例 输入:[3,1,2, ...
- LeetCode153.寻找旋转排序数组中的最小值
153.寻找旋转排序数组中的最小值 描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 请找出 ...
- windows_study_1
描述:win8/windows server 设置用户登陆密码永不过期 解决: 第一步:打开控制面板,点击系统和安全第二部:管理工具第三步:本地安全组策略第四步:看图 第五步:把密码过期天数,改成0 ...
- 【算法笔记】B1002 写出这个数
1002 写出这个数 (20 分)读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 101 ...