浅谈spring+springMVC+mybatis框架整合(配置文件的使用)

1.文章来由:一直都是在别人搭建好的框架下进行开发,所以对框架的集成,具体文件怎么配置,怎样搭建很疑惑,于某天决定自己来搭建一次。(我搭建的只是个测试环境,细节的配置问题未涉及。看个人项目需求)

2.下看看我的项目结构目录,方便描述
浅谈spring+springMVC+mybatis框架整合(配置文件的使用)_第1张图片

3.我使用的是myeclipse,先创建一个web项目。

4.在生成的web.xml文件中进行第一步配置

**web.xml**

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    
    <listener>
        <listener-class>
        org.springframework.web.context.ContextLoaderListener
        listener-class>
    listener>
    <context-param>
        <param-name>contextConfigLocationparam-name>
        
        <param-value>
            classpath:SpringConf.xml
        param-value>
    context-param>

    
    <servlet>
     <servlet-name>MyDispatcherservlet-name>
     <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
     servlet-class>

        
        <init-param>
          <param-name>contextConfigLocationparam-name>
           <param-value>
              classpath:SpringMVC-servlet.xml
           param-value>
        init-param>

        
        <load-on-startup>1load-on-startup>
    servlet>
    

    
    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.cssurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.jsurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.gifurl-pattern>
    servlet-mapping>


    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.pngurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>defaultservlet-name>
        <url-pattern>*.jpgurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>MyDispatcherservlet-name>
        
        <url-pattern>/url-pattern>
    servlet-mapping>
    

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

web-app>

5.接着来配置springmvc的配置文件,

**SpringMVC-servlet.xml**
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="  
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans       
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">


<mvc:annotation-driven/> 

    
    <context:component-scan base-package="cn.ffyz" 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"
        p:prefix="/WEB-INF/views/" p:suffix=".jsp" />

  
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
        <property name="supportedMediaTypes">  
            <list>  
                <value>text/html;charset=UTF-8value>  
            list>  
        property>  
    bean>  

      
 <bean class="org.springframework.web.servlet.mvc.
 annotation.AnnotationMethodHandlerAdapter">  
        <property name="messageConverters">  
            <list>  
              <ref bean="mappingJacksonHttpMessageConverter" />
                  
            list>  
        property>  
    bean>
beans>

6.然后是spirng的配置

**SpringConf.xml**

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

<bean id="dataSource"   class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName"
        value="com.mysql.jdbc.Driver" />

<property name="url"      value="jdbc:mysql://localhost:3306/ssh_new" />   

<property name="username" value="yang" />
<property name="password" value="yang" />
bean> 
    
 <bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate" >
     <property name="dataSource" ref="dataSource" />
  bean>

 



    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:MyBatisConf.xml" />
    bean>


    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
       <property name="mapperInterface"  
           value="cn.ffyz.user.dao.UserMapper" />  
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
 bean> 


<context:component-scan base-package="cn.ffyz.user.service" />

beans>

7.然后是Mybatis的配置

**MyBatisConf.xml**


<configuration>

   
    <typeAliases>
      <typeAlias alias="User" type="cn.ffyz.user.domain.User"/> 
   typeAliases>  

   
   <mappers>
       <mapper resource="mapper/UserMapper.xml"/>
   mappers>
configuration>

8.最后就是接口的实现,UserMapper.xml(dao层接口的实现)



<mapper namespace="cn.ffyz.user.dao.UserMapper">    
      
    <select id="getValidate"  resultType="Map">  
       select * from t_sm_user where account = #{0} and password = #{1}  
    select>

     
    <insert id="addUser" useGeneratedKeys="true" keyProperty="id">
        insert into t_sm_user (account,password) values(#{username},#{password}) 
    insert> 

    
    <insert id="inTable" parameterType="cn.ffyz.user.domain.User">
        insert into t_sm_user (account,password) values(#{account},#{password})
    insert>

    
    <delete id="deData" parameterType="String">
        delete from t_sm_user where account = #{usName}
    delete>

    
    <update id="upData" parameterType="String">
        update t_sm_user set account="zhe" where account=#{usName}
    update>

    
    <select id="selAll" resultType="User">
        select * from t_sm_user
    select>


    <resultMap type="cn.ffyz.user.domain.User" id="user" autoMapping="true">
        <id property="id" column="id"/> 
        <result property="account" column="ACCOUNT"/>
        <result property="password" column="PASSWORD"/>
    resultMap>
mapper>

7.1具体这个文件里面的怎么写,就是mybatis的用法了,这里不讲,下面贴上代码的所在层次图
浅谈spring+springMVC+mybatis框架整合(配置文件的使用)_第2张图片

8.你的接口里面定义的方法,在这个配置文件里面实现,这个文件就相当于与原来的DaoImpl(dao接口的实现)。

9.这就是完整的配置了,具体代码上面怎么写,还有前台代码,加上一些外部引入的文件,Jar啊什么的,太多了,就不贴了,有需要源码的可以留言QQ或者邮箱,我发给你们,互相学习

你可能感兴趣的:(SSM框架整合)