Spring-SpringMVC-Hibernate Integration

Source: Internet
Author: User

Spring-SpringMVC-Hibernate Integration

Because maven is not used, check the package structure directly! Maven may be included later!

 

There are a lot of packages, but don't care about these details.

See the configuration file below

A spring-common.xml (equivalent to applicationContext. xml)

 
 
  
  
   
   
   
   
  
  
  
   
   
   
    
     
      
Org. hibernate. dialect. MySQLDialect
     
     
      
Update
     
     
      
True
     
     
      
True
     
    
   
   
    
     
      
Com. iss. model
     
    
   
  
  
  
   
  
  
  
   
   
    
     
      
PROPAGATION_REQUIRED,-Exception
     
     
      
PROPAGATION_REQUIRED,-myException
     
     
      
PROPAGATION_REQUIRED
     
     
      
PROPAGATION_REQUIRED
     
    
   
  
 

Another is the SpringMVC configuration file.

Spring-mvc.xml

 
 
  
  
  
  
  
  
  
  
   
   
  
 

Web. xml

 
 
  
   
Json_test
  
  
   
    
Login. jsp
   
  
  
  
   
    
ContextConfigLocation
   
   
    
Classpath *: spring-*. xml
   
  
  
  
   
    
Org. springframework. web. context. ContextLoaderListener
   
  
  
  
   
    
SpringMVC
   
   
    
Org. springframework. web. servlet. DispatcherServlet
   
   
    
     
ContextConfigLocation
    
    
     
WEB-INF/classes/spring-mvc.xml
    
   
   
    
1
   
  
  
   
    
SpringMVC
   
   
    
/
   
  
  
  
   
    
EncodingFilter
   
   
    
Org. springframework. web. filter. CharacterEncodingFilter
   
   
    
     
Encoding
    
    
     
UTF-8
    
   
   
    
     
ForceEncoding
    
    
     
True
    
   
  
  
   
    
EncodingFilter
   
   
    
/*
   
  
  
  
   
    
OpenSession
   
   
    
Org. springframework. orm. hibernate4.support. OpenSessionInViewFilter
   
  
  
   
    
OpenSession
   
   
    
/*
   
  
 


All the configuration files can complete the following logic code.


Com. iss. model. User. class

package com.iss.model;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;/** * @author dell *  */@Entity@Table(name = "user")public class User {private int id;private String name;@Id@GeneratedValue(strategy = GenerationType.AUTO)public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

Com. iss. dao. UserDao. class

package com.iss.dao;import java.util.List;import com.iss.model.User;public interface UserDao {public User addUser(User user);public List
 
   getAllUsers();public User getUser(int userId);public void deleteUser(User user);}
 

Com. iss. dao. impl. UserDaoImpl. class

package com.iss.dao.impl;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import com.iss.dao.UserDao;import com.iss.model.User;@Repositorypublic class UserDaoImpl implements UserDao {private SessionFactory sessionFactory;@Autowiredpublic void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}public User addUser(User user) {Session session = sessionFactory.getCurrentSession();session.saveOrUpdate(user);session.flush();return user;}public List
 
   getAllUsers() {Session session = sessionFactory.getCurrentSession();Query query = session.createQuery("from User");List
  
    users = query.list();session.flush();return users;}public User getUser(int userId) {Session session = sessionFactory.getCurrentSession();User user = (User) session.get(User.class, userId);return user;}public void deleteUser(User user) {Session session = sessionFactory.getCurrentSession();session.delete(user);session.flush();}}
  
 

Com. iss. service. UserService. class

package com.iss.service;import java.util.List;import com.iss.model.User;public interface UserService {public User addUser(User user);public List
 
   getAllUsers();public User getUser(int userId);public void deleteUser(User user);}
 

Com. iss. service. UserServiceImpl. class

package com.iss.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.iss.dao.UserDao;import com.iss.model.User;import com.iss.service.UserService;@Service("UserService")public class UserServiceImpl implements UserService {private UserDao userDao;@Autowiredpublic void setUserDao(UserDao userDao) {this.userDao = userDao;}public User addUser(User user) {// TODO Auto-generated method stubreturn userDao.addUser(user);}public List
 
   getAllUsers() {// TODO Auto-generated method stubreturn userDao.getAllUsers();}public User getUser(int userId) {// TODO Auto-generated method stubreturn userDao.getUser(userId);}public void deleteUser(User user) {userDao.deleteUser(user);}}
 
Com. iss. controller. UserController
package com.iss.controller;import java.io.IOException;import java.util.List;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;import com.iss.model.User;import com.iss.service.UserService;@Controller@RequestMapping("/user")public class UserController {private UserService userService;@Autowiredpublic void setUserService(UserService userService) {this.userService = userService;}@RequestMapping("/http://blog.csdn.net/wangdianyong/article/details/toAddUser")public String http://blog.csdn.net/wangdianyong/article/details/toAddUser() {return "addUser";}@RequestMapping("/toUpdateUser")public String toUpdateUser(int id, ModelMap modelMap) {User user = userService.getUser(id);modelMap.addAttribute("user", user);return "editUser";}@RequestMapping("/addUser")public String addUser(User user) {userService.addUser(user);return "redirect:/http://blog.csdn.net/wangdianyong/article/details/user/getAllUser";}@RequestMapping("/getAllUser")public String getAllUsers(ModelMap modelMap) {List
 
   userList = userService.getAllUsers();modelMap.addAttribute("userList", userList);return "index";}@RequestMapping("/delUser")public String delUser(int id, HttpServletResponse response)throws IOException {User user = userService.getUser(id);System.err.println(user.getName());userService.deleteUser(user);response.getWriter().print("success");return null;}}
 
Below is the code for several jsp pages

Login. jsp

Go to the user management page

Index. jsp

Add User
 
  
 
 
 
Name Age Operation
$ {User. id} $ {User. name} Edit and delete

Which of the following parameters need to be referenced when Jquery is used for deletion? Jquery package

<Script type = "text/javascript" src = ".. /js/jQuery. js "> </script> <script type =" text/javascript "> function del (id) {/* $. get ("delUser? Id = "+ id, function (data) {alert (data); if (" success "= data) {alert (" deleted successfully "); $ ("# tr" + id ). remove () ;}else {alert ("failed to delete") ;}}); */var url = "delUser? Id = "+ id; $. ajax ({type: "post", url: url, dataType: "text", success: function (data) {if ("success" = data) {alert ("deleted successfully"); $ ("# tr" + id ). remove () ;}else {alert ("failed to delete") ;}}}) ;}</script>

EditUser. jsp

Edit user
AddUser. jsp

Add User

Among them, js Code

<script type="text/javascript">function addUser() {var form = document.forms[0];form.action = "addUser";form.method = "post";form.submit();}</script>

The above completes the integration between Spring-SpringMVC-Hibernate and basic crud operations. Of course there are still many shortcomings!










Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.