java08实验
实验
2023-09-11 14:16:58 时间
读取实验内容一中的文本文件内容显示参与游戏的所有玩家昵称,登陆密码,积分,比赛次数(按照积分进行排序),界面方向参考第11-12章,可以选择swing或者javaFx(https://code.makery.ch/zh-cn/library/javafx-tutorial/)
com.lxy.dao.PlayerMapper
package com.lxy.dao;
import com.lxy.pojo.Player;
import java.util.List;
/**
* @author ChristinaXinny
* @create 2020-11-16 10:14 上午
*/
public interface PlayerMapper {
List<Player> getAllPlayer();
}
com.lxy.pojo.Player
package com.lxy.pojo;
/**
* @author ChristinaXinny
* @create 2020-11-16 10:13 上午
*/
public class Player {
private String name;
private String password;
private int score;
private int time;
@Override
public String toString() {
return "Player{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
", score=" + score +
", time=" + time +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public Player() {
}
public Player(String name, String password, int score, int time) {
this.name = name;
this.password = password;
this.score = score;
this.time = time;
}
}
com.lxy.utils.MyUtil
package com.lxy.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 ChristinaXinny
* @create 2020-11-16 10:43 上午
*/
/*
这里这个是一个sql的语句制造的工厂
resource 获取到的是一个 连接sql数据库的配置文件
把这个配置文件运行之后获得到一个来自数据库数据到流
工厂到建造者FactoryBuilder拿了这个流去建造build这个工厂Factory
最后这个工厂有一个出口给各个不同的地方 每个一个sql语句sqlSessionFactory.openSession()
*/
public class MyUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
// 使用mybatis第一步:获取SqlSessionFactory对象
String resource = "mybatis-config.xml";
// 采用流将加载进来
InputStream inputStream = Resources.getResourceAsStream(resource);
// 通过builder把流加载进来
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//获取SqlSession连接
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
com/lxy/dao/PlayerMapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lxy.dao.PlayerMapper">
<select id="getAllPlayer" resultType="player">
select * from user
</select>
</mapper>
src/main/resources/db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/school_cy
username=root
password=qwe123123
src/main/resources/mybatis-config.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--properties财产的意思:连接数据库的信息文件-->
<properties resource="db.properties"/>
<settings>
<!--日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--驼峰命名 camel case映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<!--改名字 在mapper中不用写全路径-->
<package name="com.lxy.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--这里是读取前面的db.properties,没有写.properties可以直接写driver、url、username、password-->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--mapper注册-->
<!--有三种:class、resource、package-->
<package name="com.lxy.dao"/>
<!--<mapper ="com.lxy.dao.PlayerMapper"/>-->
</mappers>
</configuration>
ConnectionMysqlTest
import com.lxy.dao.PlayerMapper;
import com.lxy.pojo.Player;
import com.lxy.utils.MyUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import java.util.List;
/**
* @author ChristinaXinny
* @create 2020-11-16 10:46 上午
*/
public class ConnectionMysqlTest {
@Test
public void test1(){
SqlSession sqlSession = MyUtil.getSqlSession();
PlayerMapper mapper = sqlSession.getMapper(PlayerMapper.class);
List<Player> allPlayer = mapper.getAllPlayer();
allPlayer.forEach(System.out::println);
sqlSession.close();
}
}
SuoHaPlayerTest
/**
* @author ChristinaXinny
* @create 2020-11-16 9:59 上午
*/
/**
*
*/
import com.lxy.dao.PlayerMapper;
import com.lxy.pojo.Player;
import com.lxy.utils.MyUtil;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import org.apache.ibatis.session.SqlSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
/**
* @author ChristinaXinny
* @create 2020-11-16 10:46 上午
*/
public class SuoHaPlayerTest extends Application {
/**
* @param args
*/
private TableView<Player> table = new TableView();
ObservableList<Player> data = FXCollections.observableArrayList();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(new Group());
stage.setTitle("Welcome to SuoHa game info!");
stage.setWidth(623);
stage.setHeight(520);
final Label label = new Label("梭哈游戏排行榜");
label.setFont(new Font("Arial", 20));
table.setEditable(true);
TableColumn PNameCol = new TableColumn("Name");
PNameCol.setMinWidth(200);
TableColumn PScoreCol = new TableColumn("Score");
PScoreCol.setMinWidth(100);
TableColumn PPasswordCol = new TableColumn("Password");
PPasswordCol.setMinWidth(200);
TableColumn PMatchTimesCol = new TableColumn("MatchTimes");
PMatchTimesCol.setMinWidth(100);
table.getColumns().addAll(PNameCol, PScoreCol, PPasswordCol, PMatchTimesCol);
Button BRefresh = new Button("Refresh");
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table, BRefresh);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
SqlSession sqlSession = MyUtil.getSqlSession();
PlayerMapper mapper = sqlSession.getMapper(PlayerMapper.class);
List<Player> allPlayer = mapper.getAllPlayer();
for (Player player : allPlayer) {
data.add(player);
PNameCol.setCellValueFactory(new PropertyValueFactory<Player, String>("name"));
PPasswordCol.setCellValueFactory(new PropertyValueFactory<Player, String>("password"));
PScoreCol.setCellValueFactory(new PropertyValueFactory<Player, Integer>("score"));
PMatchTimesCol.setCellValueFactory(new PropertyValueFactory<Player, Integer>("time"));
table.setItems(data);
}
sqlSession.close();
}
}
子工程的pom.xml文件
UniversityCY/sy08/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">
<parent>
<artifactId>MyBatisKuang</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sy08</artifactId>
</project>
父工程的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>org.example</groupId>
<artifactId>MyBatisKuang</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!--子工程-->
<modules>
<module>sy08</module>
</modules>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>