一、运行环境
mysql5.6以上
tomcat8.0+
jdk1.8
idea、eclipse或者myeclipse
二、功能截图
三、主要代码
package com.wxl.sms.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxl.sms.bean.Group;
import com.wxl.sms.bean.to.Sale;
import com.wxl.sms.service.GroupService;
import com.wxl.sms.service.ProductService;
import com.wxl.sms.service.SaleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* 销售统计Controller
*
*/
@Controller
@RequestMapping("/sale")
public class SaleController {
@Resource
private SaleService saleService;
@Resource
private ProductService productService;
@Resource
private GroupService groupService;
/**
* 重定向后进入sale.jsp页面
*
* @return 重定向到sale.jsp
*/
@RequestMapping("/toSale")
public String toSalePage() {
return "forward:/sale/saleInfo";
}
/**
* 用于显示销售统计页面的信息
*
* @param pn 页码
* @param request 原生API, 用于获取表单输入信息以进行条件查询
* @param model 存储数据域中
* @return sale.jsp
*/
@Deprecated
@RequestMapping("/saleInfo")
public String showSaleInfo(@RequestParam(value = "pn", defaultValue = "1") Integer pn,
HttpServletRequest request,
Model model) {
System.out.println("preGroupId ===> " + request.getParameter("groupIdInForm"));
Integer groupId = null;
String startDate = "";
String endDate = "";
if (request.getParameter("groupIdInForm") != null &&
!request.getParameter("groupIdInForm").equals("0")) {
// 表单输入的商品类别
groupId = Integer.parseInt(request.getParameter("groupIdInForm"));
}
if (request.getParameter("startDate") != null &&
request.getParameter("endDate") != null) {
startDate = request.getParameter("startDate");
endDate = request.getParameter("endDate");
}
// System.out.println("********************************");
// System.out.println("groupId ===> " + groupId);
// System.out.println("startDate ===> " + startDate);
// System.out.println("endDate ===> " + endDate);
// System.out.println("********************************");
List<Sale> sales = saleService.getSaleInfoByCondition(groupId, startDate, endDate);
for (Sale sale : sales) {
sale.setProduct(productService.getProductByProductId(sale.getProductId()));
sale.getProduct().setGroup(groupService.getGroupByGroupId(sale.getProduct().getGroupId()));
}
model.addAttribute("sales", sales);
// 2021年5月5日01:13:06添加, 为了让条件查询更容容易, 这里由填写商品分类, 修改为选择下拉框
List<Group> allGroups = groupService.getAllGroups();
model.addAttribute("allGroups", allGroups);
return "sale/sale";
}
/**
* 用于显示销售统计页面的信息
*
* @param pn 页码
* @param request 原生API, 用于获取表单输入信息以进行条件查询
* @param model 存储数据域中
* @param postGroupId 存储上一次表单提交的商品分类值
* @return sale.jsp
*/
@RequestMapping("/saleInfoPro")
public String showSaleInfoPro(@RequestParam(value = "pn", defaultValue = "1") Integer pn,
HttpServletRequest request,
Model model,
@RequestParam(value = "groupId", defaultValue = "0") Integer postGroupId) {
// System.out.println("preGroupId ===> " + request.getParameter("groupIdInForm"));
Integer groupId = null;
String startDate = "";
String endDate = "";
if (postGroupId == 0) {
if (request.getParameter("groupIdInForm") != null &&
!request.getParameter("groupIdInForm").equals("0")) {
// 表单输入的商品类别
groupId = Integer.parseInt(request.getParameter("groupIdInForm"));
}
} else {
groupId = postGroupId;
}
if (request.getParameter("startDate") != null &&
request.getParameter("endDate") != null) {
startDate = request.getParameter("startDate");
endDate = request.getParameter("endDate");
}
/* PageHelper.startPage(pn, 5);*/
List<Sale> sales = saleService.getSaleInfoByCondition(groupId, startDate, endDate);
for (Sale sale : sales) {
sale.setProduct(productService.getProductByProductId(sale.getProductId()));
sale.getProduct().setGroup(groupService.getGroupByGroupId(sale.getProduct().getGroupId()));
}
/* PageInfo<Sale> pageInfo = new PageInfo<>(sales, 5);*/
model.addAttribute("sales", sales);
// 2021年5月5日01:13:06添加, 为了让条件查询更容容易, 这里由填写商品分类, 修改为选择下拉框
List<Group> allGroups = groupService.getAllGroups();
model.addAttribute("allGroups", allGroups);
// 2021年5月10日14:12:48 Pro版, 解决分页BUG
model.addAttribute("groupId", groupId);
return "sale/sale";
}
}
package com.wxl.sms.dao;
import com.wxl.sms.bean.to.Sale;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 销售表Mapper
*
* @author wxl on 2021/5/2 1:10
*/
public interface SaleMapper {
/**
* 增加一条销售记录(事务操作)
*
* @return 影响的行数
*/
int insertSaleItemFinal(@Param("productId") Integer productId,
@Param("productName") String productName,
@Param("groupId") Integer groupId,
@Param("purchasePrice") float purchasePrice,
@Param("salePrice") float salePrice,
@Param("saleCount") Integer saleCount,
@Param("saleVolume") float saleVolume,
@Param("netProfit") float netProfit);
/**
* 用于根据条件展示销售信息
*
* @param groupId 商品分类信息
* @return TO集合
*/
List<Sale> getSaleInfoByCondition(@Param("groupId") Integer groupId,
@Param("startDate") String startDate,
@Param("endDate") String endDate);
}
package com.wxl.sms.service;
import com.wxl.sms.bean.to.Sale;
import java.util.List;
/**
* @author wxl on 2021/5/2 1:22
*/
public interface SaleService {
/**
* 增加一条销售记录
*
* @param productId 售出商品id
* @param saleCount 售出数量
* @return 影响的行数
*/
int insertSaleItem(Integer productId, Integer saleCount);
/**
* 用于根据条件展示销售信息
* @param startDate 搜索开始时间
* @param endDate 搜索结束时间
* @param groupId 商品分类信息
* @return TO集合
*/
List<Sale> getSaleInfoByCondition(Integer groupId, String startDate, String endDate);
}
package com.wxl.sms.service.impl;
import com.wxl.sms.bean.Product;
import com.wxl.sms.bean.to.Sale;
import com.wxl.sms.dao.ProductMapper;
import com.wxl.sms.dao.SaleMapper;
import com.wxl.sms.service.SaleService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class SaleServiceImpl implements SaleService {
@Resource
private SaleMapper saleMapper;
@Resource
private ProductMapper productMapper;
@Override
public int insertSaleItem(Integer productId, Integer saleCount) {
Product product = productMapper.getProductByProductId(productId);
String productName = product.getProductName();
Integer groupId = product.getGroupId();
float purchasePrice = product.getPurchasePrice();
float salePrice = product.getSalePrice();
// saleVolume - 销售额
float saleVolume = saleCount * purchasePrice;
// netProfit - 净利润
float netProfit = (salePrice - purchasePrice) * saleCount;
return saleMapper.insertSaleItemFinal(productId, productName, groupId, purchasePrice, salePrice,
saleCount, saleVolume, netProfit);
}
@Override
public List<Sale> getSaleInfoByCondition(Integer groupId, String startDate, String endDate) {
return saleMapper.getSaleInfoByCondition(groupId, startDate, endDate);
}
}
/* public List<Sale> getSaleInfoByCondition(Integer groupId, String startDate, String endDate) {
return saleMapper.getSaleInfoByCondition(groupId, startDate, endDate);
}*/
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--Spring配置文件, 这里主要配置和业务逻辑有关的-->
<!--数据源、事务控制、... -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--1.数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.passwd}"/>
</bean>
<!--2.扫描业务逻辑组件-->
<context:component-scan base-package="com.wxl.sms">
<!--除了控制器其它的都要-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--3.配置Spring整合MyBatis-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--MyBatis全局配置文件位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
<!--指定MyBatis的mapper文件位置-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--配置一个可以执行批量的sqlSession-->
<!-- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">-->
<!-- <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>-->
<!-- <constructor-arg name="executorType" value="BATCH"/>-->
<!-- </bean>-->
<!--4.配置扫描器, 将MyBatis接口的实现加入到IOC容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描所有的dao接口的实现加入到IOC容器中-->
<property name="basePackage" value="com.wxl.sms.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
</bean>
<!--5.事务控制-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--控制住数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--6.开启基于注解的事务/XML配置的事务(重要的都是使用配置)-->
<aop:config>
<!--切入点表达式-->
<aop:pointcut id="txPoint" expression="execution(* com.wxl.sms.service..*(..))"/>
<!--配置事务增强-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
<!--配置事务增强(事务如何切入)-->
<tx:advice id="txAdvice">
<tx:attributes>
<!--所有方法都是事务方法-->
<tx:method name="*"/>
<!--以get开始的所有方法(认为是查询, 可以调优)-->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<tx:annotation-driven/>
<!--Spring配置文件的核心
1.数据源
2.整合MyBatis
3.事务控制
-->
<!-- <bean class="org.springframework.web.filter.CharacterEncodingFilter">-->
<!-- </bean>-->
</beans>