• Spring整合Mybatis


    之间简单配置过SqlSessionFactory,但这并不是正规的整合方式,MyBatis提供了mybatis-spring.jar专门用于两大框架的整合

     Spring整合Mybatis的步骤:

    • 导入MyBatis整合Spring的相关坐标
    1. <dependency>
    2. <groupId>org.springframeworkgroupId>
    3. <artifactId>spring-jdbcartifactId>
    4. <version>5.2.5.RELEASEversion>
    5. dependency>
    6. <dependency>
    7. <groupId>org.mybatisgroupId>
    8. <artifactId>mybatis-springartifactId>
    9. <version>2.0.4version>
    10. dependency>
    • 编写Mapper和Mapper.xml

    • 配置SqlSessionFactory和MapperScannerConfigurer

    1. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
    2. <property name="dataSource" ref="dataSource">property>
    3. bean>
    4. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    5. <property name="basePackage" value="com.xfy.mapper">property>
    6. bean>
    7. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    8. <property name="driverClassName" value="com.mysql.jdbc.Driver">property>
    9. <property name="url" value="jdbc:mysql:///book?useSSL=false&useServerPrepStmts=true">property>
    10. <property name="username" value="root">property>
    11. <property name="password" value="1234">property>
    12. bean>
    13. <bean id="userService" class="com.xfy.service.impl.UserServiceImpl">
    14. <property name="userMapper" ref="userMapper">property>
    15. bean>
    •  编写测试代码
    1. public static void main(String[] args) throws ParseException, IOException {
    2. ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
    3. UserService userService = applicationContext.getBean("userService", UserService.class);
    4. userService.show();
    5. UserMapper userMapper = applicationContext.getBean("userMapper", UserMapper.class);
    6. for (User user : userMapper.SelectAll()) {
    7. System.out.println(user);
    8. }
    9. }
    10. public class UserServiceImpl implements UserService, BeanNameAware {
    11. private UserDao userDao;
    12. private UserMapper userMapper;
    13. String Name;
    14. public void setUserMapper(UserMapper userMapper) {
    15. this.userMapper = userMapper;
    16. }
    17. public void show() {
    18. List users = userMapper.SelectAll();
    19. for (User user : users) {
    20. System.out.println(user);
    21. }
    22. }
    23. public interface UserMapper {
    24. List SelectAll();
    25. }

     

    Spring整合MyBatis的原理剖析

    整合包里提供了一个SqlSessionFactoryBean和一个扫描Mapper的配置对象,SqlSessionFactoryBean一旦实例化,就开始扫描Mapper并通过动态代理产生Mapper的实现类,存储到Spring容器中,相关类:

    • SqlSessionFactory:需要进行配置,用于提供SqlSessionFactory

    • MapperSacnnerConfigurer:需要进行配置,用于扫描指定mapper注册BeanDefinition

    • MapperFactoryBean:Mapper的FactoryBean,获得指定Mapper时调用getObject方法

    • ClassPathMapperScanner:definition.setAutowireMode(2)修改了自动注入状态,所以MapperFactoryBean中setSqlSessionFactory会自动注入进去

  • 相关阅读:
    dcase_util教程
    osgEarth示例分析——osgearth_tracks
    小谈设计模式(6)—依赖倒转原则
    Android之UI Automator框架源码分析(第九篇:UiDevice获取UiAutomation对象的过程分析)
    Murata村田高压电容的国产替代--赫威斯电容(HVC Capacitor)
    SpringBoot对象拷贝
    Java给Excel设置单元格格式
    【GO】Map的操作
    计算机毕业设计springboot+vue+elementUI股票交易模拟系统
    【数据结构】线性表与顺序表
  • 原文地址:https://blog.csdn.net/qq_60619678/article/details/134521435