Archived
0

Hello, MyBatis!

This commit is contained in:
2019-02-26 11:44:45 +03:00
parent 07e8e0c366
commit 6d990d1fb4
16 changed files with 323 additions and 455 deletions

27
pom.xml
View File

@@ -11,6 +11,9 @@
<properties>
<java.encoding>UTF-8</java.encoding>
<java.version>1.8</java.version>
<project.build.sourceEncoding>${java.encoding}</project.build.sourceEncoding>
<project.reporting.outputEncoding>${java.encoding}</project.reporting.outputEncoding>
<bukkit.version>1.12-R0.1</bukkit.version>
</properties>
@@ -22,15 +25,25 @@
</repositories>
<dependencies>
<!-- SPIGOT/BUKKIT -->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>${bukkit.version}-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- COMPONENTS -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
@@ -57,6 +70,20 @@
<argLine>-Dfile.encoding=${java.encoding}</argLine>
</configuration>
</plugin>
<!-- Fat jar -->
<!-- mvn assebly:single -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<finalName>${project.artifactId}-${project.version}-fat</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -3,24 +3,14 @@ package ru.prisonlife.api;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import ru.prisonlife.api.store.CachedDaoTask;
import ru.prisonlife.api.store.DailyRespectTaskDao;
import ru.prisonlife.api.store.PlayerStatsDao;
import ru.prisonlife.api.store.SQLStore;
import java.sql.SQLException;
public class PrisonAPI extends JavaPlugin {
public static final String PLUGIN_PATH = "plugins/PrisonManager";
public static final Plugin plugin = Bukkit.getPluginManager().getPlugin("PrisonAPI");
private static SQLStore sqlStore;
private static PlayerStatsDao playerStatsDao;
private static DailyRespectTaskDao dailyRespectTaskDao;
private BukkitTask playeryStatsDaoTask;
private BukkitTask dailyRespectTaskDaoTask;
public enum Factions {
NONE(0), ASIANS(1), LATINOS(2), NIGGAZ(3), POLICE(4);
@@ -50,14 +40,6 @@ public class PrisonAPI extends JavaPlugin {
return sqlStore;
}
public static PlayerStatsDao getPlayerStatsDao() {
return playerStatsDao;
}
public static DailyRespectTaskDao getDailyRespectTaskDao() {
return dailyRespectTaskDao;
}
@Override
public void onEnable() {
saveDefaultConfig();
@@ -66,38 +48,6 @@ public class PrisonAPI extends JavaPlugin {
final String dbUser = getConfig().getString("database.user");
final String dbPasswd = getConfig().getString("database.password");
try {
sqlStore = new SQLStore(dbUrl, dbUser, dbPasswd);
playerStatsDao = new PlayerStatsDao(sqlStore);
dailyRespectTaskDao = new DailyRespectTaskDao(sqlStore);
playeryStatsDaoTask = new CachedDaoTask(playerStatsDao)
.runTaskLaterAsynchronously(this, 20 * (60 * 1000));
dailyRespectTaskDaoTask = new CachedDaoTask(dailyRespectTaskDao)
.runTaskLaterAsynchronously(this, 20 * (60 * 1000));
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onDisable() {
playeryStatsDaoTask.cancel();
dailyRespectTaskDaoTask.cancel();
try {
if (!sqlStore.getConnection().isClosed()) {
sqlStore.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlStore = null;
playerStatsDao = null;
dailyRespectTaskDao = null;
}
playeryStatsDaoTask = null;
dailyRespectTaskDaoTask = null;
}
}

View File

@@ -1,90 +0,0 @@
package ru.prisonlife.api.store;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ConcurrentSkipListMap;
public abstract class AbstractCachedDao<E> implements AutoCloseable {
private SQLStore store;
private Map<UUID, E> cache = new ConcurrentSkipListMap<>();
public AbstractCachedDao(SQLStore store) {
this.store = store;
}
public Optional<E> findByUuid(UUID uuid) {
E entity;
if ((entity = cache.get(uuid)) != null) {
return Optional.of(entity);
}
final Connection connection = store.getConnection();
try (final PreparedStatement statement = connection.prepareStatement(getSelectSQL())) {
statement.setString(1, uuid.toString());
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
entity = mappingResultEntity(resultSet);
cache.put(uuid, entity);
return Optional.of(entity);
} else {
//FIXME need log
return Optional.empty();
}
}
} catch (SQLException e) {
e.printStackTrace(); //FIXME need log
return Optional.empty();
}
}
/**
* Запись кэша в БД и последующий его сброс
*/
public void flush() {
if (cache.isEmpty()) {
return;
}
//TODO думаю лучше будет разделить на порции по N обновлений за один раз
final String updateSql = generateUpdateSQL();
final StringJoiner sj = new StringJoiner(";");
for (int i = 0; i < cache.size(); i++) {
sj.add(updateSql);
}
final Connection connection = store.getConnection();
try (final PreparedStatement statement = connection.prepareStatement(sj.toString())) {
Iterator<E> iterator = cache.values().iterator();
for (int i = 1; i < cache.size(); i += countFields()) {
final E entity = iterator.next();
mappingUpdateEntity(statement, entity, i);
}
statement.executeLargeUpdate();
} catch (SQLException e) {
e.printStackTrace(); //FIXME need log
}
cache.clear();
}
@Override
public void close() throws Exception {
store.close();
}
abstract String getSelectSQL();
abstract E mappingResultEntity(ResultSet resultSet)throws SQLException;
abstract String generateUpdateSQL();
abstract int countFields();
abstract void mappingUpdateEntity(final PreparedStatement statement, E entity, int index) throws SQLException;
}

View File

@@ -1,17 +0,0 @@
package ru.prisonlife.api.store;
import org.bukkit.scheduler.BukkitRunnable;
public class CachedDaoTask extends BukkitRunnable {
private AbstractCachedDao abstractCachedDao;
public CachedDaoTask(AbstractCachedDao abstractCachedDao) {
this.abstractCachedDao = abstractCachedDao;
}
@Override
public void run() {
abstractCachedDao.flush();
}
}

View File

@@ -1,43 +0,0 @@
package ru.prisonlife.api.store;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DailyRespectTaskDao extends AbstractCachedDao<DailyRespectTaskEntity> {
public DailyRespectTaskDao(SQLStore store) {
super(store);
}
@Override
String getSelectSQL() {
return "SELECT COOLDOWN FROM daily_respect_task WHERE UUID = ?";
}
@Override
DailyRespectTaskEntity mappingResultEntity(ResultSet resultSet) throws SQLException {
DailyRespectTaskEntity entity = new DailyRespectTaskEntity();
entity.setCooldownRespect(resultSet.getInt("COOLDOWN"));
return entity;
}
@Override
String generateUpdateSQL() {
return "UPDATE daily_respect_task " +
"SET COOLDOWN = ? " +
"WHERE UUID = ?";
}
@Override
int countFields() {
return 1;
}
@Override
void mappingUpdateEntity(PreparedStatement statement, DailyRespectTaskEntity entity, int index) throws SQLException {
statement.setInt(index, entity.getCooldownRespect());
statement.setString(index + 1, entity.getUuid().toString());
}
}

View File

@@ -1,26 +1,25 @@
package ru.prisonlife.api.store;
import java.util.Objects;
import java.util.UUID;
public class DailyRespectTaskEntity {
private UUID uuid;
private int cooldownRespect;
private String uuid;
private Integer cooldownRespect;
public UUID getUuid() {
public String getUuid() {
return uuid;
}
public void setUuid(UUID uuid) {
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getCooldownRespect() {
public Integer getCooldownRespect() {
return cooldownRespect;
}
public void setCooldownRespect(int cooldownRespect) {
public void setCooldownRespect(Integer cooldownRespect) {
this.cooldownRespect = cooldownRespect;
}

View File

@@ -1,72 +0,0 @@
package ru.prisonlife.api.store;
import ru.prisonlife.api.PrisonAPI.Factions;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
public class PlayerStatsDao extends AbstractCachedDao<PlayerStatsEntity> {
public PlayerStatsDao(SQLStore store) {
super(store);
}
@Override
String getSelectSQL() {
return "SELECT * FROM player_stats WHERE UUID = ?";
}
@Override
PlayerStatsEntity mappingResultEntity(ResultSet resultSet) throws SQLException {
PlayerStatsEntity entity = new PlayerStatsEntity();
entity.setUuid(UUID.fromString(resultSet.getString("UUID")));
entity.setWallet(resultSet.getInt("WALLET"));
entity.setFaction(Factions.valueOf(resultSet.getString("FACTION")));
entity.setRang(resultSet.getInt("F_RANG"));
entity.setRespect(resultSet.getInt("RESPECT"));
entity.setCooldownRespect(resultSet.getInt("COOLDOWN"));
entity.setPoints(resultSet.getDouble("POINTS"));
entity.setLevel(resultSet.getDouble("LEVEL"));
entity.setServerTime(resultSet.getInt("G_TIME"));
entity.setBlocks(resultSet.getInt("D_BLOCKS"));
entity.setPlayers(resultSet.getInt("K_PLAYERS"));
entity.setMobs(resultSet.getInt("K_MOBS"));
entity.setDeaths(resultSet.getInt("DEATHS"));
return entity;
}
@Override
String generateUpdateSQL() {
return "UPDATE player_stats " +
"SET WALLET = ?, FACTION = ?, F_RANG = ?, RESPECT = ?, " +
"COOLDOWN = ?, POINTS = ?, LEVEL = ?, G_TIME = ?, D_BLOCKS = ?, " +
"K_PLAYERS = ?, K_MOBS = ?, DEATHS = ? " +
"WHERE UUID = ?";
}
@Override
int countFields() {
return 13;
}
@Override
void mappingUpdateEntity(PreparedStatement statement, PlayerStatsEntity entity, int index) throws SQLException {
statement.setInt(index, entity.getWallet());
statement.setString(index + 1, entity.getFaction().name());
statement.setInt(index + 2, entity.getRang());
statement.setInt(index + 3, entity.getRespect());
statement.setInt(index + 4, entity.getCooldownRespect());
statement.setDouble(index + 5, entity.getPoints());
statement.setDouble(index + 6, entity.getLevel());
statement.setInt(index + 7, entity.getServerTime());
statement.setInt(index + 8, entity.getBlocks());
statement.setInt(index + 9, entity.getPlayers());
statement.setInt(index + 10, entity.getMobs());
statement.setInt(index + 11, entity.getDeaths());
statement.setString(index + 12, entity.getUuid().toString());
}
}

View File

@@ -1,15 +1,12 @@
package ru.prisonlife.api.store;
import ru.prisonlife.api.PrisonAPI.Factions;
import java.util.Objects;
import java.util.UUID;
public class PlayerStatsEntity {
private UUID uuid;
private String uuid;
private Integer wallet;
private Factions faction;
private Integer faction;
private Integer rang;
private Integer respect;
private Integer cooldownRespect;
@@ -21,11 +18,11 @@ public class PlayerStatsEntity {
private Integer mobs;
private Integer deaths;
public UUID getUuid() {
public String getUuid() {
return uuid;
}
public void setUuid(UUID uuid) {
public void setUuid(String uuid) {
this.uuid = uuid;
}
@@ -37,11 +34,11 @@ public class PlayerStatsEntity {
this.wallet = wallet;
}
public Factions getFaction() {
public Integer getFaction() {
return faction;
}
public void setFaction(Factions faction) {
public void setFaction(Integer faction) {
this.faction = faction;
}

View File

@@ -1,23 +1,28 @@
package ru.prisonlife.api.store;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SQLStore implements AutoCloseable {
import java.util.Properties;
private Connection connection;
public class SQLStore {
public SQLStore(String url, String user, String password) throws SQLException {
this.connection = DriverManager.getConnection(url, user, password);
private SqlSessionFactory sqlSessionFactory;
public SQLStore(String url, String user, String password) {
Properties properties = new Properties();
properties.put("url", url);
properties.put("user", user);
properties.put("password", password);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(
SQLStore.class.getResourceAsStream("config.xml"),
properties
);
}
public Connection getConnection() {
return connection;
}
@Override
public void close() throws Exception {
connection.close();
public SqlSession openSession() {
return sqlSessionFactory.openSession();
}
}

View File

@@ -1,7 +1,7 @@
package ru.prisonlife.api.template;
import org.apache.ibatis.session.SqlSession;
import ru.prisonlife.api.PrisonAPI;
import ru.prisonlife.api.store.PlayerStatsDao;
import ru.prisonlife.api.store.PlayerStatsEntity;
import java.lang.ref.WeakReference;
@@ -20,10 +20,14 @@ public abstract class AbstractApiData {
if (refPlayerStatsEntity != null && (entity = refPlayerStatsEntity.get()) != null) {
return entity;
} else {
final PlayerStatsDao playerStatsDao = PrisonAPI.getPlayerStatsDao();
entity = playerStatsDao.findByUuid(me.getPlayer().getUniqueId())
.orElseThrow(() -> new RuntimeException(
"no data in db by '" + me.getPlayer().getUniqueId().toString() + "'"));
final SqlSession session = PrisonAPI.getSqlStore().openSession();
entity = session.selectOne("PlayerStats.selectOne", me.getPlayer().getUniqueId().toString());
session.close();
if (entity == null) {
throw new RuntimeException("no data in db by '" + me.getPlayer().getUniqueId().toString() + "'");
}
refPlayerStatsEntity = new WeakReference<>(entity);
return entity;
}

View File

@@ -1,9 +1,9 @@
package ru.prisonlife.api.template;
import org.apache.ibatis.session.SqlSession;
import org.bukkit.Bukkit;
import ru.prisonlife.api.PrisonAPI;
import ru.prisonlife.api.PrisonAPI.Factions;
import ru.prisonlife.api.store.AbstractCachedDao;
import ru.prisonlife.api.store.DailyRespectTaskEntity;
import java.lang.ref.WeakReference;
@@ -28,21 +28,25 @@ public class Faction extends AbstractApiData {
if (refDailyRespectTaskEntity != null && (entity = refDailyRespectTaskEntity.get()) != null) {
return entity;
} else {
final AbstractCachedDao<DailyRespectTaskEntity> cachedDao = PrisonAPI.getDailyRespectTaskDao();
entity = cachedDao.findByUuid(me.getPlayer().getUniqueId())
.orElseThrow(() -> new RuntimeException(
"no data in db by '" + me.getPlayer().getUniqueId().toString() + "'"));
final SqlSession session = PrisonAPI.getSqlStore().openSession();
entity = session.selectOne("DailyRespectTask.selectOne", me.getPlayer().getUniqueId().toString());
session.close();
if (entity == null) {
throw new RuntimeException("no data in db by '" + me.getPlayer().getUniqueId().toString() + "'");
}
refDailyRespectTaskEntity = new WeakReference<>(entity);
return entity;
}
}
public String getType() {
return getPlayerStatsEntity().getFaction().name();
return Factions.valueOf(getPlayerStatsEntity().getFaction()).name();
}
public int getId() {
return getPlayerStatsEntity().getFaction().getId();
return getPlayerStatsEntity().getFaction();
}
public int getRangId() {
@@ -62,7 +66,7 @@ public class Faction extends AbstractApiData {
}
public void setFaction(int id) {
getPlayerStatsEntity().setFaction(Factions.valueOf(id));
getPlayerStatsEntity().setFaction(id);
}
public int getRespect() {

View File

@@ -2,6 +2,7 @@ package ru.prisonlife.api.template;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.apache.ibatis.session.SqlSession;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@@ -19,6 +20,8 @@ import java.util.List;
import java.util.Map;
import java.util.Random;
import static ru.prisonlife.api.PrisonAPI.getSqlStore;
public class Policeman extends Prisoner {
private Map<Prisoner, BukkitRunnable> timeInCage = new HashMap<>();
@@ -28,15 +31,16 @@ public class Policeman extends Prisoner {
}
public void arrestPrisoner(Prisoner prs) throws SQLException {
final Connection connection = PrisonAPI.getSqlStore().getConnection();
try (SqlSession session = getSqlStore().openSession()) {
final Connection connection = session.getConnection();
if(prs.getPlayer().isOnline()) {
if (prs.getPlayer().isOnline()) {
Manager mng = new Manager(super.getPrison());
List<String> all_cages = mng.getCages();
PreparedStatement st = connection.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?");
st.setString(1, me.getUniqueId().toString());
try(ResultSet resultSet = st.executeQuery()) {
if(!resultSet.next()) {
try (ResultSet resultSet = st.executeQuery()) {
if (!resultSet.next()) {
int su = prs.getSuspect();
st.close();
st = connection.prepareStatement(
@@ -70,11 +74,11 @@ public class Policeman extends Prisoner {
PreparedStatement st = connection.prepareStatement(
"SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
st.setString(1, uuid);
try(ResultSet resultSet = st.executeQuery()) {
if(resultSet.next())
try (ResultSet resultSet = st.executeQuery()) {
if (resultSet.next())
time = resultSet.getInt(1);
}
if(time == 0) {
if (time == 0) {
prs.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&',
configurationSuspect.getString("MESSAGES.WHEN_PRISONER_CAN_EXIT_FROM_CAGE")));
actionBar.cancel();
@@ -84,14 +88,14 @@ public class Policeman extends Prisoner {
st.setString(1, uuid);
st.execute();
} else {
if(prs.getPlayer().isOnline()) {
if (prs.getPlayer().isOnline()) {
st = connection.prepareStatement("UPDATE players_in_cages SET TIME_IN_CAGE = " + (time - 1) + " WHERE UUID = ?");
st.setString(1, uuid);
st.execute();
}
}
st.close();
} catch(SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
}
@@ -101,4 +105,5 @@ public class Policeman extends Prisoner {
timeInCage.get(prs).runTaskTimer(PrisonAPI.plugin, m, m);
}
}
}
}

View File

@@ -1,5 +1,6 @@
package ru.prisonlife.api.template;
import org.apache.ibatis.session.SqlSession;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration;
@@ -119,30 +120,34 @@ public class Prisoner {
}
public int getSuspect() {
try(PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT SUSPECT FROM player_stats WHERE UUID = ?")) {
try (SqlSession session = getSqlStore().openSession()) {
try (PreparedStatement statement = session.getConnection().prepareStatement("SELECT SUSPECT FROM player_stats WHERE UUID = ?")) {
statement.setString(1, me.getPlayer().getUniqueId().toString());
try(ResultSet resultSet = statement.executeQuery()) {
if(resultSet.next()) {
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
return resultSet.getInt(1);
}
}
} catch(SQLException z) {
} catch (SQLException z) {
z.printStackTrace();
}
}
return 0;
}
public void setSuspect(int value) {
try(PreparedStatement statement = getSqlStore().getConnection().prepareStatement("UPDATE player_stats SET SUSPECT = ? WHERE UUID = ?")) {
try (SqlSession session = getSqlStore().openSession()) {
try (PreparedStatement statement = session.getConnection().prepareStatement("UPDATE player_stats SET SUSPECT = ? WHERE UUID = ?")) {
statement.setInt(1, value);
statement.setString(2, me.getPlayer().getUniqueId().toString());
statement.executeUpdate();
} catch(SQLException z) {
} catch (SQLException z) {
z.printStackTrace();
}
}
}
public void addSuspect(int value) {
int su = getSuspect() + value;
@@ -153,29 +158,32 @@ public class Prisoner {
}
public int getTimeLeftInCage() {
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?")) {
try (SqlSession session = getSqlStore().openSession()) {
try (PreparedStatement statement = session.getConnection().prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?")) {
statement.setString(1, me.getUniqueId().toString());
try(ResultSet resultSet = statement.executeQuery()) {
if(resultSet.next()) {
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
return resultSet.getInt(1);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return 0;
}
public void setTimeLeftInCage(int value) {
try (SqlSession session = getSqlStore().openSession()) {
try {
PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
PreparedStatement statement = session.getConnection().prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
statement.setString(1, me.getUniqueId().toString());
try(ResultSet resultSet = statement.executeQuery()) {
if(resultSet.next()) {
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
statement.close();
statement = getSqlStore().getConnection().prepareStatement("UPDATE players_in_cages SET TIME_IN_CAGE = ? WHERE UUID = ?");
statement = session.getConnection().prepareStatement("UPDATE players_in_cages SET TIME_IN_CAGE = ? WHERE UUID = ?");
statement.setInt(1, value);
statement.setString(2, me.getUniqueId().toString());
statement.execute();
@@ -183,38 +191,43 @@ public class Prisoner {
statement.close();
}
}
} catch(SQLException z) {
} catch (SQLException z) {
z.printStackTrace();
}
}
}
public int getBlocksLeftToBreak() {
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT BLOCKS_LEFT FROM players_in_cages WHERE UUID = ?")) {
try (SqlSession session = getSqlStore().openSession()) {
try (PreparedStatement statement = session.getConnection().prepareStatement("SELECT BLOCKS_LEFT FROM players_in_cages WHERE UUID = ?")) {
statement.setString(1, me.getUniqueId().toString());
try(ResultSet resultSet = statement.executeQuery()) {
if(resultSet.next()) {
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
return resultSet.getInt(1);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return 0;
}
public boolean isLocateInCage() {
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT UUID FROM players_in_cages WHERE UUID = ?")) {
try (SqlSession session = getSqlStore().openSession()) {
try (PreparedStatement statement = session.getConnection().prepareStatement("SELECT UUID FROM players_in_cages WHERE UUID = ?")) {
statement.setString(1, me.getUniqueId().toString());
try(ResultSet resultSet = statement.executeQuery()) {
if(resultSet.next()) {
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
return true;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
@@ -231,13 +244,14 @@ public class Prisoner {
}
public void register() throws SQLException {
final Connection connection = getSqlStore().getConnection();
try (SqlSession session = getSqlStore().openSession()) {
final Connection connection = session.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?");
statement.setString(1, me.getUniqueId().toString());
try(ResultSet resultSet = statement.executeQuery()) {
if(!resultSet.next()) {
try (ResultSet resultSet = statement.executeQuery()) {
if (!resultSet.next()) {
statement.close();
statement = connection.prepareStatement(
@@ -264,4 +278,5 @@ public class Prisoner {
}
statement.close();
}
}
}

View File

@@ -0,0 +1,20 @@
<?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="DailyRespectTask">
<resultMap id = "result" type = "DailyRespectTask">
<result property = "uuid" column = "UUID"/>
<result property = "cooldownRespect" column = "COOLDOWN"/>
</resultMap>
<select id="selectOne" parameterType="String" resultMap="result">
SELECT COOLDOWN FROM daily_respect_task WHERE UUID = #{id}
</select>
<update id="update" parameterType="DailyRespectTask">
UPDATE daily_respect_task
SET COOLDOWN = #{cooldownRespect}
WHERE UUID = #{uuid};
</update>
</mapper>

View File

@@ -0,0 +1,42 @@
<?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="PlayerStats">
<resultMap id = "result" type = "PlayerStats">
<result property = "uuid" column = "UUID"/>
<result property = "wallet" column = "WALLET"/>
<result property = "faction" column = "FACTION"/>
<result property = "rang" column = "F_RANG"/>
<result property = "respect" column = "RESPECT"/>
<result property = "cooldownRespect" column = "COOLDOWN"/>
<result property = "points" column = "POINTS"/>
<result property = "level" column = "LEVEL"/>
<result property = "serverTime" column = "G_TIME"/>
<result property = "blocks" column = "D_BLOCKS"/>
<result property = "players" column = "K_PLAYERS"/>
<result property = "mobs" column = "K_MOBS"/>
<result property = "deaths" column = "DEATHS"/>
</resultMap>
<select id="selectOne" parameterType="String" resultMap="result">
SELECT * FROM player_stats WHERE UUID = #{id}
</select>
<update id="update" parameterType="PlayerStats">
UPDATE player_stats
SET WALLET = #{wallet},
FACTION = #{faction},
F_RANG = #{rang},
RESPECT = #{respect},
COOLDOWN = #{cooldownRespect},
POINTS = #{points},
LEVEL = #{level},
G_TIME = #{serverTime},
D_BLOCKS = #{blocks},
K_PLAYERS = #{players},
K_MOBS = #{mobs},
DEATHS = #{deaths}
WHERE UUID = #{uuid};
</update>
</mapper>

View File

@@ -0,0 +1,22 @@
<?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>
<environments default="main">
<environment id="main">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name = "driver" value = "com.mysql.jdbc.Driver"/>
<property name = "url" value = "${url}"/>
<property name = "username" value = "${user}"/>
<property name = "password" value = "${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource = "PlayerStatsMapper.xml"/>
<mapper resource = "DailyRespectTaskMapper.xml"/>
</mappers>
</configuration>