码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-07 01:36:36      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

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.     <!-- 自动扫描(自动注入) -->  
  12.     <context:component-scan base-package="wxd.service" />  
  13.   
  14.     <!-- JNDI方式配置数据源 -->  
  15.     <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->  
  16.   
  17.   
  18.     <!-- 配置数据源 -->  
  19.     <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  20.         <property name="url" value="${jdbc_url}" />  
  21.         <property name="username" value="${jdbc_username}" />  
  22.         <property name="password" value="${jdbc_password}" />  
  23.   
  24.   
  25.         <!-- 初始化连接大小 -->  
  26.         <property name="initialSize" value="0" />  
  27.         <!-- 连接池最大使用连接数量 -->  
  28.         <property name="maxActive" value="20" />  
  29.         <!-- 连接池最大空闲 -->  
  30.         <property name="maxIdle" value="20" />  
  31.         <!-- 连接池最小空闲 -->  
  32.         <property name="minIdle" value="0" />  
  33.         <!-- 获取连接最大等待时间 -->  
  34.         <property name="maxWait" value="60000" />  
  35.   
  36.   
  37.         <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->  
  38.   
  39.   
  40.         <property name="validationQuery" value="${validationQuery}" />  
  41.         <property name="testOnBorrow" value="false" />  
  42.         <property name="testOnReturn" value="false" />  
  43.         <property name="testWhileIdle" value="true" />  
  44.   
  45.   
  46.         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
  47.         <property name="timeBetweenEvictionRunsMillis" value="60000" />  
  48.         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
  49.         <property name="minEvictableIdleTimeMillis" value="25200000" />  
  50.   
  51.   
  52.         <!-- 打开removeAbandoned功能 -->  
  53.         <property name="removeAbandoned" value="true" />  
  54.         <!-- 1800秒,也就是30分钟 -->  
  55.         <property name="removeAbandonedTimeout" value="1800" />  
  56.         <!-- 关闭abanded连接时输出错误日志 -->  
  57.         <property name="logAbandoned" value="true" />  
  58.   
  59.   
  60.         <!-- 监控数据库 -->  
  61.         <!-- <property name="filters" value="stat" /> -->  
  62.         <property name="filters" value="mergeStat" />  
  63.     </bean>  
  64.   
  65.   
  66.     <!-- myBatis文件 -->  
  67.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  68.         <property name="dataSource" ref="dataSource" />  
  69.         <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->  
  70.         <property name="mapperLocations" value="classpath:wxd/mapping/*.xml" />  
  71.     </bean>  
  72.   
  73.   
  74.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  75.         <property name="basePackage" value="wxd.dao" />  
  76.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />  
  77.     </bean>  
  78.   
  79.   
  80.     <!-- 配置事务管理器 -->  
  81.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  82.         <property name="dataSource" ref="dataSource" />  
  83.     </bean>  
  84.   
  85.   
  86.     <!-- 注解方式配置事物 -->  
  87.     <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->  
  88.   
  89.   
  90.     <!-- 拦截器方式配置事物 -->  
  91.     <tx:advice id="transactionAdvice" transaction-manager="transactionManager">  
  92.         <tx:attributes>  
  93.             <tx:method name="add*" propagation="REQUIRED" />  
  94.             <tx:method name="append*" propagation="REQUIRED" />  
  95.             <tx:method name="insert*" propagation="REQUIRED" />  
  96.             <tx:method name="save*" propagation="REQUIRED" />  
  97.             <tx:method name="update*" propagation="REQUIRED" />  
  98.             <tx:method name="modify*" propagation="REQUIRED" />  
  99.             <tx:method name="edit*" propagation="REQUIRED" />  
  100.             <tx:method name="delete*" propagation="REQUIRED" />  
  101.             <tx:method name="remove*" propagation="REQUIRED" />  
  102.             <tx:method name="repair" propagation="REQUIRED" />  
  103.             <tx:method name="delAndRepair" propagation="REQUIRED" />  
  104.   
  105.   
  106.             <tx:method name="get*" propagation="SUPPORTS" />  
  107.             <tx:method name="find*" propagation="SUPPORTS" />  
  108.             <tx:method name="load*" propagation="SUPPORTS" />  
  109.             <tx:method name="search*" propagation="SUPPORTS" />  
  110.             <tx:method name="datagrid*" propagation="SUPPORTS" />  
  111.   
  112.   
  113.             <tx:method name="*" propagation="SUPPORTS" />  
  114.         </tx:attributes>  
  115.     </tx:advice>  
  116.     <aop:config>  
  117.         <aop:pointcut id="transactionPointcut" expression="execution(* wxd.service..*Impl.*(..))" />  
  118.         <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />  
  119.     </aop:config>  
  120.   
  121.   
  122.   
  123.   
  124.     <!-- 配置druid监控spring jdbc -->  
  125.     <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">  
  126.     </bean>  
  127.     <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">  
  128.         <property name="patterns">  
  129.             <list>  
  130.                 <value>wxd.service.*</value>  
  131.             </list>  
  132.         </property>  
  133.     </bean>  
  134.     <aop:config>  
  135.         <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />  
  136.     </aop:config>  
  137. </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.   
  9.   
  10.      <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->    
  11.     <context:component-scan base-package="wxd.controller" />  
  12.   
  13.   
  14.     <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->  
  15.     <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
  16.         <property name="supportedMediaTypes">  
  17.             <list>  
  18.                 <value>text/html;charset=UTF-8</value>  
  19.             </list>  
  20.         </property>  
  21.     </bean>  
  22.   
  23.   
  24.     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->  
  25.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  26.         <property name="messageConverters">  
  27.             <list>  
  28.                 <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->  
  29.             </list>  
  30.         </property>  
  31.     </bean>  
  32.   
  33.   
  34.     <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->  
  35.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />  
  36.   
  37.   
  38.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  39.         <property name="defaultEncoding">  
  40.             <value>UTF-8</value>  
  41.         </property>  
  42.         <property name="maxUploadSize">  
  43.             <value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->  
  44.         </property>  
  45.         <property name="maxInMemorySize">  
  46.             <value>4096</value>  
  47.         </property>  
  48.     </bean>  
  49. </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. #文件大小到达指定尺寸的时候产生一个新的文件    
  11. log4j.appender.File = org.apache.log4j.RollingFileAppender    
  12. #指定输出目录    
  13. log4j.appender.File.File = logs/ssm.log    
  14. #定义文件最大大小    
  15. log4j.appender.File.MaxFileSize = 10MB    
  16. # 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志    
  17. log4j.appender.File.Threshold = ALL    
  18. log4j.appender.File.layout = org.apache.log4j.PatternLayout    
  19. 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

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

标签:

原文地址:http://www.cnblogs.com/ZhuRenWang/p/4709547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!