ssm-购物车实现1

实现页面跳转
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
    //跳转购物车
    @RequestMapping("/show")
    public String show(){
        return "cart";
    }
}

创建项目



#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;
        }

    }

    #实现图片回显 配置图片服务器 image.jt.com
    server{
        listen 80;
        server_name image.jt.com;
        #个别电脑需要区分斜杠
        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;
    }
    #后台系统
    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; 
        }
    }
    #前台系统
    server{
        listen 80;
        server_name www.jt.com;
        location / {
            proxy_pass http://localhost:8092;
        }
    }
    #单点登录系统
    server{
        listen 80;
        server_name sso.jt.com;
        location / {
            proxy_pass http://localhost:8093;
        }
    }
    #购物车系统
    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结尾
        全路径:/service/*.do 以service开头以.do结尾请求
        /* 不管请求的是什么路径,都拦截(包括正规请求,静态页面.html,动态页面.jsp,css,js)
        所以前端控制器禁止写/*
        / 规定:拦截正规请求和静态资源 放行jsp等动态资源
      -->
    <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.配置数据源,它将被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配置数据源 -->
    <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" 执行该操作必须添加事务
    propagation="SUPPORTS" 事务支持的,原来的操作有事务,则添加事务
    原有的操作没事务,不添加事务
    propagation="NEVER"从不添加事务
    propagation="REQUIRES_NEW" 不管之前有没有事务,都会创建一个新的事务
    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(返回值类型 包名.类名.方法名(参数列表))
        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.配置数据源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接口创建代理对象  -->
    <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接口调用方式,表明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;//商品的数量
    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.写好层级代码


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。