App Contoller
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
| package com.honchar.springmvc.controller;
import java.util.List;
import java.util.Locale;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.honchar.springmvc.model.Guests;
import com.honchar.springmvc.service.GuestsService1;
@Controller
@RequestMapping("/")
public class AppController {
@Autowired
GuestsService1 service;
@Autowired
MessageSource messageSource;
@RequestMapping(value = { "/", "/list" }, method = RequestMethod.GET)
public String listGuests(ModelMap model) {
List<Guests> guests = service.findAllGuests();
model.addAttribute("guests", guests);
return "allguestss";
}
@RequestMapping(value = {"/new" }, method = RequestMethod.GET)
public String newGuests(ModelMap model) {
Guests guests = new Guests();
model.addAttribute("guests", guests);
model.addAttribute("edit", false);
return "registration";
}
@RequestMapping(value = { "/new" }, method = RequestMethod.POST)
public String saveGuests(@Valid Guests guests, BindingResult result,
ModelMap model) {
if (result.hasErrors()) {
return "registration";
}
if(!service.isGuestsIdUinque(guests.getId())) {
FieldError idError = new FieldError("guests","id", messageSource.getMessage("non.uique.id", new Integer[] {guests.getId()}, Locale.getDefault()));
result.addError(idError);
return "registration";
}
service.saveGuests(guests);
model.addAttribute("success", "Guests " + guests.getName() + " registered successfully");
return "success";
}
@RequestMapping(value = { "/edit-{id}-guests" }, method = RequestMethod.GET)
public String editGuests(@PathVariable int id, ModelMap model) {
Guests guests = service.findGuestsById(id);
model.addAttribute("guests", guests);
model.addAttribute("edit", true);
return "registration";
}
@RequestMapping(value = { "/edit-{id}-guests" }, method = RequestMethod.POST)
public String updateGuests(@Valid Guests guests, BindingResult result,
ModelMap model, @PathVariable int id) {
if (result.hasErrors()) {
return "registration";
}
if(!service.isGuestsIdUinque(guests.getId())) {
FieldError idError = new FieldError("guests","id", messageSource.getMessage("non.uique.id", new Integer[] {guests.getId()}, Locale.getDefault()));
result.addError(idError);
return "registration";
}
service.updateGuests(guests);
model.addAttribute("success", "Guests " + guests.getName() + " update successfully");
return "success";
}
@RequestMapping(value = { "/delete-{id}-guests"}, method = RequestMethod.GET)
public String deleteGuests(@PathVariable int id) {
service.deleteGuestsById(id);
return "redirect:/list";
}
} |
|
AbstractDao
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| package com.honchar.springmvc.dao;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class AbstractDao<PK extends Serializable, T> {
private final Class<T> persistentClass;
@SuppressWarnings("unchecked")
public AbstractDao(){
this.persistentClass =(Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
}
@Autowired
private SessionFactory sessionFactory;
protected Session getSession(){
return sessionFactory.getCurrentSession();
}
@SuppressWarnings("unchecked")
public T getByKey(PK key) {
return (T) getSession().get(persistentClass, key);
}
public void persist(T entity) {
getSession().persist(entity);
}
public void delete(T entity) {
getSession().delete(entity);
}
protected Criteria createEntityCriteria(){
return getSession().createCriteria(persistentClass);
}
} |
|
GuestsDao1
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
| package com.honchar.springmvc.dao;
import java.util.List;
import com.honchar.springmvc.model.Guests;
public interface GuestsDao1 {
Guests findById (int id);
void saveGuests(Guests guests);
void deleteGuestsById(int id);
List<Guests> findAllGuests();
Guests findGuestsById(int id);
} |
|
GuestsDaoImpl
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| package com.honchar.springmvc.dao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import com.honchar.springmvc.model.*;
@Repository("guestsDao1")
public class GuestsDaoImpl1 extends AbstractDao<Integer, Guests> implements GuestsDao1 {
public Guests findById (int id) {
return getByKey(id);
}
public void saveGuests(Guests guests) {
persist(guests);
}
public void deleteGuestsById(int id) {
Query query = getSession().createSQLQuery("delete from Guests where id = :id");
query.setInteger("id", id);
query.executeUpdate();
}
@SuppressWarnings("unchecked")
public List<Guests> findAllGuests() {
Criteria criteria = createEntityCriteria();
return (List<Guests>) criteria.list();
}
public Guests findGuestsById(int id) {
Criteria criteria = createEntityCriteria();
criteria.add(Restrictions.eq("id", id));
return (Guests) criteria.uniqueResult();
}
} |
|
Таблица Guests в которою я хочу внести данные
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
| package com.honchar.springmvc.model;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.springframework.format.annotation.DateTimeFormat;
@Entity
@Table(name="GUESTS")
public class Guests implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "SURNAME")
private String surname;
@Size(min=3, max=50)
@Column(name = "NAME")
private String name;
@Column(name = "TYPE_ROOMS")
private String type_rooms;
@Column(name = "NUMBER_ROOM")
private int number_room;
@Column(name = "ADDITIONAL_SERVICE")
private String additional_Service;
@Column(name = "SPENDING_TYPES")
private String spending_Types;
@Column(name = "NUMBER_PHONE")
private int number_phone;
@Column(name = "PAYMENT")
private String payment;
@Column(name = "TYPE_CARDS")
private String type_cards;
@Column(name = "NUMBER_CARDS")
private int number_cards;
@NotNull
@Digits(integer=8, fraction=2)
@Column(name = "AMOUNT_PAYMENT", nullable = false)
private BigDecimal amount_payment;
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern="dd/MM/yyyy")
@Column(name = "DATE" )
private Date date;
@OneToMany(mappedBy="guests", cascade = CascadeType.ALL)
private List<types_of_rooms> type_of_rooms;
@OneToMany(mappedBy="guests", cascade = CascadeType.ALL)
private List<type_of_payment> type_payment;
@OneToMany(mappedBy="guests", cascade = CascadeType.ALL)
private List<type_of_cards> types_cards;
@OneToMany(mappedBy="guests", cascade = CascadeType.ALL)
private List<spending_types> types_spending;
@OneToMany(mappedBy="guests", cascade = CascadeType.ALL)
private List<additional_services> add_services;
public Guests() {
}
public Guests(String surname, String name, String type_rooms, int number_room, String additional_Service,
String spending_Types, int number_phone,String payment,String type_cards,
int number_cards, BigDecimal amount_payment, Date date) {
this.surname = surname;
this.name = name;
this.type_rooms = type_rooms;
this.number_room = number_room;
this.additional_Service = additional_Service;
this.spending_Types = spending_Types;
this.number_phone = number_phone;
this.payment = payment;
this.type_cards = type_cards;
this.number_cards = number_cards;
this.amount_payment = amount_payment;
this.date = date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType_rooms() {
return type_rooms;
}
public void setType_rooms(String type_rooms) {
this.type_rooms = type_rooms;
}
public int getNumber_room() {
return number_room;
}
public void setNumber_room(int number_room) {
this.number_room = number_room;
}
public String getAdditional_Service() {
return additional_Service;
}
public void setAdditional_Service(String additional_Service) {
this.additional_Service = additional_Service;
}
public String getSpending_Types() {
return spending_Types;
}
public void setSpending_Types(String spending_Types) {
this.spending_Types = spending_Types;
}
public int getNumber_phone() {
return number_phone;
}
public void setNumber_phone(int number_phone) {
this.number_phone = number_phone;
}
public String getPayment() {
return payment;
}
public void setPayment(String payment) {
this.payment = payment;
}
public String getType_cards() {
return type_cards;
}
public void setType_cards(String type_cards) {
this.type_cards = type_cards;
}
public int getNumber_cards() {
return number_cards;
}
public void setNumber_cards(int number_cards) {
this.number_cards = number_cards;
}
public BigDecimal getAmount_payment() {
return amount_payment;
}
public void setAmount_payment(BigDecimal amount_payment) {
this.amount_payment = amount_payment;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public List<types_of_rooms> getType_of_rooms() {
return type_of_rooms;
}
public void setTypes_of_rooms(List<types_of_rooms> type_of_rooms) {
this.type_of_rooms = type_of_rooms;
}
public List<type_of_payment> getType_payment() {
return type_payment;
}
public void setType_payment(List<type_of_payment> type_payment) {
this.type_payment = type_payment;
}
public List<type_of_cards> getTypes_cards() {
return types_cards;
}
public void setTypes_cards(List<type_of_cards> types_cards) {
this.types_cards = types_cards;
}
public List<spending_types> getTypes_spending() {
return types_spending;
}
public void setTypes_spending(List<spending_types> types_spending) {
this.types_spending = types_spending;
}
public List<additional_services> getAdd_services() {
return add_services;
}
public void setAdd_service(List<additional_services> add_services) {
this.add_services = add_services;
}
@Override
public String toString(){
return "Guests [id=" + id +", surname=" + surname + ", name=" + name +
", type_rooms=" + type_rooms + ",additional_Service=" + additional_Service +
", spending_Types=" + spending_Types+ ", number_phone=" + number_phone +",payment="
+ payment + ",type_cards=" + type_cards+ ",number_cards=" + number_cards +",amount_payment=" + amount_payment +", date=" + date + "]";
}
} |
|
Таблица types_of_rooms с которой данные хочу перенести в виде списка
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
| package com.honchar.springmvc.model;
import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
@Entity
@Table(name = "TYPES_OF_ROOMS",catalog = "springmvc")
public class types_of_rooms implements Serializable {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
@Column(name = "ROOMS_ID")
private long roomsId;
@Column(name ="TYPE_ROOMS")
private String type_rooms;
@ManyToOne(optional = false)
@JoinColumn(name = "TYPE_ROOMS", referencedColumnName = "TYPE_ROOMS", insertable = false, updatable = false)
private Guests guests;
public Guests getGuests () {
return guests;
}
public void setGuests(Guests guests) {
this.guests = guests;
}
public long getRoomsId() {
return roomsId;
}
public void setRoomsId (long roomsId) {
this.roomsId = roomsId;
}
public String getType_rooms() {
return type_rooms;
}
public void setType_rooms(String type_rooms) {
this.type_rooms = type_rooms;
}
@Override
public String toString() {
return "types_of_rooms [roomsId=" + roomsId + ", type_rooms = " + type_rooms +"]";
}
} |
|
GuestsService1
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
| package com.honchar.springmvc.service;
import java.util.List;
import com.honchar.springmvc.model.Guests;
public interface GuestsService1 {
Guests findById (int id);
void saveGuests(Guests guests);
void updateGuests(Guests guests);
void deleteGuestsById(int id);
List<Guests> findAllGuests();
Guests findGuestsById(int id);
boolean isGuestsIdUinque(Integer id);
} |
|
GuestsServiceImpl1
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
| package com.honchar.springmvc.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.honchar.springmvc.dao.GuestsDao1;
import com.honchar.springmvc.model.Guests;
import com.honchar.springmvc.model.*;
@Service("guestsService1")
@Transactional
public class GuestsServiceImpl1 implements GuestsService1 {
@Autowired
private GuestsDao1 dao;
public Guests findById(int id) {
return dao.findById(id);
}
public void saveGuests(Guests guests) {
dao.saveGuests(guests);
}
public void updateGuests(Guests guests) {
Guests entity = dao.findById(guests.getId());
if(entity!=null){
entity.setSurname(guests.getSurname());
entity.setName(guests.getName());
entity.setType_rooms(guests.getType_rooms());
entity.setNumber_room(guests.getNumber_room());
entity.setAdditional_Service(guests.getAdditional_Service());
entity.setSpending_Types(guests.getSpending_Types());
entity.setNumber_phone(guests.getNumber_phone());
entity.setPayment(guests.getPayment());
entity.setType_cards(guests.getType_cards());
entity.setNumber_cards(guests.getNumber_cards());
entity.setAmount_payment(guests.getAmount_payment());
entity.setDate(guests.getDate());
}
}
public void deleteGuestsById(int id) {
dao.deleteGuestsById(id);
}
public List<Guests> findAllGuests() {
return dao.findAllGuests();
}
public Guests findGuestsById(int id) {
return dao.findGuestsById(id);
}
public boolean isGuestsIdUinque(Integer id) {
Guests guests = findGuestsById(id);
return ( guests == null || ((id != null) && (guests.getId() == id)));
}
} |
|
AppConfig
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| package com.honchar.springmvc.configuration;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.honchar.springmvc")
public class AppConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
} |
|
AppInitializer
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| package com.honchar.springmvc.configuration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class AppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(container);
ServletRegistration.Dynamic servlet = container.addServlet(
"dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
} |
|
HibernateConfiguration
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
| package com.honchar.springmvc.configuration;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.honchar.springmvc.configuration" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.honchar.springmvc.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
} |
|
Если нужно могу код файлов jsp скинуть
0
|