美文网首页
SSM配置文件

SSM配置文件

作者: 噜啦噜啦嘞_d31b | 来源:发表于2020-12-03 10:08 被阅读0次
    SSM.png
    • pom.xml
    <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <!--spring核心 ioc-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.2.5.RELEASE</version>
        </dependency>
    
        <!--做事务处理-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>5.2.5.RELEASE</version>
        </dependency>
    
        <!--mybatis依赖-->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.5.1</version>
        </dependency>
    
        <!--mybatis和spring集成的依赖-->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.3.1</version>
        </dependency>
        <!--mysql驱动-->
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.9</version>
        </dependency>
    
        <!--阿里数据库连接池-->
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.1.12</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
          <version>2.9.3</version>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.9.3</version>
        </dependency>
      </dependencies>
    
      <build>
        <!--目的是把src/main/java中的properties和xml文件都扫描输出到target的classes中-->
        <resources>
          <resource>
            <directory>src/main/java</directory>
            <includes>
              <include>**/*.properties</include>
              <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
          </resource>
        </resources>
        <!--指定JDK版本-->
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    
    • web.xml
        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:conf/springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:conf/spring.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    
    
    
    • springmvc.xml
         <!--配置视图解析器-->
    
        <!-- 声明SpringMVC中的视图解析器,帮助开发人员设置视图文件的路径-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    
            <!--前缀:视图文件的路径-->
            <property name="prefix" value="/WEB-INF/view/"/>
    
            <!--后缀:视图文件 的扩展名-->
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <!--注解驱动-->
        <mvc:annotation-driven/>
    
        <!--第一种处理静态资源的方式 ,原理是:加入这个标签框架会自动创建DefaultServletHttpRequestHandler(类似创建MyController)
        这个对象可以把接收的请求转发给DefaultServlet-->
        <mvc:default-servlet-handler/>
    
    • spring.xml
    <!--
            把数据库的配置信息写在一个独立的文件中,spring读取propertise文件的内容
        -->
        <context:property-placeholder location="classpath:jdbc.properties"/>
        <!--创建数据源-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    
        <!--使用set注入-->
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <!--最大连接之是20-->
            <property name="maxActive" value="${jdbc.passMax}"/>
        </bean>
    
        <!--创建sqlSessionFactory对象-->
        <bean id = "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!--
                Mybatis的主配置文件的位置
                configLocation的类型是Resource ,指定文件路径使用classpath表示文件路径
            -->
            <property name="configLocation" value="classpath:Mybatis01.xml"/>
    
        </bean>
    
        <!--
            创建dao对象,使用SqlSession的getMapper(StudentDao.class)
            MapperScannerConfigurer:在内部调用getMapper()为包下面的每个dao接口创建代理对象
        -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!--指定对象的id-->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    
            <!--指定包名,为包名下面的每个dao接口创建对象,对象名默认是类名首字母小写,放入到spring容器,多个包用,分隔-->
            <property name="basePackage" value="My.Li.Dao"/>
        </bean>
    
        <!--声明业务处理对象-->
        <bean id="service" class="My.Li.service.BuyServiceImpl">
            <property name="goodsDao" ref="goo0dsDao"/>
            <property name="saleDao" ref="saleDao"/>
        </bean>
    
        <!--声明事务管理器对象-->
        <bean id="myTransation" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!--连接数据库-->
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
    
        <!--开启事务注解驱动 -->
        <tx:annotation-driven transaction-manager="myTransation"/>
    
    • Mybatis.xml
    
       <mappers>
    
            <!--<mapper resource="my/three/dao/StudentDao.xml"/>-->
    
            <package name="My.Li.dao"/>
        </mappers>
    

    相关文章

      网友评论

          本文标题:SSM配置文件

          本文链接:https://www.haomeiwen.com/subject/htxmwktx.html