Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FordonWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
- @Override
- protected Class<?>[] getRootConfigClasses() {
- // TODO Auto-generated method stub
- return new Class<?>[] {RootConfig.class} ;
- }
- @Override
- protected Class<?>[] getServletConfigClasses() {
- // TODO Auto-generated method stub
- return new Class<?>[] {WebConfig.class} ;
- }
- @Override
- protected String[] getServletMappings() {
- // TODO Auto-generated method stub
- return new String[]{"/"};
- }
- }
- @Configuration
- @EnableWebMvc
- @ComponentScan("")
- public class WebConfig extends WebMvcConfigurerAdapter{
- @Bean
- public ViewResolver viewResolver(){
- InternalResourceViewResolver resolver = new InternalResourceViewResolver();
- resolver.setPrefix("/WEB-INF/views/");
- resolver.setSuffix(".jsp");
- return resolver;
- }
- @Override
- public void configureDefaultServletHandling(
- DefaultServletHandlerConfigurer configurer) {
- // TODO Auto-generated method stub
- super.configureDefaultServletHandling(configurer);
- configurer.enable();
- }
- }
- @Configuration
- @ComponentScan(basePackages = {"com"},excludeFilters=
- {@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)})
- public class RootConfig {
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dbname</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">password</property>
- <mapping class="com.FordonTelematik.Entity.LocationTrack"/>
- <mapping class="com.FordonTelematik.Entity.LoginDetail"/>
- <mapping class="com.FordonTelematik.Entity.OBDdata"/>
- <mapping class="com.FordonTelematik.Entity.ParentChild"/>
- </session-factory>
- </hibernate-configuration>
- public class HibernateUtil {
- private static final SessionFactory sessionFactory = buidSessionFactory();
- private static SessionFactory buidSessionFactory(){
- try{
- Configuration config = new Configuration();
- config.addAnnotatedClass(LocationTrack.class);
- config.addAnnotatedClass(LoginDetail.class);
- config.addAnnotatedClass(OBDdata.class);
- config.addAnnotatedClass(ParentChild.class);
- return config.buildSessionFactory(new StandardServiceRegistryBuilder().
- applySetting("Config", config.getProperties()).build());
- } catch(Exception e){
- }
- return null;
- }
- public static SessionFactory getSessionFactory(){
- return sessionFactory;
- }
- }
- @Controller
- public class FTController {
- @RequestMapping(value = "/", method = RequestMethod.GET)
- public String home() {
- Session session = null;
- try {
- session = HibernateUtil.getSessionFactory().openSession();
- session.beginTransaction();
- LoginDetail loginDetails = new LoginDetail();
- loginDetails.setCity("DC");
- loginDetails.setCountry("USA");
- loginDetails.setPassowrd("TKp1234");
- loginDetails.setTimeStamp(new Date());
- loginDetails.setPhoneNo("123456789");
- loginDetails.setUserID(1234);
- session.persist(loginDetails);
- session.getTransaction().commit();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (session != null && !session.beginTransaction().wasCommitted()) {
- session.beginTransaction().rollback();
- }
- }
- session.flush();
- session.close();
- return "home";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement