spring框架整合hibernate框架简单操作数据库
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框架简单操作数据库的更多相关文章
- Spring_day04--Spring框架整合hibernate框架
Spring框架整合hibernate框架 1 把hibernate核心配置文件中配置数据库信息,把数据库信息在spring进行配置 2 把hibernate里面的sessionFactory创建交给 ...
- JavaWeb_(Spring框架)整合Mybatis加入事务操作数据库
整合Mybatis a)导包: i.Spring:基本包.aop.aspects.jdbc.tx.test: ii.Mybatis:mybatis-3.4.6 iii.整合包:mybatis-spri ...
- Java框架:spring框架整合hibernate框架的xml配置(使用注解的方式)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring 框架整合Struts2 框架和 Hibernate 框架
1. Spring 框架整合 Struts2 框架 // [第一种整合方式(不推荐)](http://www.cnblogs.com/linkworld/p/7718274.html) // 从 Se ...
- 【SSH框架】系列之 Spring 整合 Hibernate 框架
1.SSH 三大框架整合原理 Spring 与 Struts2 的整合就是将 Action 对象交给 Spring 容器来负责创建. Spring 与 Hibernate 的整合就是将 Session ...
- 整合Spring框架和Hibernate框架
-------------------siwuxie095 整合 Spring 框架和 Hibernate 框架 1.导 ...
- Spring Security 整合freemaker 实现简单登录和角色控制
Spring Security 整合freemaker 实现简单登录和角色控制 写这篇文章是因为我做了一个电商网站项目,近期刚加上权限控制.整个过程很简单,在此给大家梳理一下,也算是自己对知识 ...
- Spring_boot简单操作数据库
Spring_boot搭配Spring Data JPA简单操作数据库 spring boot 配置文件可以使用yml文件,默认spring boot 会加载resources目录的下的applica ...
- 【Hibernate学习笔记-3】在Spring下整合Hibernate时, 关于sessionFactory的类型的说明
摘要 在Spring下整合Hibernate时,关于sessionFactory的配置方式主要有两种,分别为注解配置方式,和xml配置方式,下面将对这两种配置方式进行介绍. 1. sessionFac ...
随机推荐
- cocos2dx 图片压缩工具 推荐
随着版本的第二次迭代,包体越来越大;随之而来的也就是下载量会有所影响;毕竟每增大一点包体大小,下载的玩家就会损失一小批; 但是我们的图片都经过美术压缩打包,依然包体不小; 最后发现是因为把图片都打包成 ...
- Ubuntu安装桌面环境
1.安装Ubuntu默认的Gnome桌面: sudo apt-get install ubuntu-desktop 2.不安装默认组件,例如Evolution和OpenOffice: sudo apt ...
- [luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- 初次使用Mybatis配置出现错误待解决
数据库: create table STUDENT ( STUID NUMBER(9) not null, SNAME VARCHAR2(50) not null, SEX VARCHAR2(4), ...
- 2017年1月1日 App Store中的所有应用都必须启用 App Transport Security安全功能
2017年1月1日 App Store中的所有应用都必须启用 App Transport Security安全功能,否则极有可能被拒! 在WWDC 2016开发者大会上,苹果宣布了一个最后期限:到20 ...
- Hybris商品图片导入与压缩有关的配置
1. 在电脑上安装 ImageMagick 软件(windows平台还需要安装VC++),下载路径:http://www.imagemagick.org/script/download.php#w ...
- 【HTML】ie=edge(转)
< meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" /> 这是个是 ...
- 让初学者快速了解Git
Git工作原理 为了更好的学习Git,我们们必须了解Git管理我们文件的3种状态,分别是已提交(committed).已修改(modified)和已暂存(staged),由此引入 Git 项目的三个工 ...
- android rss阅读器开发一点小技巧
这几天一直在学习开发Rss阅读器,遇到一个很坑的问题,InputSource这里总是出错.弄了好久,终于让我找到一个解决方法----看代码: new Thread(){ @Override publi ...
- 15. leetcode 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...