一. 网上商城

1.1 商城类别

  • B2B

    • 商家对商家
  • B2C
    • 商家对客户
  • C2C
    • 客户对客户
  • O2O
    • 线上线下相结合

1.2 商城常见模块

  • 后台常见功能模块

    • 商品管理

      • 包括后台商品库存管理, 上货, 出货, 编辑管理和商品分类管理, 商品品牌管理等
    • 订单管理
      • 在线订单程序, 使消费者能够顺利的通过WEB在线的方式, 直接生成购买订单
    • 商品促销
      • 一般的购物系统多有商品促销功能, 通过商品促销功能, 能够迅速的促进商城的消费积极性
    • 支付方式
      • 即通过网上钱包, 电子支付卡, 进行网上资金流转换的业务流程, 国内主流支付方式包括: 微信, 支付宝, 网银等, 还有部分网上商城支持货到付款
    • 配送方式
      • 购物系统集成的物流配送方式, 从而方便消费者对物流方式进行在线选择, 如顺丰, EMS, 中通, 圆通, 韵达等
    • 会员模块
      • 在购物系统中, 集成会员注册是吸引会员进行二次购买和提升转换率的方式
  • 前端界面功能模块
    • 模板风格自定义

      • 即通过系统内置的模板引擎, 可以方便的通过后台可视化编辑, 设计出符合自身需求的风格界面
    • 商品多图显示
      • 随着电子商务的发展, 商品图片成为吸引消费者的第一要素, 多图展示即提供前台多张图片的展示, 从而提升消费者的购物欲望
    • 自定义广告模块
      • 内置在系统中的广告模块, 网站管理员能够顺利的通过操作就可以在前端页面中添加各种广告图片
    • 购物车
      • 用户可以把想要购买的商品添加进购物车, 随时增删商品

二. 软件开发流程[重点]

  • 软件开发流程即软件设计思路和方法的一般过程, 包括对软件先进行需求分析, 设计软件的功能和实现的算法和方法, 软件的总体结构设计和模块设计, 编码和调试, 程序联调和测试以及编写, 提交程序等一系列操作以满足客户的需求并且解决客户的问题, 如果有更高需求, 还需要对软件进行维护, 升级处理, 报废处理.

2.1 周期和阶段

从管理的角度, 即从业务和经济的角度来看, 软件的生命周期包括四个主要阶段

  • 起始阶段
  • 细化阶段
  • 构建阶段
  • 移交阶段

2.2 开发流程

  • 需求分析
  • 概要设计
  • 详细设计
  • 编码
  • 测试
  • 软件交付
  • 验收
  • 维护

三. MyShop项目介绍

3.1 开发环境

  • 操作系统

    • Window8.1
  • 数据库平台
    • MySQL5.7
  • 应用服务器
    • tomcat 8.5.54
  • Java开发工具
    • IDEA
  • 框架
  • 项目名称
    • MyShop

3.2 主要功能

  1. 用户模块

    • 用户注册, 登录, 注销
  2. 商品显示模块
    • 分页显示所有商品, 查看单个商品详细信息, 搜索商品
  3. 购物车模块
    • 加入商品到购物车, 修改购物车内商品数据
  4. 地址模块
    • 管理用户收货地址
  5. 订单模块
    • 提交订单前先预览订单, 用户确认后生成正式订单, 用户查看历史订单
  6. 支付模块
    • 完成在线支付功能
  7. 后台模块
    1. 主要实现商品的增删改查

四. 数据库设计[重点]

  • 数据库设计是指根据用户的需求, 在某一具体的数据库管理系统上, 设计数据库的结构和建立数据库的过程

4.1 PowerDesigner

  • PowerDesigner是能进行数据库设计的强大的软件, 是一款开发人员常用的数据库建模工具, 使用它可以分别从概念数据模型(Conceptual Data Model)和物理数据模型(Physical Data Model)两个层次对数据库进行设计
4.1.1 概念数据模型CDM
  • 概念数据模型描述的是独立于数据库管理系统(DBMS)的实体定义和实体关系定义
  • 在CDM中用来标识实体的是属性(Attribute)
4.1.2 物理数据模型PDM
  • 物理数据模型是在概念数据模型的基础山针对目标数据库管理系统的具体化
  • PDM的对象: 表(Table) 表中的列(Table Column), 主外键(Primary, Foreign Key), 参照(Reference), 索引(Index), 视图(View)等
  • 在PDM中用来表示实体属性的是列(Column)
4.1.3 CDM和PDM对比
  • CDM概念数据模型: 描述要存储的实体, 属性, 实体关系
  • PDM物理数据模型: 对概念数据库模型进行升级, 将实体转化为具有数据库特性的模型
    • 实体-->表
    • 属性-->列
  • 总结: 类似抽象类和实现的关系
4.1.4 数据库设计步骤
  • 设计CDM模型
  • 生成PDM模型
  • 生成DBMS脚本

4.2 用户模块涉及表

4.2.1 用户表
  1. create table user
  2. (
  3. u_id int not null auto_increment comment '用户实体的主键属性',
  4. u_name varchar(20) not null comment '用户账号',
  5. u_password varchar(64) not null comment '用户密码',
  6. u_email varchar(50) not null comment '用户邮箱',
  7. u_sex varchar(4) comment '用户性别',
  8. u_status int comment '用户的激活状态0未激活1激活',
  9. u_code varchar(64) comment '邮件激活码',
  10. u_role int comment '用户0管理员1',
  11. primary key (u_id)
  12. )
  13. charset = UTF8;
4.2.2 地址表
  1. create table address
  2. (
  3. a_id int not null auto_increment comment '地址实体',
  4. u_id int comment '用户实体的主键属性',
  5. a_name varchar(30) comment '地址的收件人',
  6. a_phone varchar(14) comment '收件人电话',
  7. a_detail varchar(200) comment '收货人详细地址',
  8. a_state int comment '0不是默认地址,1是默认地址',
  9. primary key (a_id)
  10. )
  11. charset = UTF8;

4.3 商品模块涉及表

4.3.1 商品类别表
  1. create table type
  2. (
  3. t_id int not null auto_increment comment '类别的主键id',
  4. t_name varchar(20) comment '类别的名称',
  5. t_info varchar(200) comment '类别的描述',
  6. primary key (t_id)
  7. )
  8. charset = UTF8;
4.3.2 商品表
  1. create table product
  2. (
  3. p_id int not null auto_increment comment '商品的唯一主键',
  4. t_id int comment '类别的主键id',
  5. p_name varchar(50) comment '商品名称',
  6. p_time date comment '商品上市时间',
  7. p_image varchar(100) comment '商品图片路径',
  8. p_price decimal(12) comment '商品价格',
  9. p_state int comment '商品热门指数',
  10. p_info varchar(200) comment '商品描述',
  11. primary key (p_id)
  12. )
  13. charset = UTF8;

4.4 购物车模块涉及表

4.4.1 购物车表
  1. create table cart
  2. (
  3. c_id int not null auto_increment comment '购物车的唯一标识',
  4. p_id int comment '商品的唯一主键',
  5. u_id int comment '用户实体的主键属性',
  6. c_count decimal(12) comment '购物车小计',
  7. c_num int comment '购物车商品数量',
  8. primary key (c_id)
  9. )
  10. charset = UTF8;

4.5 订单模块涉及表

4.5.1 订单表
  1. create table orders
  2. (
  3. o_id varchar(64) not null comment '订单编号是字符串类型,但也是唯一标识',
  4. a_id int comment '地址实体',
  5. u_id int comment '用户实体的主键属性',
  6. o_count decimal(12) comment '订单总金额',
  7. o_time datetime comment '订单的详细时间',
  8. o_state int comment '订单状态 0未付款,1付款未发货,,2发货未收货,,3收货待评价,4订单完成,5退货状态',
  9. o_detail varchar(200) comment '订单收货地址',
  10. primary key (o_id)
  11. )
  12. charset = UTF8;
4.5.2 订单详情表
  1. create table item
  2. (
  3. i_id int not null auto_increment comment '订单项的唯一标识',
  4. o_id varchar(64) comment '订单编号是字符串类型,但也是唯一标识',
  5. p_id int comment '商品的唯一主键',
  6. i_num int comment '订单项数量',
  7. i_count decimal(12) comment '订单项小计',
  8. primary key (i_id)
  9. )
  10. charset = UTF8;

4.6 后台管理模块

  • 没有新表

4.7 约束

  1. alter table address add constraint FK_u_a_fk foreign key (u_id)
  2. references user (u_id) on delete restrict on update restrict;
  3. alter table cart add constraint FK_p_c_fk foreign key (p_id)
  4. references product (p_id) on delete restrict on update restrict;
  5. alter table cart add constraint FK_u_c_fk foreign key (u_id)
  6. references user (u_id) on delete restrict on update restrict;
  7. alter table item add constraint FK_o_i_fk foreign key (o_id)
  8. references orders (o_id) on delete restrict on update restrict;
  9. alter table item add constraint FK_p_i_fk foreign key (p_id)
  10. references product (p_id) on delete restrict on update restrict;
  11. alter table orders add constraint FK_a_o_fk foreign key (a_id)
  12. references address (a_id) on delete restrict on update restrict;
  13. alter table orders add constraint FK_u_o_fk foreign key (u_id)
  14. references user (u_id) on delete restrict on update restrict;
  15. alter table product add constraint FK_t_p_fk foreign key (t_id)
  16. references type (t_id) on delete restrict on update restrict;

4.8 导入.sql文件

导入外部sql文件到mysql数据库中的某张表中

  • 方法一:

    • Windows+R打开控制台输入: mysql -uroot -p 数据库名 < .sql文件路径,Enter后输入密码等待一会儿即可
  • 方法二:
    • Windows+R打开控制台输入: mysql -uroot -p, Enter后输入密码进入数据库
    • use 数据库名;
    • source后面输入外部sql文件路径,Enter

五. 项目搭建和准备工作

  • 创建项目
  • 导入工具类
  • 导入jar包
  • 导入配置文件
  • 导入页面

5.1 创建BaseServlet[重点]

  • 作用: 简化和优化Servlet的创建和数量
  • 实现思路: 使一个Servlet对应多个方法! 每个方法都实现一个业务逻辑!
    1. 在一个Servlet中声明多个处理业务逻辑的方法
    2. 在每次访问Servlet时在路径上添加一个标识, 用于判断要调用具体哪一个处理业务逻辑的方法
    3. 当每次请求到doget/dopost/service方法的时候, 判断参数的标识, 调用对应的业务逻辑方法即可
  • BaseServlet的实现
    1. 实现基本的Servlet
    2. 优化方法调用
      • 利用标识符的字符串和方法名相同
      • 利用反射技术, 调用对应的执行方法
      • 避免了大量判断
      • 当有新的方法出现时, 也不需要额外的添加判断方法
    3. 优化多Servlet实现
      • 实现思路:
      • 创建一个BaseServlet类, 然后去继承HttpServlet,
      • BaseServlet中写service方法
      • service方法中: 1. 获取标识符 2. 反射调用业务逻辑
      • 每个功能模块对应的Controller只需继承BaseServlet即可
      • HttpServlet-->BaseServlet-->模块的Controller
    4. 优化返回值问题
      • 每个方法都要进行响应
      • 响应的方式固定: 转发, 重定向, 返回字符串, 返回字节流
      • 如果在每个方法中写转发重定向和返回字符串的语法比较繁琐
      • 我们可以统一在BaseServlet中进行处理
      • 操作
      • 步骤一: 将方法的返回值改成字符串
      • 步骤二: 根据约定的内容, 添加特殊的标识
        • 转发 forward: 路径
        • 重定向 redirect: 路径
      • 步骤三: BaseServlet集中处理
      • 执行方法-->获取返回值-->进行非空判断
      • 截取标识, 根据标识进行转发, 重定向 或 写回字符串
      • 好处: 简化方法的响应和操作
      • 注意: 返回字节,我们只要将方法的返回值改为void,然后使用response对象手动操作即可
5.1.1 BaseServlet
  1. package com.dz.controller;
  2. import com.dz.utils.Constants;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.lang.reflect.Method;
  10. /**
  11. * description: BaseServlet用于集中处理方法的调用!
  12. * 返回值的处理!
  13. * 以及默认页对应方法!
  14. */
  15. @WebServlet(name = "BaseServlet")
  16. public class BaseServlet extends HttpServlet {
  17. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. //1. 获取请求参数(标识符)
  19. String methodStr = req.getParameter(Constants.TAG);
  20. //2. 如果method没有获取到值,就跳转到首页(标识符异常处理)
  21. if (methodStr == null || methodStr.equals("")) {
  22. methodStr = Constants.INDEX;
  23. }
  24. //3. 反射调用对应的业务逻辑方法
  25. //3.1 获取类的class对象,
  26. Class<? extends BaseServlet> aClass = this.getClass();
  27. try {
  28. //3.2 获取方法
  29. /*
  30. 参数1: 方法名
  31. 参数2...: 方法参数的类型
  32. */
  33. Method method = aClass.getMethod(methodStr, HttpServletRequest.class, HttpServletResponse.class);
  34. //3.3 执行方法
  35. /*
  36. 参数1: 要执行方法的对象
  37. 参数2...:执行方法传入具体的参数
  38. 返回值: 执行方法的返回值!如果方法为void,返回值为null
  39. */
  40. Object result = method.invoke(this, req, resp);
  41. //4. 集中处理返回值响应(根据返回值约定的表示符判断)
  42. if (result != null) {
  43. //转发 重定向 返回字符串
  44. String str = (String) result;
  45. String path = str.substring(str.indexOf(Constants.FLAG) + 1);
  46. if (str.startsWith(Constants.FORWARD)) {
  47. //转发
  48. req.getRequestDispatcher(path).forward(req,resp);
  49. }else if (str.startsWith(Constants.REDIRECT)) {
  50. //重定向
  51. //重写url防止用户禁用cookie
  52. String newPath = resp.encodeRedirectURL(path);
  53. resp.sendRedirect(newPath);
  54. }else {
  55. //输出字符串
  56. resp.getWriter().println(str);
  57. }
  58. }
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. req.getSession().setAttribute("masg","程序异常, 请稍后再试!");
  62. resp.sendRedirect("/message.jsp");
  63. }
  64. }
  65. /*
  66. 当method中没有值时,为其赋默认值index,我们将index方法提取到BaseServlet中
  67. 没有值就默认跳转index首页
  68. */
  69. public String index(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  70. return Constants.FORWARD + "/index.jsp";
  71. }
  72. }

六. 项目代码

6.1 Controller

6.1.1 用户模块
  1. package com.dz.controller;
  2. import com.dz.entity.User;
  3. import com.dz.service.UserService;
  4. import com.dz.service.impl.UserServiceImpl;
  5. import com.dz.utils.Base64Utils;
  6. import com.dz.utils.Constants;
  7. import com.dz.utils.MD5Utils;
  8. import com.dz.utils.RandomUtils;
  9. import org.apache.commons.beanutils.BeanUtils;
  10. import javax.servlet.annotation.WebServlet;
  11. import javax.servlet.http.Cookie;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import javax.servlet.http.HttpSession;
  15. import java.lang.reflect.InvocationTargetException;
  16. import java.util.Map;
  17. /**
  18. * description: 用户模块的Controller
  19. */
  20. @WebServlet("/user")
  21. public class UserController extends BaseServlet {
  22. /**
  23. * 检测用户名
  24. */
  25. public String check(HttpServletRequest request, HttpServletResponse response) {
  26. String result = Constants.HAS_USER;
  27. //1. 获取用户名
  28. String username = request.getParameter("username");
  29. if (username != null) {
  30. //2. 调用业务逻辑判断用户名是否存在
  31. UserServiceImpl userService = new UserServiceImpl();
  32. boolean b = userService.checkedUser(username);
  33. //3. 响应字符串 1 存在 0 不存在
  34. if (b) {
  35. //用户存在.返回"1",不能注册
  36. return Constants.HAS_USER;
  37. }else {
  38. //不存在,返回0,可以注册
  39. return Constants.NOT_HAS_USER;
  40. }
  41. }
  42. return result;
  43. }
  44. /**
  45. * 注册
  46. */
  47. public String register(HttpServletRequest request, HttpServletResponse response) {
  48. //1. 获取用户信息
  49. Map<String,String[]> parameterMap = request.getParameterMap();
  50. User user = new User();
  51. try {
  52. //BeanUtils.populate( Object bean, Map properties )
  53. //这个方法会遍历map<key, value>中的key,如果bean中有这个属性,就把这个key对应的value值赋给bean的属性
  54. BeanUtils.populate(user,parameterMap);
  55. } catch (IllegalAccessException | InvocationTargetException e) {
  56. e.printStackTrace();
  57. }
  58. //2. 完善用户信息
  59. //已经赋值的: 用户id(自动生成) 用户名 密码 邮箱 性别
  60. //没有赋值的: 账号状态 账号类型 激活码
  61. user.setUstatus(Constants.USER_NOT_ACTIVE);//0 未激活 1 激活
  62. user.setUrole(Constants.ROLE_CUSTOMER);//0 普通用户 1 管理员
  63. user.setCode(RandomUtils.createActive());
  64. //需要处理的属性: 密码 MD5进行加密处理
  65. user.setUpassword(MD5Utils.md5(user.getUpassword()));
  66. //3. 调用用户的业务逻辑进行注册
  67. UserService userService = new UserServiceImpl();
  68. int row = userService.registerUser(user);
  69. if (row != 1) {//注册失败
  70. request.setAttribute("registerMsg","注册失败!");
  71. return Constants.FORWARD + "/register.jsp";
  72. }else {
  73. HttpSession session = request.getSession();
  74. session.setAttribute("user",user);
  75. session.setAttribute("registerMsg","您已注册成功,可以登陆啦!");
  76. return Constants.REDIRECT + "/registerSuccess.jsp";
  77. }
  78. }
  79. /**
  80. * 激活用户账号
  81. */
  82. public String active(HttpServletRequest request, HttpServletResponse response) {
  83. //1. 获取激活码
  84. //已经转成base64格式
  85. String c = request.getParameter("c");
  86. //base64解码
  87. String code = Base64Utils.decode(c);
  88. //2. 调用业务逻辑进行激活
  89. UserService userService = new UserServiceImpl();
  90. int row = userService.activeUser(code);
  91. //3. 响应
  92. if (row == Constants.ACTIVE_FAIL) {
  93. request.setAttribute("msg","激活失败!请重新操作!");
  94. }else if (row == Constants.ACTIVE_SUCCESS) {
  95. request.setAttribute("msg","恭喜您激活成功!可以登陆啦!");
  96. }else {
  97. request.setAttribute("msg","您已经激活过了哦!不用再次激活!");
  98. }
  99. return Constants.FORWARD + "/message.jsp";
  100. }
  101. /**
  102. * 登陆
  103. * 1. 获取前端提交的账号, 密码, 验证码
  104. * 2. 对比验证码 成功--->对比账号密码
  105. * 3. 对比账号密码
  106. * 失败: 回到登陆页面 进行提示
  107. * 成功: 未激活 登陆界面进行提示
  108. * 已激活 跳转至程序的首页 将用户放进session共享域
  109. * 4. 判断是否勾选自动登陆
  110. * 没有勾选: 清空cookie,返回响应
  111. * 勾选: 保存cookie, 返回响应
  112. */
  113. public String login(HttpServletRequest request, HttpServletResponse response) {
  114. //1. 获取请求参数(用户名, 密码, 验证码)
  115. String username = request.getParameter("username");
  116. String password = request.getParameter("password");
  117. String code = request.getParameter("code");//用户输入的验证码
  118. String auto = request.getParameter("auto");//自动登陆
  119. //正确的验证码 从Session中提取
  120. HttpSession session = request.getSession();
  121. String codes = (String) session.getAttribute("codes");
  122. //2. 判断验证码是否正确
  123. if (code == null || !code.equalsIgnoreCase(codes)) {
  124. request.setAttribute("msg","验证码输入错误!");
  125. return Constants.FORWARD + "/login.jsp";
  126. }
  127. //3. 调用业务逻辑判断账号密码
  128. UserService userService = new UserServiceImpl();
  129. User user = userService.login(username, password);
  130. //4. 响应
  131. //user 等于null证明账号密码错误,无法登陆
  132. //user 不为null, 但user的状态是未激活,无法登陆
  133. if (user == null) {
  134. request.setAttribute("msg","登陆失败!");
  135. return Constants.FORWARD + "/login.jsp";
  136. }
  137. if (user.getUstatus().equals(Constants.USER_NOT_ACTIVE)) {
  138. request.setAttribute("msg","账号未激活!!");
  139. return Constants.FORWARD + "/login.jsp";
  140. }
  141. session.setAttribute("loginUser",user);
  142. //判断是否勾选自动登陆
  143. if (auto == null) {
  144. //没有勾选
  145. //将本地浏览器存储的自动登陆cookie 清空
  146. Cookie cookie = new Cookie(Constants.AUTO_NAME,"");//清空
  147. cookie.setPath("/");
  148. cookie.setMaxAge(0);
  149. response.addCookie(cookie);
  150. }else {
  151. //勾选自动登陆,cookie存储在用户本地浏览器两周
  152. String content = username + Constants.FLAG + password;
  153. content = Base64Utils.encode(content);
  154. Cookie cookie = new Cookie(Constants.AUTO_NAME,content);
  155. cookie.setPath("/");
  156. cookie.setMaxAge(14*24*60*60);
  157. response.addCookie(cookie);
  158. }
  159. return Constants.REDIRECT + "/index.jsp";
  160. }
  161. /**
  162. * 注销!
  163. * 1. 清空数据(session和cookie)!
  164. * 2. 跳转到登陆界面!
  165. */
  166. public String logout(HttpServletRequest request, HttpServletResponse response) {
  167. //1. 清空session中的用户数据
  168. HttpSession session = request.getSession();
  169. session.removeAttribute("loginUser");
  170. //2. 清空和覆盖cookie存储的自动登陆信息
  171. Cookie cookie = new Cookie(Constants.AUTO_NAME,"");//清空autoUser
  172. cookie.setPath("/");
  173. cookie.setMaxAge(0);
  174. response.addCookie(cookie);
  175. //3. 转发到登陆界面
  176. request.setAttribute("msg","注销成功!");
  177. return Constants.FORWARD + "/login.jsp";
  178. }
  179. }
6.2.1 地址模块
  1. package com.dz.controller;
  2. import com.dz.entity.Address;
  3. import com.dz.entity.User;
  4. import com.dz.service.AddressService;
  5. import com.dz.service.impl.AddressServiceImpl;
  6. import com.dz.utils.Constants;
  7. import org.apache.commons.beanutils.BeanUtils;
  8. import javax.servlet.annotation.WebServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import javax.servlet.http.HttpSession;
  12. import java.lang.reflect.InvocationTargetException;
  13. import java.util.List;
  14. import java.util.Map;
  15. /**
  16. * description: 地址模块的Controller
  17. */
  18. @WebServlet("/address")
  19. public class AddressController extends BaseServlet{
  20. private AddressService addressService = new AddressServiceImpl();
  21. public String getAddress(HttpServletRequest request, HttpServletResponse response) {
  22. //1. 判断用户是否登陆
  23. HttpSession session = request.getSession();
  24. User user = (User) session.getAttribute("loginUser");
  25. if (user == null) {//如未登录
  26. session.setAttribute("msg","请先登陆哦!");
  27. return Constants.REDIRECT + "/login.jsp";
  28. }
  29. int uid = user.getUid();
  30. List<Address> addressList = addressService.findAddressByUid(uid);
  31. request.setAttribute("addressList",addressList);
  32. return Constants.FORWARD + "/self_info.jsp";
  33. }
  34. public String add(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
  35. //1. 获取请求参数
  36. Map<String, String[]> map = request.getParameterMap();
  37. Address address = new Address();
  38. BeanUtils.populate(address, map);
  39. //2. 调用业务逻辑添加地址
  40. int row = addressService.addAddress(address);
  41. //3. 转发到展示的方法
  42. if (row != 1) {
  43. return Constants.FORWARD + "/index.jsp";
  44. }
  45. return Constants.FORWARD + "/address?method=getAddress";
  46. }
  47. public String delete(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
  48. //1. 获取请求参数
  49. String aid = request.getParameter("aid");
  50. //2. 调用业务逻辑添加地址
  51. int row = addressService.deleteAddress(aid);
  52. //3. 转发到展示的方法
  53. if (row != 1) {
  54. return Constants.FORWARD + "/index.jsp";
  55. }
  56. return Constants.FORWARD + "/address?method=getAddress";
  57. }
  58. public String setDefault(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
  59. HttpSession session = request.getSession();
  60. User user = (User) session.getAttribute("loginUser");
  61. if (user == null) {//如未登录
  62. session.setAttribute("msg","请先登陆哦!");
  63. return Constants.REDIRECT + "/login.jsp";
  64. }
  65. int uid = user.getUid();
  66. //1. 获取请求参数
  67. String aid = request.getParameter("aid");
  68. //2. 调用业务逻辑添加地址
  69. int row = addressService.defaultAddr(aid, uid);
  70. //3. 转发到展示的方法
  71. if (row != Constants.DEFAULT_ADDRESS) {
  72. return Constants.FORWARD + "/index.jsp";
  73. }
  74. return Constants.FORWARD + "/address?method=getAddress";
  75. }
  76. public String update(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
  77. //1. 获取请求参数
  78. Map<String, String[]> map = request.getParameterMap();
  79. Address address = new Address();
  80. BeanUtils.populate(address, map);
  81. //2. 调用业务逻辑添加地址
  82. int row = addressService.updateAddress(address);
  83. //3. 转发到展示的方法
  84. if (row != 1) {
  85. return Constants.FORWARD + "/index.jsp";
  86. }
  87. return Constants.FORWARD + "/address?method=getAddress";
  88. }
  89. }
6.3.1 商品类别模块
  1. package com.dz.controller;
  2. import com.dz.entity.Type;
  3. import com.dz.service.TypeService;
  4. import com.dz.service.impl.TypeServiceImpl;
  5. import com.google.gson.Gson;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.util.List;
  10. /**
  11. * description: 商品类别模块的Controller
  12. */
  13. @WebServlet("/type")
  14. public class TypeController extends BaseServlet{
  15. public String findAll(HttpServletRequest request, HttpServletResponse response) {
  16. TypeService typeService = new TypeServiceImpl();
  17. List<Type> typeList = typeService.findAll();
  18. Gson gson = new Gson();
  19. return gson.toJson(typeList);//转为json并返回
  20. }
  21. }
6.4.1 商品模块
  1. package com.dz.controller;
  2. import com.dz.entity.PageBean;
  3. import com.dz.entity.Product;
  4. import com.dz.service.ProductService;
  5. import com.dz.service.impl.ProductServiceImpl;
  6. import com.dz.utils.Constants;
  7. import javax.servlet.annotation.WebServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. /**
  11. * description: 商品模块的Controller
  12. */
  13. @WebServlet("/product")
  14. public class ProductController extends BaseServlet{
  15. public String show(HttpServletRequest request, HttpServletResponse response) {
  16. //1. 获取请求参数 tid(类别id)
  17. String tid = request.getParameter("tid");
  18. //从前端获取当前页数和页容量
  19. int pageSize = 8;
  20. String currentPage = request.getParameter("currentPage");
  21. int page = 1;
  22. if (currentPage != null) {
  23. page = Integer.parseInt(currentPage);
  24. }
  25. //2. 调用业务逻辑得到前端需要展示的PageBean
  26. ProductService productService = new ProductServiceImpl();
  27. PageBean<Product> pageBean = productService.findPage(tid, page, pageSize);
  28. //3. 响应即可
  29. request.setAttribute("pageBean",pageBean);
  30. return Constants.FORWARD + "/goodsList.jsp";
  31. }
  32. public String detail(HttpServletRequest request, HttpServletResponse response) {
  33. //1. 获取请求参数
  34. String pid = request.getParameter("pid");
  35. //2. 调用业务逻辑
  36. ProductService productService = new ProductServiceImpl();
  37. Product product = productService.findProductByPid(pid);
  38. //3. 响应
  39. request.setAttribute("product",product);
  40. return Constants.FORWARD + "/goodsDetail.jsp";
  41. }
  42. }
6.5.1 购物车模块
  1. package com.dz.controller;
  2. import com.dz.entity.Cart;
  3. import com.dz.entity.User;
  4. import com.dz.service.CartService;
  5. import com.dz.service.impl.CartServiceImpl;
  6. import com.dz.utils.Constants;
  7. import javax.servlet.annotation.WebServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import javax.servlet.http.HttpSession;
  11. import java.util.List;
  12. /**
  13. * description: 购物车模块的Controller
  14. */
  15. @WebServlet("/cart")
  16. public class CartController extends BaseServlet {
  17. private CartService cartService = new CartServiceImpl();
  18. //加入购物车
  19. public String addCart(HttpServletRequest request, HttpServletResponse response) {
  20. //1. 判断用户是否登陆
  21. HttpSession session = request.getSession();
  22. User user = (User) session.getAttribute("loginUser");
  23. if (user == null) {//如未登录
  24. session.setAttribute("msg","添加购物车前请先登陆哦!");
  25. return Constants.REDIRECT + "/login.jsp";
  26. }
  27. //不为空,登陆状态
  28. //2. 获取用户id和商品id
  29. int uid = user.getUid();
  30. String pid = request.getParameter("pid");
  31. int row = cartService.addCart(uid, pid);
  32. if (row != 1) {
  33. //2. 结果不为1则添加失败
  34. return Constants.REDIRECT + "/login.jsp";
  35. }
  36. return Constants.FORWARD + "/cartSuccess.jsp";
  37. }
  38. //展示购物车
  39. public String show(HttpServletRequest request, HttpServletResponse response) {
  40. //1. 判断用户是否登陆
  41. HttpSession session = request.getSession();
  42. User user = (User) session.getAttribute("loginUser");
  43. if (user == null) {//如未登录
  44. session.setAttribute("msg","添加购物车前请先登陆哦!");
  45. return Constants.REDIRECT + "/login.jsp";
  46. }
  47. //2. 获取参数
  48. int uid = user.getUid();
  49. //3. 调用业务逻辑
  50. List<Cart> cartList = cartService.findAll(uid);
  51. request.setAttribute("cartList",cartList);
  52. return Constants.FORWARD + "/cart.jsp";
  53. }
  54. //购物车内商品删除
  55. public String delete(HttpServletRequest request, HttpServletResponse response) {
  56. //1. 获取cid
  57. String cid = request.getParameter("cid");
  58. //2. 调用业务逻辑
  59. int row = cartService.deleteCart(cid);
  60. if (row != 1) {
  61. return Constants.FORWARD + "/index.jsp";
  62. }
  63. return Constants.FORWARD + "/cart?method=show";
  64. }
  65. //购物车商品加减
  66. public String update(HttpServletRequest request, HttpServletResponse response) {
  67. //1. 获取cid
  68. String cid = request.getParameter("cid");
  69. String cnum = request.getParameter("cnum");
  70. String pprice = request.getParameter("pprice");
  71. //2. 调用业务逻辑
  72. int row = cartService.updateCart(cid, cnum, pprice);
  73. if (row != 1) {
  74. return Constants.FORWARD + "/index.jsp";
  75. }
  76. return Constants.FORWARD + "/cart?method=show";
  77. }
  78. public String clear(HttpServletRequest request, HttpServletResponse response) {
  79. //1. 获取uid
  80. String uid = request.getParameter("uid");
  81. //2. 调用业务逻辑
  82. int row = cartService.clearCart(uid);
  83. return Constants.FORWARD + "/cart?method=show";
  84. }
  85. }
6.6.1 订单模块
  1. package com.dz.controller;
  2. import com.dz.entity.Address;
  3. import com.dz.entity.Cart;
  4. import com.dz.entity.Orders;
  5. import com.dz.entity.User;
  6. import com.dz.service.AddressService;
  7. import com.dz.service.CartService;
  8. import com.dz.service.OrderService;
  9. import com.dz.service.impl.AddressServiceImpl;
  10. import com.dz.service.impl.CartServiceImpl;
  11. import com.dz.service.impl.OrderServiceImpl;
  12. import com.dz.utils.Constants;
  13. import javax.servlet.annotation.WebServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import javax.servlet.http.HttpSession;
  17. import java.util.List;
  18. /**
  19. * description: 订单模块的Controller
  20. */
  21. @WebServlet("/order")
  22. public class OrderController extends BaseServlet {
  23. private OrderService orderService = new OrderServiceImpl();
  24. private AddressService addressService = new AddressServiceImpl();
  25. private CartService cartService = new CartServiceImpl();
  26. //进入订单页面
  27. public String preview(HttpServletRequest request, HttpServletResponse response) {
  28. //1. 获取参数
  29. String uid = request.getParameter("uid");
  30. //2. 调用业务逻辑
  31. //获取地址列表
  32. List<Address> addressList = addressService.findAddressByUid(Integer.parseInt(uid));
  33. //获取购物车列表
  34. List<Cart> cartList = cartService.findAll(Integer.parseInt(uid));
  35. //3. 放入共享域
  36. request.setAttribute("addressList",addressList);
  37. request.setAttribute("cartList",cartList);
  38. //4. 跳转到订单页面
  39. return Constants.FORWARD + "order.jsp";
  40. }
  41. //提交订单
  42. public String create(HttpServletRequest request, HttpServletResponse response) {
  43. //1. 获取请求参数
  44. String uid = request.getParameter("uid");
  45. String sum = request.getParameter("sum");
  46. String aid = request.getParameter("aid");
  47. //2. 调用业务逻辑结构
  48. orderService.createOrder(uid, sum, aid);
  49. return Constants.FORWARD + "order?method=show";
  50. }
  51. public String show(HttpServletRequest request, HttpServletResponse response) {
  52. HttpSession session = request.getSession();
  53. User user = (User) session.getAttribute("loginUser");
  54. if (user == null) {//如未登录
  55. session.setAttribute("msg","请先登陆哦!");
  56. return Constants.REDIRECT + "/login.jsp";
  57. }
  58. int uid = user.getUid();
  59. List<Orders> orderList = orderService.showOrders(uid);
  60. request.setAttribute("orderList",orderList);
  61. return Constants.FORWARD + "/orderList.jsp";
  62. }
  63. public String detail(HttpServletRequest request, HttpServletResponse response) {
  64. String oid = request.getParameter("oid");
  65. Orders orderDetails = orderService.showDetails(oid);
  66. request.setAttribute("orderDetails",orderDetails);
  67. return Constants.FORWARD + "/orderDetail.jsp";
  68. }
  69. }
6.7.1 验证码
  1. package com.dz.controller;
  2. import cn.dsna.util.images.ValidateCode;
  3. import javax.servlet.ServletOutputStream;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. /**
  9. * description: 生成验证码的Controller
  10. */
  11. @WebServlet("/code")
  12. public class CodeController extends BaseServlet{
  13. public void createCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
  14. //1. 生成验证码对象
  15. ValidateCode validateCode = new ValidateCode(100,35,4,20);
  16. //2. 将验证码放入session
  17. String codes = validateCode.getCode();
  18. request.getSession().setAttribute("codes",codes);
  19. //3. 向页面写回验证码
  20. ServletOutputStream outputStream = response.getOutputStream();
  21. validateCode.write(outputStream);
  22. }
  23. }

6.2 Service

6.2.1 UserServiceImpl
  1. package com.dz.service.impl;
  2. import com.dz.dao.UserDao;
  3. import com.dz.dao.impl.UserDaoImpl;
  4. import com.dz.entity.User;
  5. import com.dz.service.UserService;
  6. import com.dz.utils.Constants;
  7. import com.dz.utils.DbUtils;
  8. import com.dz.utils.EmailUtils;
  9. import com.dz.utils.MD5Utils;
  10. /**
  11. * description: 用户模块 业务逻辑实现类
  12. */
  13. public class UserServiceImpl implements UserService {
  14. // 创建Dao访问对象
  15. private UserDao userDao = new UserDaoImpl();
  16. @Override
  17. public boolean checkedUser(String username) {
  18. User user = null;
  19. //1. 查找用户名
  20. try {
  21. DbUtils.begin();
  22. user = userDao.selectUserByUN(username);
  23. DbUtils.commit();
  24. } catch (Exception e) {
  25. DbUtils.rollback();
  26. e.printStackTrace();
  27. }
  28. //2. 判断用户名是否存在
  29. //true 存在 false 不存在
  30. return user != null;
  31. }
  32. @Override
  33. public int registerUser(User user) {
  34. int row = 0;
  35. try {
  36. DbUtils.begin();
  37. //1. 将用户信息保存到数据库
  38. row = userDao.insertUser(user);
  39. //2. 发送一封邮件
  40. EmailUtils.sendEmail(user);
  41. DbUtils.commit();
  42. } catch (Exception e) {
  43. DbUtils.rollback();
  44. e.printStackTrace();
  45. }
  46. return row;
  47. }
  48. @Override
  49. public int activeUser(String code) {
  50. int row = Constants.ACTIVE_FAIL;
  51. try {
  52. DbUtils.begin();
  53. //1. 根据激活码查找用户
  54. User user = userDao.selectUserByCode(code);
  55. if (user != null) {//用户不为空,找到用户
  56. //2. 判断用户是否激活
  57. if (user.getUstatus().equals(Constants.USER_ACTIVE)) {
  58. row = Constants.ACTIVE_ALREADY;//如用户已激活则返回2(已激活状态)
  59. }else {
  60. //3. 进行激活操作
  61. row = userDao.updateStatusByUid(user.getUid());
  62. }
  63. }
  64. DbUtils.commit();
  65. } catch (Exception e) {
  66. DbUtils.rollback();
  67. e.printStackTrace();
  68. }
  69. return row;
  70. }
  71. @Override
  72. public User login(String username, String password) {
  73. User user = null;
  74. try {
  75. DbUtils.begin();
  76. //1. 需要将密码用MD5处理
  77. String md5password = MD5Utils.md5(password);
  78. //2. 根据用户名查找用户
  79. User temp = userDao.selectUserByUN(username);
  80. if (temp != null) {
  81. if (temp.getUpassword().equals(md5password)) {
  82. user = temp;
  83. }
  84. }
  85. DbUtils.commit();
  86. } catch (Exception e) {
  87. DbUtils.rollback();
  88. e.printStackTrace();
  89. }
  90. return user;
  91. }
  92. }
6.2.2 AddressServiceImpl
  1. package com.dz.service.impl;
  2. import com.dz.dao.AddressDao;
  3. import com.dz.dao.impl.AddressDaoImpl;
  4. import com.dz.entity.Address;
  5. import com.dz.service.AddressService;
  6. import com.dz.utils.Constants;
  7. import com.dz.utils.DbUtils;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. /**
  11. * description: 地址模块 业务逻辑实现类
  12. */
  13. public class AddressServiceImpl implements AddressService {
  14. private AddressDao addressDao = new AddressDaoImpl();
  15. @Override
  16. public List<Address> findAddressByUid(int uid) {
  17. List<Address> addressList = new ArrayList<>();
  18. try {
  19. DbUtils.begin();
  20. addressList = addressDao.selectAddressByUid(uid);
  21. DbUtils.commit();
  22. } catch (Exception e) {
  23. DbUtils.rollback();
  24. e.printStackTrace();
  25. }
  26. return addressList;
  27. }
  28. @Override
  29. public int addAddress(Address address) {
  30. int row = 0;
  31. try {
  32. DbUtils.begin();
  33. row = addressDao.insertAddress(address);
  34. DbUtils.commit();
  35. } catch (Exception e) {
  36. DbUtils.rollback();
  37. e.printStackTrace();
  38. }
  39. return row;
  40. }
  41. @Override
  42. public int deleteAddress(String aid) {
  43. int row = 0;
  44. try {
  45. DbUtils.begin();
  46. row = addressDao.delAddress(aid);
  47. DbUtils.commit();
  48. } catch (Exception e) {
  49. DbUtils.rollback();
  50. e.printStackTrace();
  51. }
  52. return row;
  53. }
  54. @Override
  55. public int defaultAddr(String aid, int uid) {
  56. int row = Constants.COMMON_ADDRESS;
  57. try {
  58. DbUtils.begin();
  59. //1. 将aid的状态改为1
  60. int row1 = addressDao.setDefaultByAid(aid);
  61. //2. 将非aid的状态改为0
  62. int row2 = addressDao.setCommonByAid(aid, uid);
  63. if (row1 == Constants.DEFAULT_ADDRESS && row2 == Constants.DEFAULT_ADDRESS) {
  64. row = Constants.DEFAULT_ADDRESS;
  65. }
  66. DbUtils.commit();
  67. } catch (Exception e) {
  68. DbUtils.rollback();
  69. e.printStackTrace();
  70. }
  71. return row;
  72. }
  73. @Override
  74. public int updateAddress(Address address) {
  75. int row = 0;
  76. try {
  77. DbUtils.begin();
  78. row = addressDao.uptAddressByAid(address);
  79. DbUtils.commit();
  80. } catch (Exception e) {
  81. DbUtils.rollback();
  82. e.printStackTrace();
  83. }
  84. return row;
  85. }
  86. }
6.2.3 TypeServiceImpl
  1. package com.dz.service.impl;
  2. import com.dz.dao.TypeDao;
  3. import com.dz.dao.impl.TypeDaoImpl;
  4. import com.dz.entity.Type;
  5. import com.dz.service.TypeService;
  6. import com.dz.utils.DbUtils;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /**
  10. * description: 商品类别模块 业务逻辑实现类
  11. */
  12. public class TypeServiceImpl implements TypeService {
  13. private TypeDao typeDao = new TypeDaoImpl();
  14. @Override
  15. public List<Type> findAll() {
  16. List<Type> typeList = new ArrayList<>();
  17. try {
  18. DbUtils.begin();
  19. List<Type> temp = typeDao.selectAll();
  20. if (temp != null) {
  21. typeList = temp;
  22. }
  23. DbUtils.commit();
  24. } catch (Exception e) {
  25. DbUtils.rollback();
  26. e.printStackTrace();
  27. }
  28. return typeList;
  29. }
  30. }
6.2.4 ProductServiceImpl
  1. package com.dz.service.impl;
  2. import com.dz.dao.ProductDao;
  3. import com.dz.dao.impl.ProductDaoImpl;
  4. import com.dz.entity.PageBean;
  5. import com.dz.entity.Product;
  6. import com.dz.service.ProductService;
  7. import com.dz.utils.DbUtils;
  8. import java.util.List;
  9. /**
  10. * description: 商品模块 业务逻辑实现类
  11. */
  12. public class ProductServiceImpl implements ProductService {
  13. private ProductDao productDao = new ProductDaoImpl();
  14. @Override
  15. public PageBean<Product> findPage(String tid, int page, int pageSize) {
  16. PageBean<Product> pageBean = null;
  17. try {
  18. DbUtils.begin();
  19. long counts = productDao.selectCountByTid(tid);
  20. List<Product> list = productDao.selectProductByPage(page, pageSize, tid);
  21. pageBean = new PageBean<Product>(list, page, pageSize,counts);
  22. DbUtils.commit();
  23. } catch (Exception e) {
  24. DbUtils.rollback();
  25. e.printStackTrace();
  26. }
  27. return pageBean;
  28. }
  29. @Override
  30. public Product findProductByPid(String pid) {
  31. Product product = null;
  32. try {
  33. DbUtils.begin();
  34. product = productDao.selectProductByPid(pid);
  35. DbUtils.commit();
  36. } catch (Exception e) {
  37. DbUtils.rollback();
  38. e.printStackTrace();
  39. }
  40. return product;
  41. }
  42. }
6.2.5 CartServiceImpl
  1. package com.dz.service.impl;
  2. import com.dz.dao.CartDao;
  3. import com.dz.dao.ProductDao;
  4. import com.dz.dao.impl.CartDaoImpl;
  5. import com.dz.dao.impl.ProductDaoImpl;
  6. import com.dz.entity.Cart;
  7. import com.dz.entity.Product;
  8. import com.dz.service.CartService;
  9. import com.dz.utils.DbUtils;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. /**
  13. * description: 购物车模块 业务逻辑实现类
  14. */
  15. public class CartServiceImpl implements CartService {
  16. private CartDao cartDao = new CartDaoImpl();
  17. @Override
  18. public int addCart(int uid, String pid) {
  19. int row = 0;
  20. try {
  21. DbUtils.begin();
  22. //1. 判断购物车中是否存在此商品
  23. Cart cart = cartDao.hasThisProduct(uid, pid);
  24. if (cart != null) {
  25. //2. 购物车中已存在此商品, 则修改数量和小计
  26. cart.setCnum(cart.getCnum() + 1);
  27. row = cartDao.updateCart(cart);
  28. }else {
  29. //3. 若购物车不存在此商品则添加
  30. //根据商品id查询此商品
  31. ProductDao productDao = new ProductDaoImpl();
  32. Product product = productDao.selectProductByPid(pid);
  33. //为购物车对象赋值
  34. cart = new Cart();
  35. cart.setPid(Integer.parseInt(pid));
  36. cart.setUid(uid);
  37. cart.setProduct(product);
  38. cart.setCnum(1);
  39. //加入购物车
  40. row = cartDao.insertCart(cart);
  41. }
  42. DbUtils.commit();
  43. } catch (NumberFormatException e) {
  44. DbUtils.rollback();
  45. e.printStackTrace();
  46. }
  47. return row;
  48. }
  49. @Override
  50. public List<Cart> findAll(int uid) {
  51. List<Cart> cartList = new ArrayList<>();
  52. try {
  53. DbUtils.begin();
  54. cartList = cartDao.selectCartByUid(uid);
  55. DbUtils.commit();
  56. } catch (Exception e) {
  57. DbUtils.rollback();
  58. e.printStackTrace();
  59. }
  60. return cartList;
  61. }
  62. @Override
  63. public int deleteCart(String cid) {
  64. int row = 0;
  65. try {
  66. DbUtils.begin();
  67. row = cartDao.deleteCartByCid(cid);
  68. DbUtils.commit();
  69. } catch (Exception e) {
  70. DbUtils.rollback();
  71. e.printStackTrace();
  72. }
  73. return row;
  74. }
  75. @Override
  76. public int updateCart(String cid, String cnum, String pprice) {
  77. int row = 0;
  78. try {
  79. DbUtils.begin();
  80. row = cartDao.updateCartByCid(cid, cnum, pprice);
  81. DbUtils.commit();
  82. } catch (Exception e) {
  83. DbUtils.rollback();
  84. e.printStackTrace();
  85. }
  86. return row;
  87. }
  88. @Override
  89. public int clearCart(String uid) {
  90. int row = 0;
  91. try {
  92. DbUtils.begin();
  93. row = cartDao.clearCartByUid(uid);
  94. DbUtils.commit();
  95. } catch (Exception e) {
  96. DbUtils.rollback();
  97. e.printStackTrace();
  98. }
  99. return row;
  100. }
  101. }
6.2.6 OrderServiceImpl
  1. package com.dz.service.impl;
  2. import com.dz.dao.AddressDao;
  3. import com.dz.dao.CartDao;
  4. import com.dz.dao.OrderDao;
  5. import com.dz.dao.impl.OrderDaoImpl;
  6. import com.dz.dao.impl.AddressDaoImpl;
  7. import com.dz.dao.impl.CartDaoImpl;
  8. import com.dz.entity.Address;
  9. import com.dz.entity.Cart;
  10. import com.dz.entity.Item;
  11. import com.dz.entity.Orders;
  12. import com.dz.service.OrderService;
  13. import com.dz.utils.DbUtils;
  14. import com.dz.utils.RandomUtils;
  15. import java.math.BigDecimal;
  16. import java.util.ArrayList;
  17. import java.util.Date;
  18. import java.util.List;
  19. /**
  20. * description: 订单模块 业务逻辑实现类
  21. */
  22. public class OrderServiceImpl implements OrderService {
  23. private OrderDao orderDao = new OrderDaoImpl();
  24. @Override
  25. public void createOrder(String uid, String sum, String aid) {
  26. try {
  27. DbUtils.begin();
  28. //1. 创建一个订单对象进行保存
  29. Orders orders = new Orders();
  30. String orderId = RandomUtils.createOrderId();//使用随机数工具类得到订单id
  31. AddressDao addressDao = new AddressDaoImpl();
  32. Address address = addressDao.selectAddressByAid(aid);//获取到aid对应的地址对象
  33. BigDecimal sumBig = new BigDecimal(sum);//总钱数要转为BigDecimal
  34. orders.setOid(orderId);
  35. orders.setAid(Integer.parseInt(aid));
  36. orders.setUid(Integer.parseInt(uid));
  37. orders.setOtime(new Date());
  38. orders.setOcount(sumBig);
  39. orders.setOstate(0);
  40. orders.setOdetail(address.getAdetail());
  41. //2. 保存订单
  42. orderDao.insetOrders(orders);
  43. //3. 将购物车转成订单项
  44. CartDao cartDao = new CartDaoImpl();
  45. //获取到购物车集合
  46. List<Cart> cartList = cartDao.selectCartByUid(Integer.parseInt(uid));
  47. //创建订单集合
  48. List<Item> itemList = new ArrayList<>();
  49. for (Cart cart : cartList) {//遍历购物车集合
  50. Item item = new Item();//创建订单项对象
  51. //为订单项对象赋值
  52. item.setOid(orderId);
  53. item.setPid(cart.getPid());
  54. item.setInum(cart.getCnum());
  55. item.setIcount(cart.getCcount());
  56. //加入订单项集合
  57. itemList.add(item);
  58. }
  59. //4. 保存订单对应的订单项
  60. orderDao.insertItems(itemList);
  61. //5. 清空购物车
  62. cartDao.clearCartByUid(uid);
  63. DbUtils.commit();
  64. } catch (NumberFormatException e) {
  65. DbUtils.rollback();
  66. e.printStackTrace();
  67. }
  68. }
  69. @Override
  70. public List<Orders> showOrders(int uid) {
  71. List<Orders> orderList = new ArrayList<>();
  72. try {
  73. DbUtils.begin();
  74. orderList = orderDao.selectOrderByUid(uid);
  75. DbUtils.commit();
  76. } catch (Exception e) {
  77. DbUtils.rollback();
  78. e.printStackTrace();
  79. }
  80. return orderList;
  81. }
  82. @Override
  83. public Orders showDetails(String oid) {
  84. //1. 通过oid查询订单和订单地址信息
  85. //订单和地址
  86. Orders orders = orderDao.selectOrdersByOid(oid);
  87. //2. 通过oid查询对应的订单项和商品信息
  88. //订单项和商品
  89. List<Item> itemList = orderDao.selectItemsByOid(oid);
  90. //3. 订单项集合设置给订单对象
  91. orders.setItems(itemList);
  92. return orders;
  93. }
  94. }

6.3 Dao

6.3.1 UserDaoImpl
  1. package com.dz.dao.impl;
  2. import com.dz.dao.UserDao;
  3. import com.dz.entity.User;
  4. import com.dz.utils.Constants;
  5. import com.dz.utils.DbUtils;
  6. import org.apache.commons.dbutils.QueryRunner;
  7. import org.apache.commons.dbutils.handlers.BeanHandler;
  8. import java.sql.SQLException;
  9. /**
  10. * description: 用户模块 数据访问实现类
  11. */
  12. public class UserDaoImpl implements UserDao {
  13. // 创建一个queryRunner对象
  14. private QueryRunner queryRunner = new QueryRunner();
  15. @Override
  16. public User selectUserByUN(String username) {
  17. User user = null;
  18. //1. 编写SQL语句
  19. String sql = "select u_id as uid, u_name as username, u_password as upassword" +
  20. ", u_email as email, u_sex as usex, u_status as ustatus, u_code as code" +
  21. ", u_role as urole from user where u_name = ?";
  22. try {
  23. //2. 执行SQL语句
  24. user = queryRunner.query(DbUtils.getConnection(),sql, new BeanHandler<User>(User.class), username);
  25. } catch (SQLException throwables) {
  26. throwables.printStackTrace();
  27. }
  28. //3. 返回查询到的结果
  29. return user;
  30. }
  31. @Override
  32. public int insertUser(User user) {
  33. int row = 0;
  34. //1. 编写SQL语句
  35. String sql = "insert into user(u_name, u_password, u_email, u_sex, u_status, u_code, u_role) values(?,?,?,?,?,?,?)";
  36. try {
  37. //2. 执行SQL语句
  38. row = queryRunner.update(DbUtils.getConnection(), sql, user.getUsername(), user.getUpassword(),
  39. user.getEmail(), user.getUsex(), user.getUstatus(), user.getCode(), user.getUrole());
  40. } catch (SQLException throwables) {
  41. throwables.printStackTrace();
  42. }
  43. //3. 返回受影响行数
  44. return row;
  45. }
  46. @Override
  47. public User selectUserByCode(String code) {
  48. User user = null;
  49. //1. 编写SQL语句
  50. String sql = "select u_id as uid, u_name as username, u_password as upassword" +
  51. ", u_email as email, u_sex as usex, u_status as ustatus, u_code as code" +
  52. ", u_role as urole from user where u_code = ?";
  53. try {
  54. //2. 执行SQL语句
  55. user = queryRunner.query(DbUtils.getConnection(),sql, new BeanHandler<User>(User.class), code);
  56. } catch (SQLException throwables) {
  57. throwables.printStackTrace();
  58. }
  59. //3. 返回查询到的结果
  60. return user;
  61. }
  62. @Override
  63. public int updateStatusByUid(int uid) {
  64. int row = 0;
  65. //1. 编写SQL语句
  66. String sql = "update user set u_status = ? where u_id = ?";
  67. try {
  68. row = queryRunner.update(DbUtils.getConnection(), sql, Constants.USER_ACTIVE, uid);
  69. } catch (SQLException throwables) {
  70. throwables.printStackTrace();
  71. }
  72. return row;
  73. }
  74. }
6.3.2 AddressDaoImpl
  1. package com.dz.dao.impl;
  2. import com.dz.dao.AddressDao;
  3. import com.dz.entity.Address;
  4. import com.dz.utils.Constants;
  5. import com.dz.utils.DbUtils;
  6. import org.apache.commons.dbutils.QueryRunner;
  7. import org.apache.commons.dbutils.handlers.BeanHandler;
  8. import org.apache.commons.dbutils.handlers.BeanListHandler;
  9. import java.sql.SQLException;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. /**
  13. * description: 地址模块 数据访问实现类
  14. */
  15. public class AddressDaoImpl implements AddressDao {
  16. private QueryRunner queryRunner = new QueryRunner();
  17. @Override
  18. public List<Address> selectAddressByUid(int uid) {
  19. List<Address> addressList = new ArrayList<>();
  20. String sql = "select a_id as aid, u_id as uid, a_name as aname, a_phone as aphone," +
  21. " a_detail as adetail,a_state as astate from address where u_id = ? order by astate desc ";
  22. try {
  23. addressList = queryRunner.query(DbUtils.getConnection(), sql, new BeanListHandler<Address>(Address.class), uid);
  24. } catch (SQLException throwables) {
  25. throwables.printStackTrace();
  26. }
  27. return addressList;
  28. }
  29. @Override
  30. public int insertAddress(Address address) {
  31. int row = 0;
  32. String sql = "insert into address(u_id,a_name,a_phone,a_detail,a_state) values(?,?,?,?,?)";
  33. try {
  34. row = queryRunner.update(DbUtils.getConnection(), sql, address.getUid(), address.getAname(), address.getAphone(), address.getAdetail(), address.getAstate());
  35. } catch (SQLException throwables) {
  36. throwables.printStackTrace();
  37. }
  38. return row;
  39. }
  40. @Override
  41. public int delAddress(String aid) {
  42. int row = 0;
  43. String sql = "delete from address where a_id = ?";
  44. try {
  45. row = queryRunner.update(DbUtils.getConnection(), sql, aid);
  46. } catch (SQLException throwables) {
  47. throwables.printStackTrace();
  48. }
  49. return row;
  50. }
  51. @Override
  52. public int setDefaultByAid(String aid) {
  53. int row = 0;
  54. String sql = "update address set a_state = ? where a_id = ?";
  55. try {
  56. row = queryRunner.update(DbUtils.getConnection(), sql, Constants.DEFAULT_ADDRESS, aid);
  57. } catch (SQLException throwables) {
  58. throwables.printStackTrace();
  59. }
  60. return row;
  61. }
  62. @Override
  63. public int setCommonByAid(String aid, int uid) {
  64. int row = 0;
  65. String sql = "update address set a_state = ? where a_id != ? and u_id = ?";
  66. try {
  67. row = queryRunner.update(DbUtils.getConnection(), sql, Constants.COMMON_ADDRESS, aid, uid);
  68. } catch (SQLException throwables) {
  69. throwables.printStackTrace();
  70. }
  71. return row;
  72. }
  73. @Override
  74. public int uptAddressByAid(Address address) {
  75. int row = 0;
  76. String sql = "update address set a_name = ?, a_phone = ?, a_detail = ?, a_state = ? where a_id = ?";
  77. try {
  78. row = queryRunner.update(DbUtils.getConnection(), sql, address.getAname(), address.getAphone(), address.getAdetail(), address.getAstate(), address.getAid());
  79. } catch (SQLException throwables) {
  80. throwables.printStackTrace();
  81. }
  82. return row;
  83. }
  84. @Override
  85. public Address selectAddressByAid(String aid) {
  86. Address address = null;
  87. String sql = "select a_detail as adetail from address where a_id = ? ";
  88. try {
  89. address = queryRunner.query(DbUtils.getConnection(), sql, new BeanHandler<Address>(Address.class), aid);
  90. } catch (SQLException throwables) {
  91. throwables.printStackTrace();
  92. }
  93. return address;
  94. }
  95. }
6.3.3 TypeDaoImpl
  1. package com.dz.dao.impl;
  2. import com.dz.dao.TypeDao;
  3. import com.dz.entity.Type;
  4. import com.dz.utils.DbUtils;
  5. import org.apache.commons.dbutils.QueryRunner;
  6. import org.apache.commons.dbutils.handlers.BeanListHandler;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. /**
  11. * description: 商品类别模块 数据访问实现类
  12. */
  13. public class TypeDaoImpl implements TypeDao {
  14. private QueryRunner queryRunner = new QueryRunner();
  15. @Override
  16. public List<Type> selectAll() {
  17. List<Type> typeList = new ArrayList<>();
  18. String sql = "select t_id as tid, t_name as tname, t_info as tinfo from type limit 5";
  19. try {
  20. typeList = queryRunner.query(DbUtils.getConnection(), sql, new BeanListHandler<Type>(Type.class));
  21. } catch (SQLException throwables) {
  22. throwables.printStackTrace();
  23. }
  24. return typeList;
  25. }
  26. }
6.3.4 ProductDaoImpl
  1. package com.dz.dao.impl;
  2. import com.dz.dao.ProductDao;
  3. import com.dz.entity.Product;
  4. import com.dz.utils.DbUtils;
  5. import org.apache.commons.dbutils.QueryRunner;
  6. import org.apache.commons.dbutils.handlers.BeanHandler;
  7. import org.apache.commons.dbutils.handlers.BeanListHandler;
  8. import org.apache.commons.dbutils.handlers.ScalarHandler;
  9. import java.sql.SQLException;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. /**
  13. * description: 商品模块 数据访问实现类
  14. */
  15. public class ProductDaoImpl implements ProductDao {
  16. private QueryRunner queryRunner = new QueryRunner();
  17. @Override
  18. public long selectCountByTid(String tid) {
  19. long counts = 0;
  20. String sql = "select count(*) from product where t_id = ?";
  21. try {
  22. Object result = queryRunner.query(DbUtils.getConnection(), sql, new ScalarHandler(), tid);
  23. counts = (long) result;
  24. } catch (SQLException throwables) {
  25. throwables.printStackTrace();
  26. }
  27. return counts;
  28. }
  29. @Override
  30. public List<Product> selectProductByPage(int page, int pageSize, String tid) {
  31. List<Product> productList = new ArrayList<>();
  32. String sql = "select p_id as pid, t_id as tid, p_name as pname, p_time as ptime" +
  33. ", p_image as pimage, p_price as pprice, p_state as pstate, p_info as pinfo" +
  34. " from product where t_id = ? limit ?,?";
  35. try {
  36. productList = queryRunner.query(DbUtils.getConnection(), sql, new BeanListHandler<Product>(Product.class), tid, (page-1)*pageSize, pageSize);
  37. } catch (SQLException throwables) {
  38. throwables.printStackTrace();
  39. }
  40. return productList;
  41. }
  42. @Override
  43. public Product selectProductByPid(String pid) {
  44. Product product = null;
  45. String sql = "select p_id as pid, t_id as tid, p_name as pname, p_time as ptime" +
  46. ", p_image as pimage, p_price as pprice, p_state as pstate, p_info as pinfo" +
  47. " from product where p_id = ?";
  48. try {
  49. product = queryRunner.query(DbUtils.getConnection(), sql, new BeanHandler<Product>(Product.class), pid);
  50. } catch (SQLException throwables) {
  51. throwables.printStackTrace();
  52. }
  53. return product;
  54. }
  55. }
6.3.5 CartDaoImpl
  1. package com.dz.dao.impl;
  2. import com.dz.dao.CartDao;
  3. import com.dz.entity.Cart;
  4. import com.dz.entity.Product;
  5. import com.dz.utils.DbUtils;
  6. import org.apache.commons.beanutils.BeanUtils;
  7. import org.apache.commons.dbutils.QueryRunner;
  8. import org.apache.commons.dbutils.handlers.MapHandler;
  9. import org.apache.commons.dbutils.handlers.MapListHandler;
  10. import java.lang.reflect.InvocationTargetException;
  11. import java.math.BigDecimal;
  12. import java.sql.SQLException;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Map;
  16. /**
  17. * description: 购物车模块 数据访问实现类
  18. */
  19. public class CartDaoImpl implements CartDao {
  20. private QueryRunner queryRunner = new QueryRunner();
  21. //查询购物车中是否有此商品
  22. @Override
  23. public Cart hasThisProduct(int uid, String pid) {
  24. Cart cart = null;
  25. //连接查询,多表查询
  26. String sql = "select product.p_id as pid, t_id as tid, p_name as pname, p_time as ptime, " +
  27. "p_image as pimage, p_price as pprice, p_state as pstate, p_info as pinfo, " +
  28. "c_id as cid, cart.p_id as pid, u_id as uid, c_count as ccount, c_num as cnum " +
  29. "from product inner join cart on product.p_id = cart.p_id where cart.u_id = ? and cart.p_id = ?";
  30. try {
  31. //存入Map集合
  32. Map<String, Object> map = queryRunner.query(DbUtils.getConnection(), sql, new MapHandler(), uid, pid);
  33. if (map != null) {
  34. cart = new Cart();
  35. Product product = new Product();
  36. //BeanUtils.populate( Object bean, Map properties )
  37. //这个方法会遍历map<key, value>中的key,如果bean中有这个属性,就把这个key对应的value值赋给bean的属性
  38. BeanUtils.populate(cart,map);
  39. BeanUtils.populate(product,map);
  40. cart.setProduct(product);
  41. }
  42. } catch (SQLException | IllegalAccessException | InvocationTargetException throwables) {
  43. throwables.printStackTrace();
  44. }
  45. return cart;
  46. }
  47. //更新购物车(如购物车内已有此商品,更新即可)
  48. @Override
  49. public int updateCart(Cart cart) {
  50. int row = 0;
  51. String sql = "update cart set c_num = ?,c_count = ? where c_id = ?";
  52. try {
  53. row = queryRunner.update(DbUtils.getConnection(), sql, cart.getCnum(), cart.getCcount(), cart.getCid());
  54. } catch (SQLException throwables) {
  55. throwables.printStackTrace();
  56. }
  57. return row;
  58. }
  59. //添加购物车
  60. @Override
  61. public int insertCart(Cart cart) {
  62. int row = 0;
  63. String sql = "insert into cart(p_id,u_id,c_count,c_num) values(?,?,?,?)";
  64. try {
  65. row = queryRunner.update(DbUtils.getConnection(), sql, cart.getPid(), cart.getUid(), cart.getCcount(), cart.getCnum());
  66. } catch (SQLException throwables) {
  67. throwables.printStackTrace();
  68. }
  69. return row;
  70. }
  71. //查询用户所有的购物车(一个用户有多个购物车,每个商品对应一个购物车)
  72. @Override
  73. public List<Cart> selectCartByUid(int uid) {
  74. List<Cart> cartList = new ArrayList<>();
  75. //连接查询,多表查询,查询cart需要关联到商品表
  76. String sql = "select product.p_id as pid, t_id as tid, p_name as pname, p_time as ptime, " +
  77. "p_image as pimage, p_price as pprice, p_state as pstate, p_info as pinfo, " +
  78. "c_id as cid, cart.p_id as pid, u_id as uid, c_count as ccount, c_num as cnum " +
  79. "from product inner join cart on product.p_id = cart.p_id where cart.u_id = ?";
  80. try {
  81. List<Map<String, Object>> mapList = queryRunner.query(DbUtils.getConnection(), sql, new MapListHandler(), uid);
  82. if (mapList != null) {
  83. for (Map<String, Object> map : mapList) {
  84. //cart + product
  85. Cart cart = new Cart();
  86. Product product = new Product();
  87. BeanUtils.populate(cart, map);
  88. BeanUtils.populate(product, map);
  89. cart.setProduct(product);
  90. cartList.add(cart);
  91. }
  92. }
  93. } catch (SQLException | IllegalAccessException | InvocationTargetException throwables) {
  94. throwables.printStackTrace();
  95. }
  96. return cartList;
  97. }
  98. //删除用户一个购物车中的商品(不是清空购物车,用户还有其他购物车,存着其他商品)
  99. @Override
  100. public int deleteCartByCid(String cid) {
  101. int row = 0;
  102. String sql = "delete from cart where c_id = ?";
  103. try {
  104. row = queryRunner.update(DbUtils.getConnection(), sql, cid);
  105. } catch (SQLException throwables) {
  106. throwables.printStackTrace();
  107. }
  108. return row;
  109. }
  110. //修改某一个购物车中的商品数据(数量,小计),实现购物车内商品加减操作
  111. @Override
  112. public int updateCartByCid(String cid, String cnum, String pprice) {
  113. BigDecimal cnumBig = new BigDecimal(cnum);
  114. BigDecimal ppriceBig = new BigDecimal(pprice);
  115. BigDecimal ccount = cnumBig.multiply(ppriceBig);
  116. int row = 0;
  117. String sql = "update cart set c_num = ?, c_count = ? where c_id = ?";
  118. try {
  119. row = queryRunner.update(DbUtils.getConnection(), sql, cnum, ccount, cid );
  120. } catch (SQLException throwables) {
  121. throwables.printStackTrace();
  122. }
  123. return row;
  124. }
  125. //清空所有的购物车
  126. @Override
  127. public int clearCartByUid(String uid) {
  128. int row = 0;
  129. String sql = "delete from cart where u_id = ?";
  130. try {
  131. row = queryRunner.update(DbUtils.getConnection(), sql, uid);
  132. } catch (SQLException throwables) {
  133. throwables.printStackTrace();
  134. }
  135. return row;
  136. }
  137. }
6.3.6 OrderDaoImpl
  1. package com.dz.dao.impl;
  2. import com.dz.dao.OrderDao;
  3. import com.dz.entity.Address;
  4. import com.dz.entity.Item;
  5. import com.dz.entity.Orders;
  6. import com.dz.entity.Product;
  7. import com.dz.utils.DbUtils;
  8. import org.apache.commons.beanutils.BeanUtils;
  9. import org.apache.commons.dbutils.QueryRunner;
  10. import org.apache.commons.dbutils.handlers.MapHandler;
  11. import org.apache.commons.dbutils.handlers.MapListHandler;
  12. import java.lang.reflect.InvocationTargetException;
  13. import java.sql.SQLException;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.Map;
  17. /**
  18. * description: 订单模块 数据访问实现类
  19. */
  20. public class OrderDaoImpl implements OrderDao {
  21. private QueryRunner queryRunner = new QueryRunner();
  22. @Override
  23. public void insetOrders(Orders orders) {
  24. String sql = "insert into orders(o_id,a_id,u_id,o_count,o_time,o_state,o_detail) values(?,?,?,?,?,?,?)";
  25. try {
  26. queryRunner.update(DbUtils.getConnection(), sql, orders.getOid(), orders.getAid(), orders.getUid(),
  27. orders.getOcount(), orders.getOtime(), orders.getOstate(), orders.getOdetail());
  28. } catch (SQLException throwables) {
  29. throwables.printStackTrace();
  30. }
  31. }
  32. @Override
  33. public void insertItems(List<Item> itemList) {
  34. Object [][] params = new Object[itemList.size()][];
  35. String sql = "insert into item(o_id,p_id,i_num,i_count) values(?,?,?,?)";
  36. for (int i = 0; i < itemList.size(); i++) {
  37. Item item = itemList.get(i);
  38. params[i] = new Object[]{item.getOid(), item.getPid(), item.getInum(), item.getIcount()};
  39. }
  40. try {
  41. queryRunner.batch(DbUtils.getConnection(), sql, params);
  42. } catch (SQLException throwables) {
  43. throwables.printStackTrace();
  44. }
  45. }
  46. @Override
  47. public List<Orders> selectOrderByUid(int uid) {
  48. List<Orders> orderList = new ArrayList<>();
  49. String sql = "select o_id as oid, o.a_id as aid, o.u_id as uid, o_count as ocount, o_time as otime," +
  50. "o_state as ostate, o_detail as odetail, a.a_id as aid, a.u_id as uid, a_name as aname, " +
  51. "a_phone as aphone, a_detail as adetail, a_state as astate from orders o inner join " +
  52. "address a on o.a_id = a.a_id where o.u_id = ?";
  53. try {
  54. List<Map<String, Object>> mapList = queryRunner.query(DbUtils.getConnection(), sql, new MapListHandler(), uid);
  55. if (mapList != null) {
  56. for (Map<String, Object> map : mapList) {
  57. Orders orders = new Orders();
  58. Address address = new Address();
  59. BeanUtils.populate(orders, map);
  60. BeanUtils.populate(address, map);
  61. orders.setAddress(address);
  62. orderList.add(orders);
  63. }
  64. }
  65. } catch (SQLException | IllegalAccessException | InvocationTargetException throwables) {
  66. throwables.printStackTrace();
  67. }
  68. return orderList;
  69. }
  70. @Override
  71. public Orders selectOrdersByOid(String oid) {
  72. Orders order = null;
  73. String sql = "select o_id as oid, o.a_id as aid, o.u_id as uid, o_count as ocount, o_time as otime," +
  74. "o_state as ostate, o_detail as odetail, a.a_id as aid, a.u_id as uid, a_name as aname, " +
  75. "a_phone as aphone, a_detail as adetail, a_state as astate from orders o inner join " +
  76. "address a on o.a_id = a.a_id where o_id = ?";
  77. try {
  78. Map<String, Object> map = queryRunner.query(DbUtils.getConnection(), sql, new MapHandler(), oid);
  79. if (map != null) {
  80. order = new Orders();
  81. Address address = new Address();
  82. BeanUtils.populate(order, map);
  83. BeanUtils.populate(address, map);
  84. order.setAddress(address);
  85. }
  86. } catch (SQLException | IllegalAccessException | InvocationTargetException throwables) {
  87. throwables.printStackTrace();
  88. }
  89. return order;
  90. }
  91. @Override
  92. public List<Item> selectItemsByOid(String oid) {
  93. List<Item> itemList = new ArrayList<>();
  94. String sql = "select p.p_id as pid, t_id as tid, p_name as pname, p_time as ptime" +
  95. ", p_image as pimage, p_price as pprice, p_state as pstate, p_info as pinfo," +
  96. "i_id as iid, o_id as oid, i.p_id as pid, i_num as inum, i_count as icount " +
  97. "from product p inner join item i on p.p_id = i.p_id where o_id = ?";
  98. try {
  99. List<Map<String, Object>> mapList = queryRunner.query(DbUtils.getConnection(), sql, new MapListHandler(), oid);
  100. if (mapList != null) {
  101. for (Map<String, Object> map : mapList) {
  102. Item item = new Item();
  103. Product product = new Product();
  104. BeanUtils.populate(item, map);
  105. BeanUtils.populate(product, map);
  106. item.setProduct(product);
  107. itemList.add(item);
  108. }
  109. }
  110. } catch (SQLException | IllegalAccessException | InvocationTargetException throwables) {
  111. throwables.printStackTrace();
  112. }
  113. return itemList;
  114. }
  115. }

6.4 Filter

6.4.1 AutoFilter
  1. package com.dz.filter;
  2. import com.dz.entity.User;
  3. import com.dz.service.impl.UserServiceImpl;
  4. import com.dz.utils.Base64Utils;
  5. import com.dz.utils.Constants;
  6. import javax.servlet.*;
  7. import javax.servlet.annotation.WebFilter;
  8. import javax.servlet.http.Cookie;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import javax.servlet.http.HttpSession;
  12. import java.io.IOException;
  13. /**
  14. * description: 自动登陆过滤器
  15. */
  16. @WebFilter("/login.jsp") //默认情况下, 过滤器只过滤正常请求和重定向
  17. public class AutoFilter implements Filter {
  18. @Override
  19. public void init(FilterConfig filterConfig) throws ServletException {
  20. }
  21. @Override
  22. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  23. HttpServletRequest request = (HttpServletRequest)servletRequest;
  24. HttpServletResponse response = (HttpServletResponse)servletResponse;
  25. Cookie[] cookies =request.getCookies();
  26. if (cookies != null) {
  27. //本地存储了cookie
  28. String content = null;//用来存储用户名和密码
  29. for (Cookie cookie : cookies) {
  30. //如果找到存储 自动登陆cookie 的名字就读取里面存储的账号密码
  31. if (cookie.getName().equals(Constants.AUTO_NAME)) {
  32. content = cookie.getValue();
  33. }
  34. }
  35. if (content != null) {
  36. //读取到了 自动登陆cookie 存储的用户名和密码
  37. //base64解码
  38. content = Base64Utils.decode(content);
  39. //从:处切割,分成用户名和密码
  40. String[] split = content.split(Constants.FLAG);
  41. String username = split[0];
  42. String password = split[1];
  43. //调用业务逻辑进行登陆, 如果可以登陆证明用户名和密码正确
  44. UserServiceImpl userService = new UserServiceImpl();
  45. User user = userService.login(username, password);
  46. if (user != null) {
  47. //可以自动登陆
  48. HttpSession session = request.getSession();
  49. session.setAttribute("loginUser",user);
  50. response.sendRedirect(request.getContextPath() + "/index.jsp");
  51. }else {
  52. filterChain.doFilter(servletRequest,servletResponse);
  53. }
  54. }else {
  55. //没有读取到, 放行
  56. filterChain.doFilter(servletRequest,servletResponse);
  57. }
  58. }else {
  59. //本地没有存储cookie,放行即可
  60. filterChain.doFilter(servletRequest,servletResponse);
  61. }
  62. }
  63. @Override
  64. public void destroy() {
  65. }
  66. }
6.4.2 EncodingFilter
  1. package com.dz.filter;
  2. import javax.servlet.*;
  3. import javax.servlet.annotation.WebFilter;
  4. import java.io.IOException;
  5. /**
  6. * description: 处理乱码问题
  7. */
  8. @WebFilter(filterName = "EncodingFilter",value = "/*")
  9. public class EncodingFilter implements Filter {
  10. public void destroy() {
  11. }
  12. public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
  13. req.setCharacterEncoding("UTF-8");
  14. resp.setContentType("text/html;charset=UTF-8");
  15. chain.doFilter(req, resp);
  16. }
  17. public void init(FilterConfig config) throws ServletException {
  18. }
  19. }

6.5 Utils

6.5.1 Constants
  1. package com.dz.utils;
  2. /**
  3. * description: 项目的常量类
  4. */
  5. public class Constants {
  6. /**
  7. * 定义BaseServlet中涉及的常量
  8. */
  9. public static final String TAG = "method";
  10. public static final String FORWARD = "forward:";
  11. public static final String REDIRECT = "redirect:";
  12. public static final String FLAG = ":";
  13. public static final String INDEX = "index";
  14. /**
  15. * 定义用户模块涉及的常量
  16. */
  17. public static final String HAS_USER = "1";
  18. public static final String NOT_HAS_USER = "0";
  19. public static final String USER_ACTIVE = "1";
  20. public static final String USER_NOT_ACTIVE = "0";
  21. public static final int ROLE_ADMIN = 1;
  22. public static final int ROLE_CUSTOMER = 0;
  23. /**
  24. * 用户模块账号激活结果
  25. */
  26. public static final int ACTIVE_SUCCESS = 1;
  27. public static final int ACTIVE_FAIL = 0;
  28. public static final int ACTIVE_ALREADY = 2;
  29. /**
  30. * 自动登陆cookie名
  31. */
  32. public static final String AUTO_NAME = "autoUser";
  33. /**
  34. * 地址模块 1 默认地址 0 普通地址
  35. */
  36. public static final int DEFAULT_ADDRESS = 1;
  37. public static final int COMMON_ADDRESS = 0;
  38. }
6.5.2 DbUtils
  1. package com.dz.utils;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import com.alibaba.druid.pool.DruidDataSourceFactory;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.sql.Connection;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.Properties;
  11. /**
  12. * belongsProject: MyShop
  13. */
  14. public class DbUtils {
  15. private static DruidDataSource ds;
  16. private static final ThreadLocal<Connection> THREAD_LOCAL = new ThreadLocal<>();
  17. static {
  18. Properties properties = new Properties();
  19. InputStream inputStream = DbUtils.class.getResourceAsStream("/database.properties");
  20. try {
  21. properties.load(inputStream);
  22. ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. public static Connection getConnection(){
  30. Connection conn = THREAD_LOCAL.get();
  31. try {
  32. if (conn == null) {
  33. conn = ds.getConnection();
  34. THREAD_LOCAL.set(conn);
  35. }
  36. } catch (SQLException throwables) {
  37. throwables.printStackTrace();
  38. }
  39. return conn;
  40. }
  41. public static void begin() {
  42. Connection conn = null;
  43. try {
  44. conn = getConnection();
  45. conn.setAutoCommit(false);
  46. } catch (SQLException throwables) {
  47. throwables.printStackTrace();
  48. }
  49. }
  50. public static void commit() {
  51. Connection conn = null;
  52. try {
  53. conn = getConnection();
  54. conn.commit();
  55. } catch (SQLException throwables) {
  56. throwables.printStackTrace();
  57. } finally {
  58. closeAll(conn,null,null);
  59. }
  60. }
  61. public static void rollback() {
  62. Connection conn = null;
  63. try {
  64. conn = getConnection();
  65. conn.rollback();
  66. } catch (SQLException throwables) {
  67. throwables.printStackTrace();
  68. } finally {
  69. closeAll(conn,null,null);
  70. }
  71. }
  72. public static void closeAll(Connection conn, Statement statement, ResultSet resultSet) {
  73. try {
  74. if (resultSet != null) {
  75. resultSet.close();
  76. }
  77. if (statement != null) {
  78. statement.close();
  79. }
  80. if (conn != null) {
  81. conn.close();
  82. THREAD_LOCAL.remove();
  83. }
  84. } catch (SQLException throwables) {
  85. throwables.printStackTrace();
  86. }
  87. }
  88. }
6.5.3 RandomUtils
  1. package com.dz.utils;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Random;
  5. //生成邮箱验证 激活码 使用的随机数
  6. public class RandomUtils {
  7. //当前时间 + 随机数
  8. public static String createActive(){
  9. return getTime()+Integer.toHexString(new Random().nextInt(900)+100);
  10. }
  11. public static String getTime(){
  12. return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar.getInstance().getTime());
  13. }
  14. //生成订单编号
  15. public static String createOrderId(){
  16. return getTime();
  17. }
  18. }
6.5.4 Base64Utils
  1. package com.dz.utils;
  2. import java.util.Base64;
  3. //base64 加密 解密 激活邮件的时候 为 邮箱地址 code验证码 进行加密
  4. //当 回传回来后 进行 code 的解密
  5. public class Base64Utils {
  6. //加密
  7. public static String encode(String msg){
  8. return Base64.getEncoder().encodeToString(msg.getBytes());
  9. }
  10. //解密
  11. public static String decode(String msg){
  12. return new String(Base64.getDecoder().decode(msg));
  13. }
  14. }
6.5.5 EmailUtils
  1. package com.dz.utils;
  2. import com.dz.entity.User;
  3. import javax.mail.MessagingException;
  4. import javax.mail.Session;
  5. import javax.mail.Transport;
  6. import javax.mail.internet.InternetAddress;
  7. import javax.mail.internet.MimeMessage;
  8. import java.io.UnsupportedEncodingException;
  9. import java.net.Inet4Address;
  10. import java.net.UnknownHostException;
  11. import java.util.Date;
  12. import java.util.Properties;
  13. /*
  14. * 1.首先需要获取发送邮件的Session对象
  15. * Session session = Session.getDefaultInstance(Properties prop)
  16. * 2.使用session对象 获取待发送的邮件信息
  17. * MimeMessage mime = new MimeMessage(session)
  18. * 3.设置发件人 收件人 标题 邮件内容 附件 发送时间等等
  19. * 4.利用Transport 发送邮件
  20. * */
  21. public class EmailUtils {
  22. public static void sendEmail(User user){
  23. //发送方
  24. String myAccount = "dzjava137@163.com";
  25. //授权码
  26. String myPass = "IDKINWUVBANSGGVJ";
  27. //发件人 邮箱的 SMTP 服务器地址
  28. String SMTPHost = "smtp.163.com";
  29. //组成 properties
  30. Properties prop = new Properties();
  31. prop.setProperty("mail.transport.protocol", "smtp");//设置协议类型
  32. prop.setProperty("mail.smtp.host", SMTPHost);//定义发件人的邮箱服务器地址
  33. prop.setProperty("mail.smtp.auth", "true");//设置请求验证
  34. //1.Session对象 创建会话 用于和邮箱服务器进行交互
  35. Session session = Session.getDefaultInstance(prop);
  36. //设置debug模式 可以查看详细发送信息 可略
  37. //session.setDebug(true);
  38. //2.创建方法 用来组成一封完整的邮件
  39. //参数 session(参数配置), myAccount 发送方 , user.getEmail() 接收方
  40. MimeMessage message = createMsg(session,myAccount,user);
  41. //4.利用Transport 发送邮件
  42. try {
  43. Transport tran = session.getTransport();
  44. //连接服务器 确认发送方 是否授权
  45. tran.connect(myAccount, myPass);
  46. //发送邮件 将message 对象 传给 Transport 对象 将邮件发送出去
  47. //参数1 要发的内容 参数2 要给哪些人发
  48. //message.getAllRecipients() 获取到所有的收件人 | 抄送 | 密送
  49. tran.sendMessage(message, message.getAllRecipients());
  50. //关闭连接
  51. tran.close();
  52. } catch (MessagingException e) {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. }
  56. }
  57. private static MimeMessage createMsg(Session session, String myAccount, User user) {
  58. //使用session对象 获取待发送的邮件信息
  59. MimeMessage message = new MimeMessage(session);
  60. //3.设置发件人 收件人 标题 邮件内容 附件 发送时间等等
  61. try {
  62. //3.1发件人 from
  63. message.setFrom(new InternetAddress(myAccount, "小米", "utf-8"));
  64. //3.2收件人 to 支持可以添加多个收件人 | 抄送 | 密送 如果想要发送给多个人 可以重复下面代码多次
  65. /*
  66. * MimeMessage.RecipientType.TO 发送
  67. * MimeMessage.RecipientType.CC 抄送
  68. * MimeMessage.RecipientType.BCC 密送
  69. * */
  70. message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(user.getEmail(), user.getUsername(), "utf-8"));
  71. //3.3生成邮件主题
  72. message.setSubject("MyShop商城账号激活邮件","utf-8");
  73. String ip = Inet4Address.getLocalHost().getHostAddress();
  74. String url = "http://"+ip+":8080/user?method=active&c="+ Base64Utils.encode(user.getCode());
  75. //设置邮件正文 setContent 可以使用html标签
  76. message.setContent(user.getUsername()+",你好<br>欢迎注册myshop商城! 请点击链接进行激活:<a href='"+url+"'>点击此处</a>","text/html;charset=utf-8");
  77. //设置邮件的发送时间 是立即发送
  78. message.setSentDate(new Date());
  79. //保存设置
  80. message.saveChanges();
  81. } catch (UnsupportedEncodingException | MessagingException | UnknownHostException e) {
  82. // TODO Auto-generated catch block
  83. e.printStackTrace();
  84. }
  85. return message;
  86. }
  87. }
6.5.6 MD5Utils
  1. package com.dz.utils;
  2. import java.math.BigInteger;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. //MD5 生成器
  6. public class MD5Utils {
  7. public static String md5(String password){
  8. //生成一个md5加密器
  9. try {
  10. MessageDigest md = MessageDigest.getInstance("MD5");
  11. //计算MD5 的值
  12. md.update(password.getBytes());
  13. //BigInteger 将8位的字符串 转成16位的字符串 得到的字符串形式是哈希码值
  14. //BigInteger(参数1,参数2) 参数1 是 1为正数 0为0 -1为负数
  15. return new BigInteger(1, md.digest()).toString(16);
  16. } catch (NoSuchAlgorithmException e) {
  17. // TODO Auto-generated catch block
  18. e.printStackTrace();
  19. }
  20. return null;
  21. }
  22. }
6.5.7 PaymentUtil
  1. package com.dz.utils;
  2. import java.io.UnsupportedEncodingException;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.util.Arrays;
  6. public class PaymentUtil {
  7. private static String encodingCharset = "UTF-8";
  8. /**
  9. * 生成hmac方法
  10. *
  11. * @param p0_Cmd 业务类型
  12. * @param p1_MerId 商户编号
  13. * @param p2_Order 商户订单号
  14. * @param p3_Amt 支付金额
  15. * @param p4_Cur 交易币种
  16. * @param p5_Pid 商品名称
  17. * @param p6_Pcat 商品种类
  18. * @param p7_Pdesc 商品描述
  19. * @param p8_Url 商户接收支付成功数据的地址
  20. * @param p9_SAF 送货地址
  21. * @param pa_MP 商户扩展信息
  22. * @param pd_FrpId 银行编码
  23. * @param pr_NeedResponse 应答机制
  24. * @param keyValue 商户密钥
  25. * @return
  26. */
  27. public static String buildHmac(String p0_Cmd,String p1_MerId,
  28. String p2_Order, String p3_Amt, String p4_Cur,String p5_Pid, String p6_Pcat,
  29. String p7_Pdesc,String p8_Url, String p9_SAF,String pa_MP,String pd_FrpId,
  30. String pr_NeedResponse,String keyValue) {
  31. StringBuilder sValue = new StringBuilder();
  32. // 业务类型
  33. sValue.append(p0_Cmd);
  34. // 商户编号
  35. sValue.append(p1_MerId);
  36. // 商户订单号
  37. sValue.append(p2_Order);
  38. // 支付金额
  39. sValue.append(p3_Amt);
  40. // 交易币种
  41. sValue.append(p4_Cur);
  42. // 商品名称
  43. sValue.append(p5_Pid);
  44. // 商品种类
  45. sValue.append(p6_Pcat);
  46. // 商品描述
  47. sValue.append(p7_Pdesc);
  48. // 商户接收支付成功数据的地址
  49. sValue.append(p8_Url);
  50. // 送货地址
  51. sValue.append(p9_SAF);
  52. // 商户扩展信息
  53. sValue.append(pa_MP);
  54. // 银行编码
  55. sValue.append(pd_FrpId);
  56. // 应答机制
  57. sValue.append(pr_NeedResponse);
  58. return PaymentUtil.hmacSign(sValue.toString(), keyValue);
  59. }
  60. /**
  61. * 返回校验hmac方法
  62. *
  63. * @param hmac 支付网关发来的加密验证码
  64. * @param p1_MerId 商户编号
  65. * @param r0_Cmd 业务类型
  66. * @param r1_Code 支付结果
  67. * @param r2_TrxId 易宝支付交易流水号
  68. * @param r3_Amt 支付金额
  69. * @param r4_Cur 交易币种
  70. * @param r5_Pid 商品名称
  71. * @param r6_Order 商户订单号
  72. * @param r7_Uid 易宝支付会员ID
  73. * @param r8_MP 商户扩展信息
  74. * @param r9_BType 交易结果返回类型
  75. * @param keyValue 密钥
  76. * @return
  77. */
  78. public static boolean verifyCallback(String hmac, String p1_MerId,
  79. String r0_Cmd, String r1_Code, String r2_TrxId, String r3_Amt,
  80. String r4_Cur, String r5_Pid, String r6_Order, String r7_Uid,
  81. String r8_MP, String r9_BType, String keyValue) {
  82. StringBuilder sValue = new StringBuilder();
  83. // 商户编号
  84. sValue.append(p1_MerId);
  85. // 业务类型
  86. sValue.append(r0_Cmd);
  87. // 支付结果
  88. sValue.append(r1_Code);
  89. // 易宝支付交易流水号
  90. sValue.append(r2_TrxId);
  91. // 支付金额
  92. sValue.append(r3_Amt);
  93. // 交易币种
  94. sValue.append(r4_Cur);
  95. // 商品名称
  96. sValue.append(r5_Pid);
  97. // 商户订单号
  98. sValue.append(r6_Order);
  99. // 易宝支付会员ID
  100. sValue.append(r7_Uid);
  101. // 商户扩展信息
  102. sValue.append(r8_MP);
  103. // 交易结果返回类型
  104. sValue.append(r9_BType);
  105. String sNewString = PaymentUtil.hmacSign(sValue.toString(), keyValue);
  106. return sNewString.equals(hmac);
  107. }
  108. /**
  109. * @param aValue
  110. * @param aKey
  111. * @return
  112. */
  113. public static String hmacSign(String aValue, String aKey) {
  114. byte k_ipad[] = new byte[64];
  115. byte k_opad[] = new byte[64];
  116. byte keyb[];
  117. byte value[];
  118. try {
  119. keyb = aKey.getBytes(encodingCharset);
  120. value = aValue.getBytes(encodingCharset);
  121. } catch (UnsupportedEncodingException e) {
  122. keyb = aKey.getBytes();
  123. value = aValue.getBytes();
  124. }
  125. Arrays.fill(k_ipad, keyb.length, 64, (byte) 54);
  126. Arrays.fill(k_opad, keyb.length, 64, (byte) 92);
  127. for (int i = 0; i < keyb.length; i++) {
  128. k_ipad[i] = (byte) (keyb[i] ^ 0x36);
  129. k_opad[i] = (byte) (keyb[i] ^ 0x5c);
  130. }
  131. MessageDigest md = null;
  132. try {
  133. md = MessageDigest.getInstance("MD5");
  134. } catch (NoSuchAlgorithmException e) {
  135. return null;
  136. }
  137. md.update(k_ipad);
  138. md.update(value);
  139. byte dg[] = md.digest();
  140. md.reset();
  141. md.update(k_opad);
  142. md.update(dg, 0, 16);
  143. dg = md.digest();
  144. return toHex(dg);
  145. }
  146. public static String toHex(byte input[]) {
  147. if (input == null)
  148. return null;
  149. StringBuffer output = new StringBuffer(input.length * 2);
  150. for (int i = 0; i < input.length; i++) {
  151. int current = input[i] & 0xff;
  152. if (current < 16)
  153. output.append("0");
  154. output.append(Integer.toString(current, 16));
  155. }
  156. return output.toString();
  157. }
  158. /**
  159. *
  160. * @param args
  161. * @param key
  162. * @return
  163. */
  164. public static String getHmac(String[] args, String key) {
  165. if (args == null || args.length == 0) {
  166. return (null);
  167. }
  168. StringBuffer str = new StringBuffer();
  169. for (int i = 0; i < args.length; i++) {
  170. str.append(args[i]);
  171. }
  172. return (hmacSign(str.toString(), key));
  173. }
  174. /**
  175. * @param aValue
  176. * @return
  177. */
  178. public static String digest(String aValue) {
  179. aValue = aValue.trim();
  180. byte value[];
  181. try {
  182. value = aValue.getBytes(encodingCharset);
  183. } catch (UnsupportedEncodingException e) {
  184. value = aValue.getBytes();
  185. }
  186. MessageDigest md = null;
  187. try {
  188. md = MessageDigest.getInstance("SHA");
  189. } catch (NoSuchAlgorithmException e) {
  190. e.printStackTrace();
  191. return null;
  192. }
  193. return toHex(md.digest(value));
  194. }
  195. // public static void main(String[] args) {
  196. // System.out.println(hmacSign("AnnulCard1000043252120080620160450.0http://localhost/SZXpro/callback.asp杩?4564868265473632445648682654736324511","8UPp0KE8sq73zVP370vko7C39403rtK1YwX40Td6irH216036H27Eb12792t"));
  197. // }
  198. }
6.5.8 StrUtils
  1. package com.dz.utils;
  2. //字符串工具类 判定字符串是否为空
  3. public class StrUtils {
  4. public static boolean empty(String msg){
  5. return msg!=null && msg.length()>0;
  6. }
  7. public static boolean empty(String...msg){
  8. boolean res = true;
  9. for(String s:msg){
  10. res = (s!=null && s.length()>0);
  11. if(!res){
  12. break;
  13. }
  14. }
  15. return res;
  16. }
  17. }

5_项目实战MyShop的更多相关文章

  1. Asp.Net Core 项目实战之权限管理系统(4) 依赖注入、仓储、服务的多项目分层实现

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

  2. 给缺少Python项目实战经验的人

    我们在学习过程中最容易犯的一个错误就是:看的多动手的少,特别是对于一些项目的开发学习就更少了! 没有一个完整的项目开发过程,是不会对整个开发流程以及理论知识有牢固的认知的,对于怎样将所学的理论知识应用 ...

  3. 【腾讯Bugly干货分享】React Native项目实战总结

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/577e16a7640ad7b4682c64a7 “8小时内拼工作,8小时外拼成长 ...

  4. Asp.Net Core 项目实战之权限管理系统(0) 无中生有

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

  5. Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

  6. Asp.Net Core 项目实战之权限管理系统(2) 功能及实体设计

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

  7. Asp.Net Core 项目实战之权限管理系统(3) 通过EntityFramework Core使用PostgreSQL

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

  8. Asp.Net Core 项目实战之权限管理系统(5) 用户登录

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

  9. Asp.Net Core 项目实战之权限管理系统(6) 功能管理

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

随机推荐

  1. ajax01_基础知识

    ajax请求 ajax概述 组成:asynchronous + javascript + xml 特点: 可发送异步请求(这也是与传统请求方式的区别) 用javascript语言编写代码 前后端数据交 ...

  2. 部署CDH集群环境准备

    一.系统centOS7以上,至少三台主机 添加ip 主机名映射关系:(每台主机都要做) vim /etc/hosts 127.0.0.1 localhost localhost.localdomain ...

  3. python 日志类

    简介 在所有项目中必不可少的一定是日志记录系统,python为我们提供了一个比较方便的日志模块logging,通常,我们都会基于此模块编写一个日志记录类,方便将项目中的日志记录到文件中. loggin ...

  4. 1000-ms-HashMap 线程安全安全问题

    问题: HashMap是否是线程安全 详解 http://www.importnew.com/21396.html 有源码分析 和代码性能比较 CHM性能最好 HashMap不是线程安全的:Hasht ...

  5. nodejs学习总结01

    主流渲染引擎介绍1.渲染引擎又叫 排版引擎 或 浏览器内核 .(双内核:执行html和css的)2,主流的渲染引擎有**Chrome浏览器**:Blink引壁(WebKit的一个分支)**Safari ...

  6. SpringBoot定时任务 - 什么是ElasticJob?如何集成ElasticJob实现分布式任务调度?

    前文展示quartz实现基于数据库的分布式任务管理和job生命周期的控制,那在分布式场景下如何解决弹性调度.资源管控.以及作业治理等呢?针对这些功能前当当团队开发了ElasticJob,2020 年 ...

  7. 我与Apache DolphinScheduler社区的故事

    我与DolphinScheduler社区的故事 Apache DolphinScheduler 是一个开源的分布式去中心化.易扩展的可视化DAG大数据调度系统. 于2017年在易观数科立项,2019年 ...

  8. idea中设置注释颜色

    1. 打开idea,右上角File-->Settings 2.找到Language Defaults-->Comments里面Block comment和Line comment 3.Bl ...

  9. C# using()的本质

    " 程序世界没有秘密,所有答案都在源码里 " 01.点明观点 C#中,非托管资源使用之后必须释放,而using()是使用非托管资源的最佳方式,可以确保资源在代码块结束之后被正确释放 ...

  10. 02_Linux基础-文件系统层次结构-提示符-进程-命令格式-隐藏文件-通配符-绝对相对路径-{1..100}-ls-mkdir-其他基础命令

    02_Linux基础-ls-mkdir-cd-pwd-man-useradd-su-rm-tree-tab-passwd-w-ssh-touch-date-stat-cp-mv-du-文件系统层次结构 ...