zoukankan      html  css  js  c++  java
  • spring boot整合mybatis

    spring boot本来可以使用jpa进行数据库操作,但是考虑到jpa的资料比较少,学习成本比较大,不是所有的人都可以十分了解,因此考虑采用mybatis来进行数据库操作。

    1、新建maven项目,在pom中添加相关依赖。

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-parent</artifactId>
          <version>1.5.7.RELEASE</version>
      </parent>
      <artifactId>domain</artifactId>
      
      <dependencies>
            <!-- 测试包 -->
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-test</artifactId>
              <scope>test</scope>
          </dependency>
            <!-- druid数据源 -->
          <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
            <!-- oracle驱动 -->
        <dependency>
            <groupId>com.jslsolucoes</groupId>
            <artifactId>ojdbc6</artifactId>
        </dependency>
            <!-- mybatis整合包 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
      </dependencies>
      
      <build/>
    </project>

    注意,引入org.mybatis.spring.boot后不再引入spring boot的jdbc包,因为前者里面已经包含了。

    同时MyBatis-Spring-Boot-Starter会提供如下:

    • 自动检测现有的DataSource。
    • 将创建并注册SqlSessionFactory的实例,该实例使用SqlSessionFactoryBean将DataSource作为输入参数传递。
    • 将创建并注册从SqlSessionFactory中获取的SqlSessionTemplate的实例。
    • 自动扫描Mappers,将它们链接到SqlSessionTemplate并将其注册到spring上下文,以便注册到需要的bean中。

    即使用该依赖后,只需要定义一个DataSource(application.properteis中可自动配置),会自动创建使用该DataSource的SqlSessionFactory和SqlSessionTemplate,会自动扫描Mappers,链接到SqlSessionTempate,并注册到spring上下文。

    2、创建配置文件application.properteis

    #mybatis
    mybatis.type-aliases-package=com.btw.dao
    #datasource
    spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
    spring.datasource.url=jdbc:oracle:thin:@192.168.**.***:1521:f***
    spring.datasource.username=f****
    spring.datasource.password=*****

    上面只是一个最简单的dataSource配置,还可以增加其他诸如最大连接数最小连接数之类的参数。

    3、在application.properties中指定的包位置"com.btw.dao",创建mybatis的接口

    package com.btw.dao;
    
    import org.apache.ibatis.annotations.Select;
    
    public interface TGgCzyMapper {
    
        @Select("select count(*) from t_xt_czy")
        int selectAll();
    }

    4、创建application类。

    package com.btw;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    
    @EnableAutoConfiguration
    @MapperScan("com.btw.dao")
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

    到这一步正式配置完成,可以正常的对数据库进行操作。

    5、测试类

    package com.btw.dto;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import com.btw.Application;
    import com.btw.dao.TGgCzyMapper;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes=Application.class)
    public class TGgCzyTest {
    
        @Autowired
        private TGgCzyMapper mapper;
        
        @Test
        public void test() {
            System.out.println("-----------------------------------");
            int i = mapper.selectAll();
            System.out.println("i=" + i);
            System.out.println("-----------------------------------");
        }
    
    }
  • 相关阅读:
    [iOS]为什么不要在init初始化方法里调用self.view
    [iOS]ARC和MRC下混编
    CollectionView的基础代码
    关于ios项目中加入webp格式的图片
    【音频】远程链接音频播放(AVPlayer)
    iOS微信支付回调和iOS9系统左上角返回的冲突解决
    【转载】iOS开发经验总结
    【转载】iOS超全开源框架、项目和学习资料汇总(4)数据库、缓存处理、图像浏览、摄像照相视频音频篇
    【转载】3分钟实现iOS语言本地化/国际化(图文详解)
    微信小程序推荐网站
  • 原文地址:https://www.cnblogs.com/yxth/p/9441920.html
Copyright © 2011-2022 走看看