ssm整合,mybatis-config.xml文件的配置信息:
applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 启动自动扫描该包下所有的Bean(例如@Controller) -->
<context:component-scan base-package="com.macw" />
<!--
把小配置文件 jdbc.properties引入到spring配置文件中
location:引入的小配置文件的路径
system-properties-mode:表示不从系统属性里面获取变量,否则会跟我们的小配置文件冲突
-->
<context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"></context:property-placeholder>
<!-- 数据源(数据库连接池),jdbc连接数据库需要的数据库连接都从这个类中获取 -->
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
<property name="password" value="${password}"/>
<property name="username" value="${username}"/>
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="maxWait" value="${maxWait}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="initialSize" value="${initialSize}"/>
</bean>
<!-- 配置sqlSessionFactoy的bean对象,配置好后,这个bean就可以帮助创建sqlSession对象了 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"/>
<!-- 注册映射文件 -->
<property name="mapperLocations">
<list>
<!-- 把所有的dao包里面的mapper文件引入进来 -->
<value>classpath:com/macw*.xml</value>
</list>
</property>
<!-- 还可以设置其他配置 -->
<property name="typeAliasesPackage" value="com.macw.entity"/>
<property name="configurationProperties">
<props>
<prop key="logImpl">LOG4J</prop>
</props>
</property>
</bean>
<!-- 让spring帮我生成dao接口的实现类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 告知spring生成哪些接口的实现类 -->
<property name="basePackage" value="com.macw.dao"/>
<!-- dao操作数据库时需要sqlSessonfactory,spring会自动找一个id="sqlSessionFactrory"的bean标签 -->
</bean>
<!-- 相当于配置了一个 bean标签 id="bookDao" -->
<!-- 扫描业务包里面的注解 -->
<context:component-scan base-package="com.macw.service.impl"/>
<!-- 事务管理器类 -->
<bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"/>
</bean>
<!--
配置事务增强类
在这个里面我们使用了特殊的标签tx:advice,spring会自动判断出使用哪个增强类
id:这个增强类的标记
transaction-manager是使用哪个事务管理器。要跟上面的bean的id一致
-->
<tx:advice id="ct" transaction-manager="tm">
<!-- 事务的使用策略,哪些方法需要使用事务,哪些不需要使用事务 -->
<tx:attributes>
<!-- 方法名是select开头的,以后我们定义业务类的方法名不能随意写了。 -->
<tx:method name="select*" read-only="true"/>
<!-- 其余的方法要开启事务 -->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置切入点和织入 -->
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.macw.service..*.*(..))"/>
<aop:advisor advice-ref="ct" pointcut-ref="pc"/>
</aop:config>
</beans>
小配置文件jdbc.properties
username=hr
password=hr
url=jdbc:oracle:thin:@localhost:1521:xe
driverClassName=oracle.jdbc.OracleDriver
initialSize=10
maxActive=15
maxWait=10000
web.xml文件配置信息:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 1.配置监听,spring-mybatis.xml文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mybatis-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 2.配置spring-mvc.xml,加载spring容器文件 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<display-name>Archetype Created Web Application</display-name>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 3.过滤器,字符编码,p125 -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 4.静态资源访问 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>*.jpg</url-pattern>
<url-pattern>*.png</url-pattern>
<url-pattern>*.PNG</url-pattern>
<url-pattern>*.gif</url-pattern>
<url-pattern>*.json</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!-- 5.jsp配置 -->
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>