• spring整合mybatis(纯注解)


    我们在写一个独立的小项目的时候往往不需要引入mvc模块,仅仅需要操作数据库即可
    所以就整合一下spring和mybatis,方便进行对数据库的操作

    注意:源码地址:源码点我

    一. 整体项目结构

    file

    二. 依赖相关

    Spring核心依赖

    spring-core,spring-context,spring-beans

            
                org.springframework
                spring-core,spring-context,spring-beans
                5.2.19.RELEASE
                compile
            
            
                org.springframework
                spring-context
                5.2.19.RELEASE
                compile
            
            
                org.springframework
                spring-beans
                5.2.19.RELEASE
                compile
            
            
                org.springframework
                spring-jdbc
                5.2.19.RELEASE
                compile
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    mybatis依赖

    		
                org.mybatis
                mybatis
                3.5.6
            
    
    • 1
    • 2
    • 3
    • 4
    • 5

    spring整合mybatis整合依赖

            
                org.mybatis
                mybatis-spring
                2.0.6
            
    
    • 1
    • 2
    • 3
    • 4
    • 5

    MySQL驱动

            
                mysql
                mysql-connector-java
                8.0.11
            
    
    • 1
    • 2
    • 3
    • 4
    • 5

    三. 配置文件

    db.properties数据库参数配置文件

    database.driver=com.mysql.cj.jdbc.Driver
    database.url=jdbc:mysql://localhost/dblog?characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&failOverReadOnly=false
    database.username=root
    database.password=root
    
    • 1
    • 2
    • 3
    • 4

    mybatis.properties Mybatis参数配置文件

    mybatis.aliasesPackage=com.it1997.domain
    mybatis.mapperLocations=classpath:mapper/*Mapper.xml
    
    • 1
    • 2

    四. 配置类

    SpringConfig配置类

    @Configuration
    @ComponentScan("com.it1997") // 开启Spring注解组件扫描
    @MapperScan("com.it1997.mapper") // 扫描mapper接口地址
    public class SpringConfig {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    这个配置类指定了spring扫描的包路径和mapper接口路径

    JdbcConfig配置类

    这个配置类实现的功能是
    读取properties配置文件
    构造数据源
    配置动态创建数据库连接的对象

    @Configuration // 该注解声明该类是一个配置类,不能省略
    @PropertySource({"classpath:db.properties","classpath:mybatis.properties"})
    public class JdbcConfig {
        // 驱动名称
        @Value("${database.driver}")
        private String driverClassName;
        // 数据库连接地址
        @Value("${database.url}")
        private String url;
        // 数据库用户名
        @Value("${database.username}")
        private String username;
        // 数据库密码
        @Value("${database.password}")
        private String password;
        @Value("${mybatis.aliasesPackage}")
        public String aliasesPackage;
        @Value("${mybatis.mapperLocations}")
        private String mapperLocations;
    
        // 配置数据源
        @Bean("dataSource")
        public DataSource createDataSource() {
            // 创建Druid连接池对象
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driverClassName);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            dataSource.setDefaultAutoCommit(true); // 自动提交事务,默认true
            return dataSource;
        }
    
        // 配置创建数据库连接的对象
        @Bean
        public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource) throws IOException {
            SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
            // 设置连接池
            sqlSessionFactoryBean.setDataSource(dataSource);
            // 实体类起别名
            sqlSessionFactoryBean.setTypeAliasesPackage(aliasesPackage);
            // 配置mapper.xml配置文件路径
            sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
            return sqlSessionFactoryBean;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    五. 测试类

    public static void main( String[] args ) {
            System.out.println( "Hello World!" );
            ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
            UserMapper userMapper = applicationContext.getBean(UserMapper.class);
            List list = userMapper.selectUserId();
            System.out.println(list.toString());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    网络设备详细
    QT+OSG/osgEarth编译之二十四:libbz2+Qt编译(一套代码、一套框架,跨平台编译,版本:libbz2-1.0.6)
    Nginx 文件名逻辑漏洞(CVE-2013-4547)
    PDPS16.0单机版及许可证服务器授权安装教程分享
    CSS笔记——Flex、Grid布局、css响应式布局
    造轮子之设置管理
    浅议信息系统控制在企业中的应用(lunwen+开题报告)
    【C++】动态内存管理 ④ ( 对象的动态创建和释放引申思考 | 基础数据类型 内存分析 | malloc 分配内存 delete 释放 | new 分配内存 free 释放内存 )
    java代理Proxy以及实际PRC场景中的使用
    【Qt系列】QtableWidget表格列宽自适应表格大小
  • 原文地址:https://blog.csdn.net/qq_41772384/article/details/126561496