Spring-Spring整合Mybatis

Spring-Spring整合Mybatis

1.1 导入坐标

  • 除了Spring,mybatis,mysql,druid(数据源)外,还需要导入两个坐标:

    • spring-jdbc

    • mybatis-spring

      <!-- Spring操作数据库需要的坐标 -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-jdbc</artifactId>
         <version>5.2.1.RELEASE</version>
      </dependency>
      
      <!-- 整合Mybatis需要的坐标 -->
      <dependency>
         <groupId>org.mybatis</groupId>
         <artifactId>mybatis-spring</artifactId>
         <version>2.0.3</version>
      </dependency>
      </dependencies>
      

1.2 编写配置类

  • 整合Mybatis框架一共需要编写三个配置类,分别是:

    • Spring配置类:SpringConfig.java
    /**
     * @author: sea
     * @date: 2023/7/10 09:30
     */
    
    @Configuration //声明该类为配置类
    @ComponentScan({"com.sea"}) //配置bean扫描的包路径
    @Import({JdbcConfig.class, MybatisConfig.class}) //引入其他配置类
    public class SpringConfig {
        
    }
    
    • 数据源配置类:JdbcConfig.java
      jdbc.driver=com.mysql.jdbc.Driver
      jdbc.url=jdbc:mysql://localhost:3306/springdb?useSSL=false
      jdbc.username=root
      jdbc.password=12345678
      
    /**
     * @author: sea
     * @date: 2023/7/10 09:47
     */
    @PropertySource("classpath:jdbc.properties") //配置引入的properties文件路径
    public class JdbcConfig {
        //通过注解给简单类型注入值
        @Value("${jdbc.driver}")
        private String driver;
        @Value("${jdbc.url}")
        private String url;
        @Value("${jdbc.username}")
        private String username;
        @Value("${jdbc.password}")
        private String password;
    
        //1.定义一个方法获取要管理的bean对象
        //2.给该方法的返回值添加@Bean,定义成Bean对象
        @Bean("dataSource_druid")
        public DataSource dataSource(){
            DruidDataSource druidDataSource = new DruidDataSource();
            druidDataSource.setDriverClassName(driver);
            druidDataSource.setUrl(url);
            druidDataSource.setUsername(username);
            druidDataSource.setPassword(password);
            return druidDataSource;
        }
    }
    
    • Mybatis配置类:MybatisConfig.java
    /**
     * @author: sea
     * @date: 2023/7/10 10:00
     */
    public class MybatisConfig {
        /**
         * 构建SqlSessionFactoryBean
         */
      
      	//引用类型通过方法形参注入
        @Bean
        public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setTypeAliasesPackage("com.sea.domain");
            sqlSessionFactoryBean.setDataSource(dataSource);
            return sqlSessionFactoryBean;
        }
    
        /**
         * 构建mapper映射器
         */
        @Bean
        public MapperScannerConfigurer mapperScannerConfigurer(){
            MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
            mapperScannerConfigurer.setBasePackage("com.sea.dao");
            return mapperScannerConfigurer;
        }
    }
    
    • 【未解决!!】此处有疑问:通过形参注入DataSource,本质上也是将相应的bean注入该参数,那么我通过在MybatisConfig类中定义一个DataSource类型的变量,然后进行自动装配正常来说也可以,但是实际运行发现:DataSource并没有自动装配成功!!
      • 1.既然可以通过形参方式注入DataSource类型的bean,说明Spring容器能够扫描到并管理该类型的bean对象;
      • 2.那么为什么无法自动装配呢???

1.3 dao层和service层

  • dao层

    /**
     * @author: sea
     * @date: 2023/7/5 14:27
     */
    @Repository
    public interface AccountDao {
        void save();
        void delete();
        void update();
        @Select("select * from account;")
        List<Account> findAll();
        Account findById(Integer id);
    }
    
  • service层

    /**
     * @author: sea
     * @date: 2023/7/10 09:34
     */
    @Service
    public class AccountServiceImpl implements AccountService {
        @Autowired
        private AccountDao accountDao;
        @Override
        public void save() {
            accountDao.save();
        }
    
        @Override
        public void update() {
            accountDao.update();
        }
    
        @Override
        public void delete() {
            accountDao.delete();
        }
    
        @Override
        public Account findById(int id) {
            return accountDao.findById(id);
        }
    
        @Override
        public List<Account> findAll() {
            return accountDao.findAll();
        }
    }
    

1.4 测试

  • 编写App类测试是否能够访问数据库:

    /**
     * @author: sea
     * @date: 2023/7/10 10:20
     */
    public class App {
        public static void main(String[] args) {
            ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    
            AccountService accountService = context.getBean(AccountService.class);
            List<Account> serviceAll = accountService.findAll();
            System.out.println(serviceAll);
        }
    }
    
  • 结果:

    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SEA-365

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值