SpringBoot MyBatis 整合(打成Jar在Azkaban上運行)

需求實現:SpringBoot 整合 MyBatis,從數據庫獲取數據轉換爲json,模擬post 請求推送數據到其他服務

1.創建表

數據庫使用MySQL
建表語句:

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(11) NOT NULL,
  `name` varchar(50) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `sex` int(11) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

插入數據:

INSERT INTO `student` VALUES ('1', '張三', '18', '1', '北京市朝陽區');
INSERT INTO `student` VALUES ('2', '王五', '20', '1', '天津市南開區');
INSERT INTO `student` VALUES ('3', '李麗', '22', '0', '上海市虹橋區');

2.創建項目

這裏寫圖片描述
Next,選擇SQL,選擇MyBatis即可
這裏寫圖片描述
Next,Finish即可
這裏寫圖片描述

3.配置 pom.xml

數據庫使用MySQL,數據源使用druid,json操作使用jackson,在pom.xml中配置即可,後面我們要講項目打包成jar,上傳到azkaban運行,所以pom.xml中需要配置maven的插件,完整的pom.xml文件如下

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.harvey</groupId>
    <artifactId>springboot-mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-mybatis</name>
    <description>springboot mybatis demo</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.4</version>
        </dependency>
    </dependencies>

    <!--<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>-->
    <build>
        <finalName>springboot-mybatis-demo</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.harvey.SpringbootMybatisApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

配置 application.properties

mybatis.typeAliasesPackage=com.harvey.domain
mybatis.mapperLocations=classpath:mapper/*.xml

# DataSouce
spring.datasource.url=jdbc:mysql://192.168.191.65:3306/springbootdemo?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-wait=10000
spring.datasource.min-idle=2
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.maxActive=10
spring.datasource.initial-size=5
spring.datasource.validation-query=SELECT 1
spring.datasource.test-on-borrow=false
spring.datasource.test-while-idle=true
spring.datasource.time-between-eviction-runs-millis=18800
spring.datasource.filters=stat

日誌配置,使用logback

<?xml version="1.0" encoding="UTF-8"?>

<configuration scan="false" scanPeriod="1 minutes">
    <property name="APP_NAME" value="tradeareadatapush-job"/>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender" >
        <encoder>
            <pattern>%d{yyyy-MM-dd.HH:mm:ss.SSS} %t %p %c{0} %X{traceId} : %m%n</pattern>
        </encoder>
    </appender>

    <root level="INFO" >
        <appender-ref ref="CONSOLE" />
    </root>
</configuration>

6.項目結構

這裏寫圖片描述
注意:banner.txt 是項目啓動時加載,默認是springboot,筆者從網上自己生成了一個,自定義banner訪問這個就可以,字符畫生成:
http://patorjk.com/software/taag/#p=display&f=Henry%203D&t=CC11001100%0A

7.實體類:student.java

package com.harvey.domain;

public class Student {
    private int id;
    private String name;
    private int age;
    private int sex;
    private String address;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", address='" + address + '\'' +
                '}';
    }
}

8.mapper.xml 及接口

package com.harvey.dao;

import com.harvey.domain.Student;

import java.util.List;

public interface StudentDao {

    List<Student> listStudentInfo();
}
<?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.harvey.dao.StudentDao" >
    <select id="listStudentInfo" resultType="com.harvey.domain.Student">
        SELECT
            id,
            `name`,
            age,
            sex,
            address
        FROM
            student
    </select>
</mapper>

9.Service 代碼

package com.harvey.service;

public interface IStudentService {

    void datapush();

}
package com.harvey.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.harvey.dao.StudentDao;
import com.harvey.domain.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements IStudentService {

    private static Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class);

    @Autowired
    private StudentDao studentDao; // 這裏會報錯,忽略即可,不會影響程序正常運行

    // 業務處理
    @Override
    public void datapush() {
        logger.info("--------------------學生信息數據推送任務開始執行----------");
        List<Student> students = studentDao.listStudentInfo();
        ObjectMapper objectMapper = new ObjectMapper();
        String result = null;
        try {
            result = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(students);
            logger.info("學生信息數據:\n" + result);
        } catch (JsonProcessingException e) {
            logger.error("數據轉換異常[List to Json]", e);
        }

        // 模擬http post 推送數據到其它數據服務
        try {
            Thread.sleep(1000);
            logger.info("數據推送成功!");
        } catch (InterruptedException e) {
            logger.error("服務異常!", e);
        }
    }
}

10.運行測試

項目創建後,會在創建項目Group中填寫的內容對應的包下生成一個類,就是項目得啓動入口類,默認代碼如下
這裏寫圖片描述
我們需要做一些修改,如下
這裏寫圖片描述
這樣做的目的是既可以直接運行測試,之後我們打包jar的時候,該類就是我的項目啓動入口類(main方法入口類)

啓動運行,查看控制檯,部分截圖
這裏寫圖片描述

11.打成jar,上傳到Azkaban運行

具體Azkaban的使用,筆者不做介紹,可以參考筆者博客:https://blog.csdn.net/hg_harvey/article/details/80521562

編寫腳本studentinfodatapush.sh(注意要賦可執行權限),內容如下

#!/bin/bash
# Author:harvey
# FileName:studentinfodatapush.sh
export JAVA_HOME=/home/hadoop/software/jdk
export PATH=$JAVA_HOME/bin:$PATH

start_time=`date +%s`
java -jar /home/hadoop/jar/springboot-mybatis-demo.jar
end_time=`date +%s`

elapse_time=$((${end_time}-${start_time}))
echo -e "\n exec jar takes ${elapse_time} seconds\n"

創建job,然後打包上傳到azkaban,studatapush.job內容如下

# studatapush.job
type = command
command = sh /home/hadoop/shell/studatapush.sh

azkaban運行,部分日誌截圖
這裏寫圖片描述
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章