ssh框架整合的基本结构

一、springmvc.xml配置

springmvc.xml中主要需要配置的东西如下:

<?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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">




    <!--    让springmvc不处理静态资源-->
    <mvc:default-servlet-handler/>

    <!--支持mvc注解驱动,
    在springmvc中,一般采用@RequestMapping注解来完成映射关系
    想使得@RequestMapping注解生效
    必须向上下文中注册DefaultAnnotationHandlerMapping(处理器映射器)
    和一个AnnotationMethodHandlerMapping(处理器适配器)实例
    这两个实例分别在类级别和方法级别处理
    而Annotion-driven配置会帮助我们自动完成上述两个实例的注入-->
    <!--    <mvc:annotation-driven/>-->

<!--    <mvc:annotation-driven/>-->
    
    <!--  解决json中文乱码-->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 注解请求映射
            默认是ISO-88859-1,避免乱码这里设置为UTF-8 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
            </bean>
            <!-- 启动JSON格式的配置,自动将格式转换成JSON格式,不需要其他类 -->
            <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                        <property name="failOnEmptyBeans" value="false" />
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!--视图解析器:DispatcherServlet给他的ModelAndView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 4.让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 -->
    <context:component-scan base-package="com.wei.controller" />
</beans>

二、springmvc和spring的整合

通过web.xml可以实现这两个框架的整合:
其中web.xml的配置如下:

  1. 定义前端控制器,用于启动spingmvc.xml配置文件
  2. 配置编码方式过滤器,注意一点:要配置在所有过滤器的前面
  3. 配置适配器spring,用于关联spring.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" id="WebApp_ID" version="3.1">
	<display-name>Archetype Created Web Application</display-name>

	<!-- 配置编码方式过滤器,注意一点:要配置在所有过滤器的前面 -->
	<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>

	<!-- 定义前端控制器 -->
	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 指定路径 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:Springmvc-servlet.xml</param-value>
		</init-param>
		<!-- 随spring启动而启动 -->
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>spring-mvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- 配置适配器spring -->
	<listener>
		<description>启动spring容器</description>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<!--CONFIG_LOCATION_PARAM = "contextConfigLocation";-->
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:application-context.xml</param-value>
	</context-param>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

三、mybatis的配置

在mybatis中,我们只需要进行settings配置,其他的交给sping去做

<?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>
    <settings>
        <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
        <setting name="useGeneratedKeys" value="true" />
        <!-- 设置mybatis是否缓存 -->
        <setting name="cacheEnabled" value="true" />
        <!-- 设置响应时间 -->
        <setting name="defaultStatementTimeout" value="3000" />
        <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
        <setting name="mapUnderscoreToCamelCase" value="true" />
        <!-- CGLIB代理 -->
        <setting name="proxyFactory" value="CGLIB" />
        <!-- 是否使用延迟加载 -->
        <setting name="lazyLoadingEnabled" value="true" />
    </settings>
</configuration>

四、spring整合mybatis

在spring.xml中我们需要

  1. 关联数据库配置文件
  2. 配置连接池
  3. 配置事务管理器
  4. 整合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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
  	http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">


<!--关联数据库配置文件-->
    <context:property-placeholder location="classpath:db.properties"/>
<!--    连接池
 dbcp,半自动化,不能自动化连接
c3p0,自动化,自动化加载配置文件,并且自动设置到对象中
druid  -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 配置连接池属性 -->
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30" />
        <property name="minPoolSize" value="10" />

        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false" />

        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000" />

        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2" />
    </bean>



    <!-- 5.配置事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

<!--    aop事务支持-->

                   <!--    mybatis-->
<!--    sqlsessionfactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- 扫描entity包 使用别名 -->
        <property name="typeAliasesPackage" value="com.wei.entity" />
        <!-- 扫描sql配置文件:mapper需要的xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

<!--    配置dao接口扫描-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--        注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!--        要扫描的dao包-->
        <property name="basePackage" value="com.wei.mapper"></property>
    </bean>

    <!-- 6.注解扫描所有的包 -->
    <context:component-scan base-package="com.wei" />

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值