SSM整合小Demo

SSM整合

数据库部分(Oracle)

  1. 创建表

    CREATE TABLE product(
    id varchar2(32) default SYS_GUID() PRIMARY KEY,
    productNum VARCHAR2(50) NOT NULL,
    productName VARCHAR2(50),
    cityName VARCHAR2(50),
    DepartureTime timestamp,
    productPrice Number,
    productDesc VARCHAR2(500),
    productStatus INT,
    CONSTRAINT product UNIQUE (id, productNum)
    )
    
  2. 添加数据

    insert into PRODUCT (id, productnum, productname, cityname, departuretime, productprice, productdesc, productstatus)
    values ('D71D9D079C224C749F8FC526DA39E7B7', 'junyang-002', '北京三日游', '北京', to_timestamp('10-10-2020 10:10:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 1299, '不到北京非好汉', 1);
    insert into PRODUCT (id, productnum, productname, cityname, departuretime, productprice, productdesc, productstatus)
    values ('F4971A072D4C482196A39E219BCBF932', 'junyang-003', '上海七日游', '上海', to_timestamp('10-10-2020 11:10:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 1599, '魔都,你值得来', 0);
    insert into PRODUCT (id, productnum, productname, cityname, departuretime, productprice, productdesc, productstatus)
    values ('058BBC3C69C54FADB14CFAD2517ACF48', 'junyang-003', '北京七日游', '北京', to_timestamp('05-01-2020 08:10:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 3000, '好好感受首都的魅力', 0);
    commit;
    

项目部分

  1. 创建maven工程
    1. 父工程 --com.junyang.ssm

      1. pom.xml添加依赖.
      
      
          4.0.0
      
          com.junyang
          ssm
          pom
          1.0-SNAPSHOT
          
              5.0.2.RELEASE
              1.6.6
              1.2.12
              11.2.0.1.0
              3.4.5
              5.0.1.RELEASE
          
      
                  
              
                  org.aspectj
                  aspectjweaver
                  1.6.8
              
              
                  org.springframework
                  spring-aop
                  ${spring.version}
              
              
                  org.springframework
                  spring-context
                  ${spring.version}
              
              
                  org.springframework
                  spring-context-support
                  ${spring.version}
              
              
                  org.springframework
                  spring-web
                  ${spring.version}
              
              
                  org.springframework
                  spring-orm
                  ${spring.version}
              
              
                  org.springframework
                  spring-beans
                  ${spring.version}
              
              
                  org.springframework
                  spring-core
                  ${spring.version}
              
              
                  org.springframework
                  spring-test
                  ${spring.version}
              
              
                  org.springframework
                  spring-webmvc
                  ${spring.version}
      
              
              
                  org.springframework
                  spring-tx
                  ${spring.version}
              
              
                  junit
                  junit
                  4.12
                  test
              
      
              
                  javax.servlet
                  javax.servlet-api
                  3.1.0
                  provided
              
              
                  javax.servlet.jsp
                  jsp-api
                  2.0
                  provided
              
              
                  jstl
                  jstl
                  1.2
                      
              
                  log4j
                  log4j
                  ${log4j.version}
              
              
                  org.slf4j
                  slf4j-api
                  ${slf4j.version}
              
              
                  org.slf4j
                  slf4j-log4j12
                  ${slf4j.version}
                  
      
              
                  org.mybatis
                  mybatis
                  ${mybatis.version}
              
              
                  org.mybatis
                  mybatis-spring
                  1.3.0
              
              
                  c3p0
                  c3p0
                  0.9.1.2
                  jar
                  compile
              
              
                  com.github.pagehelper
                  pagehelper
                  5.1.2
              
              
                  org.springframework.security
                  spring-security-web
                  ${spring.security.version}
              
              
                  org.springframework.security
                  spring-security-config
                  ${spring.security.version}
              
              
                  org.springframework.security
                  spring-security-core
                  ${spring.security.version}
              
              
                  org.springframework.security
                  spring-security-taglibs
                  ${spring.security.version}
              
      
              
                  com.oracle
                  ojdbc6
                  12.1.0.1-atlassian-hosted
              
      
              
                  javax.annotation
                  jsr250-api
                  1.0
              
          
          
              
                  
                      
                          org.apache.maven.plugins
                          maven-compiler-plugin
                          3.2
                          
                              1.8
                              1.8
                              UTF-8
                              true
                          
                      
                  
              
          
      
      
    2. ssm_domain – com.junyang.ssm_domain

      1. 建立实体类

        package com.junyang.ssm.domain;
        
        import java.io.Serializable;
        import java.util.Date;
        
        /**
         * 产品信息模型
         */
        public class Product implements Serializable {
            private String id; // 主键
            private String productNum; // 编号 唯一
            private String productName; // 名称
            private String cityName; // 出发城市
            private Date departureTime; // 出发时间
            private String departureTimeStr;
            private Double productPrice; // 产品价格
            private String productDesc; // 产品描述
            private Integer productStatus; // 状态 0 关闭 1 开启
            private String productStatusStr;
        
            @Override
            public String toString() {
                return "Product{" +
                        "id='" + id + '\'' +
                        ", productNum='" + productNum + '\'' +
                        ", productName='" + productName + '\'' +
                        ", cityName='" + cityName + '\'' +
                        ", departureTime=" + departureTime +
                        ", departureTimeStr='" + departureTimeStr + '\'' +
                        ", productPrice=" + productPrice +
                        ", productDesc='" + productDesc + '\'' +
                        ", productStatus=" + productStatus +
                        ", productStatusStr='" + productStatusStr + '\'' +
                        '}';
            }
        
            public String getId() {
                return id;
            }
        
            public void setId(String id) {
                this.id = id;
            }
        
            public String getProductNum() {
                return productNum;
            }
        
            public void setProductNum(String productNum) {
                this.productNum = productNum;
            }
        
            public String getProductName() {
                return productName;
            }
        
            public void setProductName(String productName) {
                this.productName = productName;
            }
        
            public String getCityName() {
                return cityName;
            }
        
            public void setCityName(String cityName) {
                this.cityName = cityName;
            }
        
            public Date getDepartureTime() {
                return departureTime;
            }
        
            public void setDepartureTime(Date departureTime) {
                this.departureTime = departureTime;
            }
        
            public String getDepartureTimeStr() {
                return departureTimeStr;
            }
        
            public void setDepartureTimeStr(String departureTimeStr) {
                this.departureTimeStr = departureTimeStr;
            }
        
            public double getProductPrice() {
                return productPrice;
            }
        
            public void setProductPrice(double productPrice) {
                this.productPrice = productPrice;
            }
        
            public String getProductDesc() {
                return productDesc;
            }
        
            public void setProductDesc(String productDesc) {
                this.productDesc = productDesc;
            }
        
            public Integer getProductStatus() {
                return productStatus;
            }
        
            public void setProductStatus(Integer productStatus) {
                this.productStatus = productStatus;
            }
        
            public String getProductStatusStr() {
                return productStatusStr;
            }
        
            public void setProductStatusStr(String productStatusStr) {
                this.productStatusStr = productStatusStr;
            }
        }						
        
    3. ssm_dao – com.junyang.ssm_dao

      1. 在pom.xml中添加依赖
      
      
          
              ssm
              com.junyang
              1.0-SNAPSHOT
          
          4.0.0
          
              
                  com.junyang
                  ssm_domain
                  1.0-SNAPSHOT
              
          
          ssm_dao
      
      
      
      
      1. 编写dao代码

        package com.junyang.ssm.dao;
        
        import com.junyang.ssm.domain.Product;
        import org.apache.ibatis.annotations.Select;
        import org.springframework.stereotype.Repository;
        
        import java.util.List;
        
        /**
         * 产品信息Dao
         */
        @Repository
        public interface ProductDao {
            /**
             * 查询所有产品信息
             * @return
             */
            @Select("select * from product")
            List findAll();
        }
        
        
    4. ssm_service – com.junyang.ssm_service

      1. pom.xml中添加依赖

        
        
            
                ssm
                com.junyang
                1.0-SNAPSHOT
            
            4.0.0
        
            ssm_service
            
                
                    com.junyang
                    ssm_dao
                    1.0-SNAPSHOT
                
            
        
        
        
      2. 编写service代码

        package com.junyang.ssm.service;
        
        import com.junyang.ssm.domain.Product;
        import org.apache.ibatis.annotations.Select;
        import org.springframework.stereotype.Service;
        import org.springframework.transaction.annotation.Transactional;
        
        import java.util.List;
        
        public interface ProductService {
            /**
             * 查询所有信息
             * @return
             */
            @Select("select * from product")
            List findAll();
        }
        
      3. 创建impl包并编写service实现类ProductServiceImpl

        package com.junyang.ssm.service.impl;
        
        import com.junyang.ssm.dao.ProductDao;
        import com.junyang.ssm.domain.Product;
        import com.junyang.ssm.service.ProductService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;
        import org.springframework.transaction.annotation.Transactional;
        
        import java.util.List;
        @Transactional
        @Service
        public class ProductServiceImpl implements ProductService {
            @Autowired
            private ProductDao ProductDao;
            @Override
            public List findAll() {
                return ProductDao.findAll();
            }
        }
        
        
    5. ssm_web – com.junyang.ssm_web

      1. pom.xml中添加依赖

        
        
        
            
                ssm
                com.junyang
                1.0-SNAPSHOT
            
            4.0.0
        
            ssm_web
            war
            
                UTF-8
                1.8
                1.8
            
        
            
                
                    com.junyang
                    ssm_service
                    1.0-SNAPSHOT
                
            
            
                
                    
                        org.apache.tomcat.maven
                        tomcat7-maven-plugin
                        
                            8889
                        
        
                        2.2
                    
                
            
        
        
        
      2. 编写controller代码

        package com.junyang.ssm.web;
        
        import com.junyang.ssm.domain.Product;
        import com.junyang.ssm.service.ProductService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.servlet.ModelAndView;
        
        import java.util.List;
        
        @Controller
        @RequestMapping("/product")
        public class ProductController {
            @Autowired
            private ProductService ProductService;
            @RequestMapping("/findAll1")
            public void findAll1(){
                List productList = ProductService.findAll();
                for (Product product : productList) {
                    System.out.println(product);
                }
            }
        }
        
  2. 添加配置文件 – 在ssm_webresources下创建
    1. applicationContext.xml

      
      
          
          
          
          
          
          
          
              
              
              
              
          
          
          
              
          
          
          
              
          
          
          
          
              
          
          
      
      
      
    2. db.properties

      jdbc.driver=oracle.jdbc.driver.OracleDriver
      jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
      jdbc.username=C##ssm
      jdbc.password=root
      
    3. spring-mvc.xml

      
      
          
          
          
          
              
              
              
              
          
          
          
          
          
          
          
          
          
          
      
      
    4. 修改web.xml

      
      
      
        
        
          contextConfigLocation
          classpath*:applicationContext.xml
        
      
        
        
          org.springframework.web.context.ContextLoaderListener
        
        
        
          org.springframework.web.context.request.RequestContextListener
        
      
        
        
          dispatcherServlet
          org.springframework.web.servlet.DispatcherServlet
          
          
            contextConfigLocation
            classpath:spring-mvc.xml
          
          
          1
        
        
          dispatcherServlet
          /
        
      
        
        
          characterEncodingFilter
          org.springframework.web.filter.CharacterEncodingFilter
          
            encoding
            UTF-8
          
        
        
          characterEncodingFilter
          /*
        
      
        
          index.html
          index.htm
          index.jsp
          default.html
          default.htm
          default.jsp
        
      
      
      
    5. 日志记录文件

      # Set root category priority to INFO and its only appender to CONSOLE.
      #log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
      log4j.rootCategory=debug, CONSOLE, LOGFILE
      
      # Set the enterprise logger category to FATAL and its only appender to CONSOLE.
      log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
      
      # CONSOLE is set to be a ConsoleAppender using a PatternLayout.
      log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
      log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
      log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
      
      # LOGFILE is set to be a File appender using a PatternLayout.
      # log4j.appender.LOGFILE=org.apache.log4j.FileAppender
      # log4j.appender.LOGFILE.File=d:\axis.log
      # log4j.appender.LOGFILE.Append=true
      # log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
      # log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
      
      
      
    6. 创建index.jsp

      <%@ page language="java" contentType="text/html; charset=UTF-8"
               pageEncoding="UTF-8"%>
      
      
          点击超链接查询所有商品信息
      
      
      
      
    7. 使用父工程maven项目的install命令.

    8. 配置maven
      SSM整合小Demo_第1张图片

    9. 启动,浏览器输入项目地址,访问到首页.点击index中的连接,观察控制台输出.

      Product{id='D71D9D079C224C749F8FC526DA39E7B7', productNum='junyang-002', productName='北京三日游', cityName='北京', departureTime=Sat Oct 10 10:10:00 CST 2020, departureTimeStr='null', productPrice=1299.0, productDesc='不到北京非好汉', productStatus=1, productStatusStr='null'}
      Product{id='F4971A072D4C482196A39E219BCBF932', productNum='junyang-003', productName='上海七日游', cityName='上海', departureTime=Sat Oct 10 11:10:00 CST 2020, departureTimeStr='null', productPrice=1599.0, productDesc='魔都,你值得来', productStatus=0, productStatusStr='null'}
      Product{id='058BBC3C69C54FADB14CFAD2517ACF48', productNum='junyang-003', productName='北京七日游', cityName='北京', departureTime=Sun Jan 05 08:10:00 CST 2020, departureTimeStr='null', productPrice=3000.0, productDesc='好好感受首都的魅力', productStatus=0, productStatusStr='null'}
      
      

SSM整合成功!!!

你可能感兴趣的:(JavaWeb)