• ssm初始化环境搭建


    一、创建一个工程

    选择Maven,勾选create from archetype,选中maven-archetype-webapp

    二、编辑Maven

    1、Spring框架必须

    2、Spring整合ORM框架(如调用事务管理器)

    3、Springweb的支持(不管Struts2还是SpringMVC都需要,它不是SpringMVC

    4、SpringMVC

    5、Spring依赖的jacksonSpringMVC使用@ResponseBody时输出JSON要用到)

    6、Mybatis

    7、Mybatis-spring 1SqlSessionFactory 2、完成Dao的实现类)

    8、Mybatis需要依赖的数据库的驱动

    9、Mybatis需要依赖的C3P0

    10、JSTL

    POM.XML参考:

    <properties>
        <spring-version>5.0.5.RELEASE</spring-version>
        <jackson-version>2.9.5</jackson-version>
        <mybatis-version>3.4.6</mybatis-version>
        <mybatis-spring-version>1.3.2</mybatis-spring-version>
        <c3p0-version>0.9.5.2</c3p0-version>
        <!--oracle的使用私有库(Maven本身不提供Oracle的驱动)-->
        <mysql-version>8.0.11</mysql-version>
        <jstl-version>1.2</jstl-version>
    </properties>

    <!--1、Spring框架必须-->
    <!--Spring框架必须:Spring Core、Spring Context、Spring Beans、Spring AOP-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <!--2、Spring整合ORM框架(如调用事务管理器)-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <!--3、Spring对web的支持(不管Struts2还是SpringMVC都需要,它不是SpringMVC)-->
    <!--spring对web的支持 1.新建一个applicationContext.xml 2.把applicationContext.xml写到web.xml(整个web项目的启动文件)-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <!--4.SpringMVC-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <!--5.Spring依赖的jackson-->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson-version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson-version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson-version}</version>
    </dependency>
    <!--6、Mybatis-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis-version}</version>
    </dependency>
    <!--7、Mybatis 和Spring的整合-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis-spring-version}</version>
    </dependency>
    <!--8、Mybatis需要依赖的数据库的驱动,Oracle,MySQL-->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc8</artifactId>
        <version>12.2.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql-version}</version>
    </dependency>
    <!--9.c3p0是一个非常稳定的数据库连接池-->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>${c3p0-version}</version>
    </dependency>
    <!--10.jsp使用JSTL-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>${jstl-version}</version>
    </dependency>

    三、5份配置文件

    1. web.xml
    2. 先保证web.xml3.0以上的版本

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             id="WebApp_ID" version="3.1">
    </web-app>

       加入Spring的支持

    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

      加入SpringMVC(分别是中央控制器和编码过滤器)

    <!--SpringMVC的中央控制器-->
    <servlet>
        <servlet-name>springmvc-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc-servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

     

    <!--字符集编码过滤器,设置为UTF8-->
    <!--相当于request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");-->
    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    2. db.properties

    jdbc_driver=oracle.jdbc.driver.OracleDriver
    jdbc_url=jdbc:oracle:thin:@192.168.88.88:1521:orcl
    jdbc_username=oa
    jdbc_password=123456

    #oracle参考字符串
    #oracle.jdbc.driver.OracleDriver
    #jdbc:oracle:thin:@192.168.88.88:1521:orcl
    #mysql参考字符串(注意6.0以上的jar包发生了变化)
    #jdbc:mysql://localhost:3306/pyq?serverTimezone=UTC
    #com.mysql.cj.jdbc.Driver

    3.applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           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.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
    ">
        <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
        <context:component-scan base-package="com.nf"></context:component-scan>
        <!--以下配置数据库相关的信息-->
        <!-- 使用c3p0连接池注册数据源 -->
        <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!-- 基础配置 -->
            <property name="jdbcUrl" value="${jdbc_url}"></property>
            <property name="driverClass" value="${jdbc_driver}"></property>
            <property name="user" value="${jdbc_username}"></property>
            <property name="password" value="${jdbc_password}"></property>
            <!-- 关键配置 -->
            <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
            <property name="initialPoolSize" value="3"></property>
            <!--连接池中保留的最小连接数。Default: 2 -->
            <property name="minPoolSize" value="2"></property>
            <!--连接池中保留的最大连接数。Default: 15 -->
            <property name="maxPoolSize" value="15"></property>
            <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
            <property name="acquireIncrement" value="1"></property>
            <!-- 性能配置 -->
            <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
            <property name="maxStatements" value="8"></property>
            <!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
            <property name="maxStatementsPerConnection" value="5"></property>
            <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
            <property name="maxIdleTime" value="1800"></property>
            <property name="testConnectionOnCheckout" value="false"></property>
        </bean>
        <!--配置Mybatis SqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!--就这一行,我们取代了手动写Builder,sqlMapConfig.xml-->
            <property name="dataSource" ref="myDataSource"></property>
            <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
        </bean>
        <!--由于不用写Dao的实现类,这个交给Mybatis完成,但还是需要写接口-->
        <!---DAO接口所在的包名,spring会自动查找-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.nf.dao"></property>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        </bean>

        <!--事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="myDataSource"></property>
        </bean>

        <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

    </beans>

     

    4. spring-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           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.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
    ">
        <!--扫描包-->
        <context:component-scan base-package="com.nf"></context:component-scan>
        <!--启动注解驱动-->
        <mvc:annotation-driven></mvc:annotation-driven>
        <!--启动视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        <!--定义静态资源(css,js),统统不用SpringMVC进行解析-->
        <mvc:resources mapping="/static/**" location="/static/"></mvc:resources>
    </beans>

    5.1. sqlMapConfig.xml(其实这个文件也可以跟spring融合)

    <?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>
       <typeAliases>
          <package name="nf.entity"/>
       </typeAliases>
       <mappers>
          <mapper resource="mapper/XxxDao.xml"/>
       </mappers>
    </configuration>

    四 建立Xxx模版

    14个包

    com.nf.action

    com.nf.service

    com.nf.dao

    com.nf.entity

     

    2、新建一个实体类

    取名Xxx (可以跟表名一致)

     

    3、新建XxxDao (根据需求,例如查询所有,查询一个,添加、修改、删除等)

     

    4、对应Dao层接口,新建Mapper

    建立一个/mapper/XxxMapper.xml

     

    5、新建XxxServiceInterface

    6、新建XxxServiceImpl的实现类

     

    7、新建XxxAction

     

    8、新建showxxx.jsp

     

     

    1. db.properties

  • 相关阅读:
    套接口编程
    传输层
    1 简介
    共享池2
    jquery.dataTables列中内容居中问题?求解?
    bootstrap-multiselect.js多选下拉框初始化时默认选中初始值
    datatable 动态显示/隐藏列
    bootstrap table 主子表 局部数据刷新(刷新子表)
    bootstrap table 怎么实现前两列固定冻结?
    bootstrapTable表格表头换行
  • 原文地址:https://www.cnblogs.com/sheying/p/8909064.html
Copyright © 2020-2023  润新知