Spring+SpringMVC+Hibernate整合注解版

这篇博客详细介绍了如何将Spring、SpringMVC和Hibernate进行注解方式的整合,涵盖了从bean层到controller层的各个关键组件配置。
摘要由CSDN通过智能技术生成

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

   结果目录如下:

 

 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:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">

    <!--包扫描器-->
    <context:component-scan base-package="cn.happy"></context:component-scan>
    <!--mvc注解驱动   作用:创建7个HttpMessaeingConvert-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--1.Datasource-->
    <!--1.配置数据源c3p0-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
    </bean>

    <!--jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!--2.SessionFactory         类:Local-->
      <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
          <property name="hibernateProperties">
              <props>
                   <!--hibernate.xxxxxx必须以hibernate-->
                  <prop key="hibernate.show_sql">true</prop>
                  <prop key="hibernate.format_sql">true</prop>
                  <prop key="hbm2ddl.auto">update</prop>
                  <prop key="hibernate.dialect">	org.hibernate.dialect.Oracle10gDialect</prop>
                  <!--with current thread bind session-->
                  <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
              </props>
          </property>
<!--
          <property name="mappingDirectoryLocations" value="classpath:cn/happy/bean"></property>
-->
          <property name="packagesToScan" >
              <list>
                  <value>cn.happy.bean</value>
              </list>
          </property>
    </bean>
  <!--  5.事务管理器-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!--6.事务-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>


</beans>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值