本系統(tǒng)使用Spring+SpringMVC+MyBatis架構(gòu)梁丘,數(shù)據(jù)庫使用MySQL,開發(fā)完成了從商家發(fā)布商品烦粒,到用戶查看商品并下單購買這樣的一個(gè)閉合的流程祝高。
開發(fā)環(huán)境
- jdk 8
- intellij idea
- tomcat 8.5.40
- mysql 5.7
所用技術(shù)
- Spring+SpringMVC+MyBatis
- layui
- jsp
項(xiàng)目架構(gòu)
項(xiàng)目結(jié)構(gòu).png
項(xiàng)目截圖
-
登錄
登錄.png -
首頁
買家-首頁.png -
商品詳情
商品詳情.png -
購物車
購物車.png -
訂單詳情
訂單詳情.png
框架配置
- applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
<context:annotation-config />
<context:component-scan base-package="com.yzx.service" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/market?characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root123</value>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.yzx.entity" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/yzx/mapper/xml/*.xml"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置參數(shù)恬偷,一行配置一個(gè) -->
<value>
</value>
</property>
</bean>
</array>
</property>
</bean>
<!--增加tkmybatis注解依賴-->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yzx.mapper"/>
<property name="beanName" value="normal"/>
</bean>
</beans>
- springMVC.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:annotation-config/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<context:component-scan base-package="com.yzx.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
- mybatis依賴配置
// 繼承Mapper失乾,MySqlMapper 其他業(yè)務(wù)Mapper繼續(xù)“MyMapper”宪哩,可直接操作增刪改查方法
public interface MyMapper<T> extends Mapper<T>,MySqlMapper<T>{
}
業(yè)務(wù)代碼
1.首頁代碼-controller層
@RequestMapping(value = {"/","index","main",""})
public ModelAndView index(ModelAndView mav,HttpServletRequest request){
Person person=(Person)request.getSession().getAttribute("user");
List<Product> productList= productService.getProductList();
if(null!=person){
Trx trx=new Trx();
if(person.getUserType().equals(0)){ //買家
trx.setPersonId(person.getId());
}
List<Product> productList2=new ArrayList<>();
List<Trx> trxList=trxService.getTrxList(); //根據(jù)當(dāng)前登錄id查詢是否購買
if(null!=trxList&&trxList.size()>0){
for(Product p:productList){
for(Trx t:trxList){
if(t.getProductId().equals(p.getId())){
p.setIsSell(1);
}
}
if(person.getUserType()==1){ //賣家角色 查詢當(dāng)前商品買過多少
Trx t1=new Trx();
t1.setProductId(p.getId());
List<Trx> list=trxService.getTrxByParm(t1);
p.setBuyNum(list.size());
}
productList2.add(p);
}
mav.addObject("productList",productList2); //過濾后的商品列表
}
mav.addObject("productList",productList);
}else{
mav.addObject("productList",productList); //商品列表
}
mav.setViewName("index");
return mav;
}
1.1 首頁代碼-service層
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
ProductMapper productMapper;
//商品列表
public List<Product> getProductList(){
return productMapper.selectAll();
}
}
1.2 首頁代碼-dao層-Mapper
//繼承MyMapper類娩贷,獲取超類的增刪改查方法
public interface ProductMapper extends MyMapper<Product> {
}
1.3 首頁代碼-jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta charset="utf-8"/>
<title>java</title>
<link rel="stylesheet" href="/css/style.css"/>
</head>
<body>
<div class="n-support">請(qǐng)使用Chrome、Safari等webkit內(nèi)核的瀏覽器锁孟!</div>
<div class="n-head">
<div class="g-doc f-cb">
<c:if test="${not empty user}">
<div class="user">
買家你好彬祖,<span class="name">${user.nickName}</span>!<a href="logout">[退出]</a>
</div>
</c:if>
<c:if test="${ empty user}">
請(qǐng)<a href="login">[登錄]</a>
</c:if>
<ul class="nav">
<li><a href="index">首頁</a></li>
<c:if test="${not empty user}">
<c:if test="${user.userType==0}">
<li><a href="account">賬務(wù)</a></li>
<li><a href="shopCart">購物車</a></li>
</c:if>
<c:if test="${user.userType==1}">
<li><a href="publish">發(fā)布</a></li>
</c:if>
</c:if>
</ul>
</div>
</div>
<div class="g-doc">
<div class="m-tab m-tab-fw m-tab-simple f-cb">
<div class="tab">
<ul>
<li class="z-sel"><a href="index">所有內(nèi)容</a></li>
<c:if test="${ user.userType!=1}">
<li><a href="isNoBuy">未購買的內(nèi)容</a></li>
</c:if>
</ul>
</div>
</div>
<div class="n-plist">
<ul class="f-cb" id="plist">
<c:forEach items="${productList}" var="product" varStatus="st">
<li id="p-1">
<a href="details?pid=${product.id}" class="link">
<div class="img"><img src="${product.image}" alt=""></div>
<h3>${product.title}</h3>
<div class="price"><span class="v-unit">¥</span><span class="v-value">${product.price}</span></div>
<c:if test="${ not empty user}">
<c:if test="${ user.userType==0 and product.isSell==1}">
<span class="had"><b>已購買</b></span>
</c:if>
<c:if test="${ user.userType==1 and product.isSell==1}">
<span class="had"><b>已售出${product.buyNum}</b></span>
</c:if>
</c:if>
</a>
<c:if test="${ user.userType==1 and product.isSell==0}">
<span class="u-btn u-btn-normal u-btn-xs del" data-del="5">刪除</span>
</c:if>
</li>
</c:forEach>
</ul>
</div>
</div>
<script type="text/javascript" src="/js/global.js"></script>
<script type="text/javascript" src="/js/pageIndex.js"></script>
</body>
</html>
2 購物車列表-controller
@RequestMapping(value = {"shopCart"})
public ModelAndView shopCart(ModelAndView mav, HttpServletRequest request){
Person person=(Person) request.getSession().getAttribute("user");
ShopCart shopCart3=new ShopCart();
shopCart3.setUid(person.getId()+"");
List<ShopCart> shopCarts=shopCartService.getShopCartList(shopCart3);
mav.addObject("shopCarts",shopCarts);
mav.setViewName("shopCart");
return mav;
}
2.1 購物車列表-jsp頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta charset="utf-8"/>
<title>java</title>
<link rel="stylesheet" href="/css/style.css"/>
</head>
<body>
<div class="n-head">
<div class="g-doc f-cb">
<c:if test="${not empty user}">
<div class="user">
買家你好品抽,<span class="name">${user.nickName}</span>储笑!<a href="logout">[退出]</a>
</div>
</c:if>
<c:if test="${ empty user}">
請(qǐng)<a href="login">[登錄]</a>
</c:if>
<ul class="nav">
<li><a href="index">首頁</a></li>
<c:if test="${not empty user}">
<c:if test="${user.userType==0}">
<li><a href="account">賬務(wù)</a></li>
<li><a href="shopCart">購物車</a></li>
</c:if>
<c:if test="${user.userType==1}">
<li><a href="publish">發(fā)布</a></li>
</c:if>
</c:if>
</ul>
</div>
</div>
<div class="g-doc">
<div class="m-tab m-tab-fw m-tab-simple f-cb" id="settleAccount">
<h2>已添加到購物車的內(nèi)容</h2>
</div>
<table class="m-table m-table-row n-table g-b3">
<colgroup><col class="img"/><col/><col class="time"/><col class="price"/></colgroup>
<thead>
<tr><th>內(nèi)容圖片</th><th>內(nèi)容名稱</th><th>購買時(shí)間</th><th>數(shù)量</th><th>購買價(jià)格</th></tr>
</thead>
<tbody>
<c:forEach items="${shopCarts}" var="cart">
<tr>
<td><a href="details?pid=${cart.pid}"><img src="${cart.image}" alt=""></a></td>
<td><h4><a href="details?pid=${cart.pid}">${cart.title}</a></h4></td>
<td><span class="v-time">${cart.time}</span></td>
<td><span class="v-num">${cart.num}</span></td>
<td><span class="v-unit">¥</span><span class="value">${cart.price}</span></td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<tr>
<td><button class="u-btn u-btn-primary" onclick="goout();">退出</button></td>
<td><button class="u-btn u-btn-primary" onclick="buy();">購買</button></td>
</tr>
</tfoot>
</table>
</div>
<script type="text/javascript" src="/js/global.js"></script>
<script type="text/javascript" src="/js/pageShow.js"></script>
<script type="text/javascript" src="/js/jquery-3.3.1.js" ></script>
<script type="text/javascript" >
function goout() {
window.history.go(-1);
}
function buy(){
if(confirm("確定購買嗎?")){
$.ajax({
type:"post",
url:"saveTrx",
data:{pid:$("#pid").val()},
dataType:"json",//返回的
success:function(data) {
if(data==0) {
window.location.href="account";
} else {
alert('用戶密碼錯(cuò)誤圆恤,請(qǐng)重新登錄');
}
},
error:function(msg) {
alert(msg);
}
});
}
}
</script>
</body>
</html>
項(xiàng)目總結(jié)
項(xiàng)目做得比較簡易突倍,頁面偏向簡潔,不過有一個(gè)完整的商城主流程盆昙,此外還編寫了freemaker版本的購物商城代碼羽历,后續(xù)上傳源碼。