SpringMVC框架简介②

SSH整合(SpringMVC/Spring/Hibernate三大框架的整合):

引包: 链接:https://pan.baidu.com/s/1i5Y6DZeNm5pVJHrm2R7LbA 密码:uue3

dao实现类:

package com.rl.dao.impl;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.rl.dao.PersonDao;
import com.rl.model.Person;
@Repository
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {

	@Autowired
	public void setMySessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	@Override
	public void savePerson(Person p) {
		this.getHibernateTemplate().save(p);
	}
}

service实现类:

package com.rl.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rl.dao.PersonDao;
import com.rl.model.Person;
import com.rl.service.PersonService;
@Service
public class PersonServiceImpl implements PersonService {

	@Autowired
	private PersonDao personDao;
	
	@Override
	public void savePerson(Person p) {
		personDao.savePerson(p);
	}
}

实体类Person:

package com.rl.model;
// Generated 2018-8-12 21:26:01 by Hibernate Tools 3.6.0.Final

import java.util.Date;

/**
 * Person generated by hbm2java
 */
public class Person implements java.io.Serializable {

    private Integer personId;
    private String name;
    private Integer gender;
    private String address;
    private Date birthday;

    public Person() {
    }

    public Person(String name, Integer gender, String address, Date birthday) {
        this.name = name;
        this.gender = gender;
        this.address = address;
        this.birthday = birthday;
    }

    public Integer getPersonId() {
        return this.personId;
    }

    public void setPersonId(Integer personId) {
        this.personId = personId;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getGender() {
        return this.gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getBirthday() {
        return this.birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

}

控制类Controller:

package com.rl.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rl.model.Person;
import com.rl.service.PersonService;

@Controller
@RequestMapping("/person")
public class PersonController {

	@Autowired
	private PersonService personService;
	
	@RequestMapping("/save.do")
	public String save(Person person){
		personService.savePerson(person);
		return "success";
	}
	
	@RequestMapping("/toForm.do")
	public String toForm(){
		return "form";
	}
	
}

映射文件:





    
        
            
            
        
        
            
        
        
            
        
        
            
        
        
            
        
    

Spring配置文件:



	
	
			
	
		
		
		
		
	
	
	
		
		
		
			
				org.hibernate.dialect.MySQL5Dialect
				true
				update
			
		
	
	
	
	
	
		
	
	
	
		
			
			
			
			
		
	
	
	
		
	

SpringMVC配置文件:



		
		
		
	
		
			
			
			
			
				
 

web.xml配置文件:



  
  	springmvc
  	org.springframework.web.servlet.DispatcherServlet
  	
  		contextConfigLocation
  		classpath:springmvc.xml
  	
  
  
  	springmvc
  	
  	*.do
  
  
  
  	org.springframework.web.context.ContextLoaderListener
  
  
  	contextConfigLocation
  	classpath:beans.xml
  

表单页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
  
  
  
   	
姓名:
地址:
性别:
生日:

数据库结果:

SpringMVC框架简介②_第1张图片

你可能感兴趣的:(SSH,SpringMVC,Spring,Hibernate,SpringMVC)