欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate+Spring实现对数据库的操作

程序员文章站 2022-06-02 08:21:59
...

优点

  1. 通过Spring的 IoC 容器来管理 Hibernate
  2. 使用Hibernate的模板类,更方便

准备

必要的jar

数据库连接包

Hibernate 相关 jar

Spring 核心包 (5 个)

Spring aop 包 (4 个)

spring-orm-3.2.5.RELEASE.jar                 【spring 对 hibernate 的支持】

spring-tx-3.2.5.RELEASE.jar                     【事务相关】

 

步骤

  • 实体类Category :必须有无参数的构造函数和getter  setter方法
package com.syc.entity;
 
public class Category {
 
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private int id;
    private String name;
}
  • 实体类对应的配置文件Category.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.syc.entity">
	<class name="Category" table="category_">
		<id name="id" column="id">
			<generator class="native">
			</generator>
		</id>
		<property name="name" />

	</class>
	
</hibernate-mapping>
  • DAO层

在这里Category继承了Hibernate封装好的模板类HibernateTemplate,功能很全面,不必自己写简单的方法了,直接调用即可。

package com.syc.dao;

import org.springframework.orm.hibernate3.HibernateTemplate;

public class CategoryDAO extends HibernateTemplate{

	
}
  • spring 配置连接池和 hibernate 常用配置

最关键的一步,配置MySQL数据源

在src下创建xml: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 name="c" class="com.syc.entity.Category">
        <property name="name" value="yyy" />
    </bean>
    
    <bean name="dao" class="com.syc.dao.CategoryDAO">
        <property name="sessionFactory" ref="sf" />
    </bean>

	<bean name="sf"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="ds" />
		<property name="mappingResources">
			<list>
				<value>com/syc/entity/Category.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.show_sql=true
				hbm2ddl.auto=update
   			</value>
		</property>
	</bean>    
        
    <bean name="ds"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/train?characterEncoding=UTF-8" />
		<property name="username" value="SYC" />
		<property name="password" value="123456" />
	</bean>	

	
    


 
 
</beans>
  • 测试主类

package com.syc.test;

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

import com.syc.dao.CategoryDAO;
import com.syc.entity.Category;

public class TestSpring {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
		CategoryDAO dao = (CategoryDAO) context.getBean("dao");
		Category c = new Category();
		c.setName("category yyy");
		dao.save(c);	
		Category c2 = dao.get(Category.class, 1);
		c2.setName("category zzz");
		dao.update(c2);
		dao.delete(c2);

	}
}

 

到此Spring+Hibernate的详细步骤就结束了,算是自己的一个总结吧

相关标签: Spring Hibernate