版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
applicationContext.xml
applicationContext.xml,即Spring上下文配置文件,用于完成Spring和MyBatis的整合。主要配置bean自动扫描、依赖注入、数据库、事务等。如下
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 包扫描 -->
<context:component-scan base-package="com.ali.clc">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源(数据库连接池) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置sqlSessionFactroy -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/ali/clc/mapper/*.xml"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 配置扫描器,将mybatis接口的实现装配到IOC容器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ali.clc.dao"></property>
</bean>
<!-- 事务管理 -->
<!-- 装配transactionManager-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务管理的方案(在某些方法上添加事务) -->
<tx:advice transaction-manager="transactionManager" id="txAdvice">
<tx:attributes>
<tx:method name="query*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="add*"/>
<tx:method name="insert*"/>
<tx:method name="delete*"/>
<tx:method name="update*"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务管理切面 -->
<aop:config>
<aop:pointcut expression="execution(* com.ali.clc.service.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
db.properties
applicationContext.xml文件同级目录(类路径classpath)下创建db.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_personmanager
jdbc.user=******
jdbc.password=******
mybatis-config.xml
配置sqlSessionFactory时需要引用mybatis-config.xml(用于配置mybatis相关参数),mybatis-config.xml编写方法见http://www.mybatis.org/mybatis-3/zh/getting-started.html(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>
<!-- <typeAliases>
<package name="com.ali.clc.bean"/>
</typeAliases> -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--分页参数合理化,请求小于1则跳转到首页;请求大于总页码则跳转到末页 -->
<property name="reasonable" value="true"/>
</plugin>
</plugins>
</configuration>