SpringMVC+Spring+Hibernate整合

搭建环境,导入依赖


    
        javax.servlet
        javax.servlet-api
        3.1.0
        provided
    
    
        javax.servlet.jsp
        javax.servlet.jsp-api
        2.3.2-b01
    
    
     
    
        javax.servlet
        jstl
        1.2
    
    
    
    
        mysql
        mysql-connector-java
        5.1.34
    
    
    
    
        org.hibernate
        hibernate-core
        5.2.10.Final
    

    
    
        org.hibernate
        hibernate-entitymanager
        5.2.10.Final
    

    

    
        org.hibernate
        hibernate-osgi
        5.2.10.Final
    
    
        org.hibernate
        hibernate-envers
        5.2.10.Final
    
    
        org.hibernate
        hibernate-c3p0
        5.2.10.Final
    

    
        org.hibernate
        hibernate-proxool
        5.2.10.Final
    
    
        org.hibernate
        hibernate-infinispan
        5.2.10.Final
    
    
        org.hibernate
        hibernate-ehcache
        5.2.10.Final
    
    
    
        com.fasterxml.jackson.core
        jackson-core
        2.7.4
    
    
        com.fasterxml.jackson.core
        jackson-databind
        2.7.4
           
    
        com.fasterxml.jackson.core
        jackson-annotations
        2.7.4
    
    
    
    
        org.springframework
        spring-webmvc
        4.3.6.RELEASE
     
    
    
        org.springframework
        spring-context
        4.3.6.RELEASE
    
    
        org.springframework
        spring-context-support
        4.3.6.RELEASE
    
    
        org.springframework
        spring-web
        4.3.6.RELEASE
    
    
        org.springframework
        spring-jdbc
        4.3.6.RELEASE
    
    
    
    
        org.springframework
        spring-test
        4.3.6.RELEASE
    
    
        org.springframework
        spring-aspects
        4.3.6.RELEASE
    
    
        org.springframework
        spring-expression
        4.3.6.RELEASE
    
    
        org.springframework
        spring-orm
        4.3.6.RELEASE
    

根据需求做设计

各种文档的编写
数据库设计


SpringMVC+Spring+Hibernate整合_第1张图片
t_car表

持久层反向工程生成映射文件和实体类

这一步参照我的文章《Spring第三天(Spring整合Hibernate)》有详细介绍

创建持久层接口和实现类

接口:

import java.util.List;
import com.chenx.pojo.TCar;

public interface TCarDao {
    
    void add(TCar tCar);
    
    List findAll();
    
    void update(Long id,String color);
    
}

实现类:

import java.util.List;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.chenx.dao.TCarDao;
import com.chenx.pojo.TCar;

public class TCarDaoImpl extends HibernateDaoSupport implements TCarDao {

    public void add(TCar tCar) {
        getHibernateTemplate().save(tCar);
    }

    public List findAll() {
        return (List) getHibernateTemplate().find("from TCar");
    }

    public void update(Long id, String color) {
        getHibernateTemplate().get(TCar.class, id).setColor(color);
    }

}

创建业务层接口和实现类

接口:

import java.util.List;

import com.chenx.pojo.TCar;

public interface TCarService {
    
    void addCar(TCar tCar);
    
    void updateCar(Long id,String color);
    
    List findAllCar();
}

实现类:

import java.util.List;

import com.chenx.dao.TCarDao;
import com.chenx.pojo.TCar;
import com.chenx.service.TCarService;

public class TCarServiceImpl implements TCarService {
    private TCarDao tCarDao;
    
    public void settCarDao(TCarDao tCarDao) {
        this.tCarDao = tCarDao;
    }

    public void addCar(TCar tCar) {
        
        tCarDao.add(tCar);
    }

    public void updateCar(Long id, String color) {
        tCarDao.update(id, color);

    }

    public List findAllCar() {
        return tCarDao.findAll();
    }

}

编写表现层Action

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chenx.pojo.TCar;
import com.chenx.service.TCarService;

@Controller
public class TCarAction {
    //自动装配TCarService对象
    @Autowired
    @Qualifier("tCarService")
    private TCarService tCarService;
    /*
     * 添加车辆
     */
    @RequestMapping("/add.action")
    protected ModelAndView addTCar(HttpServletRequest request,HttpServletResponse response){
        ModelAndView mav = new ModelAndView();
        TCar  tCar = new TCar();
        String name = request.getParameter("name");
        String color = request.getParameter("color");
        String description = request.getParameter("description");
        tCar.setName(name);
        tCar.setColor(color);
        tCar.setDescription(description);
        tCarService.addCar(tCar);
        mav.setViewName("car.jsp");
        return mav;
    }
    /*
     * 显示所有车辆
     */
    @RequestMapping("/show.action")
    @ResponseBody
    protected  List showAllCar(HttpServletRequest request,HttpServletResponse response){
        
        return tCarService.findAllCar();
    }
}

Spring整合持久层和表现层配置

applicationContext.xml


    
        
        
        
        
    
    
    
        
        
        
        
        
            
                org.hibernate.dialect.MySQL5InnoDBDialect
                true
                true
            
        
        
        
            
                com/chenx/pojo/TCar.hbm.xml
            
        
    
    
    
        
     
    
    
        
    
    
    
        
        
    
    
    
        
            
            
            
        
    
    
    
        
        
    
    
    
    
    
    
    

web.xml


  
    characterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
        encoding
        utf-8
    
    
        forceEncoding
        true
    
  
  
    characterEncodingFilter
    /*
  
  
  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:applicationContext.xml
    
  
  
    springmvc
    *.action
  

贴上JSP页面代码,方便测试




Insert title here



    
车得名字:
车得颜色:
车得描述:
目前所有的车:
车名 车得颜色 车得介绍

测试结果

SpringMVC+Spring+Hibernate整合_第2张图片
image.png

输入车得信息,点击添加车辆,下方所有车辆使用ajax局部刷新一条新数据

你可能感兴趣的:(SpringMVC+Spring+Hibernate整合)