-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JDBC 라이브러리 구현하기 - 3,4단계] 케로(김지현) 미션 제출합니다. (#606)
* study * feat: UserHistoryDao 로직 추가 * feat: UserService에서 changePassword를 한 connection에서 처리할 수 있도록 기능을 구현함 * feat: TransactionSynchronizationManager이정상 동작하도록 기능추가 * feat: 서비스와 DAO에서 DataSourceUtils를 사용하여 Connection을 가져오도록 함. * feat: DataSourceUtils에서 오토 커밋일 경우 커넥션을 반납할 수 있도록 함. * refactor: JdbcTemplate의 반복을 줄이고,dao에서 커넥션을 보내지 않도록 수정함 * feat: userService 에서 커넥션 관리를 하지 않도록 TransactionTemplate을 만들음
- Loading branch information
Showing
14 changed files
with
235 additions
and
149 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
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/techcourse/service/AppUserService.java
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,35 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
|
||
public class AppUserService implements UserService { | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
|
||
public AppUserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
final var findUser = findById(id); | ||
findUser.changePassword(newPassword); | ||
userDao.update(findUser); | ||
userHistoryDao.log(new UserHistory(findUser, createBy)); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/techcourse/service/TxUserService.java
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,37 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final AppUserService userService; | ||
|
||
private final TransactionTemplate transactionTemplate; | ||
|
||
public TxUserService(final AppUserService userService, final TransactionTemplate transactionTemplate) { | ||
this.userService = userService; | ||
this.transactionTemplate = transactionTemplate; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return userService.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
transactionTemplate.execute(() -> { | ||
userService.insert(user); | ||
return null; | ||
}); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionTemplate.execute(() -> { | ||
userService.changePassword(id, newPassword, createBy); | ||
return null; | ||
}); | ||
} | ||
} |
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,32 +1,11 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.UserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
|
||
public class UserService { | ||
public interface UserService { | ||
User findById(final long id); | ||
|
||
private final UserDao userDao; | ||
private final UserHistoryDao userHistoryDao; | ||
void insert(final User user); | ||
|
||
public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) { | ||
this.userDao = userDao; | ||
this.userHistoryDao = userHistoryDao; | ||
} | ||
|
||
public User findById(final long id) { | ||
return userDao.findById(id); | ||
} | ||
|
||
public void insert(final User user) { | ||
userDao.insert(user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userDao.update(user); | ||
userHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
} |
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
9 changes: 9 additions & 0 deletions
9
jdbc/src/main/java/org/springframework/jdbc/core/PreparedStateExecutor.java
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,9 @@ | ||
package org.springframework.jdbc.core; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
public interface PreparedStateExecutor<T> { | ||
|
||
T execute(final PreparedStatement preparedStatement) throws SQLException; | ||
} |
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
7 changes: 7 additions & 0 deletions
7
jdbc/src/main/java/org/springframework/transaction/support/TransactionExecutor.java
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,7 @@ | ||
package org.springframework.transaction.support; | ||
|
||
@FunctionalInterface | ||
public interface TransactionExecutor<T> { | ||
|
||
T execute(); | ||
} |
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
Oops, something went wrong.