欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在eclipse中使用spring,springmvc,mybatis搭建web项目

程序员文章站 2022-05-11 14:28:50
前不久刚学过ssm框架的使用,在此记录下来,如果内容有误欢迎指正! 这篇文章只展示我建立项目的过程,至于各种配置这么写的原因在今后的时间里我会为Spring,Springmvc,mybatis各写一篇文章进行说明! 创建一个web项目 我使用的是eclipse,相信大多数新手都和我一样吧,新建web ......

前不久刚学过ssm框架的使用,在此记录下来,如果内容有误欢迎指正!

这篇文章只展示我建立项目的过程,至于各种配置这么写的原因在今后的时间里我会为spring,springmvc,mybatis各写一篇文章进行说明!

创建一个web项目

我使用的是eclipse,相信大多数新手都和我一样吧,新建web项目就不说了,各位应该都如喝水吃饭一样熟练了。

导入项目需要的jar包

我是在eclipse中建立的maven项目,因此在pom.xml中写导入jar包的配置,maven自行下载jar包,目前只会这一种导入jar包的方式。

 

 

  <dependencies>

        <!-- spring mvc -->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-webmvc</artifactid>
            <version>4.3.9.release</version>
        </dependency>

        <!-- spring jdbc -->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-jdbc</artifactid>
            <version>4.3.9.release</version>
        </dependency>

        <!-- 单元测试 -->
        <dependency>
            <groupid>junit</groupid>
            <artifactid>junit</artifactid>
            <version>4.12</version>
        </dependency>

        <!-- mybatis -->
        <dependency>
            <groupid>org.mybatis</groupid>
            <artifactid>mybatis</artifactid>
            <version>3.4.6</version>
        </dependency>

        <!-- mybatis整合spring -->
        <dependency>
            <groupid>org.mybatis</groupid>
            <artifactid>mybatis-spring</artifactid>
            <version>1.3.2</version>
        </dependency>

        <!-- 数据源/数据库连接池 -->
        <dependency>
            <groupid>commons-dbcp</groupid>
            <artifactid>commons-dbcp</artifactid>
            <version>1.4</version>
        </dependency>

        <!-- mysql数据库连接驱动 -->
        <dependency>
            <groupid>mysql</groupid>
            <artifactid>mysql-connector-java</artifactid>
            <version>5.1.8</version>
        </dependency>

        <!-- jackson -->
        <dependency>
            <groupid>com.fasterxml.jackson.core</groupid>
            <artifactid>jackson-databind</artifactid>
            <version>2.9.7</version>
        </dependency>

        <!-- 消息摘要 -->
        <dependency>
            <groupid>commons-codec</groupid>
            <artifactid>commons-codec</artifactid>
            <version>1.10</version>
        </dependency>

        <!-- 文件上传 -->
        <dependency>
            <groupid>commons-fileupload</groupid>
            <artifactid>commons-fileupload</artifactid>
            <version>1.3.3</version>
        </dependency>

    </dependencies>

编写web.xml配置文件

打开src/main/webapp/web-inf文件夹下的web.xml文件,在其中主要是配置dispatcherservlet,在其中添加如下配置:

 

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
    <init-param>
      <param-name>contextconfiglocation</param-name>
      <param-value>classpath:spring-*.xml</param-value> <!-- spring配置文件的路径,根据自己需要选择路径 -->
    </init-param>
    <load-on-startup>1</load-on-startup> <!-- 初始化优先级为1,使应用启动时就初始化dispatcherservlet -->
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.yc</url-pattern> <!-- 需要dispatcherservlet拦截的请求的路径 -->
  </servlet-mapping>

 

编写spring配置文件

我习惯将spring文件拆分为3个来写,分别是spring-mvc.xml,spring-service.xml,spring-dao.xml。

spring-mvc.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:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemalocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
    <!-- 组件扫描 -->
    <context:component-scan base-package="controller" /> <!-- controller所在的包路径 -->
        
    <!-- 配置视图解析器viewresolver -->
    <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
        <!-- 配置前缀 -->
        <property name="prefix" value="/web-inf/" />
        <!-- 配置后缀 -->
        <property name="suffix" value=".jsp" />
        <!-- 经过以上配置后, -->
        <!-- 当前项目中的jsp文件都应该放在/web-inf/下 -->
    </bean>
    
    <!-- commonsmultipartresolver 
        字符编码过滤器
    -->
    <bean id="multipartresolver"
        class="org.springframework.web.multipart.commons.commonsmultipartresolver">
        <property name="defaultencoding" 
            value="utf-8" />
    </bean>
    
    <!-- 注解驱动 -->
    <mvc:annotation-driven />
    
    
    
</beans>

spring-service.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:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemalocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
    <!-- 组件扫描 -->
    <context:component-scan 
        base-package="serviceimpl" /> <!-- service所在的包路径 -->
    
    
</beans>

spring-dao.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:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemalocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
    <!-- 读取db.properties
      db.properties文件的内容在下面
--> <util:properties id="dbconfig" location="classpath:db.properties" /> <!-- 配置数据源 --> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"> <property name="url" value="#{dbconfig.url}" /> <property name="driverclassname" value="#{dbconfig.driver}" /> <property name="username" value="#{dbconfig.username}" /> <property name="password" value="#{dbconfig.password}" /> <property name="initialsize" value="#{dbconfig.initialsize}" /> <property name="maxactive" value="#{dbconfig.maxactive}" /> </bean> <!-- sqlsessionfactorybean --> <bean class="org.mybatis.spring.sqlsessionfactorybean"> <!-- 指定数据源,值为以上配置的数据源 --> <property name="datasource" ref="datasource" /> <!-- 指定xml映射文件的位置 --> <property name="mapperlocations" value="classpath:mappers/*.xml" /> </bean> <!-- mapperscannerconfigurer --> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <!-- 指定接口文件的位置 --> <property name="basepackage" value="mapper" /> </bean> </beans>

db.properties

url=jdbc:mysql://localhost:3306/epos?useunicode=true&characterencoding=utf8 #数据库的路径
driver=com.mysql.jdbc.driver #驱动
username=root #数据库登陆用户名
password=root #密码
initialsize=2 #初始连接数
maxactive=10 #最大连接数

经过如上配置,这个应用的基本配置就完成了,然后就可以根据配置的位置去写你的代码了!

 

 

============================================================分割线==================================================================

 

新人第一次写博客,回头一看写的很烂,但人都要有第一次嘛,以后会越来越好的,加油!!!