歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Spring3 整合 Mybatis3

這兩天,項目需要使用spring+ibatis,於是去網上下載了,結果發現和之前我用的版本變化不小,整了兩天才將功能實現,不敢怠慢,趕緊寫份博客記錄一下。

首先是依賴的庫:

接著是web.xml的配置,這裡,我使用的是Spring3 MVC

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"version="2.5">
  3. <display-name>Log</display-name>
  4. <welcome-file-list>
  5. <welcome-file>welcome.html</welcome-file>
  6. </welcome-file-list>
  7. <!--日志配置文件的載入 -->
  8. <context-param>
  9. <param-name>log4jConfigLocation</param-name>
  10. <param-value>/WEB-INF/classes/log4j.properties</param-value>
  11. </context-param>
  12. <!--Spring配置文件的載入 -->
  13. <context-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>classpath:applicationContext*.xml</param-value>
  16. </context-param>
  17. <!--Spring的ApplicationContext 載入 -->
  18. <listener>
  19. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  20. </listener>
  21. <!-- Spring字符集過濾 -->
  22. <filter>
  23. <filter-name>encodingFilter</filter-name>
  24. <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class>
  25. <init-param>
  26. <param-name>encoding</param-name>
  27. <param-value>utf8</param-value>
  28. </init-param>
  29. </filter>
  30. <filter-mapping>
  31. <filter-name>encodingFilter</filter-name>
  32. <url-pattern>*.do</url-pattern>
  33. </filter-mapping>
  34. <!-- Server啟動時加載的應用 -->
  35. <!-- Spring過濾器 -->
  36. <servlet>
  37. <servlet-name>dispatchServlet</servlet-name>
  38. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  39. <init-param>
  40. <param-name>contextConfigLocation</param-name>
  41. <param-value>classpath:servlet-config.xml</param-value>
  42. </init-param>
  43. <load-on-startup>1</load-on-startup>
  44. </servlet>
  45. <servlet-mapping>
  46. <servlet-name>dispatchServlet</servlet-name>
  47. <url-pattern>*.do</url-pattern>
  48. </servlet-mapping>
  49. </web-app>

然後是spring的servlet配置servlet-config.xml:

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:mvc="http://www.springframework.org/schema/mvc"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  13. <!-- 自動掃描bean,把作了注��的類轉換為bean -->
  14. <context:component-scanbase-package="com.log.bean.dao, com.log.report.controller"/>
  15. <!-- 默認的注解映射的支持 -->
  16. <mvc:annotation-driven/>
  17. <!-- 支持JSON數據格式 -->
  18. <beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
  19. <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  20. <propertyname="messageConverters">
  21. <list>
  22. <beanclass="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
  23. <propertyname="supportedMediaTypes">
  24. <list>
  25. <value>text/html;charset=UTF-8</value>
  26. </list>
  27. </property>
  28. </bean>
  29. </list>
  30. </property>
  31. </bean>
  32. <!-- 聲明viewResolver -->
  33. <beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  34. <propertyname="prefix"value="/"/>
  35. <propertyname="suffix"value=".html"/>
  36. </bean>
  37. </beans>

然後是Spring的Bean的配置文件applicationContext.xml:

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  15. <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
  16. <propertyname="maxActive"value="20"/>
  17. <propertyname="maxIdle"value="20"/>
  18. <propertyname="maxWait"value="3000"/>
  19. <propertyname="testWhileIdle"value="true"/>
  20. <propertyname="timeBetweenEvictionRunsMillis"value="3600000"/>
  21. <propertyname="validationQuery"value="select 1"/>
  22. <propertyname="removeAbandoned"value="true"/>
  23. <propertyname="removeAbandonedTimeout"value="1"/>
  24. <propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
  25. <propertyname="url"value="jdbc:mysql://localhost:53306/search"/>
  26. <propertyname="username"value="root"/>
  27. <propertyname="password"value="8bewp2rJHfRGrl9VTQmc"/>
  28. </bean>
  29. <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
  30. <propertyname="configLocation"value="classpath:ibatis-config.xml"/>
  31. <propertyname="dataSource"ref="dataSource"/>
  32. <!-- mapper和resultmap配置路徑 -->
  33. <propertyname="mapperLocations">
  34. <list>
  35. <value>classpath:com/log/bean/mapper/*.xml</value>
  36. </list>
  37. </property>
  38. </bean>
  39. <!-- 通過掃描的模式,掃描目錄在com/log/bean/mapper目錄下,所有的mapper都繼承
  40. SQLMapper接口的接口, 這樣一個bean就可以了 -->
  41. <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
  42. <propertyname="basePackage"value="com.log.bean.mapper"/>
  43. <propertyname="markerInterface"value="com.log.bean.mapper.SQLMapper"/>
  44. </bean>
  45. <beanid="sqlSession"class="org.mybatis.spring.SqlSessionTemplate">
  46. <constructor-argindex="0"ref="sqlSessionFactory"/>
  47. </bean>
  48. <!--事務配置-->
  49. <tx:annotation-driventransaction-manager="transactionManager"/>
  50. <beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"autowire="byName"/>
  51. </beans> 
Copyright © Linux教程網 All Rights Reserved