1.添加依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring-version}</version> </dependency>
2. 配置Spring配置文件
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
3.声明注解配置
<context:annotation-config/> <!-- 使用注解--> <context:component-scan base-package="com.zdq"/> <!-- 声明注解扫描范围--> <aop:aspectj-autoproxy/> <!--声明基于注解的aop代理配置--> <context:property-placeholder location="druid.properties"/> <!-- 引入属性文件-->
4.通过注解 Service调用Dao
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public void queryBookById(int bookId) {
Book book = bookDao.selectBookById(bookId);
System.out.println(book);
}
}
Spring整合Mybatis,步骤
Spring的两大核心思想:IoC和AOP
IoC:控制反转,SPring容器可以完成对象的创建、属性注入、对象管理等工作
AOP:面向切面编程,基于动态代理可以在不改变原有业务的情况实现对业务的增强
-
IoC支持:SpringIoC可以为MyBatis完成DataSource、SqlSessionFactory、SqlSession以及DAO对象的创建
-
AOP支持:使用Spring提供的事务管理类,基于AOP完成对MyBatis数据库操作的事务管理
第一步:配置Mybatis-conf.xml和mapper
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency><dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
第二步:mybatis.conf.xml:不用配置?为什么 -->交给Spring容器管理mapper
<?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>
第三步:写完xxxMapper.xml后编写 Spring配置文件
添加依赖(context 、aspects)
<properties>
<spring.version>5.3.15</spring.version>
</properties><dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
第四步:在resources目录创建spring配置文件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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.qfedu"/>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
详细配置信息
<context:annotation-config/>
<!-- 使用注解-->
<context:component-scan base-package="com.zdq"/>
<!-- 声明注解扫描范围-->
<aop:aspectj-autoproxy/>
<!--声明基于注解的aop代理配置-->
<context:property-placeholder location="druid.properties"/>
<!-- 引入属性文件-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${druid.driver}"/>
<property name="url" value="${druid.url}"/>
<property name="username" value="${druid.username}"/>
<property name="password" value="${druid.password}"/>
<property name="initialSize" value="${druid.pool.init}"/>
<property name="maxActive" value="${druid.pool.maxActive}"/>
<property name="minIdle" value="${druid.pool.minIdle}"/>
<property name="maxWait" value="${druid.pool.timeout}"/>
</bean>
<!--spring创建mybayissqlsessionfactory-->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- MyBatis主配置文件 -->
<property name="configLocation" value="mybatis-conf.xml"/>
<!-- 映射文件 -->
<property name="mapperLocations"
value="classpath:mappers/*Mapper.xml"/>
<!-- 配置实体类的包名,自定进行别名定义 -->
<property name="typeAliasesPackage" value="com.zdq.Entity"/>
</bean>
<!-- MapperScannerConfigurer 扫描包中的daoMapper接口 给spring指定mapper工厂-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName"
value="sqlSessionFactory"/>
<!-- spring创建mybayissqlsessionfactory-->
<property name="basePackage" value="com.zdq.Dao"/>
<!-- 读取包中所有的dao接口 创建所有dao对象放到spring容器中;默认id就是首字母小写-->
</bean>
<!--以上是spring ioc配置-->
<!-- 加载事务管理-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--2.配置事务管理策略(引入tx命名空间)
isolation:配置事务的隔离级别
propagation:配置事务的传播机制
-->
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">-->
<!-- <tx:attributes>-->
<!-- <tx:method name="add*" isolation="REPEATABLE_READ" propagation="REQUIRED"/>-->
<!-- <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED"/>-->
<!-- <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED"/>-->
<!-- <tx:method name="get*" isolation="REPEATABLE_READ" propagation="SUPPORTS"/>-->
<!-- <tx:method name="list*" isolation="REPEATABLE_READ" propagation="SUPPORTS"/>-->
<!-- <tx:method name="select*" isolation="REPEATABLE_READ" propagation="SUPPORTS"/>-->
<!-- </tx:attributes>-->
<!-- </tx:advice>-->
<!-- 3.aop配置-->
<!-- <aop:config>-->
<!-- <aop:pointcut id="service_crud" expression="execution(* com.zdq.Service.Impl.*ServiceImpl.*(..))"/>-->
<!-- <aop:advisor advice-ref="txAdvice" pointcut-ref="service_crud"/>-->
<!-- </aop:config>-->
<!-- 2.声明事务采用注解配置 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
第五步:添加Spring-Mybatis依赖和spring-jdbc、druid
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency><dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency><dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
第六步:创建druid.properties
druid.driver=com.mysql.cj.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
druid.username=root
druid.password=rootdruid.pool.init=1
druid.pool.minIdle=3
druid.pool.maxActive=20
druid.pool.timeout=30000
第七步:在application.xml配置读取druid配置信息
<!--加载druid.properties -->
<context:property-placeholder location="classpath:druid.properties"/><!--通过Spring容器常见Druid数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${druid.driver}"/>
<property name="url" value="${druid.url}"/>
<property name="username" value="${druid.username}"/>
<property name="password" value="${druid.password}"/>
<property name="initialSize" value="${druid.pool.init}"/>
<property name="maxActive" value="${druid.pool.maxActive}"/>
<property name="minIdle" value="${druid.pool.minIdle}"/>
<property name="maxWait" value="${druid.pool.timeout}"/>
</bean>
第八步:在Spring的配置文件中配置创建SqlSessionFactory对象:让spring创建sqlsession工厂对象
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 配置数据源 --> <property name="dataSource" ref="dataSource"/> <!-- MyBatis主配置文件 --> <property name="configLocation" value="mybatis-conf.xml"/> <!-- 映射文件 --> <property name="mapperLocations" value="classpath:mappers/*Mapper.xml"/> <!-- 配置实体类的包名,自定进行别名定义 --> <property name="typeAliasesPackage" value="com.zdq.Entity"/> </bean>
第九步:MapperScannerConfigurer用于创建DAO/Mapper接口对象存放到Spring容器
<!-- MapperScannerConfigurer 扫描包中的daoMapper接口 给spring指定mapper工厂--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><!-- spring创建mybayissqlsessionfactory--> <property name="basePackage" value="com.zdq.Dao"/> <!-- 读取包中所有的dao接口 创建所有dao对象放到spring容器中;默认id就是首字母小写--> </bean>
十:在serviceImpl中加入spring注解
@Service public class BookServiceImpl implements BookService { @Autowired private BookDao bookDao; @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)//事务管理 public Book selectBookById(int bid) { Book book = bookDao.selectBookById(bid); System.out.println(book); return book; } }
十一:引入Spring Test
添加依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-version}</version> <scope>test</scope> </dependency>