Skip to content

Commit 5b8991d

Browse files
authored
Added a DAO/hibernate implementation
1 parent 144aa39 commit 5b8991d

78 files changed

Lines changed: 5614 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎frameworks/spring/app5/pom.xml‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.herrj</groupId>
4+
<artifactId>app5</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<packaging>war</packaging>
7+
<name>app5 Maven Webapp</name>
8+
<url>http://maven.apache.org</url>
9+
<properties>
10+
<failOnMissingWebXml>false</failOnMissingWebXml>
11+
</properties>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>junit</groupId>
16+
<artifactId>junit</artifactId>
17+
<version>3.8.1</version>
18+
<scope>test</scope>
19+
</dependency>
20+
<!-- Spring MVC Dependency -->
21+
<dependency>
22+
<groupId>org.springframework</groupId>
23+
<artifactId>spring-webmvc</artifactId>
24+
<version>4.3.7.RELEASE</version>
25+
</dependency>
26+
27+
<!-- Spring ORM -->
28+
<dependency>
29+
<groupId>org.springframework</groupId>
30+
<artifactId>spring-orm</artifactId>
31+
<version>4.3.7.RELEASE</version>
32+
</dependency>
33+
34+
<!-- Mysql Connector -->
35+
<dependency>
36+
<groupId>mysql</groupId>
37+
<artifactId>mysql-connector-java</artifactId>
38+
<version>6.0.6</version>
39+
</dependency>
40+
41+
<!-- Hibernate ORM -->
42+
<dependency>
43+
<groupId>org.hibernate</groupId>
44+
<artifactId>hibernate-core</artifactId>
45+
<version>5.2.10.Final</version>
46+
</dependency>
47+
48+
<!-- Hibernate-C3P0 Integration -->
49+
<dependency>
50+
<groupId>org.hibernate</groupId>
51+
<artifactId>hibernate-c3p0</artifactId>
52+
<version>5.2.10.Final</version>
53+
</dependency>
54+
55+
<!-- c3p0 -->
56+
<dependency>
57+
<groupId>com.mchange</groupId>
58+
<artifactId>c3p0</artifactId>
59+
<version>0.9.5.2</version>
60+
</dependency>
61+
62+
<!-- Hibernate Validator -->
63+
<dependency>
64+
<groupId>org.hibernate</groupId>
65+
<artifactId>hibernate-validator</artifactId>
66+
<version>5.4.1.Final</version>
67+
</dependency>
68+
69+
<!-- JSTL Dependency -->
70+
<dependency>
71+
<groupId>javax.servlet.jsp.jstl</groupId>
72+
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
73+
<version>1.2.1</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>taglibs</groupId>
77+
<artifactId>standard</artifactId>
78+
<version>1.1.2</version>
79+
</dependency>
80+
81+
<!-- Servlet Dependency -->
82+
<dependency>
83+
<groupId>javax.servlet</groupId>
84+
<artifactId>javax.servlet-api</artifactId>
85+
<version>3.1.0</version>
86+
<scope>provided</scope>
87+
</dependency>
88+
89+
<!-- JSP Dependency -->
90+
<dependency>
91+
<groupId>javax.servlet.jsp</groupId>
92+
<artifactId>javax.servlet.jsp-api</artifactId>
93+
<version>2.3.1</version>
94+
<scope>provided</scope>
95+
</dependency>
96+
</dependencies>
97+
<build>
98+
<sourceDirectory>src/main/java</sourceDirectory>
99+
<resources>
100+
<resource>
101+
<directory>src/main/resources</directory>
102+
</resource>
103+
</resources>
104+
<plugins>
105+
<plugin>
106+
<artifactId>maven-compiler-plugin</artifactId>
107+
<version>3.5.1</version>
108+
<configuration>
109+
<source>1.8</source>
110+
<target>1.8</target>
111+
</configuration>
112+
</plugin>
113+
114+
<!-- Embedded Apache Tomcat required for testing war -->
115+
<plugin>
116+
<groupId>org.apache.tomcat.maven</groupId>
117+
<artifactId>tomcat7-maven-plugin</artifactId>
118+
<version>2.2</version>
119+
<configuration>
120+
<path>/</path>
121+
</configuration>
122+
</plugin>
123+
</plugins>
124+
</build>
125+
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.herrj.config;
2+
3+
import java.util.Properties;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.context.annotation.ComponentScans;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.context.annotation.PropertySource;
11+
import org.springframework.core.env.Environment;
12+
import org.springframework.orm.hibernate5.HibernateTransactionManager;
13+
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
14+
import org.springframework.transaction.annotation.EnableTransactionManagement;
15+
import static org.hibernate.cfg.Environment.*;
16+
import com.herrj.model.User;
17+
18+
@Configuration
19+
@PropertySource("classpath:db.properties")
20+
@EnableTransactionManagement
21+
@ComponentScans(value = { @ComponentScan("com.herrj.dao"),
22+
@ComponentScan("com.herrj.service") })
23+
public class AppConfig {
24+
25+
@Autowired
26+
private Environment env;
27+
28+
@Bean
29+
public LocalSessionFactoryBean getSessionFactory() {
30+
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
31+
32+
Properties props = new Properties();
33+
// Setting JDBC properties
34+
props.put(DRIVER, env.getProperty("mysql.driver"));
35+
props.put(URL, env.getProperty("mysql.url"));
36+
props.put(USER, env.getProperty("mysql.user"));
37+
props.put(PASS, env.getProperty("mysql.password"));
38+
39+
// Setting Hibernate properties
40+
props.put(SHOW_SQL, env.getProperty("hibernate.show_sql"));
41+
props.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto"));
42+
43+
// Setting C3P0 properties
44+
props.put(C3P0_MIN_SIZE,
45+
env.getProperty("hibernate.c3p0.min_size"));
46+
props.put(C3P0_MAX_SIZE,
47+
env.getProperty("hibernate.c3p0.max_size"));
48+
props.put(C3P0_ACQUIRE_INCREMENT,
49+
env.getProperty("hibernate.c3p0.acquire_increment"));
50+
props.put(C3P0_TIMEOUT,
51+
env.getProperty("hibernate.c3p0.timeout"));
52+
props.put(C3P0_MAX_STATEMENTS,
53+
env.getProperty("hibernate.c3p0.max_statements"));
54+
55+
factoryBean.setHibernateProperties(props);
56+
factoryBean.setAnnotatedClasses(User.class);
57+
return factoryBean;
58+
}
59+
60+
@Bean
61+
public HibernateTransactionManager getTransactionManager() {
62+
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
63+
transactionManager.setSessionFactory(getSessionFactory().getObject());
64+
return transactionManager;
65+
}
66+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.herrj.config;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class MyWebAppInitializer
6+
extends AbstractAnnotationConfigDispatcherServletInitializer {
7+
8+
@Override
9+
protected Class<?>[] getRootConfigClasses() {
10+
return new Class[] { AppConfig.class };
11+
}
12+
13+
@Override
14+
protected Class<?>[] getServletConfigClasses() {
15+
return new Class[] { WebConfig.class };
16+
}
17+
18+
@Override
19+
protected String[] getServletMappings() {
20+
return new String[] { "/" };
21+
}
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.herrj.config;
2+
3+
import org.springframework.context.MessageSource;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.support.ResourceBundleMessageSource;
8+
import org.springframework.validation.Validator;
9+
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
10+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
11+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
12+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
13+
import org.springframework.web.servlet.view.JstlView;
14+
15+
@Configuration
16+
@EnableWebMvc
17+
@ComponentScan(basePackages = { "com.herrj.controller",
18+
"com.herrj.validator" })
19+
public class WebConfig extends WebMvcConfigurerAdapter {
20+
21+
@Bean
22+
public InternalResourceViewResolver resolver() {
23+
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
24+
resolver.setViewClass(JstlView.class);
25+
resolver.setPrefix("/WEB-INF/views/");
26+
resolver.setSuffix(".jsp");
27+
return resolver;
28+
}
29+
30+
@Bean
31+
public MessageSource messageSource() {
32+
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
33+
source.setBasename("messages");
34+
return source;
35+
}
36+
37+
@Override
38+
public Validator getValidator() {
39+
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
40+
validator.setValidationMessageSource(messageSource());
41+
return validator;
42+
}
43+
44+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.herrj.controller;
2+
3+
import java.util.Locale;
4+
import javax.validation.Valid;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.validation.BindingResult;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.ModelAttribute;
11+
import org.springframework.web.bind.annotation.PostMapping;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
15+
import com.herrj.model.User;
16+
import com.herrj.service.UserService;
17+
18+
19+
@Controller
20+
public class UserController {
21+
22+
@Autowired
23+
private UserService userService;
24+
25+
@GetMapping("/")
26+
//@RequestMapping(value= {"/","/userForm"}, method= RequestMethod.GET)
27+
public String userForm(Locale locale, Model model) {
28+
29+
model.addAttribute("user", new User());
30+
model.addAttribute("users", userService.list());
31+
32+
return "userForm";
33+
}
34+
35+
@PostMapping("/saveUser")
36+
public String saveUser(@ModelAttribute("user") @Valid User user,
37+
BindingResult result, Model model) {
38+
39+
if (result.hasErrors()) {
40+
41+
model.addAttribute("users", userService.list());
42+
return "userForm";
43+
}
44+
45+
userService.save(user);
46+
47+
return "redirect:/";
48+
}
49+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.herrj.dao;
2+
3+
import java.util.List;
4+
5+
import com.herrj.model.User;
6+
7+
public interface UserDao {
8+
void save(User user);
9+
List<User> list();
10+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.herrj.dao;
2+
3+
import java.util.List;
4+
import javax.persistence.TypedQuery;
5+
import org.hibernate.SessionFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Repository;
8+
import com.herrj.model.User;
9+
10+
@Repository
11+
public class UserDaoImp implements UserDao {
12+
13+
@Autowired
14+
private SessionFactory sessionFactory;
15+
16+
public void save(User user) {
17+
sessionFactory.getCurrentSession().save(user);
18+
}
19+
20+
public List<User> list() {
21+
@SuppressWarnings("unchecked")
22+
TypedQuery<User> query = sessionFactory.getCurrentSession().createQuery("from User");
23+
return query.getResultList();
24+
}
25+
26+
}

0 commit comments

Comments
 (0)