aea867b919d2848c42e5962db8e69e750e694d65
[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                 adMapper.updateByPrimaryKey(adTestOne);
79                 
80                 adTestTwo.setAdMobileImage("updatedBildTwo.jpg");
81                 adMapper.updateByPrimaryKey(adTestTwo);
82                 
83                 
84                 
85                 
86                 LOGGER.info("Insert two new Ads");
87                 
88                 final Ad adTestThree = new Ad();
89                 adTestThree.setAdMobileImage("bildThree.jpg");
90                 adTestThree.setCompanyCategId(200L);
91                 adTestThree.setCreatedAt(new Date());
92                 adTestThree.setCompanyId(2L);
93                 adTestThree.setUpdatedAt(new Date());
94                 adMapper.insert(adTestThree);
95                 
96                 
97                 final Ad adTestFour = new Ad();
98                 adTestFour.setAdMobileImage("bildFour.jpg");
99                 adTestFour.setCompanyCategId(200L);
100                 adTestFour.setCreatedAt(new Date());
101                 adTestFour.setCompanyId(2L);
102                 adTestFour.setUpdatedAt(new Date());
103                 adMapper.insert(adTestFour);
104                 
105                 
106                 listAds();
107         }
108 }