1 package de.example.mybatis.spring.service.impl;
3 import java.io.UnsupportedEncodingException;
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;
13 import de.example.mybatis.model.Ad;
14 import de.example.mybatis.repository.mapper.AdMapper;
15 import de.example.mybatis.spring.service.ExampleService;
17 @Service("exampleService")
18 public class ExampleServiceImpl implements ExampleService {
19 private static final Logger LOGGER = LoggerFactory.getLogger(ExampleServiceImpl.class);
21 private final AdMapper adMapper;
24 public ExampleServiceImpl(AdMapper adMapper) {
25 this.adMapper = adMapper;
30 public void listAds() {
31 LOGGER.info("listAds");
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) {
38 LOGGER.info("Ad GPS: " + new String(ad.getAdGps(), "UTF-8"));
39 } catch (final UnsupportedEncodingException e) {
40 LOGGER.error("Encoding error", e);
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());
54 public void insertAndUpdateAds() {
55 LOGGER.info("Insert two new Ads");
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);
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);
75 LOGGER.info("Update two Ads");
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);
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);
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!!!!!
96 LOGGER.info("Insert two new Ads");
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);
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);