Spring Mvc With Hibernate: Example
@Override public User getUserById(Long id) { Session session = sessionFactory.getCurrentSession(); return session.get(User.class, id); }
@Configuration @ComponentScan(basePackages = "com.example") @Import({HibernateConfig.class}) public class RootConfig { // Root configuration for non-web components } package com.example.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; spring mvc with hibernate example
@GetMapping("/showFormForUpdate") public String showFormForUpdate(@RequestParam("userId") Long id, Model model) { User user = userService.getUserById(id); model.addAttribute("user", user); return "user-form"; } @Override public User getUserById(Long id) { Session session
@Override protected String[] getServletMappings() { return new String[]{"/"}; } } User Entity (User.java) package com.example.model; import javax.persistence.*; import javax.validation.constraints.Email; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.Size; } } UserController.java package com.example.controller
@Override public void deleteUser(Long id) { userDAO.deleteUser(id); } } UserController.java package com.example.controller; import com.example.model.User; import com.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List;
public String getName() { return name; } public void setName(String name) { this.name = name; }
// Constructors public User() {}