-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For retrieving all the admin messages, EntityManager and JdbcTemplate…
… used for querying instead of using repository.
- Loading branch information
Badri Paudel
committed
Mar 16, 2024
1 parent
bf5caa6
commit ca1b946
Showing
4 changed files
with
67 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,48 @@ | ||
package info.keeper.service; | ||
|
||
import info.keeper.models.AdminMessage; | ||
import info.keeper.models.User; | ||
import info.keeper.repositories.AdminRepository; | ||
import info.keeper.repositories.UserRepository; | ||
import info.keeper.utils.UserRowMapper; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.Query; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
public class UserService { | ||
private UserRepository userRepository; | ||
private JdbcTemplate jdbcTemplate; | ||
private EntityManager entityManager; | ||
|
||
public UserService(UserRepository userRepository) { | ||
public UserService(UserRepository userRepository, | ||
JdbcTemplate jdbcTemplate, EntityManager entityManager) { | ||
this.userRepository = userRepository; | ||
this.jdbcTemplate = jdbcTemplate; | ||
this.entityManager = entityManager; | ||
} | ||
|
||
public User getUserByUsername(String username) { | ||
User user = userRepository.findUserByUsername(username); | ||
return user; | ||
} | ||
|
||
public ArrayList<AdminMessage> getAllAdminMessages() { | ||
String query = "SELECT * FROM admin_message"; | ||
Query namedQuery = this.entityManager.createNativeQuery(query, AdminMessage.class); | ||
return (ArrayList<AdminMessage>)namedQuery.getResultList(); | ||
// return getAllAdminMessagesUsingJdbcT(); // Same as above. | ||
} | ||
|
||
// Using JdbcTemplate | ||
public ArrayList<AdminMessage> getAllAdminMessagesUsingJdbcT() { | ||
String query = "SELECT * FROM admin_message"; | ||
List<AdminMessage> adminMessageList = this.jdbcTemplate.query(query, new UserRowMapper()); | ||
return (ArrayList<AdminMessage>) adminMessageList; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package info.keeper.utils; | ||
|
||
import info.keeper.models.AdminMessage; | ||
import org.springframework.jdbc.core.RowMapper; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* To map data selected from the database to the object | ||
*/ | ||
public class UserRowMapper implements RowMapper<AdminMessage> { | ||
|
||
@Override | ||
public AdminMessage mapRow(ResultSet resultSet, int i) throws SQLException { | ||
AdminMessage user = new AdminMessage(); | ||
user.setId(resultSet.getInt("admin_message_id")); | ||
user.setDate(resultSet.getTimestamp("posted_data")); | ||
user.setMessage(resultSet.getString("admin_message")); | ||
user.setPostedBy(resultSet.getString("posted_by")); | ||
return user; | ||
} | ||
} |