[转]ssm整合1(环境搭建)

1 MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建
http://blog.csdn.net/zhshulin/article/details/30779873
2 apache-maven-3.2.1的安装
http://blog.csdn.net/tdmyl/article/details/31415103
3 MyEclipse安装JS代码提示(Spket插件)
http://blog.csdn.net/zhshulin/article/details/25635883

  1. </pre><p></p><div><span style="font-size:18px; color:#ff0000"><strong>1 使用maven创建web项目</strong></span></div><div>http://blog.csdn.net/zhshulin/article/details/37921705按照上述方式配置后src/main/java文件夹可能还是出不来,则</p><p>项目右键->java build path ->source->add folder 在src/main下新加一个java文件夹<strong><span style="font-size:18px; color:#ff0000">2 修改pom.xml,替换dependencies如下</span></strong></div><p></p><pre name="code" class="html"><span style="font-family: Arial, Helvetica, sans-serif;"><dependencies></span>
</pre><p></p><div><span style="font-size:18px; color:#ff0000"><strong>1 使用maven创建web项目</strong></span></div><div>http://blog.csdn.net/zhshulin/article/details/37921705按照上述方式配置后src/main/java文件夹可能还是出不来,则</p><p>项目右键->java build path ->source->add folder 在src/main下新加一个java文件夹<strong><span style="font-size:18px; color:#ff0000">2 修改pom.xml,替换dependencies如下</span></strong></div><p></p><pre name="code" class="html"><span style="font-family: Arial, Helvetica, sans-serif;"><dependencies></span>
  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-test</artifactId>
  4. <version>3.2.0.RELEASE</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework</groupId>
  8. <artifactId>spring-webmvc</artifactId>
  9. <version>3.2.0.RELEASE</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-core</artifactId>
  14. <version>3.2.0.RELEASE</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.mybatis</groupId>
  18. <artifactId>mybatis</artifactId>
  19. <version>3.1.1</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.mybatis</groupId>
  23. <artifactId>mybatis-spring</artifactId>
  24. <version>1.1.1</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>mysql</groupId>
  28. <artifactId>mysql-connector-java</artifactId>
  29. <version>5.1.21</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>junit</groupId>
  33. <artifactId>junit</artifactId>
  34. <version>4.11</version>
  35. <scope>test</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>com.alibaba</groupId>
  39. <artifactId>druid</artifactId>
  40. <version>0.2.9</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.aspectj</groupId>
  44. <artifactId>aspectjweaver</artifactId>
  45. <version>1.7.1</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.codehaus.jackson</groupId>
  49. <artifactId>jackson-mapper-asl</artifactId>
  50. <version>1.9.11</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>commons-fileupload</groupId>
  54. <artifactId>commons-fileupload</artifactId>
  55. <version>1.2.2</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>javax.servlet</groupId>
  59. <artifactId>servlet-api</artifactId>
  60. <version>3.0-alpha-1</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>log4j</groupId>
  64. <artifactId>log4j</artifactId>
  65. <version>1.2.17</version>
  66. </dependency>
  67. <dependency>
  68. <groupId>com.alibaba</groupId>
  69. <artifactId>fastjson</artifactId>
  70. <version>1.1.26</version>
  71. </dependency>
  72. <dependency>
  73. <groupId>org.springframework</groupId>
  74. <artifactId>spring-test</artifactId>
  75. <version>3.2.0.RELEASE</version>
  76. </dependency>
  77. <dependency>
  78. <groupId>org.springframework</groupId>
  79. <artifactId>spring-webmvc</artifactId>
  80. <version>3.2.0.RELEASE</version>
  81. </dependency>
  82. <dependency>
  83. <groupId>org.springframework</groupId>
  84. <artifactId>spring-core</artifactId>
  85. <version>3.2.0.RELEASE</version>
  86. </dependency>
  87. <dependency>
  88. <groupId>org.mybatis</groupId>
  89. <artifactId>mybatis</artifactId>
  90. <version>3.1.1</version>
  91. </dependency>
  92. <dependency>
  93. <groupId>org.mybatis</groupId>
  94. <artifactId>mybatis-spring</artifactId>
  95. <version>1.1.1</version>
  96. </dependency>
  97. <dependency>
  98. <groupId>mysql</groupId>
  99. <artifactId>mysql-connector-java</artifactId>
  100. <version>5.1.21</version>
  101. </dependency>
  102. <dependency>
  103. <groupId>junit</groupId>
  104. <artifactId>junit</artifactId>
  105. <version>4.11</version>
  106. <scope>test</scope>
  107. </dependency>
  108. <dependency>
  109. <groupId>com.alibaba</groupId>
  110. <artifactId>druid</artifactId>
  111. <version>0.2.9</version>
  112. </dependency>
  113. <dependency>
  114. <groupId>org.aspectj</groupId>
  115. <artifactId>aspectjweaver</artifactId>
  116. <version>1.7.1</version>
  117. </dependency>
  118. <dependency>
  119. <groupId>org.codehaus.jackson</groupId>
  120. <artifactId>jackson-mapper-asl</artifactId>
  121. <version>1.9.11</version>
  122. </dependency>
  123. <dependency>
  124. <groupId>commons-fileupload</groupId>
  125. <artifactId>commons-fileupload</artifactId>
  126. <version>1.2.2</version>
  127. </dependency>
  128. <dependency>
  129. <groupId>javax.servlet</groupId>
  130. <artifactId>servlet-api</artifactId>
  131. <version>3.0-alpha-1</version>
  132. </dependency>
  133. <dependency>
  134. <groupId>log4j</groupId>
  135. <artifactId>log4j</artifactId>
  136. <version>1.2.17</version>
  137. </dependency>
  138. <dependency>
  139. <groupId>com.alibaba</groupId>
  140. <artifactId>fastjson</artifactId>
  141. <version>1.1.26</version>
  142. </dependency>
  143. </dependencies>
		<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>0.2.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.26</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>0.2.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.26</version>
</dependency>
</dependencies>

3 spring和mybatis整合

新建config.properties文件,主要是配置mysql数据库链接,内容如下

  1. driver=com.mysql.jdbc.Driver
  2. jdbc_url=jdbc\:mysql\://localhost\:3306/sy?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull
  3. jdbc_username=root
  4. ${jdbc_password}=root
  5. validationQuery=SELECT 1
driver=com.mysql.jdbc.Driver  
jdbc_url=jdbc\:mysql\://localhost\:3306/sy?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull 
jdbc_username=root  
${jdbc_password}=root  
validationQuery=SELECT 1

4 新建spring-mybatis.xml文件,整合spring和mybatis,内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
  3. http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  5. http://www.springframework.org/schema/tx
  6. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  9. ">
  10. <!-- 自动扫描(自动注入) -->
  11. <context:component-scan base-package="wxd.service" />
  12. <!-- JNDI方式配置数据源 -->
  13. <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->
  14. <!-- 配置数据源 -->
  15. <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  16. <property name="url" value="${jdbc_url}" />
  17. <property name="username" value="${jdbc_username}" />
  18. <property name="password" value="${jdbc_password}" />
  19. <!-- 初始化连接大小 -->
  20. <property name="initialSize" value="0" />
  21. <!-- 连接池最大使用连接数量 -->
  22. <property name="maxActive" value="20" />
  23. <!-- 连接池最大空闲 -->
  24. <property name="maxIdle" value="20" />
  25. <!-- 连接池最小空闲 -->
  26. <property name="minIdle" value="0" />
  27. <!-- 获取连接最大等待时间 -->
  28. <property name="maxWait" value="60000" />
  29. <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
  30. <property name="validationQuery" value="${validationQuery}" />
  31. <property name="testOnBorrow" value="false" />
  32. <property name="testOnReturn" value="false" />
  33. <property name="testWhileIdle" value="true" />
  34. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  35. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  36. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  37. <property name="minEvictableIdleTimeMillis" value="25200000" />
  38. <!-- 打开removeAbandoned功能 -->
  39. <property name="removeAbandoned" value="true" />
  40. <!-- 1800秒,也就是30分钟 -->
  41. <property name="removeAbandonedTimeout" value="1800" />
  42. <!-- 关闭abanded连接时输出错误日志 -->
  43. <property name="logAbandoned" value="true" />
  44. <!-- 监控数据库 -->
  45. <!-- <property name="filters" value="stat" /> -->
  46. <property name="filters" value="mergeStat" />
  47. </bean>
  48. <!-- myBatis文件 -->
  49. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  50. <property name="dataSource" ref="dataSource" />
  51. <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
  52. <property name="mapperLocations" value="classpath:wxd/mapping/*.xml" />
  53. </bean>
  54. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  55. <property name="basePackage" value="wxd.dao" />
  56. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  57. </bean>
  58. <!-- 配置事务管理器 -->
  59. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  60. <property name="dataSource" ref="dataSource" />
  61. </bean>
  62. <!-- 注解方式配置事物 -->
  63. <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
  64. <!-- 拦截器方式配置事物 -->
  65. <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
  66. <tx:attributes>
  67. <tx:method name="add*" propagation="REQUIRED" />
  68. <tx:method name="append*" propagation="REQUIRED" />
  69. <tx:method name="insert*" propagation="REQUIRED" />
  70. <tx:method name="save*" propagation="REQUIRED" />
  71. <tx:method name="update*" propagation="REQUIRED" />
  72. <tx:method name="modify*" propagation="REQUIRED" />
  73. <tx:method name="edit*" propagation="REQUIRED" />
  74. <tx:method name="delete*" propagation="REQUIRED" />
  75. <tx:method name="remove*" propagation="REQUIRED" />
  76. <tx:method name="repair" propagation="REQUIRED" />
  77. <tx:method name="delAndRepair" propagation="REQUIRED" />
  78. <tx:method name="get*" propagation="SUPPORTS" />
  79. <tx:method name="find*" propagation="SUPPORTS" />
  80. <tx:method name="load*" propagation="SUPPORTS" />
  81. <tx:method name="search*" propagation="SUPPORTS" />
  82. <tx:method name="datagrid*" propagation="SUPPORTS" />
  83. <tx:method name="*" propagation="SUPPORTS" />
  84. </tx:attributes>
  85. </tx:advice>
  86. <aop:config>
  87. <aop:pointcut id="transactionPointcut" expression="execution(* wxd.service..*Impl.*(..))" />
  88. <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
  89. </aop:config>
  90. <!-- 配置druid监控spring jdbc -->
  91. <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
  92. </bean>
  93. <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
  94. <property name="patterns">
  95. <list>
  96. <value>wxd.service.*</value>
  97. </list>
  98. </property>
  99. </bean>
  100. <aop:config>
  101. <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
  102. </aop:config>
  103. </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:tx="http://www.springframework.org/schema/tx" 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.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
"> <!-- 自动扫描(自动注入) -->
<context:component-scan base-package="wxd.service" /> <!-- JNDI方式配置数据源 -->
<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> --> <!-- 配置数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" /> <!-- 初始化连接大小 -->
<property name="initialSize" value="0" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20" />
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" /> <!-- 监控数据库 -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat" />
</bean> <!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
<property name="mapperLocations" value="classpath:wxd/mapping/*.xml" />
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="wxd.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 注解方式配置事物 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> --> <!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* wxd.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config> <!-- 配置druid监控spring jdbc -->
<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
</bean>
<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
<property name="patterns">
<list>
<value>wxd.service.*</value>
</list>
</property>
</bean>
<aop:config>
<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
</aop:config>
</beans>

5 整合springMVC,新建配置文件spring-mvc.xml 主要作用:1启动注解 2 自动扫描@controller  3模型视图解析,配置如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
  3. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  4. http://www.springframework.org/schema/context
  5. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  6. http://www.springframework.org/schema/mvc
  7. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  8. <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
  9. <context:component-scan base-package="wxd.controller" />
  10. <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
  11. <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
  12. <property name="supportedMediaTypes">
  13. <list>
  14. <value>text/html;charset=UTF-8</value>
  15. </list>
  16. </property>
  17. </bean>
  18. <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
  19. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  20. <property name="messageConverters">
  21. <list>
  22. <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
  23. </list>
  24. </property>
  25. </bean>
  26. <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
  27. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />
  28. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  29. <property name="defaultEncoding">
  30. <value>UTF-8</value>
  31. </property>
  32. <property name="maxUploadSize">
  33. <value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
  34. </property>
  35. <property name="maxInMemorySize">
  36. <value>4096</value>
  37. </property>
  38. </bean>
  39. </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 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认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="wxd.controller" /> <!-- 避免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> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
</beans>

6 配置spring.xml 作用:扫描注入service

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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" xsi:schemaLocation="
  3. http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  5. http://www.springframework.org/schema/context
  6. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  7. ">
  8. <!-- 引入属性文件 -->
  9. <context:property-placeholder location="classpath:config.properties" />
  10. <!-- 自动扫描(自动注入) -->
  11. <context:component-scan base-package="wxd.service" />
  12. </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: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
">
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:config.properties" />
<!-- 自动扫描(自动注入) -->
<context:component-scan base-package="wxd.service" />
</beans>

7 配置web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns: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_3_0.xsd" id="WebApp_ID" version="3.0">
  3. <display-name>mybatis</display-name>
  4. <context-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
  7. </context-param>
  8. <filter>
  9. <description>字符集过滤器</description>
  10. <filter-name>encodingFilter</filter-name>
  11. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  12. <init-param>
  13. <description>字符集编码</description>
  14. <param-name>encoding</param-name>
  15. <param-value>UTF-8</param-value>
  16. </init-param>
  17. </filter>
  18. <filter-mapping>
  19. <filter-name>encodingFilter</filter-name>
  20. <url-pattern>/*</url-pattern>
  21. </filter-mapping>
  22. <listener>
  23. <description>spring监听器</description>
  24. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  25. </listener>
  26. <!-- 防止spring内存溢出监听器 -->
  27. <listener>
  28. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  29. </listener>
  30. <!-- spring mvc servlet -->
  31. <servlet>
  32. <description>spring mvc servlet</description>
  33. <servlet-name>springMvc</servlet-name>
  34. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  35. <init-param>
  36. <description>spring mvc 配置文件</description>
  37. <param-name>contextConfigLocation</param-name>
  38. <param-value>classpath:spring-mvc.xml</param-value>
  39. </init-param>
  40. <load-on-startup>1</load-on-startup>
  41. </servlet>
  42. <servlet-mapping>
  43. <servlet-name>springMvc</servlet-name>
  44. <url-pattern>*.do</url-pattern>
  45. </servlet-mapping>
  46. <welcome-file-list>
  47. <welcome-file>/index.jsp</welcome-file>
  48. </welcome-file-list>
  49. <!-- 配置session超时时间,单位分钟 -->
  50. <session-config>
  51. <session-timeout>15</session-timeout>
  52. </session-config>
  53. </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns: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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>mybatis</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
</context-param>
<filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description>字符集编码</description>
<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>
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- spring mvc servlet -->
<servlet>
<description>spring mvc servlet</description>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置session超时时间,单位分钟 -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>

8 log4j配置

为了方便调试,一般都会使用日志来输出信息,Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。

配置详细说明

http://blog.csdn.net/zhshulin/article/details/37937365

通用配置如下:

  1. </pre><pre name="code" class="html">#定义LOG输出级别
  2. log4j.rootLogger=DEBUG,Console,File
  3. #定义日志输出目的地为控制台
  4. log4j.appender.Console=org.apache.log4j.ConsoleAppender
  5. log4j.appender.Console.Target=System.out
  6. #可以灵活地指定日志输出格式,下面一行是指定具体的格式
  7. log4j.appender.Console.layout = org.apache.log4j.PatternLayout
  8. log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
  9. #文件大小到达指定尺寸的时候产生一个新的文件
  10. log4j.appender.File = org.apache.log4j.RollingFileAppender
  11. #指定输出目录
  12. log4j.appender.File.File = logs/ssm.log
  13. #定义文件最大大小
  14. log4j.appender.File.MaxFileSize = 10MB
  15. # 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
  16. log4j.appender.File.Threshold = ALL
  17. log4j.appender.File.layout = org.apache.log4j.PatternLayout
  18. log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
</pre><pre name="code" class="html">#定义LOG输出级别
log4j.rootLogger=DEBUG,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n #文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

9 mysql安装 代码生成

1 下载 mysql-5.5.20-winx64.msi,

2 下载Navicat_for_MySQL.exe 并配置链接,建测试表user

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(40) DEFAULT NULL,
  `passwd` varchar(9) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

3 按照http://blog.csdn.net/zhshulin/article/details/23912615操作用mybatis-generator-core-1.3.2自动创建代码

10 junit测试

建立完测试类后要执行maven test 编译java文件

问题:

maven instal时报错,找不到wxd/mapping/路径,把mapping文件夹拷贝入workspace该项目的target下的wxd目录下编译即可通过

参考http://blog.csdn.net/zhshulin/article/details/37956105

常见问题

1 测试时报*.xml不存在,或者一些类没有定义,在pom.xml中增加以下配置

<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>

原因:

maven默认会把src/main/resources下的所有配置文件以及src/main/java下的所有java文件打包或发布到target\classes下面,但是现实我们可能会在src/main/java下面也放置一些配置文件如hibernate配置文件或mybatis mapper配置文件等,如果不做一些额外配置,那我们打包后的项目可能找不到这些必须的资源文件,因此在pom.xml中增加类似如下配置,就会将src/main/java下的xml文件随同java编译后的class文件一同copy到相应的class目录

2 测试时报:java.lang.Exception: No tests found matching Method test2(test.TestMyBatis)

原因是junit 加的@Test没有生效,原因不明,过一段时间就生效了,修改代码后也是等一会才能生效

3 maven install时报错 class path resource [spring.xml] cannot be opened because it does not exist

上一篇:OpenGL之shader着色器的应用,三色渐变的三角形


下一篇:Revit API 加载族并生成实例图元