spring+ hibernate 整合

Hibernate可以方便我们更加方便快捷的使用数据库。

与spring 的整合之后更加加快了开发的速度。

下面我们介绍spring+hibernate的整合。

我们使用如下结构测试代码

其中比较核心的主要是hbm.xml文件的配置

user.hbm.xml中的配置如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping>
   <class name="cn.itcast.entity.User" table="t_user">
   <id name="uid" column="uid">
   <generator  class="native"></generator>
   </id>
   <property name="username" column="username"></property>
   <property name="address" column="address"></property>
   </class>
</hibernate-mapping>	
	

		

这个配置文件主要实现了一个建表的过程。id generate 来设置表的主键,下面property 来设置其中表的属性。

 

hibernate.cfg.xml的配置如下

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

<session-factory >

<!--JDBC驱动程序-->

<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>

<!-- 连接数据库的URL-->

<property name="connection.url">jdbc:mysql://localhost:3306/spring_day04?serverTimezone=UTC</property>

<!--连接的登录名-->

<property name="connection.username">root</property>

<!--登录密码-->
	
<property name="connection.password"></property>

<!--是否将运行期生成的SQL输出到日志以供调试-->

<property name="show_sql">true</property>

<!--指定连接的语言-->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

<property name="hibernate.hbm2ddl.auto">update</property>

<mapping resource="cn/itcast/entity/User.hbm.xml"/>
</session-factory> 
</hibernate-configuration>

这段代码上面有一些注释说明了一些hibernate的配置作用。应该注意的是 最后一句的 引用resource 这个正是我们整合的桥梁。

最后是bean1.xml spring的配置文件。(其实就是spring 中的application 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:lang="http://www.springframework.org/schema/lang" 
    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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
     

   <!-- c3p0连接池 -->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
   <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
   <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_day04?serverTimezone=UTC"></property>
   <property name="user" value="root"></property>
   <property name="password" value=""></property>
   </bean>
   
   <!-- sessionFactory创建交给spring	-->
   <bean  id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!--指定hibernate配置文件 -->
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
   </bean>
   
   <!-- 事务管理器 -->
   <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>		
   <!-- 扫描 -->
   <tx:annotation-driven transaction-manager="transactionManager" ></tx:annotation-driven>
   
   
   <!-- action-->
   <bean id="userAction" class="cn.itcast.action.UserAction" scope="prototype">
   <property name="userService" ref="userService"></property>
   </bean>
   
   <bean id="userService" class="cn.itcast.service.UserService" >
   <!-- name 写接口 ref 写下面的id-->
   <property name="userDao" ref="userDaoImpl"></property>
   </bean>
    
   <bean id="userDaoImpl" class="cn.itcast.dao.UserDaoImpl" >
   <property name="hibernateTemplate" ref="hibernateTemplate"></property>
   </bean>
   
   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate" >
   <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
      
 
</beans>

这段代码中c3p0连接池 可以代替 hibernate.cfg 中的数据库配置信息。

值得注意的是 sessionfactory的创建,我们实现数据库操作需要用到这个类,

事务管理器也一定要配置,这关系到我们后面的操作是否 提交到数据库 的实际操作。

最后,就是最后面的两句,在hibernateTemplate中加入sessionfactory的属性,在userdao测试类中加入hibernateTemplate

最后我们测试效果

package cn.itcast.dao;

import org.springframework.orm.hibernate5.HibernateTemplate;

import cn.itcast.entity.User;

public class UserDaoImpl implements UserDao{
        //注入hibernateTemplate 
	private HibernateTemplate hibernateTemplate;
	//方便注入的set方法
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
 
	//得到hibernateTemplate
	@Override
	public void add() {
	//  HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
     //hibernateTemplate.save(entity);
		User user = new User();
		user.setUsername("rose");
		user.setAddress("japan");
		hibernateTemplate.save(user);
		
		
	}
	
	
	
}

我们这时候便可以利用hibernateTemplate对数据库进行操作了。

运行后 结果正常。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值