SpringBoot整合Mybatis连接Impala入门案例详解

前期准备

创建一个SpringBoot项目,可以先不选择依赖,待会再pom.xml里面再导入相应的依赖。
在这里插入图片描述
在这里插入图片描述

项目结构

在这里插入图片描述

配置环境

  1. 导入包依赖pom.xml
  • spring-boot-starter-web(网页展示)
  • impala-jdbc41(impala-jdbc连接)
  • com.alibaba.druid(阿里巴巴数据源)
  • mybatis-spring-boot-starter(SpringBoot整合Mybatis包)
  • lombok(简化代码)
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.data</groupId>
    <artifactId>impala-connect</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>impala-connect</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>impala</groupId>
            <artifactId>impala-jdbc41</artifactId>
            <version>2.6.4.1005</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.14</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  1. 配置文件application.yml
    主要配置端口号、Impala连接、Mybatis扫描
server:
  port: 9001

spring:
  application:
    name: impala-connect
  impala:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:impala://xx.xx.xx.xx:21050/default
    driver-class-name: com.cloudera.impala.jdbc41.Driver
    # 初始化大小,最小,最大
    initialSize: 5
    minIdle: 5
    maxIdle: 100
    maxActive: 200
    # 配置获取连接等待超时的时间
    maxWait: 5000
    # 每次使用连接时进行校验,会影响系统性能。默认为false
    testOnBorrow: false
    #
    testOnReturn: false
    # 验证使用的SQL语句
    validationQuery: SELECT 1
    # 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除
    testWhileIdle: true
    # 每30秒运行一次空闲连接回收器(默认-1)
    timeBetweenEvictionRunsMillis: 30000
    # 池中的连接空闲30分钟后被回收(默认30分钟)
    minEvictableIdleTimeMillis: 1800000
    # 在每次空闲连接回收器线程(如果有)运行时检查的连接数量(设置为和maxIdle一样)
    numTestsPerEvictionRun: 100

mybatis:
  type-aliases-package: com.data.model
  impala-mapper-locations: classpath*:impalaMapping/*.xml

Impala连接配置

这个配置主要使用阿里巴巴数据源配置Impala的一些连接配置,如路径、驱动以及mybatis的扫描包路径等。

package com.data.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.data.mapper.impala", sqlSessionFactoryRef = "impalaSessionFactory")
public class ImpalaSourceConfig {
    @Value("${spring.impala.url}")
    private String url;

    @Value("${spring.impala.driver-class-name}")
    private String driverClass;

    @Value("${spring.impala.maxActive}")
    private Integer maxActive;

    @Value("${spring.impala.initialSize}")
    private Integer initialSize;

    @Value("${spring.impala.minIdle}")
    private Integer minIdle;

    @Value("${spring.impala.maxWait}")
    private Integer maxWait;

    @Value("${mybatis.type-aliases-package}")
    private String PACKAGE;

    @Value("${mybatis.impala-mapper-locations}")
    private String LOCATION;

    @Bean(name = "impalaDataSource")
    public DataSource impalaDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setMaxActive(maxActive);
        dataSource.setInitialSize(initialSize);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxWait(maxWait);
        return dataSource;
    }

    @Bean(name = "impalaTransactionManager")
    public DataSourceTransactionManager impalaTransactionManager() {
        return new DataSourceTransactionManager(impalaDataSource());
    }

    @Bean(name = "impalaSessionFactory")
    public SqlSessionFactory impalaSessionFactory() throws Exception {
        final SqlSessionFactoryBean SESSIONFACTORY = new SqlSessionFactoryBean();
        SESSIONFACTORY.setDataSource(impalaDataSource());
        SESSIONFACTORY.setTypeAliasesPackage(PACKAGE);
        System.out.println("------------------->"+PACKAGE);
        System.out.println("---------------------->"+LOCATION);
        SESSIONFACTORY.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(LOCATION));
        SESSIONFACTORY.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);//该配置将数据库中下划线自动转成驼峰命名的变量,并且只针对自定义的实体类生效,对map不生效的
        return SESSIONFACTORY.getObject();
    }
}

搭建实例

  • model:存放实体类,这里使用Lombok的@Data注释,可以自动添加getter/setter等
package com.data.model;

import lombok.Data;

@Data
public class Person {
    private Integer id;
    private String name;
}

  • mapper:持久层,添加@Repository注解,声明其是存储层bean
package com.data.mapper.impala;

import com.data.model.Person;
import org.springframework.stereotype.Repository;

@Repository
public interface ImpalaMapper {
    void executeSql(Person person);
}

  • ImpalaMapper.xml:在这里写sql语句
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.data.mapper.impala.ImpalaMapper">

    <insert id="executeSql" parameterType="com.data.model.Person">
        insert into test_person values(#{id},#{name});
    </insert>


</mapper>

  • service:服务层,添加@Component,把普通pojo实例化到spring容器中
package com.data.services;

import com.data.mapper.impala.ImpalaMapper;
import com.data.model.Person;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ImpalaService {

    @Resource
    private ImpalaMapper impalaMapper;

    public void executeSql(Person person) {
        try {
            impalaMapper.executeSql(person);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • controller:控制层,添加@RestController,相当于@RequestBody+@Controller
package com.data.controller;

import com.data.model.Person;
import com.data.services.ImpalaService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ImpalaController {

    @Resource
    private ImpalaService impalaService;

    @GetMapping("/test")
    public String insertPerson() {
        Person person = new Person();
        person.setId(1);
        person.setName("张三");
        impalaService.executeSql(person);
        return "新增记录成功!";
    }
}

验证测试

启动ImpalaConnectApplication.class,打开http://localhost:9001/test,若能出现如此画面
在这里插入图片描述
说明你已经成功了,接下来我们去Impala数据库去验证一下,执行命令select * from test_person,出现下面的画面,恭喜你已经完成这一整个项目啦!!!
在这里插入图片描述

总结

最后,我们来总结一下,首先导入相应的依赖包以及配置好application.yml,接着使用阿里巴巴数据源搭建好impala连接,然后根据自己的需求编写中间的实现逻辑,最后就可以去验证自己的功能啦。

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
好的,下面是一个简单的 Spring Boot 整合 MyBatis 和 Vue 的案例。 1. 创建 Spring Boot 项目 使用 Spring Initializr 创建一个新的 Spring Boot 项目,包含以下依赖: - Spring Web - MyBatis Framework - MySQL Driver - Lombok 2. 配置数据库 在 application.properties 文件中配置数据库连接信息,示例: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=123456 ``` 3. 配置 MyBatis 在 Spring Boot 中,可以使用 MyBatis 的注解方式进行配置。创建一个 UserMapper 接口,示例: ```java @Mapper public interface UserMapper { @Select("select * from user") List<User> getAllUsers(); } ``` 4. 创建 Vue 项目 使用 Vue CLI 创建一个新的 Vue 项目,使用 Element UI 进行 UI 设计。 5. 配置前端 API 在 Vue 项目中创建一个 api.js 文件,用来配置前端 API,示例: ```javascript import axios from 'axios' export default { getAllUsers () { return axios.get('/api/users') } } ``` 6. 创建前端组件 在 Vue 项目中创建一个 UserList.vue 组件,用来显示用户列表,示例: ```vue <template> <div> <el-table :data="users"> <el-table-column prop="id" label="ID"></el-table-column> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> </el-table> </div> </template> <script> import api from '@/api' export default { data () { return { users: [] } }, mounted () { api.getAllUsers().then(res => { this.users = res.data }) } } </script> ``` 7. 创建后端控制器 在 Spring Boot 项目中创建一个 UserController 控制器,用来处理前端 API 请求,示例: ```java @RestController @RequestMapping("/api") public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/users") public List<User> getAllUsers() { return userMapper.getAllUsers(); } } ``` 8. 运行应用程序 使用命令行或者 IDE 运行应用程序,访问 http://localhost:8080 即可看到用户列表。 以上就是一个简单的 Spring Boot 整合 MyBatis 和 Vue 的案例,实现了一个简单的用户列表功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值