Advertisement
Guest User

Untitled

a guest
Jan 9th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. public class FordonWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  2.  
  3. @Override
  4. protected Class<?>[] getRootConfigClasses() {
  5. // TODO Auto-generated method stub
  6. return new Class<?>[] {RootConfig.class} ;
  7. }
  8.  
  9. @Override
  10. protected Class<?>[] getServletConfigClasses() {
  11. // TODO Auto-generated method stub
  12. return new Class<?>[] {WebConfig.class} ;
  13. }
  14.  
  15. @Override
  16. protected String[] getServletMappings() {
  17. // TODO Auto-generated method stub
  18. return new String[]{"/"};
  19. }
  20. }
  21.  
  22. @Configuration
  23. @EnableWebMvc
  24. @ComponentScan("")
  25. public class WebConfig extends WebMvcConfigurerAdapter{
  26.  
  27. @Bean
  28. public ViewResolver viewResolver(){
  29. InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  30. resolver.setPrefix("/WEB-INF/views/");
  31. resolver.setSuffix(".jsp");
  32. return resolver;
  33. }
  34.  
  35. @Override
  36. public void configureDefaultServletHandling(
  37. DefaultServletHandlerConfigurer configurer) {
  38. // TODO Auto-generated method stub
  39. super.configureDefaultServletHandling(configurer);
  40. configurer.enable();
  41. }
  42. }
  43.  
  44. @Configuration
  45. @ComponentScan(basePackages = {"com"},excludeFilters=
  46. {@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)})
  47.  
  48. public class RootConfig {
  49.  
  50. }
  51.  
  52. <?xml version="1.0" encoding="UTF-8"?>
  53. <!DOCTYPE hibernate-configuration PUBLIC
  54. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  55. "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  56. <hibernate-configuration>
  57. <session-factory>
  58. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  59. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dbname</property>
  60. <property name="hibernate.connection.username">root</property>
  61. <property name="hibernate.connection.password">password</property>
  62.  
  63. <mapping class="com.FordonTelematik.Entity.LocationTrack"/>
  64. <mapping class="com.FordonTelematik.Entity.LoginDetail"/>
  65. <mapping class="com.FordonTelematik.Entity.OBDdata"/>
  66. <mapping class="com.FordonTelematik.Entity.ParentChild"/>
  67.  
  68. </session-factory>
  69. </hibernate-configuration>
  70.  
  71. public class HibernateUtil {
  72.  
  73. private static final SessionFactory sessionFactory = buidSessionFactory();
  74.  
  75. private static SessionFactory buidSessionFactory(){
  76. try{
  77. Configuration config = new Configuration();
  78.  
  79. config.addAnnotatedClass(LocationTrack.class);
  80. config.addAnnotatedClass(LoginDetail.class);
  81. config.addAnnotatedClass(OBDdata.class);
  82. config.addAnnotatedClass(ParentChild.class);
  83.  
  84. return config.buildSessionFactory(new StandardServiceRegistryBuilder().
  85. applySetting("Config", config.getProperties()).build());
  86.  
  87.  
  88. } catch(Exception e){
  89.  
  90. }
  91. return null;
  92.  
  93. }
  94.  
  95. public static SessionFactory getSessionFactory(){
  96. return sessionFactory;
  97. }
  98. }
  99.  
  100. @Controller
  101. public class FTController {
  102.  
  103. @RequestMapping(value = "/", method = RequestMethod.GET)
  104. public String home() {
  105. Session session = null;
  106. try {
  107. session = HibernateUtil.getSessionFactory().openSession();
  108. session.beginTransaction();
  109.  
  110. LoginDetail loginDetails = new LoginDetail();
  111. loginDetails.setCity("DC");
  112. loginDetails.setCountry("USA");
  113. loginDetails.setPassowrd("TKp1234");
  114. loginDetails.setTimeStamp(new Date());
  115. loginDetails.setPhoneNo("123456789");
  116. loginDetails.setUserID(1234);
  117.  
  118. session.persist(loginDetails);
  119. session.getTransaction().commit();
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. } finally {
  123. if (session != null && !session.beginTransaction().wasCommitted()) {
  124. session.beginTransaction().rollback();
  125. }
  126. }
  127. session.flush();
  128. session.close();
  129.  
  130. return "home";
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement