概述
基于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', '男');
最后
以上就是自然乌冬面为你收集整理的基于Mybatis的随机点名系统的全部内容,希望文章能够帮你解决基于Mybatis的随机点名系统所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复