你的位置:首页 > Java教程

[Java教程]java整合SSM框架

 

    • 使用Myeclipse搭建maven项目 

    1. 准备工作

      安装maven

  官网下载安装(        配置环境变量

             

      配置完后,使用命令行输入mvn -version查看是否配置成功,出现以下界面表示配置成功。

          

 

 

    2. 在MyEclipse中配置maven

        打开MyEclipse2015————点击菜单栏Window———选项栏Preference ,搜索Maven,进入Installations,点击Add引入安装的maven。

        

        进入User Setting,设置Global Setting,选中安装的maven的settings.

        

 


      

    3. 创建项目(检测是否配置成功,这里创建的是web项目)

        

        点击项目右键->Run as->Maven install将依赖install至本地maven库,有可能会报:

        -Dmaven.multiModuleProjectDirectorysystem propery is not set. Check $M2_HOME environment variable and mvn scriptmatch.,

        表示JDK版本和maven有冲突,更改一下jdk版本,并且加上-Dmaven.multiModuleProjectDirectory=$M2_HOME即可;

        如运行过程中控制台如下图所示,表示成功。

        

 

 

 

        

    • 在maven项目中搭建spring+springMVC+MyBatis

      修改pom.

          

<!-- servlet -->  <dependency>   <groupId>javax.servlet</groupId>   <artifactId>javax.servlet-api</artifactId>   <version>3.0.1</version>   <scope>compile</scope>  </dependency>  <dependency>   <groupId>jstl</groupId>   <artifactId>jstl</artifactId>   <version>1.2</version>  </dependency>  <!-- 数据库连接池 -->  <dependency>   <groupId>c3p0</groupId>   <artifactId>c3p0</artifactId>   <version>0.9.1</version>  </dependency>    <!-- 阿里巴巴druid连接池 -->  <dependency>   <groupId>com.alibaba</groupId>   <artifactId>druid</artifactId>   <version>1.0.25</version>   <exclusions>    <exclusion>     <groupId>com.alibaba</groupId>     <artifactId>jconsole</artifactId>    </exclusion>    <exclusion>     <groupId>com.alibaba</groupId>     <artifactId>tools</artifactId>    </exclusion>   </exclusions>   </dependency>  <!-- 1、先添加spring相关的包 -->  <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.7.RELEASE</version></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.7.RELEASE</version></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-tx</artifactId>  <version>4.1.7.RELEASE</version> </dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.7.RELEASE</version></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.7.RELEASE</version></dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.7.RELEASE</version></dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.7.RELEASE</version></dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.7.RELEASE</version></dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.1.7.RELEASE</version></dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.7.RELEASE</version></dependency>  <!--aspectjweaver包:面向切面要用到的包 -->  <dependency>   <groupId>org.aspectj</groupId>   <artifactId>aspectjweaver</artifactId>   <version>1.8.5</version>  </dependency>    <!--4、单元测试相关的包 -->  <dependency>   <groupId>junit</groupId>   <artifactId>junit</artifactId>   <version>4.12</version>  </dependency>  <!-- 2、mybatis -->  <dependency>   <groupId>org.mybatis</groupId>   <artifactId>mybatis</artifactId>   <version>3.4.1</version>  </dependency>    <!-- 日志 -->  <dependency>   <groupId>log4j</groupId>   <artifactId>log4j</artifactId>   <version>1.2.17</version>  </dependency>  <!-- 整合插件包 -->  <dependency>   <groupId>org.mybatis</groupId>   <artifactId>mybatis-spring</artifactId>   <version>1.3.0</version>  </dependency>  <!-- mysql驱动 -->  <dependency>   <groupId>mysql</groupId>   <artifactId>mysql-connector-java</artifactId>   <version>5.1.38</version>  </dependency>  <dependency>   <groupId>com.mchange</groupId>   <artifactId>mchange-commons-java</artifactId>   <version>0.2.10</version>  </dependency>  <!-- 工具包 -->  <dependency>   <groupId>commons-codec</groupId>   <artifactId>commons-codec</artifactId>   <version>1.10</version>  </dependency>    <!-- 修改pom.

 

 

      修改web.
<welcome-file-list>  <welcome-file>index.html</welcome-file>  <welcome-file>index.htm</welcome-file>  <welcome-file>index.jsp</welcome-file>  <welcome-file>default.html</welcome-file>  <welcome-file>default.htm</welcome-file>  <welcome-file>default.jsp</welcome-file> </welcome-file-list>  <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <!-- 加入spring -->  <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:applicationContext*.class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <!-- 配置Spring mvc下的配置文件的位置和名称 -->  <init-param>   <param-name>contextConfigLocation</param-name>   <param-value>classpath:springmvc.修改web.

 

      创建并配置applicationContext.

        

<? ="http://www.w3.org/2001/ ="http://www.springframework.org/schema/tx"  ="http://www.springframework.org/schema/aop" ="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/aop  http://www.springframework.org/schema/beans //cxf.apache.org/jaxws //www.springframework.org/schema/tx //www.springframework.org/schema/context "> <!-- 扫描 --> <context:component-scan base-package="cn.ft.mapper,cn.ft.service.impl,cn.ft.service.rest.impl"></context:component-scan> <!-- 导入db.properties --> <context:property-placeholder location="classpath:db.properties" /> <!-- 先配置数据源:c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  <property name="driverClass" value="${driverClassName}"></property>  <property name="jdbcUrl" value="${jdbc_url}"></property>  <property name="user" value="${jdbc_username}"></property>  <property name="password" value="${jdbc_password}"></property>   <property name="testConnectionOnCheckin" value="false"/>  <property name="testConnectionOnCheckout" value="true"/> </bean> <bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  <property name="dataSource" ref="dataSource"></property>  <!-- 加载mybatis.cfg.class="org.mybatis.spring.mapper.MapperScannerConfigurer">  <property name="basePackage" value="cn.ft.mapper"></property> </bean> <!-- 事务的配置 --> <bean id="txManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  <property name="dataSource" ref="dataSource" /> </bean> <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="txManager">  <tx:attributes>   <!-- 以如下关键字开头的方法使用事物 -->   <tx:method name="add*" 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="buyfood" propagation="REQUIRED" />   <tx:method name="order" propagation="REQUIRED" />   <!-- 以如下关键字开头的方法不使用事物 -->   <tx:method name="get*" read-only="true" />   <tx:method name="find*" read-only="true" />   <tx:method name="load*" read-only="true" />   <tx:method name="query*" read-only="true" />   <!-- 其他方法不使用事物 -->   <tx:method name="*" propagation="SUPPORTS" />  </tx:attributes> </tx:advice> <!-- 切面,将事物用在哪些对象上 --> <aop:config>  <aop:pointcut id="transactionPointcut"   expression="execution(* cn.ft.service.impl.*Impl.*(..))" />  <aop:advisor pointcut-ref="transactionPointcut"   advice-ref="transactionAdvice" /> </aop:config></beans>
applicationContext.

 

      创建并配置springmvc.
<? ="http://www.w3.org/2001/ ="http://www.springframework.org/schema/p" ="http://www.springframework.org/schema/context" ="http://www.springframework.org/schema/aop" ="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/aop  http://www.springframework.org/schema/mvc //www.springframework.org/schema/beans //www.springframework.org/schema/context "> <!-- 扫描 --> <context:component-scan base-package="cn.ft.controller"></context:component-scan> <!-- 视图解析器 --> <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <property name="prefix" value="/"></property>  <property name="suffix" value=".jsp"></property> </bean>       <bean  class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  <property name="exceptionMappings">   <props>    <prop key="org.apache.shiro.authz.UnauthorizedException">     /refuse    </prop>   </props>  </property> </bean>  <mvc:annotation-driven>    <mvc:message-converters>     <bean class="org.springframework.http.converter.StringHttpMessageConverter">      <property name="supportedMediaTypes">       <list>        <value>application/json;charset=UTF-8</value>       </list>      </property>     </bean>    </mvc:message-converters>   </mvc:annotation-driven> <!-- Ajax中文乱码暂时无效<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">   <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.StringHttpMessageConverter">   <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value>   </list> </property> </bean> </list> </property> </bean> --></beans>
springmvc.

 

      配置数据源db.properties

driverClassName=com.mysql.jdbc.DrivervalidationQuery=SELECT 1jdbc_url=jdbc\:mysql\://localhost\:3306/ft?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNulljdbc_username=rootjdbc_password=8888
db.properties

 

      配置mybatis-config.xm

<?DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <settings>  <setting name="cacheEnabled" value="true"/> </settings> <typeAliases>  <!--  <typeAlias type="cn.hd.model.Userbean" alias="userbean"/>  -->   <!-- 批量设置别名:别名就是类名(把第一个字母改小写) --> <package name="cn.ft.model"/> </typeAliases>  </configuration>
mybatis-config.

 

      配置log4j.properties

log4j.rootCategory=DEBUG, stdout , R log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n log4j.appender.R=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.R.File=D\:\\Tomcat 5.5\\logs\\qc.loglog4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
View Code