`
endual
  • 浏览: 3545207 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

spring 整合 hibernate

 
阅读更多

1.闲话少说了,先看版本吧。


2.在看要导入的包吧:


其中打圈的,我在hibernate和spring的lib包中无法找到,要自己单独下载。

因为版本的原因,或许你下载的包可能会导致包无法找到,没有关系,有些事情总是试试才知道的。

这也是学习必经之路哦。

其中的dbcp 和pool 是spring配置数据源(数据库连接池要用到的),而slf4j的几个包,是由于看了马士兵视频

才养成这个习惯,整合的时候要用到这个log包。

 

3.整合

整个工程的样子

 


其中打红圈的是主运行类,会在下面打上代码。。。

 

-----------

 

基本的代码

Entity类,Item.java

 

package com.endual.domain;

/**
 * @author Gavin King
 */
public class Item {
	private Long id;
	private String name;
	private String description;
	
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
 

 

  Item.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="com.endual.domain">

	<class name="Item" table="table_items">
		<id name="id">
			<generator class="increment"/>
		</id>
		<property name="name" />
		<property name="description" />
	</class>

	

</hibernate-mapping>
 

 

  applicationContext.xml配置代码

  我首先要向你强烈推荐的是spring的文档,里面有详细的配置信息哦。网上的资料都是那来的。

 

<?xml version="1.0" encoding="UTF-8"?>

<!--
	- Application context definition for JPetStore's business layer.
	- Contains bean references to the transaction manager and to the DAOs in
	- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<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"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">



	<bean id="myDataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/test" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>

	<bean id="mySessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="myDataSource" />
		
		<property name="mappingResources">
			<list>
				<value>com/endual/domain/Item.hbm.xml</value>
			</list>
		</property>
		
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.SQLServerDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		
	</bean>


	<bean id="hibernateTemplete"
		class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="mySessionFactory"></property>
	</bean>


	<bean name="daoImpl" class="com.endual.test.DaoImpl">
		<property name="sessionFactory" ref="mySessionFactory"></property>
	</bean>

   
	<bean name="person" class="com.endual.domain.Person">
	
		<property name="name" value="chenwei" />
	</bean>
	
	
	


</beans>
 

 

    你可能在这里会错误的:

 

    <property name="hibernateProperties">

			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.SQLServerDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
 

 因为文档中,用的HQL数据库吧,所有会导致我们的数据库自动建表的语句没有了。

 配置文件里面的信息不解释。

 

 

 Dao接口的代码

 package com.endual.test;

public interface Dao {

	public void save() ;
}
 

 

  DaoImpl.java代码

 

  package com.endual.test;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.endual.domain.Item;


public class DaoImpl extends HibernateDaoSupport implements Dao{

	
	
	public void save() {
		
		Item entity = new Item();
		entity.setName("dd") ;
		entity.setDescription("dd") ;
		try {
			this.getHibernateTemplate().save(entity) ;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//this.getHibernateTemplate().save(entity ) ;
	}
	
	
}

 

 

  主运行类:

 

  package com.endual.test;

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

import com.endual.domain.Person;

public class Main {

	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

//		DaoImpl dd = new DaoImpl() ;
//		dd.save() ;
		
		ApplicationContext application=new ClassPathXmlApplicationContext("/applicationContext.xml") ;
		DaoImpl dao =(DaoImpl)application.getBean("daoImpl");
		
		dao.save() ;
		
		
//		DaoImpl dd = new DaoImpl() ;
//		dd.save() ;
		
		
		
	}

}
 

 

 

 

 运行结果和SQL数据不显示,祝你成功吧。O(∩_∩)O

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 大小: 3.2 KB
  • 大小: 10 KB
  • 大小: 10.5 KB
分享到:
评论

相关推荐

    spring整合hibernate示例代码

    标题"spring整合hibernate示例代码"提示我们,我们将讨论如何在实际项目中结合这两个框架。Spring可以帮助管理Hibernate的SessionFactory和Transaction,提供声明式事务管理,以及通过AOP(面向切面编程)实现更灵活...

    Spring整合Hibernate.jar

    标题"Spring整合Hibernate.jar"意味着我们将讨论如何将这两个强大的框架集成在一起,以便在Spring管理的环境中使用Hibernate进行数据库操作。这通常涉及到以下步骤和知识点: 1. **引入依赖**:首先,你需要在项目...

    spring整合hibernate实现事务处理

    本篇文章将详细探讨如何通过Spring整合Hibernate来实现事务处理,重点介绍注解方式和XML配置方式。 首先,我们了解事务处理在数据库操作中的重要性。事务是一组操作,这些操作要么全部成功,要么全部失败,确保数据...

    spring整合hibernate的jar包

    标题中的“spring整合hibernate的jar包”指的是在Java Web开发中,Spring框架与Hibernate持久层框架的集成。这两个框架结合使用可以提供强大的数据访问和业务逻辑处理能力。Spring作为一个全面的轻量级框架,提供了...

    spring整合hibernate所用相关jar包

    本文将深入探讨Spring整合Hibernate的相关知识点,适合初学者入门。 首先,我们需要理解Spring的核心理念。Spring框架提供了一个轻量级的容器,它能够管理应用程序中的对象,包括初始化、配置和依赖注入。AOP则是...

    Spring整合Hibernate

    Spring4整合Hibernate4实现用户购买图书和结账等操作,整合主要实现用IoC容器来管理Hibernate的SessionFactory实例,并使Hibernate使用Spring所提供的声明式事务……

    spring整合hibernate实例

    这篇名为"spring整合hibernate实例"的内容,显然是关于如何将这两个框架协同工作,构建一个高效、灵活的Java应用的教程。在整合过程中,我们将探讨以下几个关键知识点: 1. **Spring的ApplicationContext**: 这是...

    Spring整合HIbernate

    《Spring整合Hibernate实战指南》 在Java开发领域,Spring框架以其强大的依赖注入、AOP(面向切面编程)以及丰富的模块支持,成为了企业级应用开发的首选。而Hibernate作为持久层框架,以其对象关系映射(ORM)能力...

    Spring整合Hibernate 详解.doc

    Spring整合Hibernate是现代Java开发中常见的一种技术组合,利用Spring框架的强大功能来管理和协调Hibernate的持久化操作。Spring为Hibernate提供了全面的集成方案,简化了DAO(Data Access Object)的开发,同时也...

    使用spring整合hibernate和struts时所要用到的所有jar包

    首先,我们需要理解Spring如何与Hibernate和Struts进行整合: 1. **Spring与Hibernate整合**: - Spring通过其`HibernateTemplate`或`HibernateDaoSupport`类简化了对Hibernate的操作,提供了事务管理。你可以定义...

    Spring整合Hibernate示例完整代码

    下面,我们将深入探讨Spring整合Hibernate的相关知识点。 1. **依赖注入**:Spring框架的核心特性之一是依赖注入(DI),它允许我们在不进行硬编码的情况下管理对象之间的依赖关系。在整合Hibernate时,Spring可以...

    spring整合hibernate与struts2所需jar包

    标题"spring整合hibernate与struts2所需jar包"表明我们需要关注的是如何将这三个框架整合在一起,并且提供了所需的一些基础组件。整合SSH可以让开发者利用Spring的管理能力,让Hibernate更易于使用,同时通过Struts2...

    spring整合hibernate

    《Spring整合Hibernate详解》 在Java企业级应用开发中,Spring和Hibernate是两个非常重要的框架。Spring作为一个全面的轻量级应用框架,提供了强大的依赖注入、AOP(面向切面编程)以及事务管理等功能;而Hibernate...

    Spring整合Hibernate示例

    Spring整合Hibernate配置测试示例

    spring整合hibernate开发源码

    在"spring整合hibernate开发源码"的压缩包中,可能包含了以下内容: 1. **配置文件**:如`applicationContext.xml`,其中配置了Spring和Hibernate的相关bean,如DataSource、SessionFactory、TransactionManager等。...

    Spring整合Hibernate需要用到的部分jar包

    这里的jar包只是说Spring自带的关于Hibernate的jar包,即这四个jar包: hibernate3.jar, hibernate-annotations.jar, hibernate-entitymanager.jar hibernate-commons-annotations.jar 因为我原来下载了一个,但是...

    Spring+hibernate整合源代码

    这个“Spring+hibernate整合源代码”应该包含了实现上述整合步骤的示例代码,可以作为学习和参考的资源。通过学习和实践这些代码,你可以更好地理解和掌握 Spring 和 Hibernate 整合的细节,提升你的 Java Web 开发...

    spring整合Hibernate学习笔记.docx

    Spring 整合 Hibernate 是一种常见的企业级应用开发模式,它将 Spring 框架的管理优势与 Hibernate 的持久层功能结合起来,提供了更高效、更稳定的应用解决方案。在本学习笔记中,我们将深入探讨如何实现这一整合,...

    Spring整合hibernate(2)之基于HibernateTemplate的整合

    Spring整合Hibernate基于HibernateTemplate的方式,极大地简化了数据库操作,同时也让事务管理和代码的编写变得更加规范和高效。在实际项目中,可以根据需求进一步配置和优化,比如使用JPA的...

    spring整合hibernate例子

    当我们谈到"Spring整合Hibernate例子"时,这意味着我们将探讨如何将这两个强大的框架结合在一起,以实现更高效、更灵活的数据库操作。 Spring框架的核心特性之一是依赖注入(Dependency Injection,DI),这使得...

Global site tag (gtag.js) - Google Analytics