基于Mybatis的语音播报随机点到系统

基于Mybatis的语音播报随机点到系统

引言

随机把数据库中的name列数据读取出来,实现随机性。
并把语音播报出来。每次语音播报,线程休眠2000毫秒,实现友好性。

懒人通道

项目源码下载地址

正文

- 文件结构

在这里插入图片描述

- 前期准备

点击下载jacob工具
解压后,把所有jacob开头的文件复制到jdk安装目录的bin目录下

- 导入依赖

		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.13</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.13</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.13</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>

        <dependency>
            <groupId>com.hynnet</groupId>
            <artifactId>jacob</artifactId>
            <version>1.18</version>
        </dependency>

- Config类


import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@ComponentScan("com.text")
//扫描mapper目录
@MapperScan("com.text.mapper")
//主配置类
@Configuration
public class Configration {

    //    第一种方式
//    编写datasource
    @Bean
    public DataSource dataSource(){
        PooledDataSource dataSource=new PooledDataSource();
//        加载驱动
        dataSource.setDriver("com.mysql.cj.jdbc.Driver");
//        编写url,注意这里的db10是我的数据库名
        dataSource.setUrl("jdbc:mysql://localhost:3306/db10?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8");
//        编写账号
        dataSource.setUsername("root");
//        编写密码
        dataSource.setPassword("123456");
        return dataSource;
    }

    //    编写SqlSessionFactoryBean
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(@Autowired DataSource dataSource){
        SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean;

    }

}

- Student实体类

import lombok.Data;

//为了打印tostring方法
@Data
public class Student {
    int id;
    String name;
    String sex;
}

- Mapper接口

import com.text.entry.Student;
import org.apache.ibatis.annotations.Select;

import java.util.List;

//mapper,这里是数据源,用来操作数据库
//这是一个接口
public interface Mapper {
    @Select("select name from Student where id like #{sex}")
    List<String> findByName(String sex);
}

- Service接口

import com.text.entry.Student;
import java.util.List;

//service接口
public interface Service {
    List<String> findByName(String sex);
}

- Service接口实现类

import com.text.entry.Student;
import com.text.mapper.Mapper;
import com.text.service.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

//bean
@Component
//实现接口
public class ServiceImpl implements Service {
    //    数据源
    @Resource
    Mapper mapper;
    public List<String> findByName(String sex) {
        return mapper.findByName("%");
    }
}

- jacob工具类

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.springframework.stereotype.Controller;
@Controller
public class JacobTestController {
    /**
     * 语音转文字并播放func
     *
     * @param text
     */

    //点名系统的工具类-----网上抄的


    public static void textToSpeech(String text) {
        ActiveXComponent ax = null;
        try {
            ax = new ActiveXComponent("Sapi.SpVoice");

            // 运行时输出语音内容
            Dispatch spVoice = ax.getObject();
            // 音量 0-100
            ax.setProperty("Volume", new Variant(100));
            // 语音朗读速度 -10 到 +10
            ax.setProperty("Rate", new Variant(-4));
            // 执行朗读
            Dispatch.call(spVoice, "Speak", new Variant(text));

            // 下面是构建文件流把生成语音文件

            ax = new ActiveXComponent("Sapi.SpFileStream");
            Dispatch spFileStream = ax.getObject();

            ax = new ActiveXComponent("Sapi.SpAudioFormat");
            Dispatch spAudioFormat = ax.getObject();

            // 设置音频流格式
            Dispatch.put(spAudioFormat, "Type", new Variant(22));
            // 设置文件输出流格式
            Dispatch.putRef(spFileStream, "Format", spAudioFormat);
            // 调用输出 文件流打开方法,创建一个.wav文件
            Dispatch.call(spFileStream, "Open", new Variant("./text.wav"), new Variant(3), new Variant(true));
            // 设置声音对象的音频输出流为输出文件对象
            Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
            // 设置音量 0到100
            Dispatch.put(spVoice, "Volume", new Variant(100));
            // 设置朗读速度
            Dispatch.put(spVoice, "Rate", new Variant(-4));
            // 开始朗读
            Dispatch.call(spVoice, "Speak", new Variant(text));

            // 关闭输出文件
            Dispatch.call(spFileStream, "Close");
            Dispatch.putRef(spVoice, "AudioOutputStream", null);

            spAudioFormat.safeRelease();
            spFileStream.safeRelease();
            spVoice.safeRelease();
            ax.safeRelease();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

- main方法


import com.text.config.Configration;
import com.text.service.Service;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.List;
import java.util.Random;

import static com.text.speaker.JacobTestController.textToSpeech;

public class Main {

    public static void main(String[] args) throws InterruptedException {
//        ===================1.前期准备,加载信息========================
//        加载配置类
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(Configration.class);
//        加载service接口,这里并没有直接调用service实现类
        Service service = context.getBean(Service.class);
//        测试
//        接收数据库中的学生名字
        List<String> and =service.findByName("1");


//        ====================2.工具准备================================
//        标记变量,少写了一个数据库,所以这里要写一个标记变量
        int sum=0;
//        哨兵,模仿自快排的哨兵,记录当前的姓名在数列中的索引
        int[] solder =new int [service.findByName("1").size()];
//        随机数字,旨在生成一个随机变量,然后按照索引点名
        Random random=new Random();

//        ====================3.开始点名=======================
        while (sum<service.findByName("1").size()){//记录数字在这里起作用
            int a=random.nextInt((solder.length));//哨兵在这里起作用
            if(solder[a]==0){
//                System.out.println(a);//这行语句用于测试
                System.out.println(and.get(a));//这里打印当前随机"点到"的姓名
                textToSpeech(and.get(a));//这里把随机颠倒的姓名念出来,即语音生成文字
                Thread.sleep(2000);//考虑到每次点名,需要按一次enter太麻烦,这里让它自动运行,时间间隔两秒
                solder[a]=1;//哨兵再次起作用
                sum++;//标记函数增加
            }
        }
    }
}

- 数据库文件

/*
Navicat MySQL Data Transfer

Source Server         : fiveGods
Source Server Version : 80016
Source Host           : localhost:3306
Source Database       : db10

Target Server Type    : MYSQL
Target Server Version : 80016
File Encoding         : 65001

Date: 2022-03-10 19:45:12
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `sex` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=214 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '魏振华', '男');
INSERT INTO `student` VALUES ('2', '王培贤', '男');
INSERT INTO `student` VALUES ('6', '蔡文博', '男');
INSERT INTO `student` VALUES ('7', '刘嘉骏', '男');
INSERT INTO `student` VALUES ('8', '苗宇阳', '男');
INSERT INTO `student` VALUES ('9', '王超燃', '男');
INSERT INTO `student` VALUES ('10', '刘泽宇', '男');
INSERT INTO `student` VALUES ('11', '吴宇科', '男');
INSERT INTO `student` VALUES ('12', '吕强', '男');
INSERT INTO `student` VALUES ('13', '董诗雨', '男');
INSERT INTO `student` VALUES ('14', '庞栋', '男');
INSERT INTO `student` VALUES ('15', '李文龙', '男');
INSERT INTO `student` VALUES ('16', '韩凯', '男');
INSERT INTO `student` VALUES ('17', '程霖', '男');
INSERT INTO `student` VALUES ('18', '范怡林', '男');
INSERT INTO `student` VALUES ('19', '李灏', '男');
INSERT INTO `student` VALUES ('20', '詹思雨', '男');
INSERT INTO `student` VALUES ('21', '李陆豪', '男');
INSERT INTO `student` VALUES ('22', '薛艺佳', '男');
INSERT INTO `student` VALUES ('23', '王昊宇', '男');
INSERT INTO `student` VALUES ('24', '靳亚冰', '男');
INSERT INTO `student` VALUES ('25', '马朋轩', '男');
INSERT INTO `student` VALUES ('26', '李洋', '男');
INSERT INTO `student` VALUES ('27', '刘博威', '男');
INSERT INTO `student` VALUES ('28', '王育捷', '男');
INSERT INTO `student` VALUES ('29', '路峥', '男');
INSERT INTO `student` VALUES ('30', '周晓宇', '男');
INSERT INTO `student` VALUES ('31', '王龙龙', '男');
INSERT INTO `student` VALUES ('32', '吴紫薇', '男');
INSERT INTO `student` VALUES ('33', '闫兴', '男');
INSERT INTO `student` VALUES ('34', '常席通', '男');
INSERT INTO `student` VALUES ('35', '胡玉成', '男');
INSERT INTO `student` VALUES ('36', '李晨龙', '男');
INSERT INTO `student` VALUES ('37', '赵薇', '男');
INSERT INTO `student` VALUES ('38', '周博', '男');
INSERT INTO `student` VALUES ('39', '冷晨铭', '男');
INSERT INTO `student` VALUES ('40', '郭帅', '男');
INSERT INTO `student` VALUES ('41', '李世豪', '男');
INSERT INTO `student` VALUES ('42', '徐彦明', '男');
INSERT INTO `student` VALUES ('43', '熊巍', '男');
INSERT INTO `student` VALUES ('44', '李鑫', '男');
INSERT INTO `student` VALUES ('45', '闰腾飞', '男');
INSERT INTO `student` VALUES ('46', '岳兴', '男');
INSERT INTO `student` VALUES ('47', '李伊凯', '男');
INSERT INTO `student` VALUES ('48', '闫国浩', '男');
INSERT INTO `student` VALUES ('49', '王敬业', '男');
INSERT INTO `student` VALUES ('50', '张苗苗', '男');
INSERT INTO `student` VALUES ('51', '赵圆鑫', '男');
INSERT INTO `student` VALUES ('52', '陈又夫', '男');
INSERT INTO `student` VALUES ('53', '李栋', '男');
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

君问归期魏有期

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值