Xml代碼 1.<?xml version="1.0" encoding="UTF-8"?> 2.<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6. 7. <display-name>qdcl-mart</display-name> 8. 9. <!-- 載入spring上下文 --> 10. <context-param> 11. <param-name>contextConfigLocation</param-name> 12. <param-value>classpath*:/spring/applicationContext.xml</param-value> 13. </context-param> 14. <listener> 15. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 16. </listener> 17. 18. <!-- Spring 刷新Introspector防止內存泄露 --> 19. <listener> 20. <listener-class> 21. org.springframework.web.util.IntrospectorCleanupListener 22. </listener-class> 23. </listener> 24. 25. <!-- 字符編碼過濾器 --> 26. <filter> 27. <filter-name>encodingFilter</filter-name> 28. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 29. <init-param> 30. <param-name>encoding</param-name> 31. <param-value>UTF-8</param-value> 32. </init-param> 33. </filter> 34. 35. <filter-mapping> 36. <filter-name>encodingFilter</filter-name> 37. <url-pattern>/</url-pattern> 38. </filter-mapping> 39. 40. <!-- 載入spring上下文 --> 41. <servlet> 42. <servlet-name>mart</servlet-name> 43. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 44. <init-param> 45. <param-name>contextConfigLocation</param-name> 46. <param-value>classpath*:/spring-mvc/mart-servlet.xml</param-value> 47. </init-param> 48. <load-on-startup>1</load-on-startup> 49. </servlet> 50. <servlet-mapping> 51. <servlet-name>mart</servlet-name> 52. <url-pattern>/</url-pattern> 53. </servlet-mapping> 54. 55. <!-- 歡迎頁面 --> 56. <welcome-file-list> 57. <welcome-file>index.jsp</welcome-file> 58. </welcome-file-list> 59.</web-app> <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>qdcl-mart</display-name> <!-- 載入spring上下文 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/spring/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring 刷新Introspector防止內存泄露 --> <listener> <listener-class> org.springframework.web.util.IntrospectorCleanupListener </listener-class> </listener> <!-- 字符編碼過濾器 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <!-- 載入spring上下文 --> <servlet> <servlet-name>mart</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/spring-mvc/mart-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mart</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 歡迎頁面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> 關於classpath*:/其實無所謂,至少在個人目錄結構裏無所謂,用classpath:/或者不寫均可以找到的。既然我用這個跑起來了,其餘的我就不寫了。 2:applicationContext.xml Xml代碼 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:aop="http://www.springframework.org/schema/aop" 5. xmlns:tx="http://www.springframework.org/schema/tx" 6. xmlns:jdbc="http://www.springframework.org/schema/jdbc" 7. xmlns:context="http://www.springframework.org/schema/context" 8. xsi:schemaLocation=" 9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 10. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 11. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 13. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" 14. > 15. 16. <!-- 屬性文件讀入 --> 17. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 18. <property name="locations"> 19. <list> 20. <value>classpath:spring/jdbc.properties</value> 21. </list> 22. </property> 23. </bean> 24. 25. <!-- 數據庫鏈接池 --> 26. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 27. <property name="driverClassName" value="${jdbc.driver}" /> 28. <property name="url" value="${jdbc.url}" /> 29. <property name="username" value="${jdbc.username}" /> 30. <property name="password" value="${jdbc.password}" /> 31. </bean> 32. 33. <!-- Mybatis's sqlSessionFactory config --> 34. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 35. <property name="dataSource" ref="dataSource"></property> 36. <property name="configLocation" value="classpath:mybatis-config.xml"/> 37. </bean> 38. 39. <bean name="transactionManager" 40. class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 41. <property name="dataSource" ref="dataSource"></property> 42. </bean> 43. 44. <!-- 啓用事務 --> 45. <tx:advice id="txAdvice" transaction-manager="transactionManager"> 46. <tx:attributes> 47. <tx:method name="save*" propagation="REQUIRED" /> 48. <tx:method name="delete*" propagation="REQUIRED" /> 49. <tx:method name="update*" propagation="REQUIRED" /> 50. </tx:attributes> 51. </tx:advice> 52. 53. <aop:config> 54. <aop:pointcut id="serviceOperation" expression="execution(* com.qdcl.mart.business.service.*.*(..))" /> 55. <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> 56. </aop:config> 57. 58. <!-- scan mappers and let them be autowired --> 59. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 60. <property name="basePackage" value="com.qdcl.mart.business.persistence" /> 61. </bean> 62. <!-- 激活annotation功能 --> 63. <context:annotation-config /> 64. <!-- 激活annotation功能 --> 65. <context:spring-configured/> 66. <!-- 掃描指定package下全部帶有如@controller,@services ,@resource,並把所註釋的註冊爲Spring Beans --> 67. <context:component-scan base-package="com.qdcl.mart.business.service" /> 68.</beans> <?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" > <!-- 屬性文件讀入 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:spring/jdbc.properties</value> </list> </property> </bean> <!-- 數據庫鏈接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- Mybatis's sqlSessionFactory config --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 啓用事務 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* com.qdcl.mart.business.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> </aop:config> <!-- scan mappers and let them be autowired --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.qdcl.mart.business.persistence" /> </bean> <!-- 激活annotation功能 --> <context:annotation-config /> <!-- 激活annotation功能 --> <context:spring-configured/> <!-- 掃描指定package下全部帶有如@controller,@services ,@resource,並把所註釋的註冊爲Spring Beans --> <context:component-scan base-package="com.qdcl.mart.business.service" /> </beans> jdbc.properties Properties代碼 1.jdbc.url=jdbc:mysql://localhost:3306/qdclmart?useUnicode=true&characterEncoding=UTF-8 2.jdbc.driver=com.mysql.jdbc.Driver 3.jdbc.username=root 4.jdbc.password=root jdbc.url=jdbc:mysql://localhost:3306/qdclmart?useUnicode=true&characterEncoding=UTF-8 jdbc.driver=com.mysql.jdbc.Driver jdbc.username=root jdbc.password=root 裏面的dataSource就不說了,簡單說一下 ①、sqlSessionFactory:用來進行數據庫操做的類,並且好玩的是雖然你配了org.mybatis.spring.SqlSessionFactoryBean,可是測試的時候你用getBean("sqlSessionFactory")獲得的確是mybatis的這個類: org.apache.ibatis.session.SqlSessionFactory,緣由是org.mybatis.spring.SqlSessionFactoryBean.getObject返回的類型給改變了。具體爲什麼我也不知道。你們能夠測試一下,強轉就拋錯誤了。 ②、 Java代碼 1.<!-- scan mappers and let them be autowired --> 2. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 3. <property name="basePackage" value="com.qdcl.mart.business.persistence" /> 4. </bean> <!-- scan mappers and let them be autowired --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.qdcl.mart.business.persistence" /> </bean> 這段配置是我比較喜歡滴,由於他自動掃描了全部的XxxxMapper.java,這樣就不用一個一個手動配置Mpper的映射了,只要Mapper接口類和Mapper映射文件對應起來就能夠了。詳細的你們能夠到google mybatis項目網址看一下那個文檔,中文滴。 ③、 Java代碼 1.<!-- 激活annotation功能 --> 2. <context:annotation-config /> 3. <!-- 激活annotation功能 --> 4. <context:spring-configured/> 5. <!-- 掃描指定package下全部帶有如@controller,@services ,@resource,並把所註釋的註冊爲Spring Beans --> 6. <context:component-scan base-package="com.qdcl.mart.business.service" /> <!-- 激活annotation功能 --> <context:annotation-config /> <!-- 激活annotation功能 --> <context:spring-configured/> <!-- 掃描指定package下全部帶有如@controller,@services ,@resource,並把所註釋的註冊爲Spring Beans --> <context:component-scan base-package="com.qdcl.mart.business.service" /> 以上3句不解釋了啊,就是註解式管理bean,有的大神不推薦,我我的仍是喜歡,由於對大量的配置文件有些抵觸,錯了找起來比較麻煩。懶嘛。 context:component-scan儘管他是遞歸方式掃描,我仍是建議你們base-package具體一些,你懂得。 3:mart-servlet.xml Xml代碼 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:mvc="http://www.springframework.org/schema/mvc" 6. xmlns:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 9. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" 10. > 11. <!-- 只能用於springMVC,用於配置springMVC的註解驅動 --> 12. <mvc:annotation-driven /> 13. <!-- Spring mvc視圖解析器 --> 14. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 15. <property name="prefix" value="/WEB-INF/view/" /> 16. <property name="suffix" value=".jsp" /> 17. </bean> 18. <context:component-scan base-package="com.qdcl.mart.business.web" /> 19. 20.</beans> <?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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" > <!-- 只能用於springMVC,用於配置springMVC的註解驅動 --> <mvc:annotation-driven /> <!-- Spring mvc視圖解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> <context:component-scan base-package="com.qdcl.mart.business.web" /> </beans> 只說一句,一開始沒加<context:component-scan base-package="com.qdcl.mart.business.web" />這一句,結局就是我在Controller裏面寫RequestMapping(xxxx)的時候是找不到的,我弄了一上午,而且發誓再找不到緣由就跳樓的,還好吃飯前找出緣由。要不要麼是死人,要麼是女人了。呵呵。 四、mybatis-config.xml Xml代碼 1.<?xml version="1.0" encoding="UTF-8"?> 2.<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3.<configuration> 4. <!-- 基礎設置 --> 5. <settings> 6. <!-- changes from the defaults --> 7. <setting name="lazyLoadingEnabled" value="false" /> 8. </settings> 9. <!-- 別名定義 --> 10. <typeAliases> 11. <typeAlias alias="production" type="com.qdcl.mart.business.domain.Production" /> 12. </typeAliases> 13. <!-- SQL映射文件 --> 14. <mappers> 15. <mapper resource="mapper/ProductionMapper.xml" /> 16. </mappers> 17.</configuration> <?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> <!-- 基礎設置 --> <settings> <!-- changes from the defaults --> <setting name="lazyLoadingEnabled" value="false" /> </settings> <!-- 別名定義 --> <typeAliases> <typeAlias alias="production" type="com.qdcl.mart.business.domain.Production" /> </typeAliases> <!-- SQL映射文件 --> <mappers> <mapper resource="mapper/ProductionMapper.xml" /> </mappers> </configuration> 也是一句,就是最後這個mappers的resource配置,當時手賤,寫成/mapper/ProductionMapper.xml,那叫一個悲劇,一直沒找到,這就是不仔細看文檔的下場。 五、XxxMapper.xml(ProductionMapper.xml) Xml代碼 1.<?xml version="1.0" encoding="UTF-8"?> 2.<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3."http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4. 5.<mapper namespace="com.qdcl.mart.business.persistence.ProductionMapper"> 6. 7. <!-- 檢索 --> 8. <select id="selectProductionByName" parameterType="java.lang.String" resultType="production"> 9. <!-- 檢索sql文 --> 10. SELECT productionid,productionname,price,detail FROM production 11. where productionname = #{productionname} 12. </select> 13. 14. <!-- 插入 --> 15. <insert id="insertProduction" parameterType="production"> 16. insert into production values (#{productionid},#{productionname},#{price},#{detail}); 17. </insert> 18. <!-- 更新 --> 19. <update id="updateProduction" parameterType="production"> 20. update production set productionname = #{productionname}, 21. price = #{price}, 22. detail = #{detail} 23. </update> 24. <!-- 刪除 --> 25. <delete id="deleteProduction" parameterType="java.lang.String"> 26. delete from production where productionid = #{value} 27. </delete> 28.</mapper>