ssm-購物車實現(xiàn)1

實現(xiàn)頁面跳轉(zhuǎn)
http://www.jt.com/cart/show.html

package com.jt.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/cart")
public class CartController {
    //http://www.jt.com/cart/show.html
    //跳轉(zhuǎn)購物車
    @RequestMapping("/show")
    public String show(){
        return "cart";
    }
}

創(chuàng)建項目



#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

   

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    #實現(xiàn)圖片回顯 配置圖片服務器 image.jt.com
    server{
        listen 80;
        server_name image.jt.com;
        #個別電腦需要區(qū)分斜杠
        location / {
            root D:/jtupload; 
        }
    }
    #配置tomcat負載均衡 1.輪詢 2.權重 3.ip_hash方式
    upstream jt{
        #ip_hash;
        server localhost:8091 max_fails=1 fail_timeout=60s;
        server localhost:8092 max_fails=1 fail_timeout=60s;
        server localhost:8093 max_fails=1 fail_timeout=60s;
    }
    #Linux集群部署
    upstream jtLinux{
        server 192.168.161.130:8091;
        server 192.168.161.130:8092;
        server 192.168.161.130:8093;
    }
    #后臺系統(tǒng)
    server{
        listen 80;
        server_name manage.jt.com;
        location / {
            #proxy_pass http://jtLinux;
            proxy_pass http://localhost:8091;
            #proxy_connect_timeout       1;  
            #proxy_read_timeout      1;  
            #proxy_send_timeout      1; 
        }
    }
    #前臺系統(tǒng)
    server{
        listen 80;
        server_name www.jt.com;
        location / {
            proxy_pass http://localhost:8092;
        }
    }
    #單點登錄系統(tǒng)
    server{
        listen 80;
        server_name sso.jt.com;
        location / {
            proxy_pass http://localhost:8093;
        }
    }
    #購物車系統(tǒng)
    server{
        listen 80;
        server_name cart.jt.com;
        location / {
            proxy_pass http://localhost:8094;
        }
    }
}

導入配置文件
1.修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="jt-manage" version="2.5">
    <display-name>jt-cart</display-name>

    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置springmvc配置文件 -->
        <init-param>
        <!--具體加載配置文件的路徑-->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/spring/applicationContext*.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!--.do .action 
        路徑:
        前綴型:/service/* 只要請求以service開頭就被前端控制器攔截
        后綴型:.do 請求攔截以.do結(jié)尾
        全路徑:/service/*.do 以service開頭以.do結(jié)尾請求
        /* 不管請求的是什么路徑,都攔截(包括正規(guī)請求,靜態(tài)頁面.html,動態(tài)頁面.jsp,css,js)
        所以前端控制器禁止寫/*
        / 規(guī)定:攔截正規(guī)請求和靜態(tài)資源 放行jsp等動態(tài)資源
      -->
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- 為了解決中文亂碼問題 配置過濾器  POST亂碼-->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

2.修改applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!--1.配置包掃描 -->
    <context:component-scan base-package="com.jt"></context:component-scan>
    <!--2.配置數(shù)據(jù)源,它將被mybatis引用 -->
    <!--2.1導入pro配置文件 -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <!-- private Resource[] locations; -->
            <list>
                <value>classpath:/property/jdbc.properties</value>
                <value>classpath:/property/redis.properties</value>
            </list>
        </property>
    </bean>
    <!--2.2配置數(shù)據(jù)源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!--3.配置事務控制 -->
    <tx:annotation-driven />
    <!--3.1定義事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--3.2定義事務策略
    propagation="REQUIRED" 執(zhí)行該操作必須添加事務
    propagation="SUPPORTS" 事務支持的,原來的操作有事務,則添加事務
    原有的操作沒事務,不添加事務
    propagation="NEVER"從不添加事務
    propagation="REQUIRES_NEW" 不管之前有沒有事務,都會創(chuàng)建一個新的事務
    read-only="true" 該操作只讀
    *除此之外的方法只讀
    -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!--3.3定義事務切面  
    Content Model : (pointcut*, advisor*, aspect*)
                        連接點,          通知,        自定義切面
        expression 切入點表達式
        within(包名.類名) 按類匹配-控制粒度-粗粒度
        execution(返回值類型 包名.類名.方法名(參數(shù)列表))
        execution(返回值類型 包名.類名.方法名(int))
        execution(返回值類型 包名.類名.方法名(String))
        execution(返回值類型 包名.類名.方法名(int,String))
    -->
                        
    <aop:config>
        <aop:pointcut expression="execution(* com.jt.cart.service..*.*(..))" id="pc"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
    </aop:config>
</beans>

3.修改mybatis

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!--spring整合mybatis 
        1.配置數(shù)據(jù)源ref
        2.導入mybatis自身配置文件
        3.導入映射文件
        4.配置別名包
     -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:/mybatis/mappers/*.xml"></property>
        <property name="typeAliasesPackage" value="com.jt.common.po"></property>
    </bean>
    <!--spring為Mapper接口創(chuàng)建代理對象  -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.jt.cart.mapper"></property>
    </bean>
</beans>

4.修改映射文件的路徑


<?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">
  <!--namespace="它是映射文件的唯一標識"-相當于配置文件的主鍵
  mapper接口調(diào)用方式,表明mapper接口/映射文件/表映射關系,用接口的全路徑名稱  -->
<mapper namespace="com.jt.cart.mapper.CartMapper">

</mapper>

5.編輯pojo


package com.jt.common.po;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name="tb_cart")
public class Cart extends BasePojo{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;//主鍵
    private Long userId;//用戶id
    private Long itemId;//商品id
    private String itemTitle;//商品標題
    private String itemImage;//商品的第一張圖片
    private Long itemPrice;//商品的價格
    private Integer num;//商品的數(shù)量
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Long getUserId() {
        return userId;
    }
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    public Long getItemId() {
        return itemId;
    }
    public void setItemId(Long itemId) {
        this.itemId = itemId;
    }
    public String getItemTitle() {
        return itemTitle;
    }
    public void setItemTitle(String itemTitle) {
        this.itemTitle = itemTitle;
    }
    public String getItemImage() {
        return itemImage;
    }
    public void setItemImage(String itemImage) {
        this.itemImage = itemImage;
    }
    public Long getItemPrice() {
        return itemPrice;
    }
    public void setItemPrice(Long itemPrice) {
        this.itemPrice = itemPrice;
    }
    public Integer getNum() {
        return num;
    }
    public void setNum(Integer num) {
        this.num = num;
    }
    
}

6.打包工具類


7.寫好層級代碼


最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容