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);
64 // BATCH MODE: no transaction
65 // adTestOne.id = VALUE, AUTOINCREMENT FROM DATABASE
66 // BATCH MODE: transaction
67 // adTestOne.id = null <-------------- IF YOU WANT TO USE BATCH MODE, YOUR CODE MUST BE IMPLEMENTED KNOWING THIS KIND OF STUFF!!!!
68 // SIMPLE MODE: no transaction
69 // adTestOne.id = VALUE, AUTOINCREMENT FROM DATABASE
70 // SIMPLE MODE: transaction
71 // adTestOne.id = VALUE, AUTOINCREMENT FROM DATABASE
72 // REUSE MODE: no transaction
73 // adTestOne.id = VALUE, AUTOINCREMENT FROM DATABASE
74 // REUSE MODE: transaction
75 // adTestOne.id = VALUE, AUTOINCREMENT FROM DATABASE
77 // What is good about REUSE? When running this code in some transaction (using @Transactional)
78 // the PreparedStatement being used for INSERT can be reused in the next INSERT (some lines below)
79 // As always there is a cache ONLY for DMLs where the key is the statement. If the statement is the same
80 // the PreparedStatement can be reused. Reusing some PreparedStatement means there is no need to call again
81 // getConnection saving in this way CPU cycles. Besides when running in some transaction getConnection
82 // will always return the same connection so, again calling getConnection is not needed.
83 // If there is no transaction the cache is useless because after every DML will be cleaned up. Without transaction
84 // REUSE and SIMPLE are the same. With transaction REUSE skips the code calling getConnection. With transaction
85 // getConnection returns always the same connection. So, with transaction we can avoid some CPU cycles using REUSE.
87 final Ad adTestTwo = new Ad();
88 adTestTwo.setAdMobileImage("bildTwo.jpg");
89 adTestTwo.setCompanyCategId(200L);
90 adTestTwo.setCreatedAt(new Date());
91 adTestTwo.setCompanyId(3L);
92 adTestTwo.setUpdatedAt(new Date());
93 adMapper.insert(adTestTwo);
97 LOGGER.info("Update two Ads");
99 adTestOne.setAdMobileImage("updatedBildOne.jpg");
100 // WARNING!!! adTestOne.id keeps being NULL when using BATCH Executor of MyBatis!!!!
101 // So, this code will do ANYTHING AT ALL!!!!
102 // BE CAREFUL WHEN USING BATCH MODE FOR ACCESSING DATA BASES!!!!
103 adMapper.updateByPrimaryKey(adTestOne);
105 // WARNING!!! adTestTwo.id keeps being NULL when using BATCH Executor of MyBatis!!!!
106 // So, this code will do ANYTHING AT ALL!!!!
107 // BE CAREFUL WHEN USING BATCH MODE FOR ACCESSING DATA BASES!!!!
108 adTestTwo.setAdMobileImage("updatedBildTwo.jpg");
109 adMapper.updateByPrimaryKey(adTestTwo);
111 // IF YOU WANT BATCH MODE FOR ACCESSING DATA BASES YOUR CODE MUST BE IMPLEMENTED FOR BATCH MODE.
112 // I MEAN, IN THIS EXAMPLE SIMPLE MODE WILL WORK BUT BATCH MODE WILL NOT WORK IN ANY WAY!!!!
113 // BATCH has some implications that must not be forgotten. You can not abstract your code
114 // from the access mode to your data base!!!!!
118 LOGGER.info("Insert two new Ads");
120 final Ad adTestThree = new Ad();
121 adTestThree.setAdMobileImage("bildThree.jpg");
122 adTestThree.setCompanyCategId(200L);
123 adTestThree.setCreatedAt(new Date());
124 adTestThree.setCompanyId(2L);
125 adTestThree.setUpdatedAt(new Date());
126 adMapper.insert(adTestThree);
129 final Ad adTestFour = new Ad();
130 adTestFour.setAdMobileImage("bildFour.jpg");
131 adTestFour.setCompanyCategId(200L);
132 adTestFour.setCreatedAt(new Date());
133 adTestFour.setCompanyId(2L);
134 adTestFour.setUpdatedAt(new Date());
135 adMapper.insert(adTestFour);