idea搭建Springweb專案
阿新 • • 發佈:2018-12-26
1、File–>new–>Project
2、填寫專案名稱
3、選擇maven版本
4、選擇了專案儲存路徑後,點選finash完成專案建立
5、導包spring-webmvc4.3.9.RELEASE,jackson-annotations2.8.5,jackson-core2.8.5,jackson-databind 2.8.5,mybatis3.4.5,mybatis-spring1.3.1,spring-jdbc4.3.10.RELEASE,commons-dbcp1.4,junit4.12, mysql-connnector-java5.1.6
6、上一步包匯入完成後,後面測試還少一個包,先匯入進來
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
7、新建4個資料夾
8、設定新資料夾的作用
最後點選apply ok確認
9、配置Tomcat
10、Tomcat啟動後可以看到響應的網頁資訊
11、新建wfc資料夾和jdbc.properties spring-mvc.xml兩個配置檔案
12、jdbc中的配置為
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/cloud_note
user=root
password=123456
maxActive=20
13、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:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
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/util http://www.springframework.org/schema/util/spring-util-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="wfc"/>
<mvc:annotation-driven/>
</beans>
14、在wfc目錄下新建dao、controller、service、entity等目錄。在resources下新建目錄mapper並在mapper裡面新建userMapper.xml檔案。修改spring-mvc.xml檔案,增加如下內容
<util:properties id="jdbc" location="classpath:conf/jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="#{jdbc.driver}"/>
<property name="url" value="#{jdbc.url}"/>
<property name="username" value="#{jdbc.user}"/>
<property name="password" value="#{jdbc.password}"/>
<property name="maxActive" value="#{jdbc.maxActive}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="wfc.dao"/>
</bean>
15、在dao資料夾下新建UserDao介面,在userMapper.xml中配置如下內容
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="wfc.dao.UserDao">
</mapper>
16、配置DispatcherServlet增加如下內容
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
17、至此,框架基本搭建完成,可以進行web專案的開發。不過沒有配置檢視解析器,因為我自己的專案中暫時還沒有用到。用到後可以在配置