企业项目实战 第一天 === 创建Maven项目 SSM整合

一 建立Maven项目

打开Idea软件按步骤创建Maven项目
在这里插入图片描述在这里插入图片描述

二 搭建基本框架

在 src 下的 main 包下新建 Java文件夹 和 resource 文件夹 用来存放逻辑代码和配置文件。新建完对两个文件夹做如下操作:
在这里插入图片描述
在这里插入图片描述
成功如下
在这里插入图片描述

在Java包新建如下
bean中存放逻辑代码
controller是控制层
service是服务层
dao是数据库层
在这里bea插入图片描述
打开resource新建 applicationContext.xml 文件 进行spring和mybatis整合

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!-- spring 和 Mybatis 的整合   -->
    <!--  1. 配置数据库相关参数   -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--  2. 配置数据源-->
    <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="10"/>
        <property name="minPoolSize" value="2"/>
    </bean>
    <!-- 3. 配置sqlSessionFactory对象   -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--   注入数据库连接池     -->
        <property name="dataSource" ref="datasource"/>
        <!--   扫描 bean 层包 使用别名      -->
        <property name="typeAliasesPackage" value="com.zhongruan.bean"/>
        <!--   配合加载映射文件 *mapper.xml     -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <!-- 分页配置 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <prop key="helperDialect">mysql</prop>
                            <prop key="reasonable">true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
    <!--  自动生成dao,mapper  -->
    <!--  4. 配置扫描dao接口包,将dao层注入spring容器中  -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--   配置给出需要扫描的dao层路径      -->
        <property name="basePackage" value="com.zhongruan.dao"/>
        <!--  注入 Spring容器中   -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    <!--  自动扫描全部包 ,注解支持  -->
    <context:component-scan base-package="com.zhongruan"/>
    <!--   6. 配置事务注解  -->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource"/>
    </bean>
    <!--  7. 开启事物  -->
    <tx:annotation-driven/>
</beans>
```java

在resource中先建立一个mapper包用来存放映射文件再添加 数据库链接和日志
在这里插入图片描述
在新建 spring-mvc.xml 进行springmvc整合

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!--   1. 注解扫描位置/注解支持  -->
    <context:component-scan base-package="com.zhongruan.controller"/>

    <!--  2. 配置映射处理器和适配器  -->
    <mvc:annotation-driven/>
<!--    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
<!--    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->

    <!--  3.视图解析器  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

打开 web.xml 加载application.xml 配置前端控制器 拦截器 过滤器 监听器

<?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"
         version="3.1">
  
  <!--  配置加载类路径文件   -->
  <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>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

  <!-- 配置解决中文乱码  -->
  <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>

  <!-- 前端控制器,制定加载spring-mvc.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:spring-mvc.xml</param-value>
    </init-param>

    <!-- 让服务器启动的时候,就进行创建servlet对象-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <!--
      /*, 会把用户端所有的请求都当做requestMappering指定的路径去处理,都会进controller
      /, 页面不会去拦截
      *.do
      -->
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

导入view

在这里插入图片描述

配置服务器

在这里插入图片描述
在这里插入图片描述
SSM框架整合完毕

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值