【web开发】Eclipse中SpringMVC+Mybatis+Maven搭建的项目框架(SMM框架)

SMM框架,即springmvc+mybatis+maven是目前比较主流的web框架,在实际项目开发中,一般可以满足大多数项目的开发需要,在eclipse中搭建smm框架,也较为简单,其开发具体步骤如下:

搭建步骤

1、eclipse中新建maven工程。

2、添加快速创建,记得勾选war包,建成之后,eclipse中会自动生成框架结构。

3、配置项目的jdk环境。


4、右键单击属性,选择project-facets,选择所使用的环境信息版本。


5、再重新勾选dynamic web module,进行相关配置,这一步会生成web工程对应的jsp页面。



6、maven工程找不到maven dependenices的方法

刚创建maven工程,没有引入任何jar包的情况下,由于没有jar包,maven类库为空,因此maven-dependencies不出现。引入dependencies节点之后,就会出现相对应的maven依赖库。

7、导入相应jar包到pom.xml,其中包括主要的spring相关jar包、mybatis相关jar包、spring-mybatis包、log4j日志类包等


8、建立不同的配置文件,目录为src/main/resources,其中将jdbc、spring、mybatis等的配置文件分别放在各自的文件夹中


9、在src/main/java中,新建包,根据自身业务需要。

10、将所有的配置文件,进行配置,配置完成之后,进行运行。

出现问题

1、maven导入的jar包,下载不上

        当用run as - maven install时,有时会出现maven管理的有些jar包下载不上。这种情况,可以通过下载的jar包本地导入的方式进行解决。

2、出现“Type Java compiler level does not match the version of the instal”这个问题时解决方法

        当出现编译器与project fects使用jdk版本不相同的问题,解决思路为右键单击项目——properties——查看project fects当中的jdk版本,再选择java compile下的jdk版本,进行配置,保证使用相同的jdk。

3、出现“java.util.zip.ZipException: invalid LOC header (bad signature)”解决方法

        当看到这个问题出现时,表示当前maven下载的jar包已经失效,此时可以到maven仓库中,将失效的文件删除,在run as — maven test重新下载该jar包。

4、xml配置文件中,classpath的指向问题出错,为出现“fileNotFoundException”的错误

        当xml文件中,classpath文件路径出错,就会出现文件找不到的错误。此时一定要查找自己的spring-mvc.xml,jdbc.properties等文件路径是否有问题。其中classpath一般指代的是编译后的class文件目录。如下图


配置文件源码

1、web.xml

<?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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name>maven-ssm</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<!-- 解决工程编码过滤器 -->
	<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>
	</filter>
	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 配置文件所在位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:springmvc/spring.xml,classpath:mybatis/mybatis-spring.xml</param-value>
	</context-param>
	<!-- Spring配置(监听器) -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- SpringMVC配置 -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc/spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

2、spring.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd">
	<!-- 自动注入 -->
	<context:component-scan base-package="com.xiao.smm.service.impl" />
	<!-- 加载properties文件 -->
	<bean id="configProperties"
		class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="locations">
			<list>
				<value>classpath:properties/jdbc.properties</value>
			</list>
		</property>
	</bean>
</beans>

3、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:p="http://www.springframework.org/schema/p"
	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/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">
	<!-- 启用spring mvc 注解 -->
	<context:annotation-config />
	<!-- 设置使用注解的类所在的jar包 -->
	<context:component-scan base-package="com.xiao.smm.controller" />
	<!-- 完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			 <list>
			  	<!--json视图拦截器,读取到@ResponseBody的时候去配置它-->
			 	<ref bean="mappingJacksonHttpMessageConverter"/>
			 </list>
		</property>	
	</bean>
	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
		<property name="supportedMediaTypes">
			 <list>
			 	<value>application/json;charset=UTF-8</value>
			 </list>
		</property>
	</bean>
	<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />
</beans>

4、mybatis-cfg.xml

<?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>
<!-- 全局别名设置,在映射文件中只需写别名,而不必写出整个类路径 -->
	<typeAliases>
		<typeAlias type="com.xiao.smm.entity.Person" alias="Person" />
	</typeAliases>
	<mappers>
		<mapper resource="com/xiao/smm/mapping/PersonMapper.xml" />
	</mappers>
</configuration>

5、mybatis-spring.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd">
	<!-- 配置数据源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<!-- 基本属性 url、user、password -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/zgcy" />
		<property name="username" value="root" />
		<property name="password" value="root" />
		<property name="initialSize" value="1" />
		<property name="minIdle" value="1" />
		<property name="maxActive" value="20" />
		<property name="maxWait" value="60000" />
		<!-- 超过时间限制是否回收 -->
		<property name="removeAbandoned" value="true" />
		<!-- 超过时间限制多长; -->
		<property name="removeAbandonedTimeout" value="180" />
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<!-- 用来检测连接是否有效的sql,要求是一个查询语句 -->
		<property name="validationQuery" value="SELECT 1" />
		<!-- 申请连接的时候检测 -->
		<property name="testWhileIdle" value="true" />
		<!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
		<property name="testOnBorrow" value="false" />
		<!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
		<property name="testOnReturn" value="false" />
	</bean>
	<!-- Mybatis文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="typeAliasesSuperType" value="com.xiao.smm.entity.Person" />
		<property name="configLocation" value="classpath:mybatis/mybatis-cfg.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.xiao.smm.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
	<!-- 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

6、jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/zgcy
jdbc.username=root
jdbc.password=root
jdbc.initialSize=1
jdbc.minIdle=1
jdbc.maxActive=20
jdbc.maxWait=60000
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=180
jdbc.timeBetweenEvictionRunsMillis=60000
jdbc.minEvictableIdleTimeMillis=300000
jdbc.validationQuery=SELECT 1
jdbc.testWhileIdle=true
jdbc.testOnBorrow=false
jdbc.testOnReturn=false

7、log4j.properties

log4j.rootLogger=DEBUG, stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n

业务代码结构

猜你喜欢

转载自blog.csdn.net/xiaoyinmochun/article/details/79638871