1010import  org .slf4j .Logger ;
1111import  org .slf4j .LoggerFactory ;
1212
13- import  java .sql .Connection ;
14- import  java .sql .SQLSyntaxErrorException ;
1513import  java .util .List ;
1614
1715public  class  UserDao  {
@@ -30,122 +28,54 @@ public UserDao(final JdbcTemplate jdbcTemplate) {
3028        this .jdbcTemplate  = jdbcTemplate ;
3129    }
3230
33-     public  Connection  getConnection () {
34-         return  jdbcTemplate .getConnection ();
35-     }
36- 
37-     public  void  startTransaction (Connection  connection ) {
38-         jdbcTemplate .startTransaction (connection );
39-     }
40- 
41-     public  void  commitTransaction (Connection  connection ) {
42-         jdbcTemplate .commitTransaction (connection );
43-     }
44- 
45-     public  void  insertWithTransaction (final  User  user , final  Connection  connection ) {
46-         CommandSpecification  specification  = new  CommandSpecification (createPreparedStatementSpecification (
47-                 "insert into users (account, password, email) values (?, ?, ?)" ,
48-                 List .of (
49-                         new  PreparedStatementParameter (1 , user .getAccount ()),
50-                         new  PreparedStatementParameter (2 , user .getPassword ()),
51-                         new  PreparedStatementParameter (3 , user .getEmail ())
52-                 )
53-         ));
54-         jdbcTemplate .insertWithTransaction (specification , connection );
55-     }
56- 
57-     public  void  updateWithTransaction (final  User  user , final  Connection  connection ) {
58-         CommandSpecification  specification  = new  CommandSpecification (createPreparedStatementSpecification (
59-                 "update users set account = ?, password = ?, email = ? where id = ?" ,
60-                 List .of (
61-                         new  PreparedStatementParameter (1 , user .getAccount ()),
62-                         new  PreparedStatementParameter (2 , user .getPassword ()),
63-                         new  PreparedStatementParameter (3 , user .getEmail ()),
64-                         new  PreparedStatementParameter (4 , user .getId ())
65-                 )
66-         ));
67-         jdbcTemplate .updateWithTransaction (specification , connection );
68-     }
69- 
70-     public  List <User > findAllWithTransaction (final  Connection  connection ) {
71-         QuerySpecification <User > specification  = new  QuerySpecification <>(
72-                 USER_ROW_MAPPER ,
73-                 createPreparedStatementSpecification (
74-                         "select id, account, password, email from users" ,
75-                         List .of ()
76-                 )
77-         );
78-         return  jdbcTemplate .findAllWithTransaction (specification , connection );
79-     }
80- 
81-     public  User  findByIdWithTransaction (final  Long  id , final  Connection  connection ) {
82-         QuerySpecification <User > specification  = new  QuerySpecification <>(
83-                 USER_ROW_MAPPER ,
84-                 createPreparedStatementSpecification (
85-                         "select id, account, password, email from users where id = ?" ,
86-                         List .of (new  PreparedStatementParameter (1 , id ))
87-                 )
88-         );
89-         return  jdbcTemplate .findOneWithTransaction (specification , connection )
90-                 .orElseThrow (() -> new  IllegalArgumentException ("존재하지 않는 유저입니다." ));
91-     }
92- 
93-     public  User  findByAccountWithTransaction (final  String  account , final  Connection  connection ) {
94- 
95-         QuerySpecification <User > specification  = new  QuerySpecification <>(
96-                 USER_ROW_MAPPER ,
97-                 createPreparedStatementSpecification (
98-                         "select id, account, password, email from users where account = ?" ,
99-                         List .of (new  PreparedStatementParameter (1 , account ))
100-                 )
101-         );
102-         return  jdbcTemplate .findOneWithTransaction (specification , connection )
103-                 .orElseThrow (() -> new  IllegalArgumentException ("존재하지 않는 유저입니다." ));
104-     }
105- 
10631    public  void  insert (final  User  user ) {
107-         CommandSpecification  specification  = new  CommandSpecification (createPreparedStatementSpecification (
108-                 "insert into users (account, password, email) values (?, ?, ?)" ,
109-                 List .of (
110-                         new  PreparedStatementParameter (1 , user .getAccount ()),
111-                         new  PreparedStatementParameter (2 , user .getPassword ()),
112-                         new  PreparedStatementParameter (3 , user .getEmail ())
113-                 )
114-         ));
32+         CommandSpecification  specification  = new  CommandSpecification (
33+                 PreparedStatementSpecification 
34+                         .builder ("insert into users (account, password, email) values (?, ?, ?)" )
35+                         .parameters (
36+                                 List .of (
37+                                         new  PreparedStatementParameter (1 , user .getAccount ()),
38+                                         new  PreparedStatementParameter (2 , user .getPassword ()),
39+                                         new  PreparedStatementParameter (3 , user .getEmail ())
40+                                 )
41+                         ).build ()
42+         );
11543        jdbcTemplate .insert (specification );
11644    }
11745
11846    public  void  update (final  User  user ) {
119-         CommandSpecification  specification  = new  CommandSpecification (createPreparedStatementSpecification (
120-                 "update users set account = ?, password = ?, email = ? where id = ?" ,
121-                 List .of (
122-                         new  PreparedStatementParameter (1 , user .getAccount ()),
123-                         new  PreparedStatementParameter (2 , user .getPassword ()),
124-                         new  PreparedStatementParameter (3 , user .getEmail ()),
125-                         new  PreparedStatementParameter (4 , user .getId ())
126-                 )
127-         ));
47+         CommandSpecification  specification  = new  CommandSpecification (
48+                 PreparedStatementSpecification 
49+                         .builder ("update users set account = ?, password = ?, email = ? where id = ?" )
50+                         .parameters (
51+                                 List .of (
52+                                         new  PreparedStatementParameter (1 , user .getAccount ()),
53+                                         new  PreparedStatementParameter (2 , user .getPassword ()),
54+                                         new  PreparedStatementParameter (3 , user .getEmail ()),
55+                                         new  PreparedStatementParameter (4 , user .getId ())
56+                                 )
57+                         ).build ()
58+         );
12859        jdbcTemplate .update (specification );
12960    }
13061
13162    public  List <User > findAll () {
13263        QuerySpecification <User > specification  = new  QuerySpecification <>(
13364                USER_ROW_MAPPER ,
134-                 createPreparedStatementSpecification (
135-                         "select id, account, password, email from users" ,
136-                         List .of ()
137-                 )
65+                 PreparedStatementSpecification 
66+                         .builder ("select id, account, password, email from users" )
67+                         .build ()
13868        );
13969        return  jdbcTemplate .findAll (specification );
14070    }
14171
14272    public  User  findById (final  Long  id ) {
14373        QuerySpecification <User > specification  = new  QuerySpecification <>(
14474                USER_ROW_MAPPER ,
145-                 createPreparedStatementSpecification ( 
146-                         "select id, account, password, email from users where id = ?" , 
147-                         List .of (new  PreparedStatementParameter (1 , id ))
148-                 )
75+                 PreparedStatementSpecification 
76+                         . builder ( "select id, account, password, email from users where id = ?" ) 
77+                         . parameters ( List .of (new  PreparedStatementParameter (1 , id ) ))
78+                         . build ( )
14979        );
15080        return  jdbcTemplate .findOne (specification )
15181                .orElseThrow (() -> new  IllegalArgumentException ("존재하지 않는 유저입니다." ));
@@ -155,26 +85,12 @@ public User findByAccount(final String account) {
15585
15686        QuerySpecification <User > specification  = new  QuerySpecification <>(
15787                USER_ROW_MAPPER ,
158-                 createPreparedStatementSpecification ( 
159-                         "select id, account, password, email from users where account = ?" , 
160-                         List .of (new  PreparedStatementParameter (1 , account ))
161-                 )
88+                 PreparedStatementSpecification 
89+                         . builder ( "select id, account, password, email from users where account = ?" ) 
90+                         . parameters ( List .of (new  PreparedStatementParameter (1 , account ) ))
91+                         . build ( )
16292        );
16393        return  jdbcTemplate .findOne (specification )
16494                .orElseThrow (() -> new  IllegalArgumentException ("존재하지 않는 유저입니다." ));
16595    }
166- 
167-     private  PreparedStatementSpecification  createPreparedStatementSpecification (
168-             String  sql ,
169-             List <PreparedStatementParameter > parameters 
170-     ) {
171-         try  {
172-             return  PreparedStatementSpecification 
173-                     .builder (sql )
174-                     .parameters (parameters )
175-                     .build ();
176-         } catch  (SQLSyntaxErrorException  e ) {
177-             throw  new  IllegalArgumentException (e );
178-         }
179-     }
18096}
0 commit comments