要完成一个项目,首先要做的就是需求分析,我们的需求十分明确,就是要做一个供我校大学生高效复习英语的系统,日后可能会有新的需求,到时会再次更新需求模块
既然是采用框架开发,项目第一步就是搭建环境,本系统没有用到maven,所以需要先导入相应jar包及创建符合规定的package,在config里进行相应xml文件的配置,具体配置如下
首先配置mybatis-config.xml
<?xmlversion="1.0" encoding="UTF-8" ?>
<!DOCTYPEconfiguration (View Source for full doctype...)>
- <configuration>
- <!-- 实体类,简称 -设置别名
-->
- <typeAliases>
<typeAliastype="com.kfzx.model.Cloze" alias="Cloze" />
<typeAliastype="com.kfzx.model.User" alias="User" />
</typeAliases>
- <!-- 实体接口映射资源
-->
- <!-- 说明:如果xxMapper.xml配置文件放在和xxMapper.java统一目录下,mappers也可以省略,因为org.mybatis.spring.mapper.MapperFactoryBean默认会去查找与xxMapper.java相同目录和名称的xxMapper.xml
-->
- <mappers>
<mapperresource="com/kfzx/mapper/EnglishMapper.xml" />
<mapperresource="com/kfzx/mapper/UserMapper.xml" />
</mappers>
</configuration>
然后配置spring-mvc.xml
<?xmlversion="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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
- <!-- 注解扫描包
-->
<context:component-scan base-package="com.kfzx" />
- <!-- 开启注解
-->
<mvc:annotation-driven />
- <!-- <mvc:interceptors>
<mvc:interceptor>
<mvc:mappingpath="/*"/>
<beanclass="com.kfzx.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
-->
- <!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd
-->
<mvc:resourcesmapping="/images/**" location="/images/" />
<mvc:resourcesmapping="/js/**" location="/js/" />
<mvc:resourcesmapping="/css/**" location="/css/" />
<mvc:resourcesmapping="/html/**" location="/html/" />
<mvc:resourcesmapping="/file/**" location="/file/" />
- <bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <!-- maxUploadSize:文件上传的最大值以byte为单位
-->
<propertyname="maxUploadSize" value="1024000" />
</bean>
</beans>
最后配置spring-common.xml
<?xmlversion="1.0" encoding="UTF-8" ?>
- <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
- <!-- 1. 数据源 : DriverManagerDataSource
-->
- <bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName" value="com.mysql.jdbc.Driver" />
<propertyname="url" value="jdbc:mysql://localhost:3306/englishcloze"/>
<propertyname="username" value="root" />
<propertyname="password" value="888666" />
</bean>
- <!-- 2.mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源 MyBatis定义数据源,同意加载配置
-->
- <bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource" ref="dataSource" />
<propertyname="configLocation"value="classpath:config/mybatis-config.xml" />
</bean>
- <!-- 3.mybatis自动扫描加载Sql映射文件/接口 :MapperScannerConfigurer sqlSessionFactory
basePackage:指定sql映射文件/接口所在的包(自动扫描)
-->
- <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage" value="com.kfzx.mapper" />
<propertyname="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
- <!-- 4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源
-->
- <bean id="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource" ref="dataSource" />
</bean>
- <!-- 5. 使用声明式事务 transaction-manager:引用上面定义的事务管理器
-->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
将这些配置文件写好以后,我们直接进行web.xml的配置
<?xmlversion="1.0" encoding="UTF-8" ?>
- <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5">
<display-name>english_web</display-name>
- <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
- <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-*.xml</param-value>
</context-param>
- <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*:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
- <servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
- <listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
- <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>
- <filter>
<filter-name>cors</filter-name>
<filter-class>com.kfzx.filter.SimpleCORSFilter</filter-class>
</filter>
- <filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
最后,我们将log4j进行配置,在src下新建一个配置文件 log4j.properties,内容是
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE}%5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=f:\mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE}%5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change'info' to 'debug' ###
log4j.rootLogger=info, stdout
然后在web.xml里加入
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
自此,本项目的初步搭建过程已经完成。在进行项目搭建时遇到了一些问题在此分享出来,也算对自己的一个警告,首先就是粗心,在配置相关文件时没有将路径进行复制,而是自己手写,因为粗心,打错了一个包名,在运行时找了一段时间问题才将其找到。这也在用实际的案例警告我们,在进行相关文件配置时,能复制粘贴的路径尽量不要自己手写。并且在配置时一定要小心小心再小心。