本人于2016年畢業(yè)璃赡,小白一枚判哥,經(jīng)驗(yàn)不足。各位看官如有問(wèn)題碉考,請(qǐng)百度塌计。小白我也是一步一步百度而來(lái)。小白QQ群571669420侯谁,靜請(qǐng)打擾锌仅;
一、準(zhǔn)備工具和環(huán)境
(1)PC一臺(tái)(系統(tǒng)不限良蒸,只要能運(yùn)行Java環(huán)境即可)技扼。
(2)eclisp(版本看個(gè)人喜好)。
(3)jar包若干(具體哪些jar包嫩痰,我也不知道,反正本人將spring所有jar包全部導(dǎo)入)窍箍。
二串纺、環(huán)境搭建
(1)eclisp新建web功能丽旅,命名看個(gè)人喜好。以下為本人創(chuàng)建工程目錄結(jié)構(gòu)纺棺,僅供參考榄笙;
(2)導(dǎo)入jar包。
(3)在web-inf目錄下的lib目錄下導(dǎo)入jar包祷蝌。
(4)同樣在web-inf下的web.xml中寫入配置文件茅撞。配置文件內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Matton</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml </param-value>
</context-param>
<welcome-file-list>
<welcome-file>/jsp/Reception/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ExcelServlet</display-name>
<servlet-name>ExcelServlet</servlet-name>
<servlet-class>com.matton.tools.ExcelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExcelServlet</servlet-name>
<url-pattern>/ExcelServlet</url-pattern>
</servlet-mapping>
</web-app>
(5)在項(xiàng)目工程下添加Spring配置文件,配置文件如下:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- 1)加載JDBC巨朦;配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- 2)定義數(shù)據(jù)源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化連接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 連接池最大數(shù)量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 連接池最大空閑 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 連接池最小空閑 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 獲取連接最大等待時(shí)間 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- 3)定義sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- DAO接口所在包名米丘,Spring會(huì)自動(dòng)查找其下的類 @controller -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.matton.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 掃描Service -->
<context:component-scan base-package="com.matton.service"></context:component-scan>
</beans>
(6)添加SpringMVC配置文件,配置文件如下:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- @Controller掃描 -->
<context:component-scan base-package="com.matton.Controller"></context:component-scan>
<!-- 默認(rèn)的注解映射的支持 -->
<!-- <mvc:annotation-driven /> -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="messageConverters">
<list>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!-- 視圖解析組件 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 上傳配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="2097152"></property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
</beans>
(7)添加mybaits配置文件糊啡,配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"></properties>
<!—為表結(jié)構(gòu)及實(shí)體床架別名 -->
<typeAliases>
<!-- 用戶表 -->
<typeAlias type="com.matton.model.LoginUserModel" alias="LoginUser"/>
</typeAliases>
<!—映射xml配置拄查,與數(shù)據(jù)庫(kù)交互 -->
<mappers>
<mapper resource="com/matton/mapper/LoginUserMapper.xml"/>
</mappers>
</configuration>
(8)配置數(shù)據(jù)庫(kù)連接,配置如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/matton?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
username=root
password=admin
initialSize=0
\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570
maxActive=20
\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2
maxIdle=20
\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2
minIdle=1
\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
maxWait=60000
(9)配置log4j棚蓄,配置如下:
log4j.rootLogger=debug, console,file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%5p [%t] - %m%n
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=E:/logs/log4j.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug,stdou
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss} [%t] %-5p %c(line-%L) %-4r %x - %m%n
og4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
(10)配置結(jié)束堕扶。