1616package org .springframework .data .jdbc .repository ;
1717
1818import java .io .Serializable ;
19- import java .util .ArrayList ;
2019import java .util .HashMap ;
21- import java .util .List ;
2220import java .util .Map ;
2321import java .util .stream .Collectors ;
22+ import java .util .stream .StreamSupport ;
2423import javax .sql .DataSource ;
2524import org .springframework .data .jdbc .mapping .model .JdbcPersistentEntity ;
26- import org .springframework .data .mapping .PersistentProperty ;
25+ import org .springframework .data .jdbc . mapping .model . JdbcPersistentProperty ;
2726import org .springframework .data .mapping .PropertyHandler ;
2827import org .springframework .data .repository .CrudRepository ;
2928import org .springframework .jdbc .core .namedparam .MapSqlParameterSource ;
@@ -37,117 +36,116 @@ public class SimpleJdbcRepository<T, ID extends Serializable> implements CrudRep
3736
3837 private final JdbcPersistentEntity <T > entity ;
3938 private final NamedParameterJdbcOperations template ;
39+ private final SqlGenerator sql ;
4040
41- private final String findOneSql ;
42- private final String insertSql ;
41+ private final EntityRowMapper <T > entityRowMapper ;
4342
4443 public SimpleJdbcRepository (JdbcPersistentEntity <T > entity , DataSource dataSource ) {
4544
4645 this .entity = entity ;
4746 this .template = new NamedParameterJdbcTemplate (dataSource );
4847
49- findOneSql = createFindOneSelectSql ( );
50- insertSql = createInsertSql ( );
48+ entityRowMapper = new EntityRowMapper < T >( entity );
49+ sql = new SqlGenerator ( entity );
5150 }
5251
5352 @ Override
5453 public <S extends T > S save (S entity ) {
5554
56- template .update (insertSql , getPropertyMap (entity ));
55+ template .update (sql . getInsert () , getPropertyMap (entity ));
5756
5857 return entity ;
5958 }
6059
6160 @ Override
6261 public <S extends T > Iterable <S > save (Iterable <S > entities ) {
63- return null ;
62+
63+ Map <String , ?>[] batchValues = StreamSupport
64+ .stream (entities .spliterator (), false )
65+ .map (i -> getPropertyMap (i ))
66+ .toArray (size -> new Map [size ]);
67+
68+ template .batchUpdate (sql .getInsert (), batchValues );
69+
70+ return entities ;
6471 }
6572
6673 @ Override
6774 public T findOne (ID id ) {
6875
6976 return template .queryForObject (
70- findOneSql ,
77+ sql . getFindOne () ,
7178 new MapSqlParameterSource ("id" , id ),
72- new EntityRowMapper < T >( entity )
79+ entityRowMapper
7380 );
7481 }
7582
7683 @ Override
7784 public boolean exists (ID id ) {
78- return false ;
85+
86+ return template .queryForObject (
87+ sql .getExists (),
88+ new MapSqlParameterSource ("id" , id ),
89+ Boolean .class
90+ );
7991 }
8092
8193 @ Override
8294 public Iterable <T > findAll () {
83- return null ;
95+ return template . query ( sql . getFindAll (), entityRowMapper ) ;
8496 }
8597
8698 @ Override
8799 public Iterable <T > findAll (Iterable <ID > ids ) {
88- return null ;
100+ return template . query ( sql . getFindAllInList (), new MapSqlParameterSource ( "ids" , ids ), entityRowMapper ) ;
89101 }
90102
91103 @ Override
92104 public long count () {
93- return 0 ;
105+ return template . getJdbcOperations (). queryForObject ( sql . getCount (), Long . class ) ;
94106 }
95107
96108 @ Override
97109 public void delete (ID id ) {
98-
110+ template . update ( sql . getDeleteById (), new MapSqlParameterSource ( "id" , id ));
99111 }
100112
101113 @ Override
102- public void delete (T entity ) {
114+ public void delete (T instance ) {
103115
116+ template .update (
117+ sql .getDeleteById (),
118+ new MapSqlParameterSource ("id" ,
119+ entity .getIdValue (instance )));
104120 }
105121
106122 @ Override
107123 public void delete (Iterable <? extends T > entities ) {
108124
125+ template .update (
126+ sql .getDeleteByList (),
127+ new MapSqlParameterSource ("ids" ,
128+ StreamSupport
129+ .stream (entities .spliterator (), false )
130+ .map (entity ::getIdValue )
131+ .collect (Collectors .toList ())
132+ )
133+ );
109134 }
110135
111136 @ Override
112137 public void deleteAll () {
113-
114- }
115-
116- private String createFindOneSelectSql () {
117-
118- String tableName = entity .getType ().getSimpleName ();
119- String idColumn = entity .getIdProperty ().getName ();
120-
121- return String .format ("select * from %s where %s = :id" , tableName , idColumn );
122- }
123-
124- private String createInsertSql () {
125-
126- List <String > propertyNames = new ArrayList <>();
127- entity .doWithProperties ((PropertyHandler ) persistentProperty -> propertyNames .add (persistentProperty .getName ()));
128-
129- String insertTemplate = "insert into %s (%s) values (%s)" ;
130-
131- String tableName = entity .getType ().getSimpleName ();
132-
133- String tableColumns = propertyNames .stream ().collect (Collectors .joining (", " ));
134- String parameterNames = propertyNames .stream ().collect (Collectors .joining (", :" , ":" , "" ));
135-
136- return String .format (insertTemplate , tableName , tableColumns , parameterNames );
138+ template .getJdbcOperations ().update (sql .getDeleteAll ());
137139 }
138140
139- private <S extends T > Map <String , Object > getPropertyMap (final S entity ) {
141+ private <S extends T > Map <String , Object > getPropertyMap (final S instance ) {
140142
141143 Map <String , Object > parameters = new HashMap <>();
142144
143- this .entity .doWithProperties (new PropertyHandler () {
145+ this .entity .doWithProperties (new PropertyHandler < JdbcPersistentProperty > () {
144146 @ Override
145- public void doWithPersistentProperty (PersistentProperty persistentProperty ) {
146- try {
147- parameters .put (persistentProperty .getName (), persistentProperty .getGetter ().invoke (entity ));
148- } catch (Exception e ) {
149- throw new RuntimeException (String .format ("Couldn't get value of property %s" , persistentProperty .getName ()));
150- }
147+ public void doWithPersistentProperty (JdbcPersistentProperty persistentProperty ) {
148+ parameters .put (persistentProperty .getColumnName (), entity .getPropertyAccessor (instance ).getProperty (persistentProperty ));
151149 }
152150 });
153151
0 commit comments