• Spring整合第三方框架-MyBatis整合Spring实现


    Spring整合MyBatis的步骤

    • 导入MyBatis整合Spring相关坐标。
        1. <dependency>
        2. <groupId>org.mybatisgroupId>
        3. <artifactId>mybatis-springartifactId>
        4. <version>2.0.5version>
        5. dependency>
        6. <dependency>
        7. <groupId>org.springframeworkgroupId>
        8. <artifactId>spring-jdbcartifactId>
        9. <version>5.3.13version>
        10. dependency>
    • 编写Mapper接口和Mapper.xml
    • 在配置文件中配置SqlSessionFactoryBean和MapperScannerConfigurer
      • SqlSessionFactoryBean:向Spring容器中提供SqlSessionFactory
      • MapperScannerConfigurer:将Mapper接口类对象存储到Spring容器中(交给容器管理后就可以在需要的地方直接注入Mapper接口类对象即可)
          1. "1.0" encoding="UTF-8"?>
          2. <beans xmlns="http://www.springframework.org/schema/beans"
          3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
          5. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
          6. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
          7. <property name="url" value="jdbc:mysql://localhost:3306/db02"/>
          8. <property name="username" value="root"/>
          9. <property name="password" value="123456"/>
          10. bean>
          11. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
          12. <property name="dataSource" ref="dataSource"/>
          13. bean>
          14. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          15. <property name="basePackage" value="com.example.Mapper"/>
          16. bean>
          17. <bean id="userService" class="com.example.Service.Impl.UserServiceImpl">
          18. <property name="empMapper" ref="empMapper">property>
          19. <property name="userDAO" ref="userDAO"/>
          20. <property name="name" value="hhhh"/>
          21. bean>
          22. <bean name="userDAO" class="com.example.DAO.Impl.UserDAOImpl"/>
          23. beans>
      • 上述两个文件配置完成之后就替代了MyBatis原始代码中配置MyBatis参数的配置文件
        • 原始代码的配置文件
          1. "1.0" encoding="UTF-8" ?>
          2. configuration
          3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
          4. "https://mybatis.org/dtd/mybatis-3-config.dtd">
          5. <configuration>
          6. <environments default="development">
          7. <environment id="development">
          8. <transactionManager type="JDBC"/>
          9. <dataSource type="POOLED">
          10. <property name="driver" value="com.mysql.jdbc.Driver"/>
          11. <property name="url" value="jdbc:mysql://localhost:3306/db02"/>
          12. <property name="username" value="root"/>
          13. <property name="password" value="123456"/>
          14. dataSource>
          15. environment>
          16. environments>
          17. <mappers>
          18. <package name="com.example.Mapper"/>
          19. mappers>
          20. configuration>
    • 编写测试代码
        1. package com.example.Test;
        2. import com.example.Service.UserService;
        3. import org.springframework.context.support.ClassPathXmlApplicationContext;
        4. public class TestApplicationContext {
        5. public static void main(String[] args) {
        6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        7. UserService UserServiceBean = (UserService) context.getBean(UserService.class);
        8. UserServiceBean.show();
        9. }
        10. }


         

      • 接口实现类UserServiceImpl

          1. package com.example.Service.Impl;
          2. import com.example.DAO.UserDAO;
          3. import com.example.Mapper.EmpMapper;
          4. import com.example.Service.UserService;
          5. import com.example.pojo.Emp;
          6. import java.util.List;
          7. public class UserServiceImpl implements UserService {
          8. public EmpMapper empMapper;
          9. public UserServiceImpl() {
          10. System.out.println("UserService对象创建");
          11. }
          12. private UserDAO userDAO;
          13. private String name;
          14. public void setName(String name) {
          15. this.name = name;
          16. }
          17. public void setUserDAO(UserDAO userDAO) {
          18. System.out.println("UserService执行注入UserDAO的操作:setDAO方法");
          19. this.userDAO = userDAO;
          20. }
          21. @Override
          22. public void show() {
          23. List all = empMapper.findAll();
          24. for (Emp emp : all) {
          25. System.out.println(emp);
          26. }
          27. }
          28. public void setEmpMapper(EmpMapper empMapper) {
          29. this.empMapper = empMapper;
          30. }
          31. }
        • 上述接口实现类的代码中,将Mapper接口类的对象直接进行注入(与配置文件中的配置相对应)

        • 运行测试代码结果如下

     

  • 相关阅读:
    CSS高级技巧
    小节9:Python之numpy
    Flask (Jinja2) 服务端模板注入漏洞复现
    MNN实践[C++版本]
    UI控件DevExpress WinForm新手指南——如何在应用启动时执行操作
    一起看看Python中的迭代器&生成器
    iNeuOS工业互联网操作系统,民爆远程运维平台案例
    【Python自动化】Python-docx基础入门--插入table表格样式设置
    维修ESA 数控折弯机CNC S 530 系统工控屏 S525 工业电脑显示屏
    golang数组和Slice地址
  • 原文地址:https://blog.csdn.net/weixin_64939936/article/details/133323901