1.配置文件:

<?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: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">

<!--开启spring注解配置 ,主要针对依赖属性注解-->
<context:annotation-config/>

<!--全注解扫描 针对所有的注解 -->
<context:component-scan base-package="com.zhidi"/>

<!--导入session工厂 -->
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/db_hibernate</prop>
<prop key="hibernate.connection.username">root</prop>
<prop key="hibernate.connection.password">123456</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="com.zhidi">
</property>
</bean>
</beans>

2.javadao层

package com.zhidi.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.zhidi.bean.Dept;

@Repository
public class DeptDao {
@Autowired
private SessionFactory sessionFactory;

/**
* 添加dept的方法
*/
public Integer save(Dept dept){
Session se = sessionFactory.getCurrentSession();
se.beginTransaction();
Integer id = (Integer) se.save(dept);
se.getTransaction().commit();
return id;

}
public void delete(Dept dept){
Session se = sessionFactory.getCurrentSession();
se.beginTransaction();
se.delete(dept);
se.getTransaction().commit();

}
}

3.javabean层

package com.zhidi.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="dept")
public class Dept {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer deptno;
private String loc;
private String dname;
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}

}

4.测试服务层

package com.zhidi.service;

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

import com.zhidi.bean.Dept;
import com.zhidi.dao.DeptDao;

public class DeptService {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("springContext.xml");
DeptDao d = app.getBean(DeptDao.class);
Dept dept = new Dept();
dept.setDname("小明");
dept.setLoc("12345");
Integer id = d.save(dept);
System.out.println(id);
}
}

spring框架整合hibernate框架简单操作数据库的更多相关文章

  1. Spring_day04--Spring框架整合hibernate框架

    Spring框架整合hibernate框架 1 把hibernate核心配置文件中配置数据库信息,把数据库信息在spring进行配置 2 把hibernate里面的sessionFactory创建交给 ...

  2. JavaWeb_(Spring框架)整合Mybatis加入事务操作数据库

    整合Mybatis a)导包: i.Spring:基本包.aop.aspects.jdbc.tx.test: ii.Mybatis:mybatis-3.4.6 iii.整合包:mybatis-spri ...

  3. Java框架:spring框架整合hibernate框架的xml配置(使用注解的方式)

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. Spring 框架整合Struts2 框架和 Hibernate 框架

    1. Spring 框架整合 Struts2 框架 // [第一种整合方式(不推荐)](http://www.cnblogs.com/linkworld/p/7718274.html) // 从 Se ...

  5. 【SSH框架】系列之 Spring 整合 Hibernate 框架

    1.SSH 三大框架整合原理 Spring 与 Struts2 的整合就是将 Action 对象交给 Spring 容器来负责创建. Spring 与 Hibernate 的整合就是将 Session ...

  6. 整合Spring框架和Hibernate框架

    -------------------siwuxie095                                 整合 Spring 框架和 Hibernate 框架         1.导 ...

  7. Spring Security 整合freemaker 实现简单登录和角色控制

    Spring Security 整合freemaker 实现简单登录和角色控制     写这篇文章是因为我做了一个电商网站项目,近期刚加上权限控制.整个过程很简单,在此给大家梳理一下,也算是自己对知识 ...

  8. Spring_boot简单操作数据库

    Spring_boot搭配Spring Data JPA简单操作数据库 spring boot 配置文件可以使用yml文件,默认spring boot 会加载resources目录的下的applica ...

  9. 【Hibernate学习笔记-3】在Spring下整合Hibernate时, 关于sessionFactory的类型的说明

    摘要 在Spring下整合Hibernate时,关于sessionFactory的配置方式主要有两种,分别为注解配置方式,和xml配置方式,下面将对这两种配置方式进行介绍. 1. sessionFac ...

随机推荐

  1. PHP+js实现图片上传,编辑

    文件上传,点击按钮并选择文件后,文件会临时存到一个位置,会有一个临时名字: 然后在php文件中处理,给文件起名并将文件从临时为止搬到服务器,把需要的文件信息返回给前端页面: 最后表单提交时,把文件信息 ...

  2. Java面向对象之抽象类,接口

    抽象类: 含有抽象方法的类被声明为抽象类 抽象方法由子类去实现 含有抽象方法的类必须被声明为抽象类 抽象类被子类继承,子类(如果不是抽象类)必须重写抽象类中的所有抽象方法 抽象方法: 声明而未被实现的 ...

  3. RxSwift 系列(四) -- Transforming Operators

    前言 本篇文章将要学习RxSwift中四种转换操作符: map flatMap flatMapLatest scan map 通过使用一个闭包函数将原来的Observable序列转换为一个新的Obse ...

  4. 【js】name 与 array 的纠葛 - 坑

    一. 现象 var name = new Array(); typeof(name)      // 为string 类型 var name = new Array('a' , 'b' , 'c'); ...

  5. 移动端https抓包那些事--初级篇

    对于刚刚进入移动安全领域的安全研究人员或者安全爱好者,在对手机APP进行渗透测试的时候会发现一个很大的问题,就是无法抓取https的流量数据包,导致渗透测试无法继续进行下去. 这次给大家介绍一些手机端 ...

  6. [Usaco2007 Open]Fliptile 翻格子游戏

    [Usaco2007 Open]Fliptile 翻格子游戏 题目 Farmer John knows that an intellectually satisfied cow is a happy ...

  7. 网络爬虫Web开始

    一.介绍 该程序主体是<Python核心编程第二版>例20.2.本篇会修改部分代码及添加了相关注释. ps:该书该例程不能直接运行,需要修改. 二.功能 网络爬虫crawl.py抓取web ...

  8. 【 js 基础 】【 源码学习 】backbone 源码阅读(二)

    最近看完了 backbone.js 的源码,这里对于源码的细节就不再赘述了,大家可以 star 我的源码阅读项目(source-code-study)进行参考交流,有详细的源码注释,以及知识总结,同时 ...

  9. 腾讯发布 Omix 1.0 - 用 JSX 或 hyperscript 创建用户界面

    腾讯发布 Omix 1.0 - 用 JSX 或 hyperscript 创建用户界面 今天,腾讯正式开源发布 Omix 1.0, 让开发者使用 JSX 或 hyperscript 创建用户界面. Gi ...

  10. MySQL checkpoint深入分析

    1.日常关注点的问题 2.日志点分析 3.checkpoint:脏页刷盘的检查点 4.模糊检查点发生条件 1.master thread checkpoint 2.flush_lru_list che ...