写这个博客是想希望能够帮到码农们背单词,在Main直接可以运行,修改startTest的方法能够测试英文和中文,修改数字flag能够修改背单词的范围,能写1—9。
Main
package com.Main;
import com.utils.MybatisUtils;
import com.utils.StartTest;
import org.apache.ibatis.session.SqlSession;
import java.util.Scanner;
/**
* @author MoonLeaves
* @purpose
* @create 2020-10-28 10:30
*/
public class Main {
public static void main(String[] args) {
// 先获取sqlSession
SqlSession sqlSession = MybatisUtils.getSqlSession();
// 创建scanner
Scanner scanner = new Scanner(System.in);
// 通过密码后调用执行的方法
StartTest startTest = new StartTest();
// 在startTest里有两个方法,有两个操作,这里是一个
// startTest.StartTestChinese(1, sqlSession, scanner);
startTest.StartTestChinese(1, sqlSession, scanner);
System.out.println();
System.out.println("你真棒呢,本轮问题回答完毕了哦!");
scanner.close();
}
}
UserMapper
package com.Mapper;
import com.pojo.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.sql.SQLException;
import java.util.List;
/**
* @author MoonLeaves
* @purpose
* @create 2020-10-27 19:50
*/
public interface UserMapper {
// 自动添加的score
@Insert("insert into user (name,password,id,score) values (#{name},'123456',#{id},0);")
int addUserAutoScore(User user) throws SQLException;
// 设置score
@Insert("insert into user (name,password,id,score) values(#{name},#{password},#{id},#{score})")
int addUserSetScoreAndPassword(User user) throws SQLException;
// 删除用户
@Delete("delete from user where id = #{id}")
int deleteUserById(@Param("id") int id);
//根据id查找用户
@Select("select * from user where id=#{id}")
User selectUserById(@Param("id") int id);
/**
* 这个是写在sql里的
* 返回所有的用户
* @return User的List集合
*/
List<User> getAllUser();
}
UserMapper.xml
<?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.Mapper.UserMapper">
<select id="getAllUser" resultType="com.pojo.User">
select * from user ;
</select>
</mapper>
WordsMapper
package com.Mapper;
import com.pojo.Words;
import java.util.List;
/**
* @author MoonLeaves
* @purpose
* @create 2020-10-27 19:05
*/
public interface WordsMapper {
/**
* 获取所有对象
* @return Words的List集合
*/
List<Words> getAllWords();
/**
* 获取一个对象
* @param id
* @return com.pojo.Words
*/
Words getWordById(int id);
/**
* 这个是实际调用
* flag是传入的单词区间
* 返回一个words集合对象,也就是一个数据源
* @param flag
* @return Words的List集合
*/
List<Words> getWords(int flag);
/**
* 获取总的单词数目个数long
* @return long
*/
long getTotalNumberOfWords();
}
WordsMapper.xml
<?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.Mapper.WordsMapper">
<!-- 获取一个对象-->
<select id="getWordById" resultType="com.pojo.Words" parameterType="int">
select * from words where id=#{id};
</select>
<!-- 获取所有对象-->
<select id="getAllWords" resultType="com.pojo.Words">
select * from words ;
</select>
<!-- 实际调用-->
<select id="getWords" resultType="com.pojo.Words" parameterType="int">
select * from words where 1=1
<if test="#{flag}!=null and flag==1">
and id <= 500 && id >=1
</if>
<if test="#{flag}!=null and flag==2">
and id <= 1000 && id >=501
</if>
<if test="#{flag}!=null and flag==3">
and id <=1500 && id >=1001
</if>
<if test="#{flag}!=null and flag==4">
and id <= 2000 && id >=1501
</if>
<if test="#{flag}!=null and flag==5">
and id <= 2500 && id >=2001
</if>
<if test="#{flag}!=null and flag==6">
and id <=3000 && id >=2501
</if>
<if test="#{flag}!=null and flag==7">
and id <= 3500 && id >=3001
</if>
<if test="#{flag}!=null and flag==8">
and id <= 4000 && id >=3501
</if>
<if test="#{flag}!=null and flag==9">
and id <=4500 && id >=4001
</if>
</select>
<!-- 返回总单词数目-->
<select id="getTotalNumberOfWords" resultType="long">
select count(*) from words;
</select>
</mapper>
User
package com.pojo;
import java.util.Objects;
/**
* @author MoonLeaves
* @purpose
* @create 2020-10-27 15:27
*/
public class User {
private String name;
private int score;
private int id;
private String password;
public User(String name, String password, int id, int score ){
this.name = name;
this.score = score;
this.id = id;
this.password = password;
}
public User(String name, String password , int id) {
this.name = name;
this.id = id;
this.password = password;
}
@Override
public String toString() {
return "com.pojo.User{" +
"name='" + name + '\'' +
", score=" + score +
", id=" + id +
", password='" + password + '\'' +
'}';
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
}
public String getName() {
return name;
}
public User(String name, int id) {
this.name = name;
this.id = id;
// 创建一个用户时,基础分数都为0
this.score=0;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return score == user.score &&
id == user.id &&
Objects.equals(name, user.name);
}
@Override
public int hashCode() {
return 0;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Words
package com.pojo;
/**
* @author MoonLeaves
* @purpose
* @create 2020-10-27 15:37
*/
public class Words {
private String Word;
private String ChineseMeaning;
private int id;
private int group;
public Words(String word, String chineseMeaning, int id, int group) {
Word = word;
ChineseMeaning = chineseMeaning;
this.id = id;
this.group = group;
}
public Words(String word, String chineseMeaning, int id) {
Word = word;
ChineseMeaning = chineseMeaning;
this.id = id;
this.group=0;
}
public String getWord() {
return Word;
}
public void setWord(String word) {
Word = word;
}
public String getChineseMeaning() {
return ChineseMeaning;
}
public void setChineseMeaning(String chineseMeaning) {
ChineseMeaning = chineseMeaning;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getGroup() {
return group;
}
public void setGroup(int group) {
this.group = group;
}
@Override
public String toString() {
return "com.pojo.Words{" +
"Word='" + Word + '\'' +
", ChineseMeaning='" + ChineseMeaning + '\'' +
", id=" + id +
", group=" + group +
'}';
}
public Words() {
}
}
JundgeWords
package com.utils;
import com.Mapper.WordsMapper;
import com.pojo.Words;
import org.apache.ibatis.session.SqlSession;
import java.util.*;
/**
* @author MoonLeaves
* @purpose
* @create 2020-10-27 19:56
*/
public class JudgeWords {
public static int num=0;
/**
* 这个函数被testChineseMeaning所调用,目的是判断你做的答案是否正确
* <p>
* 在里面创建两个数组,ChineseArray和TrueChineseArray,分别存储测试者回答的每个词语和答案的每个词语
* 创建一个SetList集合,里面事先存储ChineseArray的每个值,然后利用add方法判断TrueChineseArray的每个值是否可以加入
* 如果能添加,就是不相同,即回答错误
*
* @param chineseMeaning
* @param word
* @return
*/
public static boolean JudgeChineseMeaning(String chineseMeaning, Words word) {
boolean result = false;
String regex = "\\s";
//自己的回答
Set<String> SetList = new HashSet<>();
//每一项,自己回答的长度
String[] ChineseArray = chineseMeaning.split(regex);
for (int i = 0; i < ChineseArray.length; i++) {
String s = ChineseArray[i];
SetList.add(s);
}
String TrueChinese = word.getChineseMeaning();
//正确答案的数组
String[] TrueChineseArray = TrueChinese.split(regex);
for (int i = 0; i < TrueChineseArray.length; i++) {
if (!SetList.add(TrueChineseArray[i])) {
result = true;
break;
}
}
return result;
}
/**
* 这个函数被testEnglish所调用,目的是判断你做的答案是否正确
*
* @param English
* @param word
* @return
*/
public static boolean JudgeEnglish(String English, Words word) {
return English.equals(word.getWord());
}
/**
* 这个函数是核心程序,传入数据集,然后进行流程
*
* @param wordsList
* @param sqlSession
*/
public static void testEnglish(List<Words> wordsList, SqlSession sqlSession, Scanner scanner) {
for (Words word : wordsList) {
System.out.println("目前得分是"+num);
System.out.print("请看题目: ");
String chineseMeaning = word.getChineseMeaning();
System.out.println(chineseMeaning);
System.out.println("请输入单词的英文:");
String EnglishAnswer = scanner.next();
boolean b = JudgeWords.JudgeEnglish(EnglishAnswer, word);
if (b) {
System.out.println("回答正确,你真棒呢\n");
num++;
} else {
System.out.println("回答错误哦,请再次尝试");
EnglishAnswer = scanner.next();
if (JudgeWords.JudgeEnglish(EnglishAnswer, word)) {
System.out.println("回答正确,你真棒呢\n");
num++;
} else
System.out.println("在巩固一下吧,正确答案是:" + word.getWord());
System.out.println();
}
}
}
/**
* 这个函数是核心程序,传入数据集,然后进行流程
*
* @param wordsList
* @param sqlSession
*/
public static void testChineseMeaning(List<Words> wordsList, SqlSession sqlSession, Scanner scanner) {
for (Words word : wordsList) {
System.out.print("请看题目: ");
String EnglishWord = word.getWord();
System.out.println(EnglishWord);
System.out.println("请输入单词的汉语:");
String ChineseAnswer = scanner.nextLine();
boolean b = JudgeWords.JudgeChineseMeaning(ChineseAnswer, word);
if (b) {
System.out.println("回答正确,你真棒呢\n");
num++;
System.out.println("目前得分是"+num);
} else {
System.out.println("回答错误哦,请再次尝试");
String TwoChineseAnswer = scanner.nextLine();
if (JudgeWords.JudgeChineseMeaning(TwoChineseAnswer, word)) {
System.out.println("回答正确,你真棒呢\n");
num++;
System.out.println("目前得分是"+num);
} else
System.out.println("在巩固一下吧,正确答案是:" + word.getChineseMeaning());
System.out.println();
}
}
}
/**
* 返回数据集
*
* @param flag
* @param sqlSession
*/
public static List<Words> ChooseRange(int flag, SqlSession sqlSession) {
WordsMapper mapper = sqlSession.getMapper(WordsMapper.class);
List<Words> wordsList = mapper.getWords(flag);
return wordsList;
}
public static List<Words> GetRandom(int flag, SqlSession sqlSession) {
// 起始数
// int base=(flag-1)*500;
WordsMapper mapper = sqlSession.getMapper(WordsMapper.class);
List<Words> wordsList = mapper.getWords(flag);
Collections.shuffle(wordsList);
return wordsList;
}
}
MybatisUtils
package com.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
/**
* @author MoonLeaves
* @purpose
* @create 2020-10-27 15:44
*/
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory = null;
private static InputStream inputStream = null;
static {
try {
String resource = "MyEnglish1.0Config.xml";
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// 设置自动提交
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
}
StartTest
package com.utils;
import com.pojo.Words;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
import java.util.Scanner;
/**
* @author MoonLeaves
* @purpose 这个工具类是实际操作的类,不设置成static,为了数据的安全,只有在用户密码正确时才创建这个对象
* @create 2020-10-28 18:57
*/
public class StartTest {
public void StartTestEnglish(int flag, SqlSession sqlSession,Scanner scanner){
List<Words> wordsList=JudgeWords.GetRandom(flag,sqlSession);
JudgeWords.testEnglish(wordsList,sqlSession,scanner);
}
public void StartTestChinese(int flag, SqlSession sqlSession,Scanner scanner){
List<Words> wordsList=JudgeWords.GetRandom(flag,sqlSession);
JudgeWords.testChineseMeaning(wordsList,sqlSession,scanner);
}
// public void StartTestEnglishRandom(int flag, SqlSession sqlSession,Scanner scanner){
// List<com.pojo.Words> wordsList=com.utils.JudgeWords.GetRandom(flag,sqlSession);
// }
}
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/myproject?useSSL=false
username=root
password=djq
MyEnglish1.0Config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 利用properties-->
<properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 三种方式之一-->
<package name="com.Mapper"/>
</mappers>
</configuration>
pom.xml
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/Java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.* </include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
数据库
见一个user表,words表