1.项目的目录
2.将Mapper和Mapper.xml放在同一个目录
3.applicationContext.xml的配置文件
- <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: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">
- <!-- 数据源 -->
- <property name="dataSource" ref="dataSource" />
- <!-- mybatis配置文件 -->
- <property name="configLocation" value="classpath:SqlMapConfig.xml" />
- </bean>
- <!-- 配置mapper -->
- <!-- MapperScannerConfigurer:mapper的扫描器,将包下边的mapper接口自动创建代理对象, 自动创建到spring容器中,bean的id是mapper的类名(首字母小写) -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <!-- 配置扫描包的路径 如果要扫描多个包,中间使用半角逗号分隔 -->
- <property name="basePackage" value="com.mybatis.mapper" />
- <!-- 使用sqlSessionFactoryBeanName,这个要注意 -->
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
- </bean>
- <!--配置Service -->
- <bean id="userService" class="com.mybatis.service.UserService">
- <property name="userMapper" ref="userMapper" />
- </bean>
- <!-- 加入事务管理 -->
- <!-- #############5. Spring声明式事务管理配置############### -->
- <!-- 5.1 配置事务管理器类 -->
- <bean id="txManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <!-- 5.2 配置事务增强(如果管理事务?) -->
- <tx:advice id="txAdvice" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="find*" read-only="true" />
- <tx:method name="insert*" propagation="REQUIRED" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- <tx:method name="*" read-only="true" />
- </tx:attributes>
- </tx:advice>
- <!-- 5.3 Aop配置: 拦截哪些方法(切入点表表达式) + 应用上面的事务增强配置 -->
- <aop:config>
- <aop:pointcut expression="execution(* com.mybatis.service.*.*(..))"
- id="pt" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
- </aop:config>
- </beans>
4.db.properties
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/mybatistest
- jdbc.username=root
- jdbc.password=1234