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

1.文章來由:一直都是在別人搭建好的框架下進行開發,所以對框架的集成,具體文件怎麼配置,怎樣搭建很疑惑,於某天決定自己來搭建一次。(我搭建的只是個測試環境,細節的配置問題未涉及。看個人項目需求)

2.下看看我的項目結構目錄,方便描述
這裏寫圖片描述

3.我使用的是myeclipse,先創建一個web項目。

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

**web.xml**
<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!-- Spring 容器加載 -->
    <listener>
        <listener-class>
        org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!--加載spring配置文件-->
        <param-value>
            classpath:SpringConf.xml
        </param-value>
    </context-param>

    <!-- SpringMVC的前端控制器 -->
    <servlet>
     <servlet-name>MyDispatcher</servlet-name>
     <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
     </servlet-class>

        <!-- 加載springmvc配置文件路徑 -->
        <init-param>
          <param-name>contextConfigLocation</param-name>
           <param-value>
              classpath:SpringMVC-servlet.xml
           </param-value>
        </init-param>

        <!-- 何時啓動 大於0的值表示容器啓動時初始化此servlet,正值越小優先級越高 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- Spring MVC配置文件結束 -->

    <!-- SpringMVC攔截設置,保證靜態文件加載,看個人需求 -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>


    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>MyDispatcher</servlet-name>
        <!-- 由SpringMVC攔截所有請求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- SpringMVC攔截設置結束 -->

    <!--解決中文亂碼問題 -->
    <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>

</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/> 

    <!-- 掃描Controller註解的Bean -->
    <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" />

<!-- 避免IE執行AJAX時,返回JSON出現下載文件 -->  
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
        <property name="supportedMediaTypes">  
            <list>  
                <value>text/html;charset=UTF-8</value>  
            </list>  
        </property>  
    </bean>  

    <!-- 啓動Spring MVC的註解功能,完成請求和註解POJO的映射 -->  
 <bean class="org.springframework.web.servlet.mvc.
 annotation.AnnotationMethodHandlerAdapter">  
        <property name="messageConverters">  
            <list>  
              <ref bean="mappingJacksonHttpMessageConverter" />
                <!-- json轉換器 -->  
            </list>  
        </property>  
    </bean>
</beans>

6.然後是spirng的配置

**SpringConf.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: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">
   <!--下面這句是爲了引入deploy.properties來手動方便管理數據庫連接,這裏因爲我在配置文件寫死了,所以雖然創建了文件,但是沒有用到-->         
   <!--  <context:property-placeholder location="classPath:deploy.properties" /> -->
    <!-- 配置數據源 -->

<bean id="dataSource"   class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--p配置驅動,地址,用戶名,密碼-->
<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> 
    <!-- 注入jdbcTemplate -->
 <bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate" >
     <property name="dataSource" ref="dataSource" />
  </bean>

<!--下面註釋掉的文件就是上面說的引入配置文件管理數據庫的配置--> 
<!--    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${dataSource.driver}" />
        <property name="url" value="${dataSource.url}" />
        <property name="username" value="${dataSource.username}" />
        <property name="password" value="${dataSource.password}" />
    </bean> -->

<!--用spring管理Mybatis數據源,引入配置文件-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:MyBatisConf.xml" />
    </bean>

<!--創建dao 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> 

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

</beans>

7.然後是Mybatis的配置

**MyBatisConf.xml**
<?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>

   <!-- 配置映射類的別名 -->
    <typeAliases>
      <typeAlias alias="User" type="cn.ffyz.user.domain.User"/> 
   </typeAliases>  

   <!-- 配置Mapper文件的路徑,接口Dao的實現 -->
   <mappers>
       <mapper resource="mapper/UserMapper.xml"/>
   </mappers>
</configuration>

8.最後就是接口的實現,UserMapper.xml(dao層接口的實現)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<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的用法了,這裏不講,下面貼上代碼的所在層次圖
這裏寫圖片描述

8.你的接口裏面定義的方法,在這個配置文件裏面實現,這個文件就相當於與原來的DaoImpl(dao接口的實現)。

9.這就是完整的配置了,具體代碼上面怎麼寫,還有前臺代碼,加上一些外部引入的文件,Jar啊什麼的,太多了,就不貼了,有需要源碼的可以留言QQ或者郵箱,我發給你們,互相學習

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章