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


    • 之前使用xml方式整合了MyBatis,文章导航:Spring整合第三方框架-MyBatis整合Spring实现-CSDN博客
      现在使用注解的方式无非是就是将xml标签替换为注解,将xml配置文件替换为配置类而已。
      • 非自定义配置类
        1. package com.example.Configure;
        2. import com.alibaba.druid.pool.DruidDataSource;
        3. import com.example.Beans.otherBeans;
        4. import org.mybatis.spring.SqlSessionFactoryBean;
        5. import org.mybatis.spring.annotation.MapperScan;
        6. import org.springframework.beans.factory.annotation.Value;
        7. import org.springframework.context.annotation.*;
        8. import javax.sql.DataSource;
        9. @Configuration // todo 标注当前类是一个配置类(替代配置文件)、其中包含@Compoent注解
        10. //
        11. @ComponentScan({"com.example"})
        12. //
        13. @PropertySource("jdbc.properties")
        14. //
        15. @Import(otherBeans.class)
        16. // Mapper接口扫描
        17. @MapperScan("com.example.Mapper")
        18. public class SpringConfig {
        19. @Bean // 将非自定义的bean对象交给Spring容器管理
        20. public DataSource dataSource(@Value("${jdbc.driver}") String driver,
        21. @Value("${jdbc.url}") String url,
        22. @Value("${jdbc.username}") String username,
        23. @Value("${jdbc.password}") String password) {
        24. DruidDataSource dataSource = new DruidDataSource();
        25. dataSource.setDriverClassName(driver);
        26. dataSource.setUrl(url);
        27. dataSource.setUsername(username);
        28. dataSource.setPassword(password);
        29. return dataSource;
        30. }
        31. @Bean
        32. public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        33. SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        34. sqlSessionFactoryBean.setDataSource(dataSource);
        35. return sqlSessionFactoryBean;
        36. }
        37. }

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

    • mapper接口类以及其对应的xml配置文件
        1. package com.example.Mapper;
        2. import com.example.pojo.Emp;
        3. import org.springframework.stereotype.Repository;
        4. import java.util.List;
        5. @Repository
        6. public interface EmpMapper {
        7. List findAll();
        8. }
        1. "1.0" encoding="UTF-8" ?>
        2. mapper
        3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        5. <mapper namespace="com.example.Mapper.EmpMapper">
        6. <select id="findAll" resultType="com.example.pojo.Emp">
        7. select *
        8. from tb_emp;
        9. select>
        10. mapper>
      • 业务层调用持久层

        1. package com.example.Service.Impl;
        2. import com.example.Mapper.EmpMapper;
        3. import com.example.Service.UserService;
        4. import com.example.pojo.Emp;
        5. import org.springframework.beans.factory.annotation.Autowired;
        6. import org.springframework.stereotype.Service;
        7. import java.util.List;
        8. @Service("userService")
        9. public class UserServiceImpl implements UserService {
        10. @Autowired
        11. private EmpMapper empMapper;
        12. @Override
        13. public void show() {
        14. List empList = empMapper.findAll();
        15. for (Emp emp : empList) {
        16. System.out.println(emp);
        17. }
        18. }
        19. }

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

      • 测试代码

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


      •  

    • 小结

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

  • 相关阅读:
    【逗老师的无线电】宝峰1701刷OpenGD77
    旷视 CEO 印奇被敲诈勒索:不给 300 万就出售公司敏感信息!
    LeetCode(Python)—— 多数元素(简单)
    jenkins实践篇(2)—— 自动打tag的可回滚发布模式
    实现一个简单的 ctrl+ f 搜索
    数据挖掘-支持向量机(SVM)+代码实现
    多个电商平台搜索接口是否能聚合使用?
    SpringBoot整合dubbo(三)
    [LNOI2022] 吃(数学+贪心)
    Reactor模型:网络线程模型演进
  • 原文地址:https://blog.csdn.net/weixin_64939936/article/details/133522856