Maven+SSM整合.doc

Posted lilixia

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven+SSM整合.doc相关的知识,希望对你有一定的参考价值。

Maven + SSM整合

1开发环境搭建
1.1Eclipse4.7(Oxygen) + Tomcat8 + Maven3.5.2
2Maven Web项目创建
2.1新建maven项目

 2.2 选择工作空间

2.3 选择创建web项目

2.4 填写项目相关参数

2.5 完成后的目录结构图

2.6修改为maven约定目录

1、点击项目右键Properties -> Java Build Path ->Source选项

2、点击项目右键new->Source Folder

3、修改index.jsp的错误

pom.xml添加servlet的支持

4、最终目录结构图

2.7 设置 Properties -> Project Facets

选中3.0 可能会出错

 

解决方法: 修改该项目路径下的 .settings/ org.eclipse.wst.common.project.facet.core.xml 文件

关闭项目再重新打开

3SSM整合
3.1Spring和Mybatis的整合
3.1.1Maven引入需要的Jar包
1、本教程使用部分jar的版本:
       Spring: 5.0.2.RELEASE
       Spring MVC: 5.0.2.RELEASE
       MyBatis:  3.4.5
       mybatis-spring:  1.3.0
       mysql-connector-java: 5.1.2
       javax.servlet-api:3.1.0 
       commons-dbcp: 1.4
       commons-io:  2.5
       commons-codec: 1.10
2、pom.xml
<properties>  
       <!-- spring版本号 -->  
       <spring.version>5.0.2.RELEASE</spring.version>  
       <!-- mybatis版本号 -->  
       <mybatis.version>3.4.5</mybatis.version>  
       <!-- log4j日志文件管理包版本 -->  
       <slf4j.version>1.7.7</slf4j.version>  
       <log4j.version>1.2.17</log4j.version>  
 
 <dependencies>
       <dependency>
           <groupId>junit</groupId>

   </properties>
           <artifactId>junit</artifactId>
         <version>4.12</version>
           <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>javax.servlet-api</artifactId>
           <version>3.1.0</version>
       </dependency>
       <!-- spring核心包 -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-core</artifactId>
           <version>${spring.version}</version>
       </dependency>

       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-web</artifactId>
           <version>${spring.version}</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-oxm</artifactId>
           <version>${spring.version}</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-tx</artifactId>
           <version>${spring.version}</version>
       </dependency>

       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-jdbc</artifactId>
           <version>${spring.version}</version>
       </dependency>

       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-webmvc</artifactId>
           <version>${spring.version}</version>
       </dependency>
       <dependency>

          <groupId>org.springframework</groupId>
           <artifactId>spring-aop</artifactId>
           <version>${spring.version}</version>
       </dependency>

       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context-support</artifactId>
           <version>${spring.version}</version>
       </dependency>

       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-test</artifactId>
           <version>${spring.version}</version>
       </dependency>
       <!-- mybatis核心包 -->
       <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis</artifactId>
           <version>${mybatis.version}</version>
       </dependency>
       <!-- mybatis/spring包 -->
       <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis-spring</artifactId>
           <version>1.3.0</version>
       </dependency>
       <!-- 导入java ee jar 包 -->
       <dependency>
           <groupId>javax</groupId>
           <artifactId>javaee-api</artifactId>
           <version>7.0</version>
       </dependency>
       <!-- 导入Mysql数据库链接jar包 -->
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.2</version>
       </dependency>
       <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
       <dependency>
           <groupId>commons-dbcp</groupId>
           <artifactId>commons-dbcp</artifactId>


           <version>1.4</version>
       </dependency>
       <!-- JSTL标签类 -->
       <dependency>
           <groupId>jstl</groupId>
           <artifactId>jstl</artifactId>
           <version>1.2</version>
       </dependency>
       <!-- 日志文件管理包 -->
       <!-- log start -->
       <dependency>
           <groupId>log4j</groupId>
           <artifactId>log4j</artifactId>
           <version>${log4j.version}</version>
       </dependency>
       
       <!-- 格式化对象,方便输出日志 -->
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>fastjson</artifactId>
           <version>1.2.17</version>
       </dependency>

       <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-api</artifactId>
           <version>${slf4j.version}</version>
       </dependency>

       <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-log4j12</artifactId>
           <version>${slf4j.version}</version>
       </dependency>
       <!-- log end -->

       <!-- 映入JSON -->
       <dependency>
           <groupId>org.codehaus.jackson</groupId>
           <artifactId>jackson-mapper-asl</artifactId>
           <version>1.9.13</version>
       </dependency>
       <dependency>
           <groupId>com.fasterxml.jackson.core</groupId>

         <artifactId>jackson-core</artifactId>
           <version>2.9.2</version>
       </dependency>
       <dependency>
           <groupId>com.fasterxml.jackson.core</groupId>
           <artifactId>jackson-databind</artifactId>
           <version>2.9.2</version>
       </dependency>
       <dependency>
           <groupId>com.fasterxml.jackson.core</groupId>
           <artifactId>jackson-annotations</artifactId>
           <version>2.9.2</version>
       </dependency>

       <!-- 上传组件包 -->
       <dependency>
           <groupId>commons-fileupload</groupId>
           <artifactId>commons-fileupload</artifactId>
           <version>1.3.1</version>
       </dependency>
       <dependency>
           <groupId>commons-io</groupId>
           <artifactId>commons-io</artifactId>
           <version>2.5</version>
       </dependency>
       <dependency>
           <groupId>commons-codec</groupId>
           <artifactId>commons-codec</artifactId>
           <version>1.10</version>
       </dependency>
 </dependencies>

3.1.2 建立JDBC属性文件(文件编码修改为utf-8):

 

jdbc.driver_class=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssm
jdbc.username=root
jdbc.password=root
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000

 

3.1.3 建立spring-mybatis.xml配置文件

 

<?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:mybatis-spring="http://mybatis.org/schema/mybatis-spring" 
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="
    http://mybatis.org/schema/mybatis-spring 
    http://mybatis.org/schema/mybatis-spring-1.3.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc.xsd

   http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 支持注解 -->
    <context:annotation-config></context:annotation-config>
        <!--自动扫描 :通过注解过滤被扫描的类 -->  
        <context:component-scan base-package="com.bw" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>
    
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver_class}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->

        <property name="mapperLocations" value="classpath*:com/bw/mapper/*.xml"></property>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.bw.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

 

3.1.4JUnit测试Mybatis
3.1.4.1创建测试用表
DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(40) NOT NULL,
  `password` VAR

以上是关于Maven+SSM整合.doc的主要内容,如果未能解决你的问题,请参考以下文章

ssm整合简单例子

IDEA+SSM+Maven实现商品管理系统(超详细SSM整合项目)

ssm+maven 框架整合

用Maven整合SSM项目实例

ssm 整合(方案二 maven)

idea使用maven整合ssm框架