尚硅谷WebService技术讲解项目实战(完整)

1. 配置 web.xml
<!--1、启动 Spring 的容器  -->
    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 指定一处 Spring 配置文件的位置,使得项目一启动就加载该配置文件 -->
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!--2、Spring MVC 的前端控制器,拦截所有请求  -->
    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 
            我们可以在 init-param 属性中指定 Spring MVC 配置文件的位置
            当然我们一般不指定,那么就必须在与 web.xml 同级目录下创建 dispatcherServlet-servlet.xml Spring MVC 配置文件
        -->
        <!--
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>location</param-value>
        </init-param>
        -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!-- 3、字符编码过滤器,一定要放在所有过滤器之前 -->
    <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>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
     
    <!-- 4、使用 Rest 风格的 URI,将页面普通的 post 请求转为指定的 delete 或者 put 请求 -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
2. 配置 SpringMVC
1. 配置前的准备:创建如下几个包


2. 在与 web.xml 同级目录下创建 dispatcherServlet-servlet.xml Spring MVC 配置文件以及 views 文件夹
    <!--SpringMVC 的配置文件,包含网站跳转逻辑的控制,配置  -->
    <context:component-scan base-package="www.xq" use-default-filters="false">
        <!-- 只扫描控制器。-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- 配置视图解析器,方便页面返回  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- 两个标准配置  -->
    <!-- 将 springmvc 不能处理的请求交给 tomcat -->
    <mvc:default-servlet-handler/>
    <!-- 能支持 springmvc 更高级的一些功能,JSR303 校验,快捷的 ajax...映射动态请求 -->
    <mvc:annotation-driven/>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
3. 配置 Spring
1. 配置前的准备
1. 创建 ssm_crud 数据库
2. 在 resources 目录下 mybatis 全局配置文件 mybatis-config.xml
3. 在 resources 目录下创建 mapper 文件夹用来存放 mybatis 的映射文件
4. 在 resources 目录下创建 dbconfig.properties 用于存放连接数据库的数据源的配置
1
2
3
4
dbconfig.properties
    jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud
    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.user=root
    jdbc.password=123456
1
2
3
4
2. 在 resources 目录下创建 Spring 配置文件 applicationContext.xml
    <context:component-scan base-package="www.xq">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!-- Spring 的配置文件,这里主要配置和业务逻辑有关的 -->
    <!--=================== 数据源,事务控制,xxx ================-->
    <context:property-placeholder location="classpath:dbconfig.properties" />
    <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--================== 配置和 MyBatis 的整合=============== -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定 mybatis 全局配置文件的位置 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="dataSource" ref="pooledDataSource"></property>
        <!-- 指定 mybatis,mapper 文件的位置 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>

    <!-- 配置扫描器,将 mybatis 接口的实现加入到 ioc 容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--扫描所有 dao 接口的实现,加入到 ioc 容器中 -->
        <property name="basePackage" value="www.xq.crud.dao"></property>
    </bean>
    
    <!-- 配置一个可以执行批量的 sqlSession -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
        <constructor-arg name="executorType" value="BATCH"></constructor-arg>
    </bean>
    
    <!--=============================================  -->

    <!-- ===============事务控制的配置 ================-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--控制住数据源  -->
        <property name="dataSource" ref="pooledDataSource"></property>
    </bean>
    <!--开启基于注解的事务,使用 xml 配置形式的事务(必要主要的都是使用配置式)  -->
    <aop:config>
        <!-- 切入点表达式 -->
        <aop:pointcut expression="execution(* www.xq.crud.service..*(..))" id="txPoint"/>
        <!-- 配置事务增强 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>
    
    <!--配置事务增强,事务如何切入  -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 所有方法都是事务方法 -->
            <tx:method name="*"/>
            <!--以get开始的所有方法  -->
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- Spring 配置文件的核心点(数据源、与 mybatis 的整合,事务控制) -->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
4. 配置 mybatis
1. mybatis-config.xml 的编写
<configuration>
    <!-- 开启驼峰命名规则 -->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <!-- 类型别名的配置 -->
    <typeAliases>
        <package name="ww.xq.crud.bean"/>
    </typeAliases>
</configuration>
1
2
3
4
5
6
7
8
9
10
2. 配置文件的更多信息请参照:http://www.mybatis.org/mybatis-3/getting-started.html
注意:mybatis 的全局配置文件可以不要,可以直接配置在 Spring 配置文件 sqlSessionFactory 中,但是我们一般还是保留

5. 最后的工程目录

--------------------- 
作者:0start1的博客 
来源:CSDN 
原文:https://blog.csdn.net/qq_42130468/article/details/88560254 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/qq_44120897/article/details/88641453