基于Mybatis的随机点名系统

13 篇文章 0 订阅
12 篇文章 1 订阅

基于mybatis的随机点名系统

## 缺陷: jacob包导入不进去,一直报错,不然可以实现语音播报功能

导入依赖


        <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>

        <!-- https://mvnrepository.com/artifact/com.jacob/jacob 文字转语音 -->
        <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("%");
    }


}

main方法

import com.text.config.Configration;
import com.text.service.Service;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

//主方法
public class Main {
    public static void main(String[] args) throws InterruptedException {
//        加载配置类
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(Configration.class);
//        加载service接口,这里并没有直接调用service实现类
        Service service = context.getBean(Service.class);
//        测试
//        System.out.println(service.findByName("1"));
        List<String> and =service.findByName("1");


        int sum=0;
        int[] solder =new int [service.findByName("1").size()];
//        System.out.println(solder.length-1);
        Random random=new Random();


        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));
                Thread.sleep(2000);
                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-02 10:55:04
*/

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=8 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', 'wei', '男');
INSERT INTO `student` VALUES ('2', '测试2', '男');
INSERT INTO `student` VALUES ('6', '测试3', '男');
INSERT INTO `student` VALUES ('7', '测试4', '男');

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

君问归期魏有期

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

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

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

打赏作者

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

抵扣说明:

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

余额充值