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)的时候是找不到的,我弄了一上午,并且发誓再找不到原因就跳楼的,还好吃饭前找出原因。要不要么是死人,要么是女人了。呵呵。 4、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,那叫一个悲剧,一直没找到,这就是不仔细看文档的下场。 5、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>