pom.xml导入一下依赖包
<!--springIoc的依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--springMVC的依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<!--spring-jdbc的依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--mybatis和mysql的依赖包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!--mybatis和spring的整合jar包依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<!--与spring-5整合的依赖包 动态网页-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
build标签下导入
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
GoodsInfo.java
public class GoodsInfo {
private Integer goodsId;
private String goodsName;
private Float goodsPrice;
private Integer goodsState;
private Integer goodsNum;//用户选择商品的数量
private StockInfo si;
public GoodsInfo() {
}
public Integer getGoodsId() {
return goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public Float getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(Float goodsPrice) {
this.goodsPrice = goodsPrice;
}
public Integer getGoodsState() {
return goodsState;
}
public void setGoodsState(Integer goodsState) {
this.goodsState = goodsState;
}
public Integer getGoodsNum() {
return goodsNum;
}
public void setGoodsNum(Integer goodsNum) {
this.goodsNum = goodsNum;
}
public StockInfo getSi() {
return si;
}
public void setSi(StockInfo si) {
this.si = si;
}
@Override
public String toString() {
return "GoodsInfo{" +
"goodsId=" + goodsId +
", goodsName='" + goodsName + '\'' +
", goodsPrice=" + goodsPrice +
", goodsState=" + goodsState +
", goodsNum=" + goodsNum +
", si=" + si +
'}';
}
@Override
public boolean equals(Object obj) {
GoodsInfo goodsInfo = (GoodsInfo)obj;
return this.getGoodsId() == goodsInfo.getGoodsId();
}
}
StockInfo.java
public class StockInfo {
private Integer stockId;
private Integer goodsId;
private Integer goodsStockNum;
public StockInfo() {
}
public Integer getStockId() {
return stockId;
}
public void setStockId(Integer stockId) {
this.stockId = stockId;
}
public Integer getGoodsId() {
return goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public Integer getGoodsStockNum() {
return goodsStockNum;
}
public void setGoodsStockNum(Integer goodsStockNum) {
this.goodsStockNum = goodsStockNum;
}
@Override
public String toString() {
return "StockInfo{" +
"stockId=" + stockId +
", goodsId=" + goodsId +
", goodsStockNum=" + goodsStockNum +
'}';
}
}
GoodsMapper.java
public interface GoodsMapper {
List<GoodsInfo> findAllGoods();
void updateStock(GoodsInfo goodsInfo);
void addGwc(GoodsInfo goodsInfo);
GoodsInfo findGoodsById(int goodsId);
void deleteGwc(int goodsId);
}
GoodsMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.GoodsMapper">
<resultMap id="good" type="bean.GoodsInfo">
<id column="goodsid" property="goodsId"></id>
<result column="goodsname" property="goodsName"></result>
<result column="goodsprice" property="goodsPrice"></result>
<result column="goodsstate" property="goodsState"></result>
<association property="si" column="goodsId" javaType="bean.StockInfo">
<id column="stockid" property="stockId"></id>
<result column="goodsid" property="goodsId"></result>
<result column="goodsstocknum" property="goodsStockNum"></result>
</association>
</resultMap>
<select id="findAllGoods" resultMap="good">
select * from goods g,stock s where g.goodsid = s. goodsid
</select>
<update id="updateStock" parameterType="bean.GoodsInfo">
update stock set goodsStockNum=goodsStockNum-#{goodsNum} where goodsId = #{goodsId}
</update>
<insert id="addGwc" parameterType="bean.GoodsInfo">
insert into gwc(userid,goodsid,gwcnumber) values(1,#{goodsId},#{goodsNum})
</insert>
<select id="findGoodsById" resultMap="good">
SELECT * FROM goods g,stock s WHERE g.goodsId=s.goodsId and g.goodsId=#{goodsId}
</select>
<delete id="deleteGwc">
delete from gwc where goodsId=#{goodsId}
</delete>
</mapper>
GoodsBiz.java
public interface GoodsBiz {
List<GoodsInfo> findAllGoods();
void updateStock(GoodsInfo goodsInfo);
void addGwc(GoodsInfo goodsInfo);
GoodsInfo findGoodsById(int goodsId);
void deleteGwc(int goodsId);
}
GoodsBizImpl.java
@Repository
public class GoodsBizImpl implements GoodsBiz {
@Autowired
private GoodsMapper gm;
@Override
public List<GoodsInfo> findAllGoods() {
return gm.findAllGoods();
}
@Override
public void updateStock(GoodsInfo goodsInfo) {
gm.updateStock(goodsInfo);
}
@Override
public void addGwc(GoodsInfo goodsInfo) {
gm.addGwc(goodsInfo);
}
@Override
public GoodsInfo findGoodsById(int goodsId) {
return gm.findGoodsById(goodsId);
}
@Override
public void deleteGwc(int goodsId) {
gm.deleteGwc(goodsId);
}
}
GoodsController.java
@Controller
@RequestMapping("/goods")
public class GoodsController {
@Autowired
private GoodsBiz gb;
@RequestMapping("/findallgoods")
public String findAllGoods(HttpSession session){
session.setAttribute("GOODS_LIST",gb.findAllGoods());
return "index";
}
@RequestMapping("/addgwc")
public String addGwc(GoodsInfo goodsInfo,HttpSession session){
//更新库存
gb.updateStock(goodsInfo);
gb.addGwc(goodsInfo);
List<GoodsInfo> list = (List<GoodsInfo>) session.getAttribute("GOODS_LIST");
int index1 = list.indexOf(goodsInfo);
GoodsInfo gi = list.get(index1);
StockInfo si = gi.getSi();
si.setGoodsStockNum(si.getGoodsStockNum()-goodsInfo.getGoodsNum());//库存量减去传过来选的数量
session.setAttribute("GOODS_LIST",list);
//加入购物车
List<GoodsInfo> gwcList;
Object obj = session.getAttribute("GWC_LIST");
if (obj == null){
gwcList = new ArrayList<>();
}else{
gwcList = (List<GoodsInfo>) obj;
}
int index2 = gwcList.indexOf(goodsInfo);
if (index2 == -1){
GoodsInfo g2 = gb.findGoodsById(goodsInfo.getGoodsId());
g2.setGoodsNum(goodsInfo.getGoodsNum());
gwcList.add(g2);
}else{
GoodsInfo g2 = gwcList.get(index2);
g2.setGoodsNum(g2.getGoodsNum()+goodsInfo.getGoodsNum());
}
session.setAttribute("GWC_LIST",gwcList);
return "index";
}
@RequestMapping("/togwc")
public String toGwc(){
return "gwc";
}
@RequestMapping("/removegwc")
public String removeGwc(GoodsInfo gi,HttpSession session){
List<GoodsInfo> gwcList = (List<GoodsInfo>) session.getAttribute("GWC_LIST");;
gi = gwcList.get(gwcList.indexOf(gi));
// 更新库存
gi.setGoodsNum(0-gi.getGoodsNum());
gb.updateStock(gi);
// 更新购物车
gwcList.remove(gi);
session.setAttribute("GWC_LIST",gwcList);
gb.deleteGwc(gi.getGoodsId());
return "gwc";
}
}
index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script>
function jia(mid) {
var s =parseInt( $("#num"+mid).val(),10);
var stockNum = parseInt($("#stockNum"+mid).html());
if (s<stockNum){
$("#num"+mid).val(s+1);
}
}
function jian(mid) {
var s =parseInt( $("#num"+mid).val(),10);
if (s>0){
$("#num"+mid).val(s-1);
}
}
function add(mid,kuCun) {
kuCun = parseInt($("#num"+kuCun).val());
if (kuCun>0){
location.href = "addgwc.do?goodsId="+mid+"&goodsNum="+kuCun;
}
}
</script>
</head>
<body>
<a href="togwc.do">查看购物车</a>
<table border="1" width="70%">
<tr>
<th>序号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品数量</th>
<th>商品库存</th>
<th>操作</th>
</tr>
<tr th:each="goods,j:${session.GOODS_LIST}">
<td th:text="${j.count}"></td>
<td th:text="${goods.goodsName}"></td>
<td th:text="${goods.goodsPrice}"></td>
<td>
<button th:id="'jian'+${j.count}" th:onclick="'jian('+${j.count}+')'">-</button>
<input type="text" th:id="'num'+${j.count}" value="0"/>
<button th:id="'jia'+${j.count}" th:onclick="'jia('+${j.count}+')'">+</button>
</td>
<td th:id="'stockNum'+${j.count}" th:text="${goods.si.goodsStockNum}"></td>
<td><button th:onclick="'add('+${goods.goodsId}+','+${j.count}+')'">加入购物车</button></td>
</tr>
</table>
</body>
</html>
gwc.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script>
function removeGwc(mid){
location.href = "removegwc.do?goodsId="+mid;
}
</script>
</head>
<body>
<a href="findallgoods.do">返回商品页面</a>
<table border="1" align="center" width="80%">
<tr>
<th>序号</th>
<th>名称</th>
<th>价格</th>
<th>数量</th>
<th>删除</th>
</tr>
<tr th:each="goods,j:${session.GWC_LIST}">
<td th:text="${j.count}"></td>
<td th:text="${goods.goodsName}"></td>
<td th:text="${goods.goodsPrice}"></td>
<td th:text="${goods.goodsNum}"></td>
<td><button th:onclick="'removeGwc('+${goods.goodsId}+')'">删除</button></td>
</tr>
</table>
</body>
</html>
applicationContext.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--IOC注解-->
<context:annotation-config/>
<context:component-scan base-package="biz,controller"/>
<!--MVC注解-->
<mvc:annotation-driven/>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="url" value="jdbc:mysql://localhost:3306/db1?characterEncoding=utf8&useSSL=false&serverTimezone=UTC"/>
<property name="password" value="root"/>
</bean>
<!--创建会话工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:config.xml"/>
</bean>
<!--创建mapper的代理-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="mapper"/>
</bean>
<!--视图解析器-->
<bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/goods/"/>
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
</bean>
<bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>
</beans>
config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings><!--控制台可以输出sql语句-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
</configuration>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>