美文网首页
Springboot整合mybatis+mysql

Springboot整合mybatis+mysql

作者: 我爱吃蛋糕_ab5e | 来源:发表于2018-04-20 12:02 被阅读0次

    一,构建springboot工程,添加pom依赖:

      <properties>
            <mybatis-spring-boot>1.2.0</mybatis-spring-boot>
            <mysql-connector>5.1.39</mysql-connector>
        </properties>
        <dependencies>
            <!-- Spring Boot Web 依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- Spring Boot Test 依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- Spring Boot Mybatis 依赖 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis-spring-boot}</version>
            </dependency>
            <!-- MySQL 连接驱动依赖 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql-connector}</version>
            </dependency>
        </dependencies>
    

    二、配置:

     @Bean
        @ConfigurationProperties(prefix="mysql.datasource")
        public DataSource dataSource() {
            return new org.apache.tomcat.jdbc.pool.DataSource();
        }
        @Bean
        public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
    
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource());
    
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    
            sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
    
            return sqlSessionFactoryBean.getObject();
        }
        @Bean
        public PlatformTransactionManager transactionManager() {
            return new DataSourceTransactionManager(dataSource());
        }
    
    image.png

    完结。

    相关文章

      网友评论

          本文标题:Springboot整合mybatis+mysql

          本文链接:https://www.haomeiwen.com/subject/xxpakftx.html