maven项目搭建SSM框架

这是我第一次写博客。

之前已经搭建过多次,但实习期间没怎么写过。借着做毕设的时间,写一个这样的博客,一来是为了巩固知识,二是为了留这样一个参考物供自己回忆,三是也给有缘看到这篇博客的人一个参考。

我在写这篇博客的时候,以简洁明了的语言为主,会在括号里使用一些专业行话,无需太关注,博主认为需要讲解的地方会着重解释。

一、我们为什么要使用maven

    在我个人理解,maven实际上是一个为Java项目或者web项目提供便利性的软件项目管理工具。例如,对maven有一定了解的可以知道,在pom.xml写一段文字(配置)就可引入jar包,无需手动导入,十分方便。那么总的来说,我们使用maven的目的就是为了:节省开发精力,合理叙述项目间的依赖关系(依赖关系即jar包)。这就是我的浅显理解。

二、搭建maven项目的前提

    1.开发工具:eclispe、myeclispe等

    2.所需安装包: 1>maven安装包:为开发提供便捷方式的软件项目管理工具

                            2>jdk安装包

                            3>服务器安装包:博主用的是tomcat8.0

三、搭建maven项目

    1.在eclipse上new一个项目。

    点击next

   点击finish按钮,maven项目就建好了。

    注意:出现以下情况,是由于缺少javax.servlet-api依赖导致的


在pom.xml中加入以下内容

<dependency>

            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>

    </dependency>

四、导入jar包

    1.我们为什么要导入jar包呢

        如果你是一个已经使用过ssm框架开发的开发者,那么一定知道当我们要去用一个框架的时候,首先必然要导入一些所需jar包方可使用框架。在maven项目中,我们通过在pom.xml中写入一些依赖关系,pom.xml中依赖的jar包会自动实现从中央仓库下载到本地仓库,这样我们就可以愉快的使用ssm框架了。

   2.需要导入的jar包有哪些

    spring核心包,mybatis核心包,spring-mybatis核心包,还包括log4j,如下图:


    下面是我的pom.xml,可做参考

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.lyj</groupId>
  <artifactId>WirelessOrdering</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>WirelessOrdering Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>javax.servlet-api</artifactId>
         <version>3.1.0</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
    <scope>provided</scope>
</dependency>
    
   <!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
   
   <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.44</version>
</dependency>

   <!--spring 该包下载后还会连同下载所需要的包  -->
   <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.0.RELEASE</version>
</dependency>
   <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.0.RELEASE</version>
</dependency>
   <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.0.RELEASE</version>
</dependency>
<dependency>
	    <groupId>org.aspectj</groupId>
	    <artifactId>aspectjweaver</artifactId>
	    <version>1.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.3.0.RELEASE</version>
</dependency>

<!--Mybatis  -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.0</version>
</dependency>

<!--mybatis-spring  -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.0</version>
</dependency>

<!--Json  -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.0</version>
</dependency>

<!--文件的上传  -->
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>    
  </dependencies>
  <build>
    <finalName>WirelessOrdering</finalName>
  </build>
</project>

五、配置文件

    导入之后我们就该书写配置文件了

    1.web.xml

        1>springmvc的核心分发类

<!--springmvc的核心分发器  -->
  <servlet>
  	<servlet-name>springMVC</servlet-name>
    <!--想要使用springmvc,配置DispatcherServlet是第一步,框架会在web应用的 WEB-INF文件夹下寻找名为
   [servlet-name]-servlet.xml 的配置文件,生成文件中定义的bean-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <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>        2 >字符的编码自动转成utf-8
 <!-- 由spring框架提供的字符编码的过滤器,把所有的编码转换成UTF-8 -->
  <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>
        3>在tomcat启动时自动装载spring的配置文件
  <!-- 定义了一个全局的上下文参数,参数里定义了一个全局的Spring配置文件 -->
  <context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
<!--   定义了一个上下文监听器,当上下文加载的时候会自动的加载上面的applicationContext.xml -->
  <listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

    这样一来,web.xml就配置完成。为了方便理解,我们可以认为,当项目启动了,web.xml通过核心分发类联系了spring-mvc.xml,通过读取context参数加载applicationContext.xml,同时还可以利用过滤器转换编码。

    2.spring-mvc.xml

    (1)注册自动扫描带注解的包,可以解析该包的注解

    (2)注解驱动,会自动注册注解

    (3)定义静态资源访问映射路径

    (4)视图解析器

    (5)上传文件的解析器

<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<!--注解驱动会自动注册几个bean,其中一个功能是把对象和json进行转化;开启注解  -->
	<mvc:annotation-driven/>
	<!--标明可使用注解的包,包括其子集;自动扫描controller下的所有类,使其被认为spring mvc的控制器 -->
	<context:component-scan base-package="org.lyj.controller"></context:component-scan>
	<!--视图解析器  -->
	<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<!-- 定义静态资源文件映射路径 -->
	<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
	<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
	<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
	<!-- 上传文件解析器 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- one of the properties available; the maximum file size in bytes -->
		<property name="maxUploadSize" value="100000"/>
	</bean>
</beans>

    3.applicationContext.xml

        (1)  注册自动扫描的包

        (2)定义数据源

        (3)定义事务管理器

        (4)配置mybatis的sqlsessionFactory

        (5)定义dao接口所关联扫描的mapper

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<!--自动扫描与装配bean,扫描web包,将带有注解的类纳入spring容器管理  -->
<context:component-scan base-package="org.lyj.dao"></context:component-scan>
<context:component-scan base-package="org.lyj.service"></context:component-scan>

<!--加载jdbc属性文件到context  -->
<context:property-override location="classpath:jdbc.prop"/>
<!--使用dbcp数据源,用context读取属性文件里面的值  -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${jdbc.driverClassName}"></property>
	<property name="url" value="${jdbc.url}"></property>
	<property name="username" value="${jdbc.username}"></property>
	<property name="password" value="${jdbc.password}"></property>
</bean>

<!--jdbc事务管理器  -->
<bean id="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="insert" propagation="REQUIRED"/>	
		<tx:method name="update*" propagation="REQUIRED" />  
        <tx:method name="edit*" propagation="REQUIRED" />  
        <tx:method name="save*" propagation="REQUIRED" />  
        <tx:method name="add*" propagation="REQUIRED" />  
        <tx:method name="new*" propagation="REQUIRED" />  
        <tx:method name="set*" propagation="REQUIRED" />  
        <tx:method name="remove*" propagation="REQUIRED" />  
        <tx:method name="delete*" propagation="REQUIRED" />  
        <tx:method name="change*" propagation="REQUIRED" />  
        <tx:method name="start*" propagation="REQUIRED"/>
        <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="*" propagation="REQUIRED" read-only="true" /> 
	</tx:attributes>
</tx:advice>
<!--配置事务切面  -->
<aop:config>
	<!--配置切点  -->
	<aop:pointcut expression="execution(* org.lanqiao.service.*.*(..))" id="serviceOperation"/>
	<!--配置事务通知  -->
	<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
</aop:config>

<!--配置持久层mybatis的sqlSessionFactory  -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<!--自动扫描mappers  -->
	<property name="mapperLocations" value="classpath:org/lanqiao/mappers/*.xml"></property>
	<!--mybatis的配置文件  -->
	<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

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

</beans>

这样一来,我们通过spring-mvc联系了controller,applicationContext.xml通过spring联系了dao、service、mapper、mybatis

    4.log4j的配置文件


    5.jdbc.prop数据库配置文件


    6.mybatis-config.xml


之后就是简单的创建包名和文件夹了。

上述都是个人浅显的理解,有错可以指出,共同成长

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值