`
wzf7065
  • 浏览: 249313 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

spring集成hibernate的HelloWorld

阅读更多
本文中省去了创建User对象并为其做映射的部分

第一步:添加spring和hibernate的jar包;

第二步:创建接口文件
package cleversoft.spring3.h3;

import cleversoft.spring3.h3.user.User;

public interface H3Api {
    public boolean create(User u);
}


第三步:创建接口的实现类,并实现getter和setter方法
package cleversoft.spring3.h3;

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

import cleversoft.spring3.h3.user.User;

public class Impl implements H3Api{
	private SessionFactory sf = null;
	
    public void setSf(SessionFactory sf) {
		this.sf = sf;
	}

    public SessionFactory getSf() {
		return sf;
	}
    
	public boolean create(User u){
		HibernateTemplate ht = new HibernateTemplate(sf);
		ht.save(u);
		return false;
	}

	
}


第四步:创建客户端验证类
package cleversoft.spring3.h3;

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

import cleversoft.spring3.h3.user.User;

public class Client {
    public static void main(String args[]){
    	ApplicationContext ctx = new ClassPathXmlApplicationContext(
    	    new String[]{"applicationContext.xml"}		
    	);
    	H3Api api = (H3Api)ctx.getBean("api");
    	User u = new User();
    	u.setUuid("1");
    	u.setName("小强");
    	u.setAge("20");
    	api.create(u);
    }
}


第五步:配置applicationContext.xml文件,注意数据源和sessionFactory的配置以及它们之间的关系
<?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"
		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 name="api" class="cleversoft.spring3.h3.Impl">
            <property name="sf" ref="sessionFactory"></property>
        </bean>
        
        <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource">
	        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
	    	<property name="url" value="jdbc:mysql://localhost:3306/user"/>
	    	<property name="username" value="root"/>
	    	<property name="password" value=""/>   
        </bean>
        
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		    <property name="dataSource">
		          <ref bean="DataSource"/>
		    </property>		    
		    <property name="hibernateProperties">
		          <props>
		              <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
		              <prop key="hibernate.show_sql">true</prop>
		          </props>
		    </property>  
		    <property name="mappingDirectoryLocations">
		          <list>
		              <value>classpath*:cleversoft/spring3/h3/user</value>
		          </list>
		    </property>
       </bean>
</beans>


第六步:验证,出现下边的sql语句表示成功
Hibernate: insert into tbl_user (uuid, name, age) values (?, ?, ?)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics