首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

(转)Spring集成MyBatis开展项目开发(一)

2013-12-29 
(转)Spring集成MyBatis进行项目开发(一)原文出处:http://wangjie2013.iteye.com/blog/1996135下面将介绍使

(转)Spring集成MyBatis进行项目开发(一)

原文出处:http://wangjie2013.iteye.com/blog/1996135

下面将介绍使用spring+mybatis的开发样例:

? ? 首先,笔者创建的是一个maven工程,在开发先先导入相关的依赖jar:

? ? pom.xml:

?

Xml代码??(转)Spring集成MyBatis开展项目开发(一)
  1. <dependencies>??
  2. ????????<dependency>??
  3. ????????????<groupId>junit</groupId>??
  4. ????????????<artifactId>junit</artifactId>??
  5. ????????????<version>4.11</version>??
  6. ????????????<scope>test</scope>??
  7. ????????</dependency>??
  8. ????????<!--?spring3?相关依赖?-->??
  9. ????????<dependency>??
  10. ????????????<groupId>org.springframework</groupId>??
  11. ????????????<artifactId>spring-core</artifactId>??
  12. ????????????<version>3.0.5.RELEASE</version>??
  13. ????????</dependency>??
  14. ????????<dependency>??
  15. ????????????<groupId>org.springframework</groupId>??
  16. ????????????<artifactId>spring-context</artifactId>??
  17. ????????????<version>3.0.5.RELEASE</version>??
  18. ????????</dependency>??
  19. ????????<dependency>??
  20. ????????????<groupId>org.springframework</groupId>??
  21. ????????????<artifactId>spring-beans</artifactId>??
  22. ????????????<version>3.0.5.RELEASE</version>??
  23. ????????</dependency>??
  24. ????????<dependency>??
  25. ????????????<groupId>org.springframework</groupId>??
  26. ????????????<artifactId>spring-jdbc</artifactId>??
  27. ????????????<version>3.0.5.RELEASE</version>??
  28. ????????</dependency>??
  29. ????????<!--?spring?MVC?-->??
  30. ????????<dependency>??
  31. ????????????<groupId>org.springframework</groupId>??
  32. ????????????<artifactId>spring-web</artifactId>??
  33. ????????????<version>3.0.5.RELEASE</version>??
  34. ????????</dependency>??
  35. ????????<dependency>??
  36. ????????????<groupId>org.springframework</groupId>??
  37. ????????????<artifactId>spring-webmvc</artifactId>??
  38. ????????????<version>3.0.5.RELEASE</version>??
  39. ????????</dependency>??
  40. ????????<!--?mysql?jdbc?driver?-->??
  41. ????????<dependency>??
  42. ????????????<groupId>mysql</groupId>??
  43. ????????????<artifactId>mysql-connector-java</artifactId>??
  44. ????????????<version>5.1.13</version>??
  45. ????????</dependency>??
  46. ????????<!--?log?日志?-->??
  47. ????????<dependency>??
  48. ????????????<groupId>org.slf4j</groupId>??
  49. ????????????<artifactId>slf4j-api</artifactId>??
  50. ????????????<version>1.7.2</version>??
  51. ????????</dependency>??
  52. ????????<dependency>??
  53. ????????????<groupId>org.slf4j</groupId>??
  54. ????????????<artifactId>slf4j-log4j12</artifactId>??
  55. ????????????<version>1.7.2</version>??
  56. ????????</dependency>??
  57. ????????<!--?mybatis-spring?插件?-->??
  58. ????????<dependency>??
  59. ????????????<groupId>org.mybatis</groupId>??
  60. ????????????<artifactId>mybatis-spring</artifactId>??
  61. ????????????<version>1.2.1</version>??
  62. ????????</dependency>??
  63. ????????<!--?mybatis依赖?-->??
  64. ????????<dependency>??
  65. ????????????<groupId>org.mybatis</groupId>??
  66. ????????????<artifactId>mybatis</artifactId>??
  67. ????????????<version>3.2.1</version>??
  68. ????????</dependency>??
  69. ??????????
  70. ????????<dependency>??
  71. ????????????<groupId>javax.servlet</groupId>??
  72. ????????????<artifactId>javax.servlet-api</artifactId>??
  73. ????????????<version>3.1.0</version>??
  74. ????????????<scope>provided</scope>??
  75. ????????</dependency>??
  76. ????????<dependency>??
  77. ????????????<groupId>jstl</groupId>??
  78. ????????????<artifactId>jstl</artifactId>??
  79. ????????????<version>1.2</version>??
  80. ????????</dependency>??
  81. ????????<!--?数据库连接池?-->??
  82. ????????<dependency>??
  83. ????????????<groupId>proxool</groupId>??
  84. ????????????<artifactId>proxool</artifactId>??
  85. ????????????<version>0.9.1</version>??
  86. ????????</dependency>??
  87. ????????<dependency>??
  88. ????????????<groupId>proxool</groupId>??
  89. ????????????<artifactId>proxool-cglib</artifactId>??
  90. ????????????<version>0.9.1</version>??
  91. ????????</dependency>??
  92. ????????<!--?servlet-jsp?依赖,为编译jsp时使用?-->??
  93. ????????<dependency>??
  94. ????????????<groupId>javax.servlet.jsp</groupId>??
  95. ????????????<artifactId>jsp-api</artifactId>??
  96. ????????????<version>2.2</version>??
  97. ????????????<scope>provided</scope>??
  98. ????????</dependency>??
  99. ????</dependencies>??
  100. ??????

?

?

? ?web.xml中对spring和数据库连接池的配置:

Xml代码??(转)Spring集成MyBatis开展项目开发(一)
  1. <context-param>??
  2. ????<param-name>log4jConfigLocation</param-name>??
  3. ????<param-value>/WEB-INF/log4j.xml</param-value>??
  4. </context-param>??
  5. <context-param>??
  6. ????<param-name>log4jRefreshInterval</param-name>??
  7. ????<param-value>60000</param-value>??
  8. </context-param>??
  9. ??
  10. <listener>??
  11. ????<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>??
  12. </listener>??
  13. ??
  14. <!--?needed?for?ContextLoaderListener?-->??
  15. <context-param>??
  16. ????<param-name>contextConfigLocation</param-name>??
  17. ????<param-value>/WEB-INF/applicationContext.xml</param-value>??
  18. </context-param>??
  19. ??
  20. <!--?Bootstraps?the?root?web?application?context?before?servlet?initialization?-->??
  21. <listener>??
  22. ????<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>??
  23. </listener>??
  24. <!--?The?front?controller?of?this?Spring?Web?application,?responsible?for???
  25. ????handling?all?application?requests?-->??
  26. <servlet>??
  27. ????<servlet-name>spring3</servlet-name>??
  28. ????<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>??
  29. ????<load-on-startup>2</load-on-startup>??
  30. </servlet>??
  31. ??
  32. <!--?Map?all?requests?to?the?DispatcherServlet?for?handling?-->??
  33. <servlet-mapping>??
  34. ????<servlet-name>spring3</servlet-name>??
  35. ????<url-pattern>/</url-pattern>??
  36. </servlet-mapping>??
  37. ??
  38. <!--?database?proxool?config?-->??
  39. <servlet>??
  40. ????<servlet-name>ServletConfigurator</servlet-name>??
  41. ????<servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>??
  42. ????<init-param>??
  43. ????????<param-name>xmlFile</param-name>??
  44. ????????<param-value>WEB-INF/proxool.xml</param-value>??
  45. ????</init-param>??
  46. ????<load-on-startup>1</load-on-startup>??
  47. </servlet>??

?

? ? 在这里要注意,因为我使用了数据库连接池proxool,虽然已经设置servlet的启动级别是1,但是由于在servlet启动之前,spring(ContextLoaderListener)监听器已启动了,所以在spring监听启动时它会报一个找不到对应的数据源的SQLException。对于这个问题,本可以通过更改spring为servlet启动,并将启动级别设置为proxool配置加载之后解决:

?

?

Xml代码??(转)Spring集成MyBatis开展项目开发(一)
  1. <servlet>??
  2. ???<servlet-name>contextConfigLocation</servlet-name>??
  3. ???<servlet-class>??
  4. ????????????org.springframework.web.context.ContextLoaderServlet??
  5. ???</servlet-class>??
  6. ???<load-on-startup>2</load-on-startup>??
  7. </servlet>??

?

?

? ? 但是因为spring3以后就不支持使用servlet启动了,官方推荐使用listener启动applicationContext。所以这个方法在新版本不适合了,不过这个异常可以忽略,因为proxool在整个项目加载完成的时候的确以及完成了加载,所以在项目运行起来以后是不会报错的,开始报错是spring做的一个检查。

?

? ? 为了解决乱码问题最好在web.xml中加上这个配置:

?

Xml代码??(转)Spring集成MyBatis开展项目开发(一)
  1. <!--?character?filter.?use?UTF8?-->??
  2. <filter>??
  3. ????<filter-name>characterEncodingFilter</filter-name>??
  4. ????<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>??
  5. ????<init-param>??
  6. ????????<param-name>encoding</param-name>??
  7. ????????<param-value>UTF-8</param-value>??
  8. ????</init-param>??
  9. </filter>??
  10. <filter-mapping>??
  11. ????<filter-name>characterEncodingFilter</filter-name>??
  12. ????<url-pattern>/*</url-pattern>??
  13. </filter-mapping>??

?

下面是对spring3-servlet.xml的配置,它里面申明的bean都存放在webApplicationContext中:

Xml代码??(转)Spring集成MyBatis开展项目开发(一)
  1. <?xml?version="1.0"?encoding="UTF-8"??>??
  2. <beans?xmlns="http://www.springframework.org/schema/beans"??
  3. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns:mvc="http://www.springframework.org/schema/mvc"??
  4. ????default-lazy-init="false"?xmlns:context="http://www.springframework.org/schema/context"??
  5. ????xsi:schemaLocation="http://www.springframework.org/schema/mvc?http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd??
  6. ????????http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.0.xsd??
  7. ????????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.0.xsd">??
  8. ??
  9. ????<context:component-scan?base-package="com.pinche.statistic.web"></context:component-scan>??
  10. ????<!--?启用spring?mvc注解?-->??
  11. ????<mvc:annotation-driven?/>??
  12. ????<!--?静态资源访问问题?-->??
  13. ????<mvc:default-servlet-handler?/>??
  14. ????<!--?Default?ViewResolver?-->??
  15. ????<bean?class="org.springframework.web.servlet.view.InternalResourceViewResolver">??
  16. ????????<property?name="viewClass"?value="org.springframework.web.servlet.view.JstlView"?/>??
  17. ????????<property?name="prefix"?value="/WEB-INF/page/"></property>??
  18. ????????<property?name="suffix"?value=".jsp"></property>??
  19. ????</bean>??
  20. ????<!--?对抛给spring的异常的处理?-->??
  21. ????<bean?id="exceptionResolver"??
  22. ????????class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">??
  23. ????????<property?name="exceptionMappings">??
  24. ????????????<props>??
  25. ????????????????<prop?key="java.lang.Exception">error</prop>??
  26. ????????????</props>??
  27. ????????</property>??
  28. ????</bean>??
  29. </beans>??

?

? ? 下面对applicationContext.xml进行配置(这里使用了两个数据源):

Xml代码??(转)Spring集成MyBatis开展项目开发(一)
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <beans?xmlns="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:tx="http://www.springframework.org/schema/tx"??
  7. ????xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.0.xsd??
  8. ????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-3.0.xsd??
  9. ????????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.0.xsd">??
  10. ??
  11. ????<!--??
  12. ????????数据源配置:这里配置了两个数据源dataSource_statistic和dataSource_pinche,??
  13. ????分别注入到_sqlSessionFactory_statistic和_sqlSessionFactory_pinche??
  14. ????-->??
  15. ????<bean?id="dataSource_statistic"??
  16. ????????class="org.springframework.jdbc.datasource.DriverManagerDataSource">??
  17. ????????<!--?数据库连接?-->??
  18. ????????<property?name="url"?value="proxool.statistic"?/>???
  19. ????????<!--??
  20. ????????这是不使用数据库连接池的配置方式,由于proxool只有在启动web应用的时候才能加载,??
  21. ????????所以如果在初期开发阶段可以用这种简单的数据库连接方式,在通过??
  22. ????????new?FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml")??
  23. ????????获取到context,并进行测试。??
  24. ????????<property?name="driverClassName"?value="com.mysql.jdbc.Driver"/>??
  25. ????????<property?name="url"?value="jdbc:mysql://192.168.1.168:3306/statistic"/>??
  26. ????????<property?name="username"?value="statistic"></property>??
  27. ????????<property?name="password"?value="statistic"></property>???
  28. ?????????-->??
  29. ????</bean>??
  30. ????<bean?id="dataSource_pinche"??
  31. ????????class="org.springframework.jdbc.datasource.DriverManagerDataSource">??
  32. ????????<!--?数据库连接?-->??
  33. ????????<property?name="url"?value="proxool.pinche"?/>???
  34. ????</bean>??
  35. ??????
  36. ????<!--?为mybatis-spring注入数据源?-->??
  37. ????<bean?name="_sqlSessionFactory_statistic"?class="org.mybatis.spring.SqlSessionFactoryBean">??
  38. ????????<!--?注入数据源?-->??
  39. ????????<property?name="dataSource"?ref="dataSource_statistic"></property>??
  40. ????????<!--?注入别名的包名前缀,这样就可以在mybatis的文件中直接写类名不用写全名了?-->??
  41. ????????<property?name="typeAliasesPackage"?value="com.pinche.statistic.domain"?/>??
  42. ????</bean>??
  43. ????<bean?name="_sqlSessionFactory_pinche"?class="org.mybatis.spring.SqlSessionFactoryBean">??
  44. ????????<!--?注入数据源?-->??
  45. ????????<property?name="dataSource"?ref="dataSource_pinche"></property>??
  46. ????????<!--?注入别名的包名前缀,这样就可以在mybatis的文件中直接写类名不用写全名了?-->??
  47. ????????<property?name="typeAliasesPackage"?value="com.pinche.statistic.domain"?/>??
  48. ????????<!--?该配置文件用来指定Mapper映射文件的位置?,如果映射文件与相应的接口同名,且在同一路径下,那么可以不配置该选项-->??
  49. ????????<!--<property?name="mapperLocations"?value="src/UserMapper.xml"/>-->??
  50. ????????<!--??
  51. ????????????该属性用来指定MyBatis的XML配置文件路径,跟Spring整合时,??
  52. ????????????编写MyBatis映射文件的目的无非是配置一下typeAlias、setting之类的??
  53. ????????????元素。不用在其中指定数据源,或者事务处理方式。就算配置了也会被忽略。??
  54. ????????????因为这些都是使用Spring中的配置。当然如果你不打算添加typeAlias?之??
  55. ????????????类的设置的话,你连MyBatis的配置文件都不用写,更不用配置这个属性了??
  56. ????????-->??
  57. ????????<!--?<property?name="configLocation"?value=""/>?-->??
  58. ????</bean>??
  59. ????<!--??
  60. ????????注册Mapper方式一??
  61. ????????<bean?id="userMapper"?class="org.mybatis.spring.mapper.MapperFactoryBean">???
  62. ????????????<property?name="mapperInterface"?value="com.pinche.statistic.mapper.ApplicationsMapper"/>???
  63. ????????????<property?name="sqlSessionFactory"?ref="_sqlSessionFactory_statistic"/>???
  64. ????????</bean>??
  65. ????-->??
  66. ????<!--?注册Mapper方式二:也可不指定特定mapper,而使用自动扫描包的方式来注册各种Mapper?,配置如下:-->??
  67. ????<!--???
  68. ????<bean?class="org.mybatis.spring.mapper.MapperScannerConfigurer">??
  69. ????????<property?name="basePackage"?value="com.pinche.statistic.mapper"></property>??
  70. ????</bean>????
  71. ????-->??
  72. ????<bean?class="org.mybatis.spring.mapper.MapperScannerConfigurer">??
  73. ????????<property?name="basePackage"?value="com.pinche.statistic.dialstatistic.mapper"></property>??
  74. ????????<property?name="sqlSessionFactoryBeanName"?value="_sqlSessionFactory_pinche"></property>??
  75. ????</bean>??
  76. ????<bean?class="org.mybatis.spring.mapper.MapperScannerConfigurer">??
  77. ????????<property?name="basePackage"?value="com.pinche.statistic.mapper"></property>??
  78. ????????<property?name="sqlSessionFactoryBeanName"?value="_sqlSessionFactory_statistic"></property>??
  79. ????</bean>???
  80. ??????
  81. ????<!--?事务?-->??
  82. ????<!--?虽然mybatis也有自己的事务配置,但是配置了也没用,事务最终会交由spring控制,??
  83. ????由于项目没有用到可以注掉。???
  84. ????-->??
  85. ????<!--?<bean?id="transactionManager"??
  86. ????????class="org.springframework.jdbc.datasource.DataSourceTransactionManager">??
  87. ????????<property?name="dataSource"?ref="dataSource_statistic"?/>??
  88. ????</bean>??
  89. ????<tx:annotation-driven?transaction-manager="transactionManager"/>?-->??
  90. ??????
  91. ????<!--?启用注解?-->??
  92. ????<context:annotation-config?/>??
  93. ??????
  94. ????<!--?自动扫描注入?-->??
  95. ????<context:component-scan??
  96. ????????base-package="com.pinche.statistic.dao,??
  97. ????????????????com.pinche.statistic.service"?/>??
  98. </beans>??

? ? ?通过以上的工作环境的配置完成,下一章继续介绍spring-mybatis集成开发的剩余内容。

热点排行