spring和hibernate整合,基于xml的方法

直接上代码

1.项目目录


2.创建lib文件夹,将hibernate和spring各自需要的jar包和两者整合需要的jar包Cope到此文件夹下,再build  path

3.User.java

package com.ssh.pojo;

public class User {
	private Integer uid;
	private String username;
	private String password;
	public Integer getUid() {
		return uid;
	}
	public void setUid(Integer uid) {
		this.uid = uid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public User() {
		
	}
	public User(Integer uid, String username, String password) {
		super();
		this.uid = uid;
		this.username = username;
		this.password = password;
	}
	
}

User的映射文件User.hbm.xml

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

<!-- 引入Hibernate映射文件约束 -->
<!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.ssh.pojo">
    	<class name="User" table="user">
    		<id name="uid" type="java.lang.Integer">
    			<column name="id" ></column>
    			<generator class="native"></generator>
    		</id>
    		<property name="username" type="java.lang.String"></property>
    		<property name="password" type="string" ></property>
    	</class>
    </hibernate-mapping>

接口类UserDao.java

package com.ssh.dao;

public interface UserDao {
	public void test();
}

实现类UserDaoImpl.java

package com.ssh.dao.impl;

import org.springframework.orm.hibernate3.HibernateTemplate;

import com.ssh.dao.UserDao;

public class UserDaoImpl extends HibernateTemplate implements UserDao{

	@Override
	public void test() {
		System.out.println("UserDaoImpl是接口UserDao接口的实现类,而且继承了HibernateTemplate可以直接和数据库直接交互");
		
	}

}

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD是约束,可以在核心包里面找 -->
<!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>
    	<!-- Database connection settings:在applicationContext.xml文件中已经交给Spring管理了-->  
        <!-- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
        <property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</property>  
        <property name="connection.username">root</property>  
        <property name="connection.password">admin</property> -->

        <!-- hibernate的配置信息 -->
        <!-- 配置数据库的方言,根据底层的数据库生成不同的SQL -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- 配置显示SQL -->
        <property name="hibernate.show_sql">true</property>
        <!-- 配置格式化SQL -->
        <property name="hibernate.format_sql">true</property>
        <!-- 配置hbm2ddl -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- 加载映射文件 :在applicationContext.xml文件中已经指定了-->
        <!-- <mapping resource="an/pojo/Person.hbm.xml" /> -->

    </session-factory>
</hibernate-configuration>

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/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context      
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 	
 	<!-- Bean:User -->
    <bean name="user" class="com.ssh.pojo.User">
    </bean>
    
    <!-- Bean:UserDaoImpl -->
    <bean name="userDaoImpl" class="com.ssh.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="sf" />
    </bean>

    <!-- Bean:SessionFactory -->
    <bean name="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="ds" />
		<!-- pojo类对应映射文件的位置 -->
		<property name="mappingResources">
			<list>
				<value>com/ssh/pojo/User.hbm.xml</value>
			</list>
		</property>
		<!-- 自动建表失效的情况下,默认无需此属性-->
		<property name="schemaUpdate"> 
            <value>true</value> 
        </property>
        <!-- 指定hibernate配置文件的位置 -->
		<property name="hibernateProperties" value="classpath:hibernate.cfg.xml"></property>
	</bean>    
        
    <!-- Bean:DataSource -->
    <bean name="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=GBK" />
		<property name="username" value="root" />
		<property name="password" value="admin" />
	</bean>	

</beans>

测试类TestSpring.java

package com.ssh.test;

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

import com.ssh.dao.impl.UserDaoImpl;
import com.ssh.pojo.User;

public class TestSpring {
	 public static void main(String args[]) {
	        ApplicationContext bf=new ClassPathXmlApplicationContext("applicationContext.xml");  
			UserDaoImpl userDaoImpl=(UserDaoImpl)bf.getBean("userDaoImpl");
	        User u=new User();
	        u.setUsername("mm");
	        u.setPassword("123456");
	        
	        //增加
	        userDaoImpl.save(u);
	        userDaoImpl.test();
	        //获取
	        /*User u2=userDaoImpl.get(User.class, 1);
	        System.out.println(u2.getUsername()+","+u2.getPassword());*/
	        
	        //修改
	        /*u2.setUsername("Mary");
	        userDaoImpl.update(u2);*/
	        
	        
	        //删除
	        /*userDaoImpl.delete(u2);*/

	    }
}

测试结果


整合思路是,使Dao继承HibernateTemplate这个类 
HibernateTemplate这个类提供了setSessionFactory()方法用于注入SessionFactory 
通过spring获取Dao的时候,注入SessionFactory.

猜你喜欢

转载自blog.csdn.net/yiguang_820/article/details/80784853
今日推荐