ssm整合步骤

1.配置applicationContext

用spring来管理mybaits的工厂类

    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springmvc" />
        <property name="user" value="root" />
        <property name="password" value="123456" />
    bean>
    
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:SqlMapConfig.xml">property>
        <property name="dataSource" ref="dataSource">property>
    bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.wind.mapper">property>
    bean>

然后把相应的mapper下的东西可以用mybatis逆向工程来生成

SqlMapConfig.xml中设置个别名 也就是在mapper中不需要写全名了,直接写类名

<configuration>
    
    <typeAliases>
        <package name="com.wind.bean" />
    typeAliases>
configuration>

mybatis的配置差不多完成了
接下来就是springmvc的配置了
首先要写一个springmvc.xml

    
    <context:component-scan base-package="com.wind">context:component-scan>
      
    <mvc:annotation-driven conversion-service="myconversion" />
    <bean id="myconversion"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="com.wind.conversion.MyDateFormat">bean>
            list>
        property>
    bean>
    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="prefix" value="/WEB-INF/jsp/">property>
        <property name="suffix" value=".jsp">property>
    bean>

然后在web.xml中配置spring 与SpringMVC的前端控制器 还有POST提交解决乱码的filter(过滤器)

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>*.actionurl-pattern>
    servlet-mapping>


    
    <filter>
        <filter-name>encodingfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingfilter-name>
        <url-pattern>*.actionurl-pattern>
    filter-mapping>

接下来就是写controller层了
然后在controller的类中加入service的接口
在service层中加入Mapper的接口

你可能感兴趣的:(ssm整合步骤)