搭建的过程同使用maven搭建springMVC项目,
如有疑问,请参考我的上一篇文章:http://www.cnblogs.com/wugc/p/6588720.html
1、基本的目录结构
2、几个文件的基础配置
(1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--设置字符集-->
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 加载spring配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认加载方式
默认加载必须规范:
* 文件命名:servlet-name-servlet.xml====springmvc-servlet.xml
* 路径规范:必须在WEB-INF目录下面
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<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>
</web-app>
(2)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: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-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!--开启注解扫描-->
<context:component-scan
base-package="com.shuyun"></context:component-scan>
<!-- 第一步:配置数据源
-->
<!--1.1 加载属性文件-->
<context:property-placeholder
location="classpath:jdbc.properties"/>
<!--1.2获取文件中的属性-->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property
name="jdbcUrl" value="${jdbc.url}"></property>
<property
name="driverClass"
value="${jdbc.driver}"></property>
<property
name="user" value="${jdbc.username}"></property>
<property
name="password"
value="${jdbc.password}"></property>
</bean>
<!-- 第二步:创建sqlSessionFactory。生产sqlSession
-->
<bean
id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property
name="dataSource" ref="dataSource"></property>
<property
name="configLocation"
value="classpath:sqlMapConfig.xml"></property>
</bean>
<!-- 配置mybatis接口代理开发
* 接口类名和映射文件必须同名
* 接口类和映射文件必须在同一个目录 下
* 映射文件namespace名字必须是接口的全类路径名
* 接口的方法名必须和映射Statement的id一致
-->
<bean
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property
name="basePackage"
value="com.shuyun.mybatis.mapper"></property>
<property
name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 第三步:事务 -->
<bean
id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property
name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method
name="save*" propagation="REQUIRED"/>
<tx:method
name="update*" propagation="REQUIRED"/>
<tx:method
name="delete*" propagation="REQUIRED"/>
<tx:method
name="insert*" propagation="REQUIRED"/>
<tx:method
name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置拦截service
-->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.shuyun.service.*.*(..))"/>
</aop:config>
</beans>
(3)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:mvc="http://www.springframework.org/schema/mvc"
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-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/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!--扫描的包-->
<context:component-scan
base-package="com.shuyun"></context:component-scan>
<!-- annotation-driven:默认创建了多个对象:RequestMappingHandlerMapping,RequestMappingHandlerAdapter
也就提供对json格式支持
-->
<mvc:annotation-driven/>
<!-- 配置sprigmvc视图解析器:解析逻辑试图 后台返回逻辑试图:index
视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property
name="prefix" value="/WEB-INF/jsps/"></property>
<property
name="suffix" value=".jsp"></property>
</bean>
</beans>
(4)mybatis配置文件
<?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>
<!--为po类定义别名-->
<typeAliases>
<!--批量定义-->
<package
name="com.shuyun.model"/>
<!--为指定的某一个类起别名-->
<!--<typeAlias
type="com.shuyun.model.Book"/>-->
</typeAliases>
<!-- 加载映射文件 -->
<mappers>
<!--加载resource路径下的指定mapper文件-->
<mapper
resource="/mapper/BookMapper.xml" />
<!-- 批量加载映射文件,idea只能加载resource路径下的配置文件-->
<!--<package
name="com.shuyun.mybatis.mapper" />-->
</mappers>
</configuration>
注意:idea不能识别其他路径下的配置文件,所以必须将XXXMapper.xml文件放在resource文件夹下,最好resource文件夹下创建一个mapper文件夹。