Spring整合配置Mybatis
1.配置数据源(连接数据库最基本的属性配置,如数据库url,账号,密码,和数据库驱动等最基本参数配置)
<!-- 导入properties配置文件 --> <context:property-placeholder location="classpath*:properties/jdbc.properties"/> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:properties/jdbc.properties"/> </bean> <!--1 数据源基本配置 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="url" value="${jdbc.url}"/> <property name="driverClassName" value="${jdbc.driverClassName}"/> </bean> <!—2 数据源基本配置--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <property name="maxActive" value="${jdbc.maxActive}}" /> <property name="minIdle" value="${minldle}" /> </bean>
数据库参数配置统一写入jdbc.properties文件
jdbc.driver=com.mysql.jdbc.Driver #数据库地址 jdbc.url=jdbc:mysql://localhost:3306/personal_test?useUnicode=true&characterEncoding=utf8 #用户名 jdbc.username=root #密码 jdbc.password=123456 #最大连接数 maxPoolSize=30 #最小连接数 minPoolSize=10 #关闭连接后不自动commit autoCommitOnClose=false #获取连接超时时间 checkoutTimeout=10000 #当获取连接失败重试次数 acquireIncrement=2
2.配置SessionFactory(用于将数据源和mybatis的mapper映射文件进行管理和初始化)
<!-- 创建sessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 扫描mapper映射文件 --> <property name="mapperLocations" value="classpath*:mapping/*.xml" /> </bean>
3.扫描mapper映射文件所对应的dao接口类(其实dao接口的是实现类就是mapper.xml映射文件,此配置是为了将接口和映射文件进行初始化)
<!-- 扫描与mapper映射文件对应的dao接口类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value=" com.zsm.godsoftware.dao "/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
4.创建事务(事务有两种配置方式:注解方式和aop切入方式)
<!-- 创建事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
4.1注解方式
<!-- 注解式事务配置,启动事务注解驱动 -->
<tx:annotation-driven/>
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
4.2 AOP织入方式
<!-- 开启注解扫描 --> <context:component-scan base-package="com.cjh.aop2"/> <!-- 开启aop注解方式,此步骤s不能少,这样java类中的aop注解才会生效 --> <aop:aspectj-autoproxy/> <!-- 目标对象 --> <bean id="knight" class="com.cjh.aop2.BraveKnight"/> <!-- 切面bean --> <bean id="mistrel" class="com.cjh.aop2.Minstrel"/> <!-- 面向切面编程 --> <aop:config> <aop:aspect ref="mistrel"> <!-- 定义切点 --> <aop:pointcut expression="execution(* *.saying(..))" id="embark"/> <!-- 声明前置通知 (在切点方法被执行前调用)--> <aop:before method="beforSay" pointcut-ref="embark"/> <!-- 声明后置通知 (在切点方法被执行后调用)--> <aop:after method="afterSay" pointcut-ref="embark"/> </aop:aspect> </aop:config> <!-- aop切入式事务配置 --> <tx:advice id="trAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="serviceAdvice" expression="execution(* service.serviceImpl.*.*(..))"/> <aop:advisor advice-ref="trAdvice" pointcut-ref="serviceAdvice"/> </aop:config> <aop:config> <!-- 两个切面--> <aop:aspect ref="company"> <!-- 利用切面为特定的类添加新功能 --> <aop:declare-parents types-matching="com.springAop.InstanceMine+" <!-- 实现了InstanceMine接口的实现类可以强转成InstanceOtheer类型的对象并使用他的实现类的方法的方法 --> implement-interface="com.springAop.InstanceOther" default-impl="com.springAop.OtherImpl"/>
<!--InstanceOther的默认实现类,当InstanceMine对象强转成InstanceOther的对象时,默认实现类为OtherImpl--> </aop:aspect> </aop:config>
最终配置:
<?xml version="1.0" encoding="UTF-8"?> <!-- ~ @(#) applicationContext.xml ~ <br> Copyright: Copyright (c) 2017 ~ <br> @author cjh ~ <br> 2017-10-29 15:45:16 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 导入properties配置文件 --> <context:property-placeholder location="classpath*:/jdbc.properties"/> <!-- 扫描注解包 --> <context:component-scan base-package="dao.daoInterface"/> <context:component-scan base-package="service.serviceImpl" /> <!-- 数据源基本配置 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="url" value="${jdbc.url}"/> <property name="driverClassName" value="${jdbc.driverClassName}"/> </bean> <!-- 创建sessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 扫描mapper映射文件 --> <property name="mapperLocations" value="classpath*:dao/mapping/*.xml" /> </bean> <!-- 扫描与mapper映射文件对应的dao接口类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="dao.daoInterface"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 创建事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 注解式事务配置,启动事务注解驱动 --> <!--<tx:annotation-driven/>--> <!-- aop切入式事务配置 --> <tx:advice id="trAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="serviceAdvice" expression="execution(* service.serviceImpl.*.*(..))"/> <aop:advisor advice-ref="trAdvice" pointcut-ref="serviceAdvice"/> </aop:config> </beans>