Spring+SpringMVC+Hibernate整合注解版

这次是Spring+SpringMVC+Hibernate整合的注解版

   结果目录如下:

 Spring+SpringMVC+Hibernate整合注解版_第1张图片

 bean层:

  Student:

package cn.happy.bean;

import javax.persistence.*;

/**
 * Created by lenovo on 2017/10/13.
 */
@Entity
@Table(name = "studentnew")
public class Student {
    @Id
    @GeneratedValue
    private int sid;
    @Column
    private String sname;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }
}

controller层:

 StudentCTR:

package cn.happy.controller;

import cn.happy.bean.Student;
import cn.happy.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * Created by lenovo on 2017/10/13.
 */
@Controller
public class StudentCTR {
    @Resource(name = "studentService")
    private StudentService studentService;
    @RequestMapping("/add")
    public String add(Student student){
               studentService.add(student);
        return "index";
    }
}

dao层:

  StudentDao:

  

package cn.happy.dao;

import cn.happy.bean.Student;
import org.hibernate.Session;

/**
 * Created by lenovo on 2017/10/13.
 */
public interface StudentDao {
    public int add(Student student);
}

  StudentDaoImpl:

package cn.happy.dao;

import cn.happy.bean.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * Created by lenovo on 2017/10/13.
 */
@Repository("studentdao")
public class StudnetDaoImpl implements StudentDao {
@Resource
private SessionFactory sessionFactory;
    public int add(Student student) {
        Serializable s=sessionFactory.getCurrentSession().save(student);
        return (Integer) s;
    }
}

service:

StudentService:

package cn.happy.service;

import cn.happy.bean.Student;

/**
 * Created by lenovo on 2017/10/13.
 */
public interface StudentService {
    public int add(Student student);
}


StudentServiceImpl:

package cn.happy.service;

import cn.happy.dao.StudentDao;
import cn.happy.bean.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by lenovo on 2017/10/13.
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Resource(name = "studentdao")
    private StudentDao dao;
    @Transactional
    public int add(Student student) {
        int result=dao.add(student);
        return result;
    }



    public StudentDao getDao() {
        return dao;
    }

    public void setDao(StudentDao dao) {
        this.dao = dao;
    }
}
applicationContext.xml:





    
    
    
    
    
    
        
        
    

    
    
    
        
        
        
        
    

    
    

    
      
        
          
              
                   
                  true
                  true
                  update
                  	org.hibernate.dialect.Oracle10gDialect
                  
                  org.springframework.orm.hibernate5.SpringSessionContext
              
          

          
              
                  cn.happy.bean
              
          
    
  
    
        
    

    
    



其他和之前没什么不同,就省略了。

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