Spring+SpringMVC+Mybatis搭建步骤

  1. File->new->other->Maven Project(若遇到没有Deployment Descriptor,在右上角找到切换到Java EE开发界面),pom此时报错,生成web.xml即可(Deployment Descriptor->右键->Generate Deployment...)
  2. 点击src-main-webapp目录下新建web文件夹,静态资源(html/js/css)先放进来即可;(可以回到java resources中建一个Test类)
  3. webapp->WEB-INF->web.xml配置统一字符CharacterEncodingFilter、DispatcherServlet(记不住类的全路径,请在刚才的Test类中Alt+/)------参考如下:
  4. pom->Dependencies->add第四个框中输入查询jar包名spring-web 4.3.15.RELEASE版本,有了它即可在接下来的web.xml中配置统一字符CharacterEncodingFilter;DispatcherServlet则来自于spring-webmvc 4.3.15.RELEASE
<?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_2_5.xsd" version="2.5">
  <display-name>PMCDataManager</display-name>
  <welcome-file-list>
    <welcome-file>index.html</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>
  <servlet>
  	<servlet-name>MVC</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:spring-*.xml</param-value>//spring的配置文件名必须以spring-开头才能初始化加载该配置
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>MVC</servlet-name>
  	<url-pattern>*.do</url-pattern>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

5.配置spring-*.xml一系列文件

其中:context:component-scan spring扫描管理类,这里取决于你的包名叫什么名字;mvc:annotation-driven开启mvc注解;配置视图解析器:IRVR--InternalResourceViewResolver

<?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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
    <context:component-scan 
    	base-package="com.foxconn" />
    <mvc:annotation-driven />
     	
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/web/"></property>
    	<property name="suffix" value=".jsp"></property>
    </bean>
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    	<property name="maxUploadSize" value="10240000"></property>
    	<property name="defaultEncoding" value="UTF-8"></property>
    </bean>
    <mvc:interceptors>
    	<mvc:interceptor>
    		 <mvc:mapping path="/**"/>
    		 <mvc:exclude-mapping path="/user/login.do"/>
    		 <mvc:exclude-mapping path="/user/register.do"/>
    		 <mvc:exclude-mapping path="/user/handleRegister.do"/>
    		 <mvc:exclude-mapping path="/user/handleLogin.do"/>
    		 <mvc:exclude-mapping path="/user/checkUserName.do"/>
    		 <mvc:exclude-mapping path="/user/checkEmail.do"/>
    		 <mvc:exclude-mapping path="/user/checkPhone.do"/>			
    		 <bean class="com.foxconn.interceptor.UserInterceptor" />
    	</mvc:interceptor>
    </mvc:interceptors>
    
</beans>

如上:还配置了拦截器+文件上传 commons-fileupload包

6.还需要配置spring-dao.xml 新建xml文件(由于需要连接池配置,应该创建db.properties文件,并配置连接池的各属性)

如下:配置了连接池;mybatis扫描包;事物开启等;(MapperScannerConfigure来自mybatis-spring 2.0.1包DataSourceTransactionManager来自于spring-jdbc包);sqlSessionFactory(sql执行工厂)

<?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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
 
    <!-- 加载db.properties配置 -->
    <util:properties
    	id="dbConfig"
     	location="classpath:db.properties" />
     	
	<!-- 配置连接池 -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="url" value="#{dbConfig.url}" />
		<property name="driverClassName" value="#{dbConfig.driver}" />
		<property name="username" value="#{dbConfig.user}" />
		<property name="password" value="#{dbConfig.password}" />
		<property name="initialSize" value="#{dbConfig.initSize}" />
		<property name="maxActive" value="#{dbConfig.maxSize}" />
		<property name="minIdle" value="#{dbConfig.minIdle}"/>
		<property name="maxIdle" value="#{dbConfig.maxIdle}"/>
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.fox.mapper" />//此处改为具体的包名
	</bean>
	
	<bean id="sqlSessionFactory" 
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations" value="classpath*:mappers/**/*.xml" />
	</bean>	
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
</beans>

如上完成搭建,log4j日志配置,在ssm中 直接取名为log4j.properties即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值