add Spring Data
This commit is contained in:
@@ -33,6 +33,11 @@ dependencies {
|
|||||||
}
|
}
|
||||||
|
|
||||||
implementation 'org.springframework:spring-context:5.2.5.RELEASE'
|
implementation 'org.springframework:spring-context:5.2.5.RELEASE'
|
||||||
|
|
||||||
|
// compileOnly потому, что в spigot/paper уже есть драйвер для MySQL 5
|
||||||
|
compileOnly 'mysql:mysql-connector-java:5.1.48'
|
||||||
|
implementation 'org.hibernate:hibernate-core:5.4.14.Final'
|
||||||
|
implementation 'org.springframework.data:spring-data-jpa:2.2.6.RELEASE'
|
||||||
}
|
}
|
||||||
|
|
||||||
processResources {
|
processResources {
|
||||||
|
|||||||
@@ -19,9 +19,6 @@ public class BanHammerPlugin extends JavaPlugin {
|
|||||||
|
|
||||||
ApplicationContext context = createSpringContext();
|
ApplicationContext context = createSpringContext();
|
||||||
|
|
||||||
//TODO для демонстрации
|
|
||||||
getLogger().info("dburl: " + context.getBean("databaseUrl", String.class));
|
|
||||||
|
|
||||||
this.getCommand("ban").setExecutor(context.getBean(BanCommand.class));
|
this.getCommand("ban").setExecutor(context.getBean(BanCommand.class));
|
||||||
this.getCommand("unban").setExecutor(context.getBean(UnbanCommand.class));
|
this.getCommand("unban").setExecutor(context.getBean(UnbanCommand.class));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import org.bukkit.command.CommandSender;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import ru.dmitriymx.plugin.service.BannedUserService;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -13,16 +14,26 @@ import java.util.logging.Logger;
|
|||||||
public class BanCommand implements CommandExecutor {
|
public class BanCommand implements CommandExecutor {
|
||||||
|
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
|
private final BannedUserService service;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
|
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
|
||||||
public BanCommand(@Qualifier("bukkitLogger") Logger logger) {
|
public BanCommand(@Qualifier("bukkitLogger") Logger logger, BannedUserService service) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
this.service = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||||
logger.info("Call '" + label + "' command");
|
logger.info("Call '" + label + "' command");
|
||||||
|
|
||||||
|
if (args.length == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean result = service.inBanned(args[0]);
|
||||||
|
logger.info("Player '" + args[0] + "': " + (result ? "is banned" : "not banned"));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,20 @@
|
|||||||
package ru.dmitriymx.plugin.config;
|
package ru.dmitriymx.plugin.config;
|
||||||
|
|
||||||
|
import org.hibernate.jpa.HibernatePersistenceProvider;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||||
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan("ru.dmitriymx.plugin")
|
@ComponentScan(basePackages = "ru.dmitriymx.plugin")
|
||||||
|
@EnableJpaRepositories(basePackages = "ru.dmitriymx.plugin.repository")
|
||||||
public class SpringConfig {
|
public class SpringConfig {
|
||||||
|
|
||||||
@Value("${database.url}")
|
@Value("${database.url}")
|
||||||
@@ -18,9 +26,44 @@ public class SpringConfig {
|
|||||||
@Value("${database.password}")
|
@Value("${database.password}")
|
||||||
private String databasePassword;
|
private String databasePassword;
|
||||||
|
|
||||||
//TODO для демонстрации
|
@Value("${database.hibernate.show-sql}")
|
||||||
@Bean("databaseUrl")
|
private String showSql;
|
||||||
public String getDatabaseUrl() {
|
|
||||||
return databaseUrl;
|
@Bean
|
||||||
|
public DataSource dataSource() {
|
||||||
|
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||||
|
dataSource.setUrl(databaseUrl);
|
||||||
|
dataSource.setUsername(databaseUser);
|
||||||
|
dataSource.setPassword(databasePassword);
|
||||||
|
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
|
||||||
|
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
|
||||||
|
entityManagerFactoryBean.setDataSource(dataSource);
|
||||||
|
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
|
||||||
|
entityManagerFactoryBean.setPackagesToScan("ru.dmitriymx.plugin.entity");
|
||||||
|
entityManagerFactoryBean.setJpaProperties(hibernateJpaProperties());
|
||||||
|
|
||||||
|
return entityManagerFactoryBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Properties hibernateJpaProperties() {
|
||||||
|
Properties properties = new Properties();
|
||||||
|
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
|
||||||
|
|
||||||
|
// показывать выполняемые SQL в логах
|
||||||
|
properties.setProperty("hibernate.show_sql", showSql);
|
||||||
|
|
||||||
|
// validate: проверяет соответствие схемы таблиц с имеющимися Entity классами
|
||||||
|
// update: при необходимости, обновляет схемы таблиц в соответствии с имеющимися Entity классами
|
||||||
|
// create: создаёт схемы таблиц, уничтожая имеющиеся данные
|
||||||
|
// create-drop: уничтожает таблицы, если все соединения закрываются; обычно происходит при завершеннии работы приложения
|
||||||
|
// none: ничего не делать
|
||||||
|
properties.setProperty("hibernate.hbm2ddl.auto", "update");
|
||||||
|
|
||||||
|
return properties;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package ru.dmitriymx.plugin.entity;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "ban_users")
|
||||||
|
public class BannedUserEntity {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
@Column(
|
||||||
|
name = "player_name",
|
||||||
|
unique = true,
|
||||||
|
nullable = false,
|
||||||
|
length = 16
|
||||||
|
)
|
||||||
|
private String playerName;
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPlayerName() {
|
||||||
|
return playerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPlayerName(String playerName) {
|
||||||
|
this.playerName = playerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof BannedUserEntity)) return false;
|
||||||
|
BannedUserEntity that = (BannedUserEntity) o;
|
||||||
|
return playerName.equals(that.playerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(playerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "BannedUserEntity{" +
|
||||||
|
"id=" + id +
|
||||||
|
", playerName='" + playerName + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package ru.dmitriymx.plugin.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import ru.dmitriymx.plugin.entity.BannedUserEntity;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface BannedUserRepository extends CrudRepository<BannedUserEntity, Integer> {
|
||||||
|
|
||||||
|
Optional<BannedUserEntity> findByPlayerName(String playerName);
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package ru.dmitriymx.plugin.service;
|
||||||
|
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.dmitriymx.plugin.repository.BannedUserRepository;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class BannedUserService {
|
||||||
|
|
||||||
|
private final BannedUserRepository repository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public BannedUserService(BannedUserRepository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean inBanned(Player player) {
|
||||||
|
return inBanned(player.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean inBanned(String playerName) {
|
||||||
|
return repository.findByPlayerName(playerName.toLowerCase()).isPresent();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
database:
|
database:
|
||||||
url: some-url
|
url: jdbc:mysql://localhost:3306/ban_hammer
|
||||||
user: some-user
|
user: root
|
||||||
password: some-password
|
password: secret
|
||||||
|
hibernate:
|
||||||
|
show-sql: true
|
||||||
|
|||||||
Reference in New Issue
Block a user