1.jar 包
Spring 的 jar 包; Mybatis 的 jar 包; Mybatis 与 Spring 整合包; 数据库连接包; 第三方连接池包.
2. 需要配置的文件
SqlMapConfig.xml:Mybatis 核心配置文件 (主要交给 Spring 管理, 并不需要什么配置, 但必须存在)
db.properties: 数据库连接配置, 主要配置用户密码
applicationContext*.xml:Spring 的核心配置, 配置数据源, 连接池, mapper 扫描包位置, 事务, service 扫描.
(* 表示可以将事务单独成一个配置, 例如: applicationContext-dao.xml)
springmvc.xml: 配置扫描注解包 (可包括 Spring 配置中的 service 扫描包), 注解驱动 (处理器的适配器和映射器配置), 视图解析器, 自定义转换器
log4j.properties: 日志配置
web.xml: 前端配置
3.SqlMapConfig.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>
- <!-- 逆向工程生成的不需要别名 -->
- </configuration>
- 4.db.properties
- 5.applicationContext*.xml
- applicationContext-dao.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-4.0.xsd">
- <!-- 加载配置文件 -->
- <context:property-placeholder location="classpath:db.properties" />
- <!-- 数据库连接池 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${jdbc.driver}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- <property name="maxActive" value="10" />
- <property name="maxIdle" value="5" />
- </bean>
- <!-- 配置 SqlSessionFactory -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 配置 mybatis 核心配置文件 -->
- <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
- <!-- 配置数据源 -->
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- 配置 mapper 扫描设置 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <!-- 只需要设置基本包路径 -->
- <property name="basePackage" value="com.springmvc.dao"></property>
- </bean>
- </beans>
classpath 指定是文件路径. 建议将约束都导入, 方便配置.
- applicationContext-trans.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-4.0.xsd">
- <!-- 事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <!-- 数据源 -->
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- 通知 -->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <!-- 传播行为 -->
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="insert*" propagation="REQUIRED" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
- <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
- <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
- </tx:attributes>
- </tx:advice>
- <!-- 切面 -->
- <aop:config>
- <aop:advisor advice-ref="txAdvice"
- pointcut="execution(* cn.itcast.ssm.service.*.*(..))" />
- </aop:config>
- <
- </beans>
上面演示的是采用配置式, 下面采用注解式:
- <bean id="transactionManager" class=""org.springframework.jdbc.datasource.DataSourceTransactionManager>
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager"/>
6.springmvc.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-4.0.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
- <!-- 配置扫描注解 -->
- <context:component-scan base-package="com.springmvc"/>
- <!-- 注解驱动 -->
- <mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
- <!-- 配置转换器 -->
- <bean id="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
- <property name="converters">
- <set>
- <bean class="com.springmvc.converter.DateConverter" />
- </set>
- </property>
- </bean>
- <!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" ->
- "/WEB-INF/jsp/test.jsp" -->
- <!-- 配置视图解析器 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <!-- 配置逻辑视图的前缀 -->
- <property name="prefix" value="/WEB-INF/jsp/" />
- <!-- 配置逻辑视图的后缀 -->
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
7.Web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <Web-App xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
- <display-name>springmvc-mybatis</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>
- <!-- 配置 spring -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring/applicationContext*.xml</param-value>
- </context-param>
- <!-- 使用监听器加载 Spring 配置文件 -->
- <listener>
- <listener-class>org.springframework.Web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 配置 SrpingMVC 的前端控制器 -->
- <servlet>
- <servlet-name>springmvc-Web</servlet-name>
- <servlet-class>org.springframework.Web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring/springmvc.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>springmvc-Web</servlet-name>
- <!-- 配置所有以 action 结尾的请求进入 SpringMVC -->
- <url-pattern>*.action</url-pattern>
- </servlet-mapping>
- <!-- 解决 post 乱码问题 -->
- <filter>
- <filter-name>encoding</filter-name>
- <filter-class>org.springframework.Web.filter.CharacterEncodingFilter</filter-class>
- <!-- 设置编码参是 UTF8 -->
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </Web-App>
来源: http://www.bubuko.com/infodetail-2927188.html