1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.jpetstore.service;
17
18 import java.util.Optional;
19
20 import org.mybatis.jpetstore.domain.Account;
21 import org.mybatis.jpetstore.mapper.AccountMapper;
22 import org.springframework.stereotype.Service;
23 import org.springframework.transaction.annotation.Transactional;
24
25
26
27
28
29
30 @Service
31 public class AccountService {
32
33 private final AccountMapper accountMapper;
34
35 public AccountService(AccountMapper accountMapper) {
36 this.accountMapper = accountMapper;
37 }
38
39 public Account getAccount(String username) {
40 return accountMapper.getAccountByUsername(username);
41 }
42
43 public Account getAccount(String username, String password) {
44 return accountMapper.getAccountByUsernameAndPassword(username, password);
45 }
46
47
48
49
50
51
52
53 @Transactional
54 public void insertAccount(Account account) {
55 accountMapper.insertAccount(account);
56 accountMapper.insertProfile(account);
57 accountMapper.insertSignon(account);
58 }
59
60
61
62
63
64
65
66 @Transactional
67 public void updateAccount(Account account) {
68 accountMapper.updateAccount(account);
69 accountMapper.updateProfile(account);
70
71 Optional.ofNullable(account.getPassword()).filter(password -> password.length() > 0)
72 .ifPresent(password -> accountMapper.updateSignon(account));
73 }
74
75 }