1. 程式人生 > >spring boot整合mybatis

spring boot整合mybatis

tis ott 最簡 boot.s driver 大連 ins pla configure

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("-----------------------------------");
    }

}

spring boot整合mybatis