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. 怎么在linux ubuntu 上的nginx 绑定域名

    前一篇介绍了,如果在ubuntu上运行nodejs,毕竟访问的时候都是用ip地址+端口号,但是上production 环境都需要域名的,IP地址当然不行 读过上一篇的朋友知道了,如果在upstart ...

  2. nyoj_61: 传纸条(一)

    题目链接 使用双线dp,假设两个人同时从左上角移动到右下角,且满足路线不交叉,另k=x1+y1=x2+y2压缩状态进行优化.每次状态转移满足 x1,x2,y1,y2都在矩阵范围内,且(x2,y2)在相 ...

  3. JMeter 监控和记录&常用功能

    使用https连接时,如果对应站点的CA 证书错误,会直接报连接不到服务器的错误,org.apache.commons.httpclient.NoHttpResponseException,把错误证书 ...

  4. (转)Java中equals和==的区别

    java中的数据类型,可分为两类:  1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean    他们之间的比较,应用双等号( ...

  5. vue-axios配置token,上传图片

    vue1.0用 vue-resource 目前不维护了 vue2.0用 axios 使用 1.安装axios, npm install axios 2.在main.js中配置 import axios ...

  6. ubuntu下使用 chkconfig 是一种习惯

    ubuntu下使用 chkconfig 是一种习惯 习惯了chkconfig命令, 闲来写了个脚本模拟下, 步骤很简单. 如下: 第一步, 安装sysv-rc-conf sudo apt instal ...

  7. 基于angular2x+ng-bootstrap构建后台管理系统界面(干货)

    写在前面的话 近来公司要做一个后台管理系统,人手比较少,于是作为一个前端也参与进来,其实据我所知,大部分的公司还是后台自己捣鼓的. 在后台没有到位的情况下,前端应该使用什么技术也着实让我为难了一把.经 ...

  8. 21. leetcode 492

    492: 给定一个面积值,求它的长l和宽w.长和宽需满足:长大于等于宽,长和宽的差值尽可能小,长乘宽等于面积. 思路:先将l和w初始化为sqrt(area),然后看l*w是否等于面积,如果等于则返回l ...

  9. Codeforces 828B Black Square(简单题)

    Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...

  10. mybatis中的mapper接口文件以及example类的实例函数以及详解

    ##Example example = new ##Example(); example.setOrderByClause("字段名 ASC"); //升序排列,desc为降序排列 ...