Spring的注解开发-注解方式整合MyBatis代码实现

  • 之前使用xml方式整合了MyBatis,文章导航:Spring整合第三方框架-MyBatis整合Spring实现-CSDN博客
    现在使用注解的方式无非是就是将xml标签替换为注解,将xml配置文件替换为配置类而已。
    • 非自定义配置类
    • package com.example.Configure;
      
      import com.alibaba.druid.pool.DruidDataSource;
      import com.example.Beans.otherBeans;
      import org.mybatis.spring.SqlSessionFactoryBean;
      import org.mybatis.spring.annotation.MapperScan;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.context.annotation.*;
      
      import javax.sql.DataSource;
      
      @Configuration // todo 标注当前类是一个配置类(替代配置文件)、其中包含@Compoent注解
      // 
      @ComponentScan({"com.example"})
      
      // 
      @PropertySource("jdbc.properties")
      
      // 
      @Import(otherBeans.class)
      
      // Mapper接口扫描
      @MapperScan("com.example.Mapper")
      public class SpringConfig {
          @Bean  // 将非自定义的bean对象交给Spring容器管理
          public DataSource dataSource(@Value("${jdbc.driver}") String driver,
                                       @Value("${jdbc.url}") String url,
                                       @Value("${jdbc.username}") String username,
                                       @Value("${jdbc.password}") String password) {
              DruidDataSource dataSource = new DruidDataSource();
              dataSource.setDriverClassName(driver);
              dataSource.setUrl(url);
              dataSource.setUsername(username);
              dataSource.setPassword(password);
              return dataSource;
          }
      
          @Bean
          public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
              SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
              sqlSessionFactoryBean.setDataSource(dataSource);
              return sqlSessionFactoryBean;
          }
      
      
      }
      

                与数据库建立连接的同时,扫描指定的mapper接口,实现实现数据库的操作

  • mapper接口类以及其对应的xml配置文件
    • package com.example.Mapper;
      
      import com.example.pojo.Emp;
      import org.springframework.stereotype.Repository;
      
      import java.util.List;
      
      @Repository
      public interface EmpMapper {
          List findAll();
      }
      
    • 
      
      
          
      
    • 业务层调用持久层

    • package com.example.Service.Impl;
      
      
      import com.example.Mapper.EmpMapper;
      import com.example.Service.UserService;
      import com.example.pojo.Emp;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      
      import java.util.List;
      
      @Service("userService")
      public class UserServiceImpl implements UserService {
          @Autowired
          private EmpMapper empMapper;
      
          @Override
          public void show() {
              List empList = empMapper.findAll();
              for (Emp emp : empList) {
                  System.out.println(emp);
              }
          }
      
      
      }
      

      上述中直接注入的mapper接口类

    • 测试代码

    • package com.example.Test;
      
      
      import com.example.Configure.SpringConfig;
      import com.example.Service.UserService;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
      
      public class TestApplicationContext {
          public static void main(String[] args) {
              // 注解方式加载Spring容器的核心配置类
              ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
              UserService bean = context.getBean(UserService.class);
              bean.show();
          }
      }
      
      
    • 运行结果如下:

    • Spring的注解开发-注解方式整合MyBatis代码实现_第1张图片
       

  • 小结

  • 用注解的方式整合第三方框架,以MyBatis框架为例,首先得与数据库建立连接的操作由配置文件转换为配置类,使用@Bean注解,Spring框架会自动调用这两个方法,并生成对应的bean对象交给Spring容器管理,与数据库成功建立连接。然后在业务层直接注入Mapper接口对象,调用其中的方法,实现对于数据库的操作。

你可能感兴趣的:(Spring,5,spring,mybatis,tomcat)