Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hibernate #1 implements #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.15.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core-jakarta</artifactId>
<version>5.6.11.Final</version>
</dependency>
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

<build>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/game/controller/PlayerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public List<PlayerInfo> getAll(@RequestParam(required = false) Integer pageNumbe
}

@GetMapping("/count")
public Integer getAllCount() {
public long getAllCount() {
return playerService.getAllCount();
}

Expand Down
24 changes: 23 additions & 1 deletion src/main/java/com/game/entity/Player.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
package com.game.entity;

import java.util.Date;
import jakarta.persistence.*;

import java.util.Date;

@NamedQueries({
@NamedQuery(
name = "allCountQuery",
query = "select count(*) from Player"
)}
)
@Entity
@Table(name = "player", schema = "rpg")
public class Player {

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = "id")
private Long id;

@Column(name = "name", length = 12, nullable = false)
private String name;

@Column(name = "title", length = 30, nullable = false)
private String title;

@Enumerated(EnumType.ORDINAL)
@Column(name = "race", nullable = false)
private Race race;

@Enumerated(EnumType.ORDINAL)
@Column(name = "profession", nullable = false)
private Profession profession;

@Column(name = "birthday", nullable = false)
private Date birthday;

@Column(name = "banned", nullable = false)
private Boolean banned;

@Column(name = "level", nullable = false)
private Integer level;

public Player() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/game/repository/IPlayerRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public interface IPlayerRepository {
List<Player> getAll(int pageNumber, int pageSize);

int getAllCount();
long getAllCount();

Player save(Player player);

Expand Down
63 changes: 54 additions & 9 deletions src/main/java/com/game/repository/PlayerRepositoryDB.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,96 @@
package com.game.repository;

import com.game.entity.Player;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.PreDestroy;
import java.util.List;
import java.util.Optional;
import java.util.Properties;

@Repository(value = "db")
public class PlayerRepositoryDB implements IPlayerRepository {

public PlayerRepositoryDB() {
private final SessionFactory sessionFactory;

public PlayerRepositoryDB() {
Properties properties = new Properties();
properties.put(Environment.DIALECT, "org.hibernate.dialect.MySQL8Dialect");
properties.put(Environment.USER, "root");
properties.put(Environment.PASS, "my-secret-pw");
properties.put(Environment.HBM2DDL_AUTO, "update");
properties.put(Environment.DRIVER, "com.p6spy.engine.spy.P6SpyDriver");
properties.put(Environment.URL, "jdbc:p6spy:mysql://localhost:3306/rpg");
sessionFactory = new Configuration()
.setProperties(properties)
.addAnnotatedClass(Player.class)
.buildSessionFactory();
}

@Override
public List<Player> getAll(int pageNumber, int pageSize) {
return null;
try (Session session = sessionFactory.openSession()) {
String sql = String.format("select * from player limit %d offset %d", pageSize, (pageNumber * pageSize));
NativeQuery<Player> nativeQuery = session.createNativeQuery(sql, Player.class);
return nativeQuery.list();
}
}

@Override
public int getAllCount() {
return 0;
public long getAllCount() {
try (Session session = sessionFactory.openSession()) {
Query<Long> query = session.createNamedQuery("allCountQuery", Long.class);
return query.uniqueResult();
}
}

@Override
public Player save(Player player) {
return null;
try (Session session = sessionFactory.openSession()) {
Transaction transaction = session.beginTransaction();
session.save(player);
transaction.commit();
return player;
}
}

@Override
public Player update(Player player) {
return null;
try (Session session = sessionFactory.openSession()) {
Transaction transaction = session.beginTransaction();
session.update(player);
transaction.commit();
return player;
}
}

@Override
public Optional<Player> findById(long id) {
return Optional.empty();
try (Session session = sessionFactory.openSession()) {
Query<Player> query = session.createQuery("from Player where id = ?1", Player.class);
query.setParameter(1, id);
return Optional.ofNullable(query.uniqueResult());
}
}

@Override
public void delete(Player player) {

try (Session session = sessionFactory.openSession()) {
Transaction transaction = session.beginTransaction();
session.delete(player);
transaction.commit();
}
}

@PreDestroy
public void beforeStop() {

sessionFactory.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public List<Player> getAll(int pageNumber, int pageSize) {
}

@Override
public int getAllCount() {
public long getAllCount() {
return storage.size();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/game/service/PlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public List<Player> getAll(int pageNumber, int pageSize) {
return playerRepository.getAll(pageNumber, pageSize);
}

public Integer getAllCount() {
public long getAllCount() {
return playerRepository.getAllCount();
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/spy.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
driverlist=com.mysql.cj.jdbc.Driver
dateformat=yyyy-MM-dd hh:mm:ss a
appender=com.p6spy.engine.spy.appender.StdoutLogger
logMessageFormat=com.p6spy.engine.spy.appender.MultiLineFormat