Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. package com.quickstart.com.springmvc.config;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.sql.DataSource;
  6.  
  7. import org.hibernate.SessionFactory;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.ComponentScan;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.context.annotation.PropertySource;
  13. import org.springframework.core.env.Environment;
  14. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  15. import org.springframework.orm.hibernate4.HibernateTransactionManager;
  16. import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
  17. import org.springframework.transaction.annotation.EnableTransactionManagement;
  18.  
  19. @Configuration
  20. @EnableTransactionManagement
  21. @ComponentScan({ "com.quickstart.com.springmvc.config" })
  22. @PropertySource(value = { "classpath:application.properties" })
  23. public class HibernateConfigration {
  24.  
  25. @Autowired
  26. private Environment environment;
  27.  
  28. @Bean
  29. public LocalSessionFactoryBean sessionFactory() {
  30. LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
  31. sessionFactory.setDataSource(dataSource());
  32. sessionFactory.setPackagesToScan(new String[] { "com.quickstart.com.springmvc.model" });
  33. sessionFactory.setHibernateProperties(hibernateProperties());
  34. return sessionFactory;
  35. }
  36.  
  37. @Bean
  38. public DataSource dataSource() {
  39. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  40. dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
  41. dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
  42. dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
  43. dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
  44. System.out.println(environment.getRequiredProperty("jdbc.username"));
  45. return dataSource;
  46. }
  47.  
  48. private Properties hibernateProperties() {
  49. Properties properties = new Properties();
  50. properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
  51. properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
  52. properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
  53. return properties;
  54. }
  55.  
  56. @Bean
  57. @Autowired
  58. public HibernateTransactionManager transactionManager(SessionFactory s) {
  59. HibernateTransactionManager txManager = new HibernateTransactionManager();
  60. txManager.setSessionFactory(s);
  61. return txManager;
  62. }
  63. }
  64.  
  65. package com.quickstart.com.springmvc.config;
  66.  
  67. import org.springframework.context.annotation.Bean;
  68. import org.springframework.context.annotation.ComponentScan;
  69. import org.springframework.context.annotation.Configuration;
  70. import org.springframework.web.servlet.ViewResolver;
  71. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  72. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  73. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  74. import org.springframework.web.servlet.view.InternalResourceViewResolver;
  75.  
  76. @Configuration
  77. @ComponentScan(basePackages="com.quickstart.com.springmvc")
  78. @EnableWebMvc
  79. public class MvcConfiguration extends WebMvcConfigurerAdapter{
  80.  
  81. @Bean
  82. public ViewResolver getViewResolver(){
  83. InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  84. resolver.setPrefix("/WEB-INF/views/");
  85. resolver.setSuffix(".jsp");
  86. return resolver;
  87. }
  88.  
  89. @Override
  90. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  91. registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
  92. }
  93.  
  94.  
  95. }
  96.  
  97. package com.quickstart.com.springmvc.model;
  98.  
  99. import javax.persistence.Column;
  100. import javax.persistence.Entity;
  101. import javax.persistence.Id;
  102. import javax.persistence.Table;
  103.  
  104. import org.springframework.stereotype.Service;
  105.  
  106. @Service
  107. @Entity
  108. @Table(name="user")
  109. public class User {
  110. @Id
  111. @Column(name="user_id")
  112. private int id;
  113.  
  114. @Column(name="Name")
  115. private String name;
  116. @Column(name="Email")
  117. private String email;
  118. @Column(name="Age")
  119. private int age;
  120. @Column(name="Password")
  121. private String password;
  122. @Column(name="Contact")
  123. private String contact;
  124. @Column(name="User_Name")
  125. private String username;
  126.  
  127. User(){
  128.  
  129. }
  130.  
  131.  
  132. public int getId() {
  133. return id;
  134. }
  135.  
  136. public void setId(int id) {
  137. this.id = id;
  138. }
  139.  
  140. public String getName() {
  141. return name;
  142. }
  143.  
  144. public void setName(String name) {
  145. this.name = name;
  146. }
  147.  
  148. public String getEmail() {
  149. return email;
  150. }
  151.  
  152. public void setEmail(String email) {
  153. this.email = email;
  154. }
  155.  
  156. public int getAge() {
  157. return age;
  158. }
  159.  
  160. public void setAge(int age) {
  161. this.age = age;
  162. }
  163.  
  164. User(String name){
  165. this.name = name;
  166. }
  167.  
  168. public String getPassword() {
  169. return password;
  170. }
  171.  
  172. public void setPassword(String password) {
  173. this.password = password;
  174. }
  175.  
  176. public String getContact() {
  177. return contact;
  178. }
  179.  
  180. public void setContact(String contact) {
  181. this.contact = contact;
  182. }
  183.  
  184. public String getUsername() {
  185. return username;
  186. }
  187.  
  188. public void setUsername(String username) {
  189. this.username = username;
  190. }
  191.  
  192.  
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement