前言
自己老是忘記框架搭建的基本設(shè)置垦写,這次記下筆記。注:本人用的IDEA 2016彰触。
搭建步驟
一梯投、創(chuàng)建Maven項目
具體步驟如下圖:
先NEW一個project。
然后選擇Maven况毅,選擇你的JDK版本分蓖,勾選
Create from archetype
,選擇maven-archetype-webapp
尔许,點next么鹤。輸入
GroupId
,ArtifactId
味廊,點next蒸甜。選擇你的Maven版本,點next
輸入你的Project名稱和路徑然后點finish完成創(chuàng)建毡们。
二迅皇、導(dǎo)入相關(guān)的jar包
pom.xml相關(guān)設(shè)置
先設(shè)置各種jar包的版本號
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>4.0.2.RELEASE</spring.version>
<mybatis.version>3.2.6</mybatis.version>
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.15</log4j.version>
</properties>
然和導(dǎo)入spring相關(guān)的核心包
<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>
<scope>test</scope>
</dependency>
再導(dǎo)入mybais的相關(guān)包
<!-- mybatis 核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- spring 整合mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
最后導(dǎo)入其他的工具包
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.0</version>
</dependency>
<!-- JSTL標簽庫 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志管理文件 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上傳組件相關(guān)包 -->
<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.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
三、書寫ApplicationContext.xml(spring的配置文件)
首先去掃描包
<context:component-scan base-package="com.ssm.song.service"/>
然后去配置數(shù)據(jù)源
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssmdemo"/>
<property name="user" value="root"/>
<property name="password" value="song2"/>
</bean>
和mybatis的整合(注入sqlSessionFactory)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入數(shù)據(jù)源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 指出mapper.xml 文件路徑 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!-- 別名的包 -->
<property name="typeAliasesPackage" value="com.ssm.song.bean"/>
</bean>
配置Mapper掃描器和事務(wù)管理器
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"/>
<property name="basePackage" value="com.ssm.song.dao"/>
</bean>
<!-- 配置事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
最后開啟注解事務(wù)
<tx:annotation-driven transaction-manager="transactionManager"/>
四衙熔、書寫spring-mvc.xml(springMVC的配置文件)
<!-- 配置掃描的包 -->
<!-- spring mvc 只需掃描 controller -->
<context:component-scan base-package="com.ssm.song.controller"/>
<!-- 配置注解驅(qū)動 -->
<mvc:annotation-driven/>
<!-- 配置靜態(tài)資源訪問:js登颓,css等 -->
<mvc:resources mapping="/static/" location="/static/**"/>
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"/>
<property name="prefix" value="/WEB-INF/views/"/>
</bean>
五、書寫web.xml
主要是將spring和springMVC的配置文件加入web.xml中
集成spring:
<!-- 集成 spring 配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- web容器啟動完成之后红氯,立即初始化spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
集成springMVC:
<!-- 配置Spring MVC 前端控制器 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定SpringMVC的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
在這加上一個編碼過濾器防止亂碼:
<!-- 配置編碼過濾器 : 來自spring-web 包-->
<filter>
<filter-name>CharacterEncodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
六框咙、添加Tomcat服務(wù)器
具體步驟如下:
點擊右上角的 EditConfiguration:
選擇Tomcat:
在這輸入服務(wù)器名稱。即第一行的Name屬性:
再點擊Deployment痢甘,再點擊+號選擇第一個:
選擇第一個喇嘱,點擊OK,完成配置塞栅。
Tomcat配置好了以后者铜,點擊run跑一下,如果能看到頁面且控制臺沒有報錯就算是配置好了。
七作烟、后記
在搭建框架過程會遇到很多麻煩的事情愉粤,例如導(dǎo)入的包起沖突等等。這個時候要耐心的去檢查拿撩。百度是一個解決BUG的好方法衣厘。切記在書寫配置文件的時候要檢查包頭。