spring整合hibernate实例(上)

这篇主要是展示spring和hibernate的整合的实例(上)——自己也是第一弄,有些内容还不是很规范,就是项目能跑通,有好的建议欢迎提出来~

1、数据库表:(简单的用来测试的表,age是主键int类型,name是varchar类型)

2、项目工程图:

接下来的代码和配置文件就按照上图的顺序给出

3、PersonService:

package service;

import java.sql.SQLException;

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

import spring.hibernate.dao.PersonDao;
import spring.hibernate.entity.Person;

@Component
public class PersonService {

    @Autowired
    private PersonDao dao;

    public PersonService(){
        System.out.println("create personService");
    }

    public Person getPersonById(int id){
        try {
            return dao.getPerson(id);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public PersonDao getDao() {
        return dao;
    }

    public void setDao(PersonDao dao) {
        this.dao = dao;
    }

}

4、App(main函数所在的类)

package spring.hibernate;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.PersonService;
import spring.hibernate.entity.Person;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );

        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        PersonService service = (PersonService) ctx.getBean("personService");
        Person p = service.getPersonById(16);
        System.out.println(p.getAge()+" "+p.getName());

        System.out.println("end");
    }
}

5、PersonDao

package spring.hibernate.dao;

import java.sql.SQLException;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import spring.hibernate.dao.PersonDao;
import spring.hibernate.entity.Person;

public class PersonDao{

    private HibernateTemplate hibernateTemplate;

    public PersonDao(){
        System.out.println("create PersonDao");
    }

    public void setSessionFactory(SessionFactory sessionFactory){
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    public void savePerson(Person person) throws SQLException {
        // TODO Auto-generated method stub
        hibernateTemplate.save(person);

    }

    public void delPerson(Person person) throws SQLException {
        // TODO Auto-generated method stub
        hibernateTemplate.delete(person);
    }

    public void editPerson(Person person) throws SQLException {
        // TODO Auto-generated method stub
        hibernateTemplate.update(person);

    }

    public Person getPerson(int id) throws SQLException {
        // TODO Auto-generated method stub
        Person person = (Person)hibernateTemplate.get(Person.class, id);
        return person;
    }

}

6、Person(实体类)

package spring.hibernate.entity;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "person")
public class Person {

    private String name;
    @Id
    private int age;

    public Person(){
        System.out.println("create person");
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}

7、hibernate.properties(配置文件)

driver=com.mysql.jdbc.Driver
databaseurl=jdbc:mysql://localhost:3306/test
username=root
password=123456
时间: 2024-08-03 23:38:18

spring整合hibernate实例(上)的相关文章

spring整合hibernate实例(下)

这篇主要是展示spring和hibernate的整合的实例(上)——自己也是第一弄,有些内容还不是很规范,就是项目能跑通,有好的建议欢迎提出来~ 继续上篇,下篇给出两个重要的文件spring.xml和pom.xml文件: 8.spring.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans&

Spring学习(五)spring整合hibernate

上一篇博客中讲到spring dao层对jdbc的封装,用到了模板模式的设计思想 .这篇我们来看看spring中的orm层对hibernate的封装,也就是所谓的spring整合 hibernate.这里同样用了模板模式, 将hibernate开发流程封装在ORM层提供的模板类HibernateTemplate中,通过在DAO中对模板类的使用,实现对传统hibernate开发流程的代替. 一.先来看看Hibernate的传统开发流程: 1) 配置SessionFactory对象 hibernat

Spring 整合 Hibernate

Spring 整合 Hibernate •Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. •Spring 对这些 ORM 框架的支持是一致的, 因此可以把和 Hibernate 整合技术应用到其他 ORM 框架上. •Spring 2.0 同时支持 Hibernate 2.x 和 3.x. 但 Spring 2.5 只支持 Hibernate 3.1 或更高版本 1.Spring 整合 Hibernate 整合什么

Spring整合Hibernate详细步骤

阅读目录 一.概述 二.整合步骤 回到顶部 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使用上Spring的声明式事务 回到顶部 二.整合步骤 整合前准备: 持久化类: @Entity public class Book { private Integer id; private String bookName; private String isbn; private int pric

尚硅谷Spring整合Hibernate基于xml配置

描述:这是一个最简单网上书城demo. 下载地址:http://download.csdn.net/detail/u013488580/8370899 1. Spring 整合 Hibernate 整合什么 ? 1). 有 IOC 容器来管理 Hibernate 的 SessionFactory 2). 让 Hibernate 使用上 Spring 的声明式事务 2. 整合步骤: 1). 加入 hibernate ①. jar 包 ②. 添加 hibernate 的配置文件: hibernate

Spring框架学习(4)spring整合hibernate

内容源自:spring整合hibernate    spring整合注解形式的hibernate 这里和上一部分学习一样用了模板模式, 将hibernate开发流程封装在ORM层提供的模板类HibernateTemplate中,通过在DAO中对模板类的使用,实现对传统hibernate开发流程的代替. 一.先来看看Hibernate的传统开发流程: 1) 配置SessionFactory对象 hibernate.cfg.xml <session-factory> a 数据源 driver_cl

3、Spring整合Hibernate

经过前面的两节分析:1.Hibernate之生成SessionFactory源码追踪 和 2.Spring的LocalSessionFactoryBean创建过程源码分析 .我们可以得到这样一个结论,spring的LocalSessionFactoryBean具体是调用Hibernate的Configuration中configure(...)方法来读取并解析xxx.cfg.xml文件的,同样也会得到一个原生态的org.hibernate.cfg.Configuration 和 org.hibe

Spring 整合hibernate和mybatis的 applicationContext.xml的配置

Spring整合hibernate的applicationContext.xml配置 1.注解方式的实现 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" x

Spring整合hibernate(1)之基础整合

Spring整合hibernate3之基础整合 Spring集成hibernate3和4有一定的区别,目前基本都在使用3,所以此处内容以3为基础: 1.导入hibernate的包和Spring的包 1.1.导入Spring的依赖包 1.2.导入Log4j的依赖包:log4j-1.2.16.jar 1.3.导入dbcp的依赖包:commons-dbcp-1.4.jar.commons-pool-1.5.6.jar 1.4.导入hibernate3的依赖包 hibernate全部版本地址:http: