Spring xml 方式整合mybatis 第三方框架

Spring整合MyBatis

MyBatis提供了mybatis-spring.jar专门用于两大框架的整合。

①:第一步:

导入MyBatis整合Spring的相关坐标;

XML 复制代码
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>3.0.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.29</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.18</version>
</dependency>

②:第二步:

编写Mapper和Mapper.xml

java 复制代码
public interface UserMapper {
 List<User> queryList();
}
XML 复制代码
<mapper namespace="com.hu.mapper.UserMapper">
    <select id="queryList" >
        select * from user
    </select>
</mapper>

③:第三步:

配置SqlSessionFactoryBean和MapperScannerConfigurer

XML 复制代码
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
<!--将sqlsession对象配置到spring容器中-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
  <!--想连接数据 需要配置datasoure数据源  注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--将com.hu.mapper下面xml配置文件生成的对象放到spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hu.mapper"/>
    </bean>

    <!--将service对象注入到spring中-->
    <bean id="userService" class="com.hu.service.impl.UserServiceImpl" >
        <property name="userMapper" ref="userMapper"></property>
    </bean>

④:第四步:

编写测试代码

java 复制代码
   public void test02(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        UserServiceImpl userService = applicationContext.getBean("userService", UserServiceImpl.class);
       userService.getUserList();
    }
相关推荐
CoderYanger2 小时前
Java EE 进阶:12.Spring AOP(第一弹)
java·开发语言·spring boot·后端·spring·java-ee·交互
软件技术NINI5 小时前
基于SpringBootVue 的出租车拼车系统
vue.js·spring boot·spring·毕业设计·毕设
Flynt18 小时前
Spring AI Alibaba 真停更了?我把它的 GitHub 翻了个底朝天
java·spring·开源
CoderYanger1 天前
Java EE 进阶:11. SpringBoot 统一功能处理(第四弹):@ControllerAdvice 源码分析+案例代码
java·数据库·spring boot·后端·spring·java-ee·mybatis
CoderYanger1 天前
Java EE 进阶:11. SpringBoot 统一功能处理(第三弹):统一数据返回格式+统一异常处理
java·数据库·spring boot·后端·spring·java-ee·mybatis
会编程的吕洞宾2 天前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring
SongRenShuo2 天前
三年 MyBatis 排查史:从我翻日志,到让 SQL 自己浮现
sql·mybatis·idea·日志·调试
CoderYanger2 天前
Java EE 进阶:11. SpringBoot 统一功能处理(第二弹):DispatcherServlet 源码分析+适配器模式
java·数据库·hive·spring boot·面试·java-ee·mybatis
考虑考虑3 天前
记一次@Async生产事故
spring boot·后端·spring
جيون داد ناالام ميづ3 天前
三、MyBatis 与 Spring Boot 集成:极简配置 + 分页 / 乐观锁实战
spring boot·后端·mybatis