spring4.1.6整合hibernate4.0

工程目录结构:



首先是dao层

----------------------------------------------------------------------------------------------------------------------


package org.ssh.dao;

import org.ssh.model.Student;

public interface StudentDao {
    
    public void save(Student student);
}

---------------------------------------------------------一个非常简单的方法 ----------------------------------------------


dao层的实现层内容:


package org.ssh.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.ssh.dao.StudentDao;
import org.ssh.model.Student;

public class StudentDaoImpl implements StudentDao {

    

    private SessionFactory sessionFactory;

    public void save(Student student) {
        Session session=this.sessionFactory.openSession();
        
        
        Transaction tc=session.beginTransaction();
        
        session.save(student);
        
        tc.commit();
        
        
    }



    /**
     * @return the sessionFactory
     */
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * @param sessionFactory
     *            the sessionFactory to set
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    

}

-------------------------------------------service业务层------------------------------------------


package org.ssh.service;

import org.ssh.dao.StudentDao;
import org.ssh.model.Student;

public class StudentService {
    private StudentDao studentDao;
    
    public void add(Student student){
        System.out.println(student.getSname()+":service----------");
        System.out.println("studentService!!");
        studentDao.save(student);
    
    }

    /**
     * @return the studentDao
     */
    public StudentDao getStudentDao() {
        return studentDao;
    }

    /**
     * @param studentDao the studentDao to set
     */
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
}

-----------------------------------------实体类-------------------------------------




package org.ssh.model;

public class Student {

    private Integer sid;
    private String sname;

    public Integer getSid() {
        return sid;
    }

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

    public String getSname() {
        return sname;
    }

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

}

--------------------------------------hibernate.chg.xml-------------------------------------


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>
        
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
           <mapping resource="conf/model/student.hbm.xml"/>
    </session-factory>

</hibernate-configuration>


------------------------------------hibernate.hbm.xml配置----------------------------------------------




<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="org.ssh.model">
    <class name="Student">
        <id name="sid">
            <generator class="native"/>
        </id>
        <property name="sname"/>
    </class>
</hibernate-mapping>



-------------------------------------------------------------jdbc-----------------



jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ksea
jdbc.username=root
jdbc.password=root



----------------------------spring IOC-----------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        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">
    <bean id="studentDao" class="org.ssh.dao.impl.StudentDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean id="studentService" class="org.ssh.service.StudentService">
        <property name="studentDao" ref="studentDao"/>
    </bean>
</beans>

----------------------------spring  ApplicationContext.xml--------------------------------


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.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/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- database 连接配置 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <context:property-placeholder location="conf/jdbc/jdbc.properties" />
    <!-- 配置Hibernate 因用的spring4.1.6版本所以 这里需hibernate4版本 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 在spring中加载hibernate.cfg.xml配置 -->
        <property name="configLocation" value="classpath:conf/hibernate/hibernate.cfg.xml" />
        <!-- <property name="mappingResources"> <value>conf/model/student.hbm.xml</value>
            </property> 这里是spring中加载hibernate实体配置文件,因考虑项目分工,个人建议还是将实体映射交给hibernate处理-->
    </bean>
    <import resource="student_spring.xml" />
</beans>


--------------------------------------------------------------------------------------



源码如下:












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值