4bcc2c0f33064ef5cad86d26fe82cd3207b4b337
[JavaForFun] /
1 package de.example.mybatis.spring.service.impl;
2
3 import java.io.UnsupportedEncodingException;
4 import java.util.Date;
5 import java.util.Set;
6
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Service;
11 import org.springframework.transaction.annotation.Transactional;
12
13 import de.example.mybatis.model.Ad;
14 import de.example.mybatis.repository.mapper.AdMapper;
15 import de.example.mybatis.spring.service.ExampleService;
16
17 @Service("exampleService")
18 public class ExampleServiceImpl implements ExampleService {
19     private static final Logger LOGGER = LoggerFactory.getLogger(ExampleServiceImpl.class);
20
21     private final AdMapper adMapper;
22
23     @Autowired
24     public ExampleServiceImpl(AdMapper adMapper) {
25         this.adMapper = adMapper;
26     }
27
28     @Override
29     @Transactional
30         public void listAds() {
31                 LOGGER.info("listAds");
32
33                 final Set<Ad> ads = adMapper.selectAsSet();
34                 for (final Ad ad : ads) {
35                         LOGGER.info("Ad id: " + ad.getId());
36                         if (ad.getAdGps() != null) {
37                                 try {
38                                         LOGGER.info("Ad GPS: " + new String(ad.getAdGps(), "UTF-8"));
39                                 } catch (final UnsupportedEncodingException e) {
40                                         LOGGER.error("Encoding error", e);
41                                 }
42                         }
43                         LOGGER.info("Ad mobileImage: " + ad.getAdMobileImage());
44                         LOGGER.info("Ad companyCategId: " + ad.getCompanyCategId());
45                         LOGGER.info("Ad companyId: " + ad.getCompanyId());
46                         LOGGER.info("Ad createdAt: " + ad.getCreatedAt());
47                         LOGGER.info("Ad updatedAt: " + ad.getUpdatedAt());
48                         LOGGER.info("\n");
49                 }
50         }
51
52     @Override
53         @Transactional
54         public void insertAndUpdateAds() {
55                 LOGGER.info("Insert two new Ads");
56
57                 final Ad adTestOne = new Ad();
58                 adTestOne.setAdMobileImage("bildOne.jpg");
59                 adTestOne.setCompanyCategId(200L);
60                 adTestOne.setCreatedAt(new Date());
61                 adTestOne.setCompanyId(2L);
62                 adTestOne.setUpdatedAt(new Date());
63                 adMapper.insert(adTestOne);
64
65                 final Ad adTestTwo = new Ad();
66                 adTestTwo.setAdMobileImage("bildTwo.jpg");
67                 adTestTwo.setCompanyCategId(200L);
68                 adTestTwo.setCreatedAt(new Date());
69                 adTestTwo.setCompanyId(3L);
70                 adTestTwo.setUpdatedAt(new Date());
71                 adMapper.insert(adTestTwo);
72
73                 
74                 
75                 LOGGER.info("Update two Ads");
76
77                 adTestOne.setAdMobileImage("updatedBildOne.jpg");
78                 // WARNING!!! adTestOne.id keeps being NULL when using BATCH Executor of MyBatis!!!!
79                 //            So, this code will do ANYTHING AT ALL!!!!
80                 // BE CAREFUL WHEN USING BATCH MODE FOR ACCESSING DATA BASES!!!!
81                 adMapper.updateByPrimaryKey(adTestOne);
82                 
83                 // WARNING!!! adTestTwo.id keeps being NULL when using BATCH Executor of MyBatis!!!!
84                 //            So, this code will do ANYTHING AT ALL!!!!
85                 // BE CAREFUL WHEN USING BATCH MODE FOR ACCESSING DATA BASES!!!!
86                 adTestTwo.setAdMobileImage("updatedBildTwo.jpg");
87                 adMapper.updateByPrimaryKey(adTestTwo);
88                 
89                 // IF YOU WANT BATCH MODE FOR ACCESSING DATA BASES YOUR CODE MUST BE IMPLEMENTED FOR BATCH MODE.
90                 // I MEAN, IN THIS EXAMPLE SIMPLE MODE WILL WORK BUT BATCH MODE WILL NOT WORK IN ANY WAY!!!!
91                 // BATCH has some implications that must not be forgotten. You can not abstract your code
92                 // from the access mode to your data base!!!!!
93                 
94                 
95                 
96                 LOGGER.info("Insert two new Ads");
97                 
98                 final Ad adTestThree = new Ad();
99                 adTestThree.setAdMobileImage("bildThree.jpg");
100                 adTestThree.setCompanyCategId(200L);
101                 adTestThree.setCreatedAt(new Date());
102                 adTestThree.setCompanyId(2L);
103                 adTestThree.setUpdatedAt(new Date());
104                 adMapper.insert(adTestThree);
105                 
106                 
107                 final Ad adTestFour = new Ad();
108                 adTestFour.setAdMobileImage("bildFour.jpg");
109                 adTestFour.setCompanyCategId(200L);
110                 adTestFour.setCreatedAt(new Date());
111                 adTestFour.setCompanyId(2L);
112                 adTestFour.setUpdatedAt(new Date());
113                 adMapper.insert(adTestFour);
114                 
115                 
116                 listAds();
117         }
118 }