Spring整合MyBatis的步骤
- 导入MyBatis整合Spring相关坐标。
-
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.13</version> </dependency>
-
- 编写Mapper接口和Mapper.xml
- 在配置文件中配置SqlSessionFactoryBean和MapperScannerConfigurer
- SqlSessionFactoryBean:向Spring容器中提供SqlSessionFactory
- MapperScannerConfigurer:将Mapper接口类对象存储到Spring容器中(交给容器管理后就可以在需要的地方直接注入Mapper接口类对象即可)
-
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置数据源信息 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/db02"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 扫描指定的包,产生mapper对象存储到Spring容器中,不需要再配置Mapper对象--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.Mapper"/> </bean> <bean id="userService" class="com.example.Service.Impl.UserServiceImpl"> <property name="empMapper" ref="empMapper"></property> <property name="userDAO" ref="userDAO"/> <property name="name" value="hhhh"/> </bean> <bean name="userDAO" class="com.example.DAO.Impl.UserDAOImpl"/> </beans>
-
- 上述两个文件配置完成之后就替代了MyBatis原始代码中配置MyBatis参数的配置文件
- 原始代码的配置文件
-
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/db02"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!--指定需要扫描的Mapper接口的包路径--> <mappers> <package name="com.example.Mapper"/> </mappers> </configuration>
- 编写测试代码
-
package com.example.Test; import com.example.Service.UserService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApplicationContext { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); UserService UserServiceBean = (UserService) context.getBean(UserService.class); UserServiceBean.show(); } }
-
接口实现类UserServiceImpl
-
package com.example.Service.Impl; import com.example.DAO.UserDAO; import com.example.Mapper.EmpMapper; import com.example.Service.UserService; import com.example.pojo.Emp; import java.util.List; public class UserServiceImpl implements UserService { public EmpMapper empMapper; public UserServiceImpl() { System.out.println("UserService对象创建"); } private UserDAO userDAO; private String name; public void setName(String name) { this.name = name; } public void setUserDAO(UserDAO userDAO) { System.out.println("UserService执行注入UserDAO的操作:setDAO方法"); this.userDAO = userDAO; } @Override public void show() { List<Emp> all = empMapper.findAll(); for (Emp emp : all) { System.out.println(emp); } } public void setEmpMapper(EmpMapper empMapper) { this.empMapper = empMapper; } }
-
上述接口实现类的代码中,将Mapper接口类的对象直接进行注入(与配置文件中的配置相对应)
-
运行测试代码结果如下
-
-