1. 程式人生 > >在eclipse中使用spring,springmvc,mybatis搭建web項目

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

jdb 初始化 lsi nco location www. 新人 tis 和我

前不久剛學過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 #最大連接數

經過如上配置,這個應用的基本配置就完成了,然後就可以根據配置的位置去寫你的代碼了!

============================================================分割線==================================================================

新人第一次寫博客,回頭一看寫的很爛,但人都要有第一次嘛,以後會越來越好的,加油!!!

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